ZQN
2024-06-26 9c348b05a774adf47453780cf3b7a8f9823661f5
project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java
@@ -62,9 +62,10 @@
        SysCompany add = Convert.convert(SysCompany.class, bo);
        add.setCompanyStatus(1);
        validEntityBeforeSave(add);
        add.setCompanyStatus(0);
        boolean save = this.save(add);
        addCompanyUser(add, 0);
        if (isAddUser(bo.getCompanyPhone())){
            addCompanyUser(add, 0);
        }
        return save;
    }
@@ -76,11 +77,15 @@
        SysCompany update = Convert.convert(SysCompany.class, bo);
        validEntityBeforeSave(update);
        if (!old.getCompanyPhone().equals(update.getCompanyPhone())){
            boolean delUser = delCompanyUser(old.getCompanyId());
            if (!delUser) {
                throw new BaseException("原企业用户清除失败,请联系管理员!");
            if (this.count(SysCompany::getCompanyPhone, old.getCompanyPhone())==1){
                boolean delUser = delCompanyUser(old.getCompanyPhone());
                if (!delUser) {
                    throw new BaseException("原企业用户清除失败,请联系管理员!");
                }
            }
            this.addCompanyUser(update, update.getCompanyStatus()==0 ? 1 : 0);
            if (isAddUser(update.getCompanyPhone())){
                addCompanyUser(update, update.getCompanyStatus()==0 ? 1 : 0);
            }
        }
        return this.updateById(update);
    }
@@ -103,7 +108,7 @@
    public Boolean deleteByIds(Collection<Long> ids)
    {
        for (Long id : ids) {
            boolean b = delCompanyUser(id);
            boolean b = delCompanyUser(this.getById(id).getCompanyPhone());
            if (!b) {
                throw new BaseException(String.format("原企业 %1$s 用户清除失败,请联系管理员!", id));
            }
@@ -153,7 +158,9 @@
        List<SysCompany> companies = Convert.toList(SysCompany.class, resultVo.getVoList());
        boolean b = this.saveOrUpdateBatch(companies);
        for (SysCompany company : companies) {
            addCompanyUser(company,1);
            if (isAddUser(company.getCompanyPhone())){
                addCompanyUser(company,1);
            }
        }
        return b;
    }
@@ -168,6 +175,7 @@
    private void validEntityBeforeSave(SysCompany entity)
    {
        entity.setIsImport(0);
        if (StringUtils.isEmpty(entity.getCompanyImg())){
            throw new BaseException(String.format("%1$s,请上传营业执照", entity.getCompanyName()));
        }
@@ -195,15 +203,8 @@
            if (nameCount>0){
                throw new BaseException(String.format("%1$s,企业名已存在!", entity.getCompanyName()));
            }
            int phoneCount = this.count(lq().eq(SysCompany::getCompanyPhone, entity.getCompanyPhone()));
            if (phoneCount>0){
                throw new BaseException(String.format("%1$s,企业联系电话已存在!", entity.getCompanyName()));
            }
            SysUser user = new SysUser();
            user.setPhonenumber(entity.getCompanyPhone());
            if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
                throw new BaseException(String.format("%1$s,企业联系电话已存在,请更换手机号!", entity.getCompanyName()));
            }
        } else {
            int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
            if (codeCount>0){
@@ -212,10 +213,6 @@
            int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
            if (nameCount>0){
                throw new BaseException(String.format("%1$s,企业名已存在!", entity.getCompanyName()));
            }
            int phoneCount = this.count(lq().eq(SysCompany::getCompanyPhone, entity.getCompanyPhone()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
            if (phoneCount>0){
                throw new BaseException(String.format("%1$s,企业联系电话已存在!", entity.getCompanyName()));
            }
        }
    }
@@ -235,6 +232,10 @@
        }
        if (StringUtils.isEmpty(entity.getCompanyPhone())){
            errorList.add(new ImportError(3, "请填写企业联系人电话!"));
        } else {
            if (entity.getCompanyPhone().length()!=11){
                errorList.add(new ImportError(3, "请填写正确手机号!"));
            }
        }
        if (StringUtils.isEmpty(entity.getCompanyAddress())){
            errorList.add(new ImportError(4, "请填写企业地址!"));
@@ -247,15 +248,6 @@
        if (nameCount>0){
            errorList.add(new ImportError(0, "企业名已存在!"));
        }
        int phoneCount = this.count(lq().eq(SysCompany::getCompanyPhone, entity.getCompanyPhone()));
        SysUser user = new SysUser();
        user.setPhonenumber(entity.getCompanyPhone());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            errorList.add(new ImportError(3, "企业联系电话系统中已存在,请更换电话!"));
        }
        if (phoneCount>0){
            errorList.add(new ImportError(3, "企业联系电话已存在!"));
        }
        entity.setCheckStatus(2);
    }
@@ -264,7 +256,8 @@
     * @param entity    企业信息
     */
    @Async
    protected void addCompanyUser(SysCompany entity, Integer isImport)
    @Override
    public void addCompanyUser(SysCompany entity, Integer isImport)
    {
        SysUser user = new SysUser();
@@ -308,11 +301,11 @@
    /**
     * 删除企业用户
     * @param companyId    企业id
     * @param companyPhone    企业id
     */
    protected boolean delCompanyUser(Long companyId)
    protected boolean delCompanyUser(String companyPhone)
    {
        return userService.deleteUserByDeptId(companyId)>0;
        return userService.deleteUserByCompanyPhone(companyPhone)>0;
    }
    /**
@@ -346,4 +339,9 @@
        }
        return qw;
    }
    private Boolean isAddUser(String phone)
    {
       return userService.count(userService.lq().eq(SysUser::getUserName, phone).eq(SysUser::getDelFlag,0))==0;
    }
}