package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ContractTableMapper;
import com.ruoyi.system.domain.ContractTable;
import com.ruoyi.system.service.IContractTableService;

import javax.validation.Validator;


/**
 * B类Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-25
 */
@Service
public class ContractTableServiceImpl implements IContractTableService 
{
    @Autowired
    private ContractTableMapper contractTableMapper;

    @Autowired
    protected Validator validator;


    /**
     * 查询B类
     * 
     * @param id B类主键
     * @return B类
     */
    @Override
    public ContractTable selectContractTableById(Long id)
    {
        return contractTableMapper.selectContractTableById(id);
    }

    /**
     * 查询B类列表
     * 
     * @param contractTable B类
     * @return B类
     */
    @Override
    public List<ContractTable> selectContractTableList(ContractTable contractTable)
    {
        return contractTableMapper.selectContractTableList(contractTable);
    }

    /**
     * 新增B类
     * 
     * @param contractTable B类
     * @return 结果
     */
    @Override
    public int insertContractTable(ContractTable contractTable)
    {
        return contractTableMapper.insertContractTable(contractTable);
    }

    /**
     * 修改B类
     * 
     * @param contractTable B类
     * @return 结果
     */
    @Override
    public int updateContractTable(ContractTable contractTable)
    {
        return contractTableMapper.updateContractTable(contractTable);
    }

    /**
     * 批量删除B类
     * 
     * @param ids 需要删除的B类主键
     * @return 结果
     */
    @Override
    public int deleteContractTableByIds(Long[] ids)
    {
        return contractTableMapper.deleteContractTableByIds(ids);
    }

    /**
     * 删除B类信息
     * 
     * @param id B类主键
     * @return 结果
     */
    @Override
    public int deleteContractTableById(Long id)
    {
        return contractTableMapper.deleteContractTableById(id);
    }

    @Override
    public String importCon(List<ContractTable> stuList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(stuList) || stuList.size() == 0)
        {
            throw new ServiceException("导入B类证书基本信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ContractTable con : stuList)
        {
            try
            {
                // 验证是否存在这个用户
                ContractTable u = contractTableMapper.selectContractTableByCode(con.getdCode());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, con);
                    con.setCreateBy(operName);
                    this.insertContractTable(con);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、身份证或者编码 " + con.getdCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, con);
                    con.setUpdateBy(operName);
                    this.updateContractTable(con);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、身份证或者编码 " + con.getdCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、身份证或者编码 " + con.getdCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、身份证或者编码 " + con.getdCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
