xuhy
2025-09-02 3468014fba7730e1f94131d02074238f6fc0a8f1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TCrmBranchServiceImpl.java
@@ -1,10 +1,26 @@
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.utils.StringUtils;
import com.ruoyi.system.dto.TCrmBranchDTO;
import com.ruoyi.system.mapper.TCrmBranchAreaMapper;
import com.ruoyi.system.mapper.TCrmBranchMapper;
import com.ruoyi.system.model.TCrmBranch;
import com.ruoyi.system.model.TCrmBranchArea;
import com.ruoyi.system.model.TCrmDevice;
import com.ruoyi.system.query.TCrmBranchQuery;
import com.ruoyi.system.service.TCrmBranchService;
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.stream.Collectors;
/**
 * <p>
@@ -17,4 +33,59 @@
@Service
public class TCrmBranchServiceImpl extends ServiceImpl<TCrmBranchMapper, TCrmBranch> implements TCrmBranchService {
    @Autowired
    private TCrmBranchAreaMapper crmBranchAreaMapper;
    @Override
    public PageInfo<TCrmBranchVO> pageList(TCrmBranchQuery query) {
        List<TCrmBranchArea> crmBranchAreas = new ArrayList<>();
        if(StringUtils.isNotEmpty(query.getCityCode())){
            crmBranchAreas = crmBranchAreaMapper.selectList(Wrappers.lambdaQuery(TCrmBranchArea.class)
                    .eq(TCrmBranchArea::getCityCode, query.getCityCode()));
        }
        if(!StringUtils.isNotEmpty(query.getCityCode()) && StringUtils.isNotEmpty(query.getProvinceCode())){
            crmBranchAreas = crmBranchAreaMapper.selectList(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);
        }
        PageInfo<TCrmBranchVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        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 = crmBranchAreaMapper.selectList(Wrappers.lambdaQuery(TCrmBranchArea.class)
                .in(TCrmBranchArea::getBranchId, branchIds));
        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());
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public R<Boolean> add(TCrmBranchDTO dto) {
        if (isExit(dto)) {
            return R.fail("crm分公司管理名称已存在");
        }
        return null;
    }
    @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;
        }
    }
}