|  |  |  | 
|---|
|  |  |  | import com.ruoyi.common.utils.SecurityUtils; | 
|---|
|  |  |  | import com.ruoyi.common.utils.StringUtils; | 
|---|
|  |  |  | import com.ruoyi.system.dto.TCrmBranchDTO; | 
|---|
|  |  |  | import com.ruoyi.system.export.TCrmBranchExport; | 
|---|
|  |  |  | import com.ruoyi.system.mapper.SysUserMapper; | 
|---|
|  |  |  | import com.ruoyi.system.mapper.TCrmBranchMapper; | 
|---|
|  |  |  | import com.ruoyi.system.model.TCrmBranch; | 
|---|
|  |  |  | import com.ruoyi.system.model.TCrmBranchArea; | 
|---|
|  |  |  | import com.ruoyi.system.model.TCrmBranchSalary; | 
|---|
|  |  |  | import com.ruoyi.system.query.TCrmBranchQuery; | 
|---|
|  |  |  | import com.ruoyi.system.query.TSysAppUserQuery; | 
|---|
|  |  |  | import com.ruoyi.system.service.ISysUserService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TCrmBranchAreaService; | 
|---|
|  |  |  | import com.ruoyi.system.service.TCrmBranchSalaryService; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PageInfo<TCrmBranchVO> pageList(TCrmBranchQuery query) { | 
|---|
|  |  |  | PageInfo<TCrmBranchVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); | 
|---|
|  |  |  | List<TCrmBranchArea> crmBranchAreas = new ArrayList<>(); | 
|---|
|  |  |  | if(StringUtils.isNotEmpty(query.getCityCode())){ | 
|---|
|  |  |  | crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class) | 
|---|
|  |  |  | .eq(TCrmBranchArea::getCityCode, query.getCityCode())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(!StringUtils.isNotEmpty(query.getCityCode()) && StringUtils.isNotEmpty(query.getProvinceCode())){ | 
|---|
|  |  |  | if(StringUtils.isNotEmpty(query.getProvinceCode())){ | 
|---|
|  |  |  | crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class) | 
|---|
|  |  |  | .eq(TCrmBranchArea::getProvinceCode, query.getProvinceCode())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!CollectionUtils.isEmpty(crmBranchAreas)){ | 
|---|
|  |  |  | List<String> branchIds = crmBranchAreas.stream().map(TCrmBranchArea::getBranchId).collect(Collectors.toList()); | 
|---|
|  |  |  | query.setBranchIds(branchIds); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | return pageInfo; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | PageInfo<TCrmBranchVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); | 
|---|
|  |  |  | List<TCrmBranchVO> list = this.baseMapper.pageList(query,pageInfo); | 
|---|
|  |  |  | if(CollectionUtils.isEmpty(list)){ | 
|---|
|  |  |  | return pageInfo; | 
|---|
|  |  |  | 
|---|
|  |  |  | if(Objects.nonNull(sysUser)){ | 
|---|
|  |  |  | return R.fail(dto.getPhone()+"-账号已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<TCrmBranchArea> branchAreas = dto.getBranchAreas(); | 
|---|
|  |  |  | // 判断是否已存在这些区域 | 
|---|
|  |  |  | if(!CollectionUtils.isEmpty(branchAreas)){ | 
|---|
|  |  |  | List<String> branchAreaCityCodes = branchAreas.stream().map(TCrmBranchArea::getCityCode).collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(!CollectionUtils.isEmpty(branchAreaCityCodes)){ | 
|---|
|  |  |  | long count = crmBranchAreaService.count(Wrappers.lambdaQuery(TCrmBranchArea.class).in(TCrmBranchArea::getCityCode, branchAreaCityCodes)); | 
|---|
|  |  |  | if(count > 0){ | 
|---|
|  |  |  | return R.fail("区域已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.save(dto); | 
|---|
|  |  |  | // 添加区域 | 
|---|
|  |  |  | List<TCrmBranchArea> branchAreas = dto.getBranchAreas(); | 
|---|
|  |  |  | branchAreas.forEach(t -> t.setBranchId(dto.getId())); | 
|---|
|  |  |  | crmBranchAreaService.saveBatch(branchAreas); | 
|---|
|  |  |  | // 添加职位薪资 | 
|---|
|  |  |  | 
|---|
|  |  |  | return R.fail(dto.getPhone()+"-账号已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | SysUser user = sysUserService.selectUserById(dto.getUserId()); | 
|---|
|  |  |  | List<TCrmBranchArea> branchAreas = dto.getBranchAreas(); | 
|---|
|  |  |  | // 判断是否已存在这些区域 | 
|---|
|  |  |  | if(!CollectionUtils.isEmpty(branchAreas)){ | 
|---|
|  |  |  | List<String> branchAreaCityCodes = branchAreas.stream().map(TCrmBranchArea::getCityCode).collect(Collectors.toList()); | 
|---|
|  |  |  | long count = crmBranchAreaService.count(Wrappers.lambdaQuery(TCrmBranchArea.class).in(TCrmBranchArea::getCityCode, branchAreaCityCodes) | 
|---|
|  |  |  | .ne(TCrmBranchArea::getBranchId, dto.getId())); | 
|---|
|  |  |  | if(count > 0){ | 
|---|
|  |  |  | return R.fail("区域存在重复"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.updateById(dto); | 
|---|
|  |  |  | // 删除区域 | 
|---|
|  |  |  | crmBranchAreaService.remove(Wrappers.lambdaQuery(TCrmBranchArea.class).eq(TCrmBranchArea::getBranchId, dto.getId())); | 
|---|
|  |  |  | // 添加区域 | 
|---|
|  |  |  | List<TCrmBranchArea> branchAreas = dto.getBranchAreas(); | 
|---|
|  |  |  | branchAreas.forEach(t -> t.setBranchId(dto.getId())); | 
|---|
|  |  |  | crmBranchAreaService.saveBatch(branchAreas); | 
|---|
|  |  |  | // 删除职位薪资 | 
|---|
|  |  |  | 
|---|
|  |  |  | user.setPhonenumber(dto.getPhone()); | 
|---|
|  |  |  | user.setUserName(dto.getPhone()); | 
|---|
|  |  |  | user.setNickName(dto.getUserName()); | 
|---|
|  |  |  | user.setPassword(SecurityUtils.encryptPassword(dto.getPassword())); | 
|---|
|  |  |  | if(StringUtils.isNotEmpty(dto.getPassword())){ | 
|---|
|  |  |  | user.setPassword(SecurityUtils.encryptPassword(dto.getPassword())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sysUserService.updateUser(user); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //        if(Objects.nonNull(user) && user.getUserName().equals(dto.getPhone()) && StringUtils.isNotEmpty(dto.getPassword())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<TCrmBranchExport> exportList(TCrmBranchQuery query) { | 
|---|
|  |  |  | List<TCrmBranchArea> crmBranchAreas = new ArrayList<>(); | 
|---|
|  |  |  | if(StringUtils.isNotEmpty(query.getCityCode())){ | 
|---|
|  |  |  | crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class) | 
|---|
|  |  |  | .eq(TCrmBranchArea::getCityCode, query.getCityCode())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(StringUtils.isNotEmpty(query.getProvinceCode())){ | 
|---|
|  |  |  | crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class) | 
|---|
|  |  |  | .eq(TCrmBranchArea::getProvinceCode, query.getProvinceCode())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!CollectionUtils.isEmpty(crmBranchAreas)){ | 
|---|
|  |  |  | List<String> branchIds = crmBranchAreas.stream().map(TCrmBranchArea::getBranchId).collect(Collectors.toList()); | 
|---|
|  |  |  | query.setBranchIds(branchIds); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | return new ArrayList<>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<TCrmBranchExport> list = this.baseMapper.exportList(query); | 
|---|
|  |  |  | if(CollectionUtils.isEmpty(list)){ | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<String> branchIds = list.stream().map(TCrmBranchExport::getId).collect(Collectors.toList()); | 
|---|
|  |  |  | List<TCrmBranchArea> crmBranchAreaList = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class) | 
|---|
|  |  |  | .in(TCrmBranchArea::getBranchId, branchIds)); | 
|---|
|  |  |  | // 查询职位薪资 | 
|---|
|  |  |  | List<TCrmBranchSalary> crmBranchSalaries = crmBranchSalaryService.list(Wrappers.lambdaQuery(TCrmBranchSalary.class) | 
|---|
|  |  |  | .in(TCrmBranchSalary::getBranchId, branchIds)); | 
|---|
|  |  |  | for (TCrmBranchExport crmBranchExport : list) { | 
|---|
|  |  |  | List<TCrmBranchArea> tCrmBranchAreas = crmBranchAreaList.stream().filter(t -> t.getBranchId().equals(crmBranchExport.getId())).collect(Collectors.toList()); | 
|---|
|  |  |  | StringBuilder stringBuilder = new StringBuilder(); | 
|---|
|  |  |  | for (TCrmBranchArea tCrmBranchArea : tCrmBranchAreas) { | 
|---|
|  |  |  | stringBuilder.append(tCrmBranchArea.getProvinceName()).append("-").append(tCrmBranchArea.getCityName()).append(","); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 去除',' | 
|---|
|  |  |  | stringBuilder.deleteCharAt(stringBuilder.length()-1); | 
|---|
|  |  |  | crmBranchExport.setAreaNames(stringBuilder.toString()); | 
|---|
|  |  |  | // 职位薪资 | 
|---|
|  |  |  | List<TCrmBranchSalary> crmBranchSalariesList = crmBranchSalaries.stream().filter(t -> t.getBranchId().equals(crmBranchExport.getId())).collect(Collectors.toList()); | 
|---|
|  |  |  | crmBranchExport.setCrmBranchSalaries(crmBranchSalariesList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|