ZQN
2024-06-19 72468556f3709380ab3a70e07d8a916fbd47c988
project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java
@@ -3,8 +3,10 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.common.constant.UserConstants;
import com.project.common.core.domain.entity.SysUser;
import com.project.common.core.domain.model.ImportError;
import com.project.common.exception.base.BaseException;
import com.project.common.utils.SecurityUtils;
import com.project.system.domain.vo.SysCompanyResultVo;
import com.project.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,6 +24,7 @@
import com.project.system.mapper.SysCompanyMapper;
import com.project.system.service.ISysCompanyService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -57,7 +60,7 @@
    public Boolean insertByBo(SysCompanyBo bo)
    {
        SysCompany add = Convert.convert(SysCompany.class, bo);
        validEntityBeforeSave(add, 0);
        validEntityBeforeSave(add);
        boolean save = this.save(add);
        addCompanyUser(add);
        return save;
@@ -76,7 +79,7 @@
            }
            this.addCompanyUser(update);
        }
        validEntityBeforeSave(update, 0);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }
@@ -93,17 +96,47 @@
        return this.removeByIds(ids);
    }
    @Override//导入
    @Transactional
    public Boolean importList(List<SysCompanyVo> list)
    @Override
    public SysCompanyResultVo doImport(List<SysCompanyVo> list)
    {
        if (list==null || list.size()<1){
            throw new BaseException("未获取到导入信息");
            throw new BaseException("未获取到导入信息!");
        }
        List<SysCompany> companies = Convert.toList(SysCompany.class, list);
        companies.forEach(e-> validEntityBeforeSave(e,1));
        boolean b = this.saveOrUpdateBatch(companies);
        SysCompanyResultVo resultVo = new SysCompanyResultVo();
        resultVo.setVoList(list);
        return this.checkImport(resultVo);
    }
    @Override
    public SysCompanyResultVo checkImport(SysCompanyResultVo resultVo)
    {
        if (resultVo==null || StringUtils.isEmpty(resultVo.getVoList())){
            throw new BaseException("未获取到导入信息!");
        }
        for (SysCompanyVo vo : resultVo.getVoList()) {
            List<ImportError> errors = new ArrayList<>();
            validEntityBeforeImport(vo, errors);
            vo.setErrorList(errors);
        }
        return  resultVo;
    }
    @Override
    @Transactional
    public Boolean saveImport(SysCompanyResultVo resultVo)
    {
        if (resultVo==null || StringUtils.isEmpty(resultVo.getVoList())){
            throw new BaseException("未获取到导入信息!");
        }
        resultVo.getVoList().forEach(e-> {
            if (StringUtils.isNotEmpty(e.getErrorList())) {
                throw new BaseException("还有未处理错误信息!");
            }
        });
        List<SysCompany> companies = Convert.toList(SysCompany.class, resultVo.getVoList());
        boolean b = this.saveOrUpdateBatch(companies);
        companies.forEach(this::addCompanyUser);
        return b;
    }
@@ -114,15 +147,12 @@
    /**
     * 保存前校验
     * @param entity    参数
     * @param isImport  是否导入:0否,1是
     */
    private void validEntityBeforeSave(SysCompany entity, int isImport)
    private void validEntityBeforeSave(SysCompany entity)
    {
        entity.setIsImport(isImport);
        if (isImport!=1){
            if (StringUtils.isEmpty(entity.getCompanyImg())){
                throw new BaseException(String.format("%1$s,请上传营业执照", entity.getCompanyName()));
            }
        entity.setIsImport(0);
        if (StringUtils.isEmpty(entity.getCompanyImg())){
            throw new BaseException(String.format("%1$s,请上传营业执照", entity.getCompanyName()));
        }
        if (StringUtils.isEmpty(entity.getCompanyCode())){
            throw new BaseException(String.format("%1$s,请填写企业社会编码!", entity.getCompanyName()));
@@ -132,6 +162,9 @@
        }
        if (StringUtils.isEmpty(entity.getCompanyPhone())){
            throw new BaseException(String.format("%1$s,请填写企业联系人电话!", entity.getCompanyName()));
        }
        if (StringUtils.isEmpty(entity.getCompanyAddress())){
            throw new BaseException(String.format("%1$s,请填写企业地址!", entity.getCompanyName()));
        }
        if (StringUtils.isEmpty(entity.getCompanyName())){
            throw new BaseException("请填写企业名!");
@@ -163,12 +196,42 @@
                throw new BaseException(String.format("%1$s,企业联系电话已存在!", entity.getCompanyName()));
            }
        }
        if (isImport==1){
            entity.setCheckStatus(2);
        }
    }
    private void validEntityBeforeImport(SysCompanyVo entity, List<ImportError> errorList)
    {
        entity.setIsImport(1);
        if (StringUtils.isEmpty(entity.getCompanyName())){
            errorList.add(new ImportError(0, "请填写企业名!"));
        }
        if (StringUtils.isEmpty(entity.getCompanyCode())){
            errorList.add(new ImportError(1, "请填写企业社会编码!"));
        }
        if (StringUtils.isEmpty(entity.getCompanyUser())){
            errorList.add(new ImportError(2, "请填写企业联系人!"));
        }
        if (StringUtils.isEmpty(entity.getCompanyPhone())){
            errorList.add(new ImportError(3, "请填写企业联系人电话!"));
        }
        if (StringUtils.isEmpty(entity.getCompanyAddress())){
            errorList.add(new ImportError(4, "请填写企业地址!"));
        }
        int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()));
        if (codeCount>0){
            errorList.add(new ImportError(1, "企业社会编码已存在!"));
        }
        int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()));
        if (nameCount>0){
            errorList.add(new ImportError(0, "企业名已存在!"));
        }
        int phoneCount = this.count(lq().eq(SysCompany::getCompanyPhone, entity.getCompanyPhone()));
        if (phoneCount>0){
            errorList.add(new ImportError(3, "企业联系电话已存在!"));
        }
        entity.setCheckStatus(2);
    }
    /**
     * 添加企业用户
     * @param entity    企业信息