chenhj
2026-04-24 2e71bdbcdf853bb35e68016b84f0254f7366bfeb
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.CustomerPrivateDto;
import com.ruoyi.basic.mapper.CustomerPrivateMapper;
import com.ruoyi.basic.mapper.CustomerPrivatePoolMapper;
import com.ruoyi.basic.pojo.CustomerFollowUp;
import com.ruoyi.basic.pojo.CustomerPrivate;
import com.ruoyi.basic.pojo.CustomerPrivatePool;
import com.ruoyi.basic.pojo.CustomerReturnVisit;
import com.ruoyi.basic.service.CustomerFollowUpService;
import com.ruoyi.basic.service.CustomerPrivateService;
import com.ruoyi.basic.service.CustomerReturnVisitService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 客户档案 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-04-17 10:39:09
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class CustomerPrivateServiceImpl extends ServiceImpl<CustomerPrivateMapper, CustomerPrivate> implements CustomerPrivateService {
 
 
    private final CustomerPrivatePoolMapper customerPrivatePoolMapper;
 
    private final CustomerFollowUpService customerFollowUpService;
 
    private final CustomerReturnVisitService customerReturnVisitService;
    private final SalesLedgerMapper salesLedgerMapper;
 
    private final CustomerPrivateMapper customerPrivateMapper;
 
 
    @Override
    public Boolean add(CustomerPrivateDto customerPrivateDto) {
        //新增私海 客户
        this.save(customerPrivateDto);
        //新增私海记录
        CustomerPrivatePool customerPrivatePool = new CustomerPrivatePool();
        customerPrivatePool.setCustomerId(customerPrivateDto.getId());
        customerPrivatePool.setType(0L);
        customerPrivatePool.setBoundId(SecurityUtils.getLoginUser().getUserId());
        customerPrivatePoolMapper.insert(customerPrivatePool);
        return true;
    }
 
 
    @Override
    public Integer delete(List<Long> ids) {
        List<CustomerPrivatePool> customerPrivatePools = customerPrivatePoolMapper.selectList(new QueryWrapper<CustomerPrivatePool>().lambda().in(CustomerPrivatePool::getId, ids));
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().in(SalesLedger::getCustomerId, customerPrivatePools.stream().map(CustomerPrivatePool::getCustomerId).collect(Collectors.toList())));
        if (!CollectionUtils.isEmpty(salesLedgers)) {
            throw new RuntimeException("客户有销售合同,请先删除销售合同");
        }
        if (CollectionUtils.isEmpty(customerPrivatePools)) {
            throw new RuntimeException("客户不存在");
        }
        customerFollowUpService.remove(new QueryWrapper<CustomerFollowUp>().lambda().in(CustomerFollowUp::getCustomerPrivatePoolId, customerPrivatePools.stream().map(CustomerPrivatePool::getCustomerId).collect(Collectors.toList())));
        customerReturnVisitService.remove(new QueryWrapper<CustomerReturnVisit>().lambda().in(CustomerReturnVisit::getCustomerPrivatePoolId, customerPrivatePools.stream().map(CustomerPrivatePool::getCustomerId).collect(Collectors.toList())));
        customerPrivatePools.stream().forEach(customerPrivatePool -> {
            customerPrivatePool.setDeleteFlag(1);
            customerPrivatePoolMapper.updateById(customerPrivatePool);
        });
        return 1;
 
    }
 
    @Override
    public R importData(MultipartFile file) {
        try {
            List<CustomerPrivate> existingList = customerPrivateMapper.selectList(null);
            java.util.Set<String> existingCustomerNames = existingList.stream()
                    .map(CustomerPrivate::getCustomerName)
                    .collect(Collectors.toSet());
 
            ExcelUtil<CustomerPrivate> util = new ExcelUtil<CustomerPrivate>(CustomerPrivate.class);
            List<CustomerPrivate> userList = util.importExcel(file.getInputStream());
            if (CollectionUtils.isEmpty(userList)) {
                return R.fail("模板错误或导入数据为空");
            }
 
            int successCount = 0;
            int skipCount = 0;
            for (CustomerPrivate user : userList) {
                if (existingCustomerNames.contains(user.getCustomerName())) {
                    skipCount++;
                    continue;
                }
                CustomerPrivateDto customerPrivateDto = new CustomerPrivateDto();
                BeanUtils.copyProperties(user, customerPrivateDto);
                this.add(customerPrivateDto);
                existingCustomerNames.add(user.getCustomerName());
                successCount++;
            }
            return R.ok("导入完成,成功" + successCount + "条,跳过重复" + skipCount + "条");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("导入失败:" + e.getMessage());
        }
    }
}