package com.ruoyi.system.service.impl;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.common.basic.PageInfo;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.system.dto.TCrmBranchDTO;
|
import com.ruoyi.system.export.TCrmBranchAuditExport;
|
import com.ruoyi.system.export.TCrmBranchExport;
|
import com.ruoyi.system.mapper.SysUserMapper;
|
import com.ruoyi.system.mapper.TCrmBranchLiftMapper;
|
import com.ruoyi.system.mapper.TCrmBranchMapper;
|
import com.ruoyi.system.model.TCrmBranch;
|
import com.ruoyi.system.model.TCrmBranchArea;
|
import com.ruoyi.system.model.TCrmBranchLift;
|
import com.ruoyi.system.model.TCrmBranchSalary;
|
import com.ruoyi.system.query.TCrmBranchAuditQuery;
|
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;
|
import com.ruoyi.system.service.TCrmBranchService;
|
import com.ruoyi.system.vo.TCrmBranchLiftVO;
|
import com.ruoyi.system.vo.TCrmBranchVO;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* crm分公司管理 服务实现类
|
* </p>
|
*
|
* @author xiaochen
|
* @since 2025-08-20
|
*/
|
@Service
|
public class TCrmBranchServiceImpl extends ServiceImpl<TCrmBranchMapper, TCrmBranch> implements TCrmBranchService {
|
|
@Autowired
|
private TCrmBranchAreaService crmBranchAreaService;
|
@Autowired
|
private TCrmBranchSalaryService crmBranchSalaryService;
|
@Autowired
|
private ISysUserService sysUserService;
|
@Autowired
|
private TCrmBranchLiftMapper crmBranchLiftMapper;
|
|
@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.getDistrictCode())){
|
crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class)
|
.eq(TCrmBranchArea::getDistrictCode, query.getDistrictCode())
|
.eq(TCrmBranchArea::getAreaType, 2));
|
}
|
|
if(StringUtils.isNotEmpty(query.getCityCode())){
|
crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class)
|
.eq(TCrmBranchArea::getCityCode, query.getCityCode())
|
.eq(TCrmBranchArea::getAreaType, 2));
|
}
|
|
if(StringUtils.isNotEmpty(query.getProvinceCode())){
|
crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class)
|
.eq(TCrmBranchArea::getProvinceCode, query.getProvinceCode())
|
.eq(TCrmBranchArea::getAreaType, 2));
|
}
|
if(!CollectionUtils.isEmpty(crmBranchAreas)){
|
List<String> branchIds = crmBranchAreas.stream().map(TCrmBranchArea::getBranchId).collect(Collectors.toList());
|
query.setBranchIds(branchIds);
|
}
|
|
if(query.getRoleType() == 2){
|
// 查询分公司账号
|
TCrmBranch crmBranch = this.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
|
.eq(TCrmBranch::getUserId, query.getUserId())
|
.eq(TCrmBranch::getStatus, 1)
|
.last("LIMIT 1"));
|
List<TCrmBranch> crmBranches = this.list(Wrappers.lambdaQuery(TCrmBranch.class)
|
.eq(TCrmBranch::getProvinceBranchId, crmBranch.getId()));
|
if(CollectionUtils.isEmpty(crmBranches)){
|
return pageInfo;
|
}
|
List<String> branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
|
query.setBranchIdList(branchIds);
|
}
|
|
List<TCrmBranchVO> list = this.baseMapper.pageList(query,pageInfo);
|
if(CollectionUtils.isEmpty(list)){
|
return pageInfo;
|
}
|
List<String> branchIds = list.stream().map(TCrmBranchVO::getId).collect(Collectors.toList());
|
List<TCrmBranchArea> crmBranchAreaList = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class)
|
.in(TCrmBranchArea::getBranchId, branchIds));
|
// 查询是否需要置灰解除按钮
|
List<TCrmBranchLift> crmBranchLifts = crmBranchLiftMapper.selectList(Wrappers.lambdaQuery(TCrmBranchLift.class)
|
.in(TCrmBranchLift::getBranchId, branchIds)
|
.eq(TCrmBranchLift::getStatus, 1));
|
for (TCrmBranchVO tCrmBranchVO : list) {
|
List<TCrmBranchArea> tCrmBranchAreas = crmBranchAreaList.stream().filter(t -> t.getBranchId().equals(tCrmBranchVO.getId())).collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(tCrmBranchAreas)){
|
tCrmBranchVO.setProvinceName(tCrmBranchAreas.get(0).getProvinceName());
|
}
|
if(!CollectionUtils.isEmpty(crmBranchLifts)){
|
long count = crmBranchLifts.stream().filter(t -> t.getBranchId().equals(tCrmBranchVO.getId())).count();
|
tCrmBranchVO.setIsGrayOut(count > 0);
|
}
|
}
|
pageInfo.setRecords(list);
|
return pageInfo;
|
}
|
|
@Override
|
public R<Boolean> addBranch(TCrmBranchDTO dto) {
|
if (isExit(dto)) {
|
return R.fail("crm分公司管理名称已存在");
|
}
|
// 判断账号是否已存在
|
SysUser sysUser = sysUserService.selectUserByUserName(dto.getPhone());
|
if(Objects.nonNull(sysUser)){
|
return R.fail(dto.getPhone()+"-账号已存在");
|
}
|
List<TCrmBranchArea> branchAreas = dto.getBranchAreas();
|
if(dto.getPersonType() != 1 && CollectionUtils.isEmpty(branchAreas)){
|
return R.fail("请选择管辖区域");
|
}
|
List<TCrmBranchArea> responsibleBranchAreas = dto.getResponsibleBranchAreas();
|
if(dto.getPersonType() != 3 && CollectionUtils.isEmpty(responsibleBranchAreas)){
|
return R.fail("请选择负责区域");
|
}
|
if(dto.getPersonType() != 3){
|
// 判断是否已存在这些区域
|
List<String> branchAreaCityCodes = responsibleBranchAreas.stream().map(TCrmBranchArea::getCityCode).collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(branchAreaCityCodes)){
|
long count = crmBranchAreaService.count(Wrappers.lambdaQuery(TCrmBranchArea.class)
|
.in(TCrmBranchArea::getCityCode, branchAreaCityCodes)
|
.eq(TCrmBranchArea::getPersonType,dto.getPersonType())
|
.eq(TCrmBranchArea::getAreaType,1));
|
if(count > 0){
|
return R.fail("负责区域已存在");
|
}
|
}
|
}
|
if(dto.getPersonType() == 2){
|
// 查询一级
|
TCrmBranch crmBranch = this.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
|
.eq(TCrmBranch::getUserId, dto.getCorrelationPersonId())
|
.last("LIMIT 1"));
|
dto.setProvinceBranchId(crmBranch.getId());
|
}
|
|
if(dto.getPersonType() == 3){
|
// 查询二级
|
TCrmBranch crmBranch = this.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
|
.eq(TCrmBranch::getUserId, dto.getCorrelationPersonId())
|
.last("LIMIT 1"));
|
// 查询一级
|
TCrmBranch crmBranch1 = this.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
|
.eq(TCrmBranch::getUserId, crmBranch.getCorrelationPersonId())
|
.last("LIMIT 1"));
|
dto.setProvinceBranchId(crmBranch1.getId());
|
}
|
|
this.save(dto);
|
List<TCrmBranchArea> allAreas = new ArrayList<>();
|
if(dto.getPersonType() != 3){
|
for (TCrmBranchArea responsibleBranchArea : responsibleBranchAreas) {
|
responsibleBranchArea.setBranchId(dto.getId());
|
responsibleBranchArea.setAreaType(1);
|
responsibleBranchArea.setPersonType(dto.getPersonType());
|
}
|
allAreas.addAll(responsibleBranchAreas);
|
}
|
if(dto.getPersonType() != 1){
|
// 添加区域
|
for (TCrmBranchArea branchArea : branchAreas) {
|
branchArea.setBranchId(dto.getId());
|
branchArea.setAreaType(2);
|
}
|
allAreas.addAll(branchAreas);
|
}
|
crmBranchAreaService.saveBatch(allAreas);
|
// 添加职位薪资
|
List<TCrmBranchSalary> branchSalaries = dto.getBranchSalaries();
|
branchSalaries.forEach(t -> t.setBranchId(dto.getId()));
|
crmBranchSalaryService.saveBatch(branchSalaries);
|
// 添加账号
|
SysUser user = new SysUser();
|
user.setUserName(dto.getPhone());
|
user.setPhonenumber(dto.getPhone());
|
user.setNickName(dto.getUserName());
|
user.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
|
user.setStatus("0");
|
user.setDelFlag("0");
|
user.setRoleType(2);
|
user.setRoleId(2L);
|
sysUserService.insertUser(user);
|
dto.setUserId(user.getUserId());
|
this.updateById(dto);
|
return R.ok();
|
}
|
|
@Override
|
public boolean isExit(TCrmBranchDTO dto) {
|
if(StringUtils.isNotEmpty(dto.getId())){
|
// 修改
|
return this.count(Wrappers.lambdaQuery(TCrmBranch.class).ne(TCrmBranch::getId, dto.getId()).eq(TCrmBranch::getBranchName, dto.getBranchName())) > 0;
|
}else {
|
// 新增
|
return this.count(Wrappers.lambdaQuery(TCrmBranch.class).eq(TCrmBranch::getBranchName, dto.getBranchName())) > 0;
|
}
|
}
|
|
@Override
|
public R<Boolean> updateBranch(TCrmBranchDTO dto) {
|
if (isExit(dto)) {
|
return R.fail("crm分公司管理名称已存在");
|
}
|
// 判断账号是否已存在
|
SysUser sysUser1 = sysUserService.selectUserByUserName(dto.getPhone());
|
if(Objects.nonNull(sysUser1) && !sysUser1.getUserId().equals(dto.getUserId())){
|
return R.fail(dto.getPhone()+"-账号已存在");
|
}
|
SysUser user = sysUserService.selectUserById(dto.getUserId());
|
List<TCrmBranchArea> branchAreas = dto.getBranchAreas();
|
if(dto.getPersonType() != 1 && CollectionUtils.isEmpty(branchAreas)){
|
return R.fail("请选择管辖区域");
|
}
|
List<TCrmBranchArea> responsibleBranchAreas = dto.getResponsibleBranchAreas();
|
if(dto.getPersonType() != 3 && CollectionUtils.isEmpty(responsibleBranchAreas)){
|
return R.fail("请选择负责区域");
|
}
|
if(dto.getPersonType() != 3){
|
// 判断是否已存在这些区域
|
List<String> branchAreaCityCodes = responsibleBranchAreas.stream().map(TCrmBranchArea::getCityCode).collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(branchAreaCityCodes)){
|
long count = crmBranchAreaService.count(Wrappers.lambdaQuery(TCrmBranchArea.class)
|
.in(TCrmBranchArea::getCityCode, branchAreaCityCodes)
|
.eq(TCrmBranchArea::getAreaType,1)
|
.eq(TCrmBranchArea::getPersonType,dto.getPersonType())
|
.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> allAreas = new ArrayList<>();
|
if(dto.getPersonType() != 3){
|
for (TCrmBranchArea responsibleBranchArea : responsibleBranchAreas) {
|
responsibleBranchArea.setBranchId(dto.getId());
|
responsibleBranchArea.setAreaType(1);
|
responsibleBranchArea.setPersonType(dto.getPersonType());
|
}
|
allAreas.addAll(responsibleBranchAreas);
|
}
|
if(dto.getPersonType() != 1){
|
// 添加区域
|
for (TCrmBranchArea branchArea : branchAreas) {
|
branchArea.setBranchId(dto.getId());
|
branchArea.setAreaType(2);
|
}
|
allAreas.addAll(branchAreas);
|
}
|
crmBranchAreaService.saveBatch(allAreas);
|
// 删除职位薪资
|
crmBranchSalaryService.remove(Wrappers.lambdaQuery(TCrmBranchSalary.class).eq(TCrmBranchSalary::getBranchId, dto.getId()));
|
// 添加职位薪资
|
List<TCrmBranchSalary> branchSalaries = dto.getBranchSalaries();
|
branchSalaries.forEach(t -> t.setBranchId(dto.getId()));
|
crmBranchSalaryService.saveBatch(branchSalaries);
|
if(Objects.nonNull(user)){
|
// 修改账号
|
user.setPhonenumber(dto.getPhone());
|
user.setUserName(dto.getPhone());
|
user.setNickName(dto.getUserName());
|
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())) {
|
// // 修改密码
|
// user.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
|
// sysUserService.updateUser(user);
|
// }
|
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);
|
}
|
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();
|
// 特殊区域
|
StringBuilder stringBuilderResponsible = new StringBuilder();
|
for (TCrmBranchArea tCrmBranchArea : tCrmBranchAreas) {
|
if(tCrmBranchArea.getAreaType() == 1){
|
stringBuilderResponsible.append(tCrmBranchArea.getProvinceName()).append("-").append(tCrmBranchArea.getCityName()).append(",");
|
}else {
|
stringBuilder.append(tCrmBranchArea.getProvinceName()).append("-").append(tCrmBranchArea.getCityName()).append(",");
|
}
|
}
|
// 去除','
|
stringBuilder.deleteCharAt(stringBuilder.length()-1);
|
crmBranchExport.setAreaNames(stringBuilder.toString());
|
|
stringBuilderResponsible.deleteCharAt(stringBuilderResponsible.length()-1);
|
crmBranchExport.setResponsibleAreaNames(stringBuilderResponsible.toString());
|
// 职位薪资
|
List<TCrmBranchSalary> crmBranchSalariesList = crmBranchSalaries.stream().filter(t -> t.getBranchId().equals(crmBranchExport.getId())).collect(Collectors.toList());
|
crmBranchExport.setCrmBranchSalaries(crmBranchSalariesList);
|
}
|
return list;
|
}
|
|
@Override
|
public PageInfo<TCrmBranchVO> pageAuditList(TCrmBranchAuditQuery query) {
|
PageInfo<TCrmBranchVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
|
List<TCrmBranchVO> list = this.baseMapper.pageAuditList(query,pageInfo);
|
if(CollectionUtils.isEmpty(list)){
|
return pageInfo;
|
}
|
List<String> branchIds = list.stream().map(TCrmBranchVO::getProvinceBranchId).collect(Collectors.toList());
|
List<TCrmBranch> crmBranches = this.list(Wrappers.lambdaQuery(TCrmBranch.class).in(TCrmBranch::getId, branchIds));
|
for (TCrmBranchVO tCrmBranchVO : list) {
|
List<TCrmBranch> crmBranchList = crmBranches.stream().filter(t -> tCrmBranchVO.getProvinceBranchId().equals(t.getId())).collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(crmBranchList)){
|
tCrmBranchVO.setProvinceBranchName(crmBranchList.get(0).getBranchName());
|
}
|
}
|
pageInfo.setRecords(list);
|
return pageInfo;
|
}
|
|
@Override
|
public PageInfo<TCrmBranchLiftVO> pageUnbindList(TCrmBranchAuditQuery query) {
|
PageInfo<TCrmBranchLiftVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
|
List<TCrmBranchLiftVO> list = this.baseMapper.pageUnbindList(query,pageInfo);
|
if(CollectionUtils.isEmpty(list)){
|
return pageInfo;
|
}
|
List<String> branchIds = list.stream().map(TCrmBranchLiftVO::getProvinceBranchId).collect(Collectors.toList());
|
List<TCrmBranch> crmBranches = this.list(Wrappers.lambdaQuery(TCrmBranch.class).in(TCrmBranch::getId, branchIds));
|
for (TCrmBranchLiftVO crmBranchLiftVO : list) {
|
List<TCrmBranch> crmBranchList = crmBranches.stream().filter(t -> crmBranchLiftVO.getProvinceBranchId().equals(t.getId())).collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(crmBranchList)){
|
crmBranchLiftVO.setProvinceBranchName(crmBranchList.get(0).getBranchName());
|
}
|
}
|
pageInfo.setRecords(list);
|
return pageInfo;
|
}
|
|
@Override
|
public List<TCrmBranchAuditExport> exportAuditList(TCrmBranchAuditQuery query) {
|
List<TCrmBranchAuditExport> list = this.baseMapper.exportAuditList(query);
|
if(CollectionUtils.isEmpty(list)){
|
return new ArrayList<>();
|
}
|
List<String> branchIds = list.stream().map(TCrmBranchAuditExport::getProvinceBranchId).collect(Collectors.toList());
|
List<TCrmBranch> crmBranches = this.list(Wrappers.lambdaQuery(TCrmBranch.class).in(TCrmBranch::getId, branchIds));
|
for (TCrmBranchAuditExport crmBranchAuditExport : list) {
|
List<TCrmBranch> crmBranchList = crmBranches.stream().filter(t -> crmBranchAuditExport.getProvinceBranchId().equals(t.getId())).collect(Collectors.toList());
|
if(!CollectionUtils.isEmpty(crmBranchList)){
|
crmBranchAuditExport.setProvinceBranchName(crmBranchList.get(0).getBranchName());
|
}
|
}
|
return list;
|
}
|
}
|