| | |
| | | package com.project.system.service.impl; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | 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.common.utils.StringUtils; |
| | | import com.project.common.utils.file.ImageUtils; |
| | | import com.project.system.domain.SysCompany; |
| | | import com.project.system.domain.bo.editBo.SysCompanyBo; |
| | | import com.project.system.domain.bo.queryBo.SysCompanyQueryBo; |
| | | import com.project.system.domain.vo.SysCompanyResultVo; |
| | | import com.project.system.domain.vo.SysCompanyVo; |
| | | import com.project.system.mapper.SysCompanyMapper; |
| | | import com.project.system.service.ISysCompanyService; |
| | | import com.project.system.service.ISysUserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.project.common.utils.StringUtils; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.project.system.domain.vo.SysCompanyVo; |
| | | import com.project.system.domain.bo.editBo.SysCompanyBo; |
| | | import com.project.system.domain.bo.queryBo.SysCompanyQueryBo; |
| | | import com.project.system.domain.SysCompany; |
| | | import com.project.system.mapper.SysCompanyMapper; |
| | | import com.project.system.service.ISysCompanyService; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | |
| | | public SysCompanyVo queryById(Long companyId) |
| | | { |
| | | SysCompany db = this.baseMapper.selectById(companyId); |
| | | if (StringUtils.isEmpty(db.getQrcodeImg())){ |
| | | String qrcode = ImageUtils.createQrcode(db.getCompanyCode(), db.getCompanyName(), "2"); |
| | | db.setQrcodeImg(qrcode); |
| | | this.updateById(db); |
| | | } |
| | | return Convert.convert(SysCompanyVo.class , db); |
| | | } |
| | | |
| | |
| | | public Boolean insertByBo(SysCompanyBo bo) |
| | | { |
| | | SysCompany add = Convert.convert(SysCompany.class, bo); |
| | | validEntityBeforeSave(add, 0); |
| | | add.setCompanyStatus(1); |
| | | validEntityBeforeSave(add); |
| | | String qrcode = ImageUtils.createQrcode(add.getCompanyCode(), add.getCompanyName(), "2"); |
| | | add.setQrcodeImg(qrcode); |
| | | |
| | | boolean save = this.save(add); |
| | | addCompanyUser(add); |
| | | if (isAddUser(bo.getCompanyPhone())){ |
| | | addCompanyUser(add, 0); |
| | | } |
| | | return save; |
| | | } |
| | | |
| | |
| | | { |
| | | SysCompany old = this.getById(bo.getCompanyId()); |
| | | 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); |
| | | if (isAddUser(update.getCompanyPhone())){ |
| | | addCompanyUser(update, update.getCompanyStatus()==0 ? 1 : 0); |
| | | } |
| | | } |
| | | validEntityBeforeSave(update, 0); |
| | | return this.updateById(update); |
| | | } |
| | | |
| | | @Override//状态开关 |
| | | @Transactional |
| | | public Boolean updStatus(SysCompanyBo bo) |
| | | { |
| | | if (bo.getCompanyId()==null || bo.getCompanyStatus()==null) { |
| | | throw new BaseException("参数有误,状态修改失败!"); |
| | | } |
| | | SysCompany company = this.getById(bo.getCompanyId()); |
| | | company.setCompanyStatus(bo.getCompanyStatus()); |
| | | updCompanyUserStatus(company.getCompanyPhone(), bo.getCompanyStatus()); |
| | | return this.updateById(company); |
| | | } |
| | | |
| | | @Override//删除 |
| | |
| | | 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)); |
| | | } |
| | |
| | | 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); |
| | | |
| | | companies.forEach(this::addCompanyUser); |
| | | } |
| | | |
| | | @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()); |
| | | companies.forEach(e->{ |
| | | String qrcode = ImageUtils.createQrcode(e.getCompanyCode(), e.getCompanyName(), "2"); |
| | | e.setQrcodeImg(qrcode); |
| | | }); |
| | | boolean b = this.saveOrUpdateBatch(companies); |
| | | for (SysCompany company : companies) { |
| | | if (isAddUser(company.getCompanyPhone())){ |
| | | addCompanyUser(company,1); |
| | | } |
| | | } |
| | | return b; |
| | | } |
| | | |
| | | |
| | | //------------------------------------------------------------------------------------- |
| | | |
| | | |
| | | /** |
| | | * 保存前校验 |
| | | * @param entity 参数 |
| | | * @param isImport 是否导入:0否,1是 |
| | | */ |
| | | private void validEntityBeforeSave(SysCompany entity, int isImport) |
| | | private void validEntityBeforeSave(SysCompany entity) |
| | | { |
| | | 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())); |
| | |
| | | } |
| | | 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("请填写企业名!"); |
| | |
| | | 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()); |
| | | } else { |
| | | int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()).ne(SysCompany::getCompanyId,entity.getCompanyId())); |
| | | if (codeCount>0){ |
| | |
| | | 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())); |
| | | } |
| | | } |
| | | 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, "请填写企业联系人电话!")); |
| | | } else { |
| | | if (entity.getCompanyPhone().length()!=11){ |
| | | 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, "企业名已存在!")); |
| | | } |
| | | entity.setCheckStatus(2); |
| | | } |
| | | |
| | | /** |
| | | * 添加企业用户 |
| | | * @param entity 企业信息 |
| | | */ |
| | | @Async |
| | | protected void addCompanyUser(SysCompany entity) |
| | | @Override |
| | | public void addCompanyUser(SysCompany entity, Integer isImport) |
| | | { |
| | | |
| | | SysUser user = new SysUser(); |
| | | if (isImport==1){ |
| | | user.setStatus("0"); |
| | | } else { |
| | | user.setStatus("1"); |
| | | } |
| | | SysCompany one = this.getOne(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode())); |
| | | String phone = entity.getCompanyPhone(); |
| | | user.setPhonenumber(phone); |
| | | user.setUserName(entity.getCompanyUser()); |
| | | user.setUserName(phone); |
| | | user.setNickName(entity.getCompanyUser()); |
| | | user.setPassword(phone); |
| | | user.setDeptId(one.getCompanyId()); |
| | | user.setUserType("02"); |
| | | user.setRecommendUser(user.getPassword()); |
| | | if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) |
| | | { |
| | | return; |
| | |
| | | { |
| | | return; |
| | | } |
| | | user.setCreateBy(SecurityUtils.getUsername()); |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
| | | user.setRecommendUser(user.getPassword()); |
| | | userService.insertUser(user); |
| | | } |
| | | |
| | | @Async |
| | | protected void updCompanyUserStatus(String phone, Integer status) |
| | | { |
| | | userService.update(userService.lu().set(SysUser::getStatus,status).eq(SysUser::getPhonenumber, phone)); |
| | | } |
| | | |
| | | /** |
| | | * 删除企业用户 |
| | | * @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; |
| | | } |
| | | |
| | | /** |
| | |
| | | qw.eq(StringUtils.isNotEmpty(bo.getCompanyPhone()), "company_phone", bo.getCompanyPhone()); |
| | | qw.eq(StringUtils.isNotEmpty(bo.getCompanyAddress()), "company_address", bo.getCompanyAddress()); |
| | | qw.eq(StringUtils.isNotEmpty(bo.getCompanyImg()), "company_img", bo.getCompanyImg()); |
| | | qw.eq(StringUtils.isNotEmpty(bo.getCompanyType()), "company_type", bo.getCompanyType()); |
| | | qw.eq(bo.getIsImport() != null, "is_import", bo.getIsImport()); |
| | | qw.eq(bo.getCompanyStatus() != null, "company_status", bo.getCompanyStatus()); |
| | | qw.eq(StringUtils.isNotEmpty(bo.getRegionCode()), "region_code", bo.getRegionCode()); |
| | | qw.like(StringUtils.isNotEmpty(bo.getRegionName()), "region_name", bo.getRegionName()); |
| | |
| | | } |
| | | return qw; |
| | | } |
| | | |
| | | private Boolean isAddUser(String phone) |
| | | { |
| | | return userService.count(userService.lq().eq(SysUser::getUserName, phone).eq(SysUser::getDelFlag,0))==0; |
| | | } |
| | | } |