liujie
2025-09-02 76bbe4207f2d623f5e893af92022460d6b763734
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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>
 * crm分公司管理 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@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;
        }
    }
}