|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictBiz; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.doumee.dao.business.join.CompanyJoinMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Company; | 
|---|
|  |  |  | import com.doumee.dao.business.model.dto.CompanyCreatOrUpdateRequest; | 
|---|
|  |  |  | import com.doumee.dao.system.SystemDictMapper; | 
|---|
|  |  |  | import com.doumee.dao.system.SystemUserMapper; | 
|---|
|  |  |  | import com.doumee.dao.system.SystemUserRoleMapper; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDict; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemDictData; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemUser; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemUserRole; | 
|---|
|  |  |  | import com.doumee.service.business.CompanyService; | 
|---|
|  |  |  | import com.doumee.service.system.SystemDictService; | 
|---|
|  |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
|---|
|  |  |  | import org.apache.commons.lang3.RandomStringUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | if (Objects.nonNull(company1)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"企业已存在请重新输入"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | wrapper.clear(); | 
|---|
|  |  |  | wrapper.lambda() | 
|---|
|  |  |  | .eq(Company::getDeleted,Boolean.FALSE) | 
|---|
|  |  |  | .eq(Company::getCreditCode,company.getCreditCode()); | 
|---|
|  |  |  | Company company2 = companyMapper.selectOne(wrapper); | 
|---|
|  |  |  | if (Objects.nonNull(company2)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"企业已存在请重新输入"); | 
|---|
|  |  |  | if(StringUtils.isNotBlank(company.getCreditCode())){ | 
|---|
|  |  |  | wrapper.clear(); | 
|---|
|  |  |  | wrapper.lambda() | 
|---|
|  |  |  | .eq(Company::getDeleted,Boolean.FALSE) | 
|---|
|  |  |  | .eq(Company::getCreditCode,company.getCreditCode()); | 
|---|
|  |  |  | Company company2 = companyMapper.selectOne(wrapper); | 
|---|
|  |  |  | if (Objects.nonNull(company2)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"企业统一社会信用代码已存在请重新输入"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Company insert = new Company(); | 
|---|
|  |  |  | insert.setDeleted((byte)Constants.ZERO); | 
|---|
|  |  |  | 
|---|
|  |  |  | insert.setOepnValidDate(company.getOepnValidDate()); | 
|---|
|  |  |  | insert.setOepnType(company.getOepnType()); | 
|---|
|  |  |  | insert.setSalesmanId(company.getSalesmanId()); | 
|---|
|  |  |  | insert.setStatus(Constants.ZERO); | 
|---|
|  |  |  | companyMapper.insert(insert); | 
|---|
|  |  |  | //创建企业用户 | 
|---|
|  |  |  | SystemUser companyUser = createCompanyUser(company.getManagerUserName(), insert.getId()); | 
|---|
|  |  |  | SystemUser companyUser = createCompanyUser(company, insert.getId()); | 
|---|
|  |  |  | Company update = new Company(); | 
|---|
|  |  |  | update.setId(insert.getId()); | 
|---|
|  |  |  | update.setManagerId(companyUser.getId()); | 
|---|
|  |  |  | 
|---|
|  |  |  | return company.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private SystemUser createCompanyUser(String managerUserName,Integer companyId){ | 
|---|
|  |  |  | private SystemUser createCompanyUser(CompanyCreatOrUpdateRequest company, Integer companyId){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | QueryWrapper<SystemUser> query = new QueryWrapper<>(); | 
|---|
|  |  |  | query.lambda() | 
|---|
|  |  |  | .eq(SystemUser::getType,Constants.ONE) | 
|---|
|  |  |  | .eq(SystemUser::getDeleted,Boolean.FALSE) | 
|---|
|  |  |  | .eq(SystemUser::getUsername,managerUserName); | 
|---|
|  |  |  | .eq(SystemUser::getUsername,company.getManagerUserName()); | 
|---|
|  |  |  | Integer integer = systemUserMapper.selectCount(query); | 
|---|
|  |  |  | if (integer > Constants.ZERO){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"账号已存在请重新输入账号"); | 
|---|
|  |  |  | 
|---|
|  |  |  | SystemDictData dictDataPassword = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.DEFAULT_PASSWORD); | 
|---|
|  |  |  | String salt = RandomStringUtils.randomAlphabetic(6); | 
|---|
|  |  |  | SystemUser user = new SystemUser(); | 
|---|
|  |  |  | user.setUsername(managerUserName); | 
|---|
|  |  |  | user.setUsername(company.getManagerUserName()); | 
|---|
|  |  |  | user.setPassword(Utils.Secure.encryptPassword(dictDataPassword.getCode(), salt)); | 
|---|
|  |  |  | user.setSalt(salt); | 
|---|
|  |  |  | user.setCompanyId(companyId); | 
|---|
|  |  |  | user.setMobile(company.getLinkephone()); | 
|---|
|  |  |  | user.setRealname(company.getLinkname()); | 
|---|
|  |  |  | user.setType(Constants.ONE); | 
|---|
|  |  |  | user.setCreateTime(new Date()); | 
|---|
|  |  |  | user.setUpdateTime(new Date()); | 
|---|
|  |  |  | 
|---|
|  |  |  | .set(Company::getShortName,company.getShortName()) | 
|---|
|  |  |  | .set(Company::getCreditCode,company.getCreditCode()) | 
|---|
|  |  |  | .set(Company::getSalesmanId,company.getSalesmanId()) | 
|---|
|  |  |  | .set(company.getOepnValidDate()!=null,Company::getOepnValidDate,company.getOepnValidDate()) | 
|---|
|  |  |  | .set(company.getUserNum()!=null,Company::getUserNum,company.getUserNum()) | 
|---|
|  |  |  | .set(Company::getLinkname,company.getLinkname()) | 
|---|
|  |  |  | .set(Company::getLinkephone,company.getLinkephone()); | 
|---|
|  |  |  | companyMapper.update(null,wrapper); | 
|---|
|  |  |  | 
|---|
|  |  |  | queryWrapper.selectAll(Company.class) | 
|---|
|  |  |  | .selectAs(SystemUser::getUsername,Company::getManagerUserName); | 
|---|
|  |  |  | queryWrapper.select( | 
|---|
|  |  |  | "(select su1.USERNAME from system_user su1 where  su1.id = t.MANAGER_ID) as managerUserName," + | 
|---|
|  |  |  | "(select su1.USERNAME from system_user su1 where   su1.id = t.MANAGER_ID) as managerUserName," + | 
|---|
|  |  |  | "(select su2.REALNAME from system_user su2 where  su2.id = t.SALESMAN_ID) as salesmanRealName," + | 
|---|
|  |  |  | "(select count(su3.id) from system_user su3 where  su3.COMPANY_ID = t.ID and su3.TYPE = 2) as anchorUserNum," + | 
|---|
|  |  |  | "(select count(su4.id) from system_user su4 where  su4.COMPANY_ID = t.ID and su4.TYPE = 1) as empUserNum," + | 
|---|
|  |  |  | "(select count(g.id) from goods g   where  g.COMPANY_ID = t.ID )goodsNum"); | 
|---|
|  |  |  | "(select count(su3.id) from system_user su3 where su3.deleted=0 and su3.COMPANY_ID = t.ID and su3.TYPE = 2) as anchorUserNum," + | 
|---|
|  |  |  | "(select count(su4.id) from system_user su4 where su4.deleted=0 and su4.COMPANY_ID = t.ID and su4.TYPE = 1) as empUserNum," + | 
|---|
|  |  |  | "(select count(g.id) from goods g   where g.isdeleted=0 and  g.COMPANY_ID = t.ID )goodsNum"); | 
|---|
|  |  |  | queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getName()),Company::getName,pageWrap.getModel().getName()) | 
|---|
|  |  |  | .like(StringUtils.isNotBlank(pageWrap.getModel().getManagerUserName()),SystemUser::getUsername,pageWrap.getModel().getManagerUserName()) | 
|---|
|  |  |  | .eq(Objects.nonNull(pageWrap.getModel().getManagerUserName()),Company::getStatus,pageWrap.getModel().getStatus()) | 
|---|
|  |  |  | .eq(Objects.nonNull(pageWrap.getModel().getStatus()),Company::getStatus,pageWrap.getModel().getStatus()) | 
|---|
|  |  |  | .between((Objects.nonNull(pageWrap.getModel().getOepnValidDateStart()) && Objects.nonNull(pageWrap.getModel().getOepnValidDateEnd())), | 
|---|
|  |  |  | Company::getOepnValidDate,pageWrap.getModel().getOepnValidDateStart(),pageWrap.getModel().getOepnValidDateEnd()); | 
|---|
|  |  |  | Company::getOepnValidDate,pageWrap.getModel().getOepnValidDateStart(),pageWrap.getModel().getOepnValidDateEnd()) | 
|---|
|  |  |  | .orderByDesc(Company::getCreateTime); | 
|---|
|  |  |  | return PageData.from(companyJoinMapper.selectJoinPage(page,Company.class,queryWrapper)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|