tuqin
2026-09-24 0891b6708cc09c2672a19661a8555aae0e742df1
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.ruoyi.aftersalesservice.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.aftersalesservice.dto.AfterSalesServiceImportDto;
import com.ruoyi.aftersalesservice.dto.AfterSalesServiceNewDto;
import com.ruoyi.aftersalesservice.dto.CountDto;
import com.ruoyi.aftersalesservice.mapper.AfterSalesServiceMapper;
import com.ruoyi.aftersalesservice.pojo.AfterSalesService;
import com.ruoyi.aftersalesservice.service.AfterSalesServiceService;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysDictDataMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.sales.dto.SalesLedgerDto;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.sales.service.ISalesLedgerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/7/30 9:26
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AfterSalesServiceServiceImpl extends ServiceImpl<AfterSalesServiceMapper, AfterSalesService> implements AfterSalesServiceService {
 
    private final AfterSalesServiceMapper afterSalesServiceMapper;
    private final SysDeptMapper sysDeptMapper;
    private final SysUserMapper sysUserMapper;
    private final ISalesLedgerProductService salesLedgerProductService;
    private final ISalesLedgerService salesLedgerService;
    private final com.ruoyi.sales.mapper.ShippingInfoMapper shippingInfoMapper;
    private final CustomerMapper customerMapper;
    private final SalesLedgerMapper salesLedgerMapper;
    private final SysDictDataMapper sysDictDataMapper;
 
    @Override
    public IPage<AfterSalesServiceNewDto> listPage(Page page, AfterSalesServiceNewDto afterSalesService) {
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        SysDept sysDept = sysDeptMapper.selectDeptById(tenantId);
        IPage<AfterSalesServiceNewDto> afterSalesServiceIPage = afterSalesServiceMapper.listPage(page, afterSalesService);
        afterSalesServiceIPage.getRecords().forEach(item -> {
            item.setDeptName(sysDept.getDeptName());
        });
        return afterSalesServiceIPage;
    }
 
    @Override
    public boolean addAfterSalesServiceDto(AfterSalesServiceNewDto afterSalesServiceNewDto) {
        afterSalesServiceNewDto.setStatus(1);
        if (afterSalesServiceNewDto.getProductModelIdList() != null && afterSalesServiceNewDto.getProductModelIdList().isEmpty() ) {
            String productModelIds = afterSalesServiceNewDto.getProductModelIdList().stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
            afterSalesServiceNewDto.setProductModelIds(productModelIds);
        }
        SysUser sysUser = sysUserMapper.selectUserById(afterSalesServiceNewDto.getCheckUserId());
        if(sysUser == null) throw new RuntimeException("审核人不存在");
        afterSalesServiceNewDto.setCheckNickName(sysUser.getNickName());
        if (StringUtils.isEmpty(afterSalesServiceNewDto.getAfterSalesServiceNo())) {
            String string = OrderUtils.countAfterServiceTodayByCreateTime(afterSalesServiceMapper, "SH_");
            afterSalesServiceNewDto.setAfterSalesServiceNo(string);
        }
        return this.save(afterSalesServiceNewDto);
    }
 
    @Override
    public AfterSalesServiceNewDto getAfterSalesServiceNewDtoById(Long id) {
        AfterSalesService afterSalesService = afterSalesServiceMapper.selectById(id);
        SalesLedger byId = salesLedgerService.getById(afterSalesService.getSalesLedgerId());
        List<Long> collect = Arrays.stream(afterSalesService.getProductModelIds().split(",")).map(Long::valueOf).collect(Collectors.toList());
        List<SalesLedgerProduct> list = salesLedgerProductService.list(new QueryWrapper<SalesLedgerProduct>().lambda().in(SalesLedgerProduct::getId, collect));
 
        if (StringUtils.isNotEmpty(afterSalesService.getProductModelQuantities())) {
            String[] quantities = afterSalesService.getProductModelQuantities().split(",");
            for (int i = 0; i < collect.size(); i++) {
                Long productId = collect.get(i);
                for (SalesLedgerProduct product : list) {
                    if (product.getId().equals(productId)) {
                        if (i < quantities.length && StringUtils.isNotEmpty(quantities[i])) {
                            product.setQuantity(new java.math.BigDecimal(quantities[i]));
                        }
                        break;
                    }
                }
            }
        }
 
        for (SalesLedgerProduct product : list) {
            com.ruoyi.sales.pojo.ShippingInfo shippingInfo = shippingInfoMapper.selectOne(
                new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.ruoyi.sales.pojo.ShippingInfo>()
                    .eq(com.ruoyi.sales.pojo.ShippingInfo::getSalesLedgerProductId, product.getId())
                    .orderByDesc(com.ruoyi.sales.pojo.ShippingInfo::getCreateTime)
                    .last("limit 1")
            );
            if (shippingInfo != null) {
                product.setShippingCarNumber(shippingInfo.getShippingCarNumber());
                product.setShippingDate(shippingInfo.getShippingDate());
                product.setShippingStatus(shippingInfo.getStatus());
                product.setExpressCompany(shippingInfo.getExpressCompany());
                product.setExpressNumber(shippingInfo.getExpressNumber());
            }
        }
 
        AfterSalesServiceNewDto afterSalesServiceNewDto = new AfterSalesServiceNewDto();
        BeanUtils.copyProperties(afterSalesService, afterSalesServiceNewDto);
        SalesLedgerDto salesLedgerDto = new SalesLedgerDto();
        BeanUtils.copyProperties(byId, salesLedgerDto);
        salesLedgerDto.setProductData( list);
        afterSalesServiceNewDto.setSalesLedgerDto(salesLedgerDto);
        return afterSalesServiceNewDto;
    }
 
    @Override
    public List<CountDto> countAfterSalesService() {
        List<CountDto> stringIntegerHashMap = afterSalesServiceMapper.countAfterSalesService();
        Integer total = 0;
        for (CountDto countDto : stringIntegerHashMap) {
            total += countDto.getCount();
        }
        CountDto countDto = new CountDto();
        countDto.setStatus(3);
        countDto.setCount(total);
        stringIntegerHashMap.add(countDto);
 
        return stringIntegerHashMap;
 
    }
 
    @Override
    public AjaxResult importData(MultipartFile file) {
        List<AfterSalesServiceImportDto> rows;
        try {
            ExcelUtil<AfterSalesServiceImportDto> util = new ExcelUtil<>(AfterSalesServiceImportDto.class);
            rows = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            log.error("反馈登记导入-解析excel失败", e);
            return AjaxResult.error("解析Excel失败:" + e.getMessage());
        }
        if (CollectionUtils.isEmpty(rows)) {
            return AjaxResult.error("导入文件无数据");
        }
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        Long userId = SecurityUtils.getUserId();
        int success = 0;
        List<String> errors = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            AfterSalesServiceImportDto row = rows.get(i);
            int rowNo = i + 2; // 首行为表头
            try {
                String customerName = trim(row.getCustomerName());
                if (StringUtils.isEmpty(customerName)) {
                    errors.add("第" + rowNo + "行:客户名称为空");
                    continue;
                }
                Customer customer = findCustomerByName(customerName, tenantId);
                if (customer == null) {
                    errors.add("第" + rowNo + "行:客户[" + customerName + "]不存在");
                    continue;
                }
 
                String serviceTypeName = trim(row.getServiceType());
                String serviceTypeValue = dictValueByLabel("post_sale_waiting_list", serviceTypeName);
                if (StringUtils.isEmpty(serviceTypeValue)) {
                    errors.add("第" + rowNo + "行:售后类型[" + serviceTypeName + "]不存在");
                    continue;
                }
 
                String urgencyName = trim(row.getUrgency());
                String urgencyValue = dictValueByLabel("degree_of_urgency", urgencyName);
                if (StringUtils.isEmpty(urgencyValue)) {
                    errors.add("第" + rowNo + "行:紧急程度[" + urgencyName + "]不存在");
                    continue;
                }
 
                Long salesLedgerId = null;
                String contractNo = trim(row.getSalesContractNo());
                if (StringUtils.isNotEmpty(contractNo)) {
                    SalesLedger ledger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
                            .eq(SalesLedger::getSalesContractNo, contractNo)
                            .eq(SalesLedger::getCustomerId, customer.getId())
                            .last("limit 1"));
                    if (ledger == null) {
                        ledger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
                                .eq(SalesLedger::getSalesContractNo, contractNo)
                                .last("limit 1"));
                    }
                    if (ledger == null) {
                        errors.add("第" + rowNo + "行:销售单号[" + contractNo + "]不存在");
                        continue;
                    }
                    salesLedgerId = ledger.getId();
                }
 
                AfterSalesServiceNewDto dto = new AfterSalesServiceNewDto();
                dto.setCustomerName(customer.getCustomerName());
                dto.setSalesLedgerId(salesLedgerId);
                dto.setServiceType(serviceTypeValue);
                dto.setUrgency(urgencyValue);
                dto.setProDesc(row.getProDesc());
                dto.setFeedbackDate(row.getFeedbackDate() != null ? row.getFeedbackDate() : new Date());
                dto.setCheckUserId(userId);
                dto.setProductModelIds("");
                dto.setProductModelQuantities("");
 
                if (addAfterSalesServiceDto(dto)) {
                    success++;
                } else {
                    errors.add("第" + rowNo + "行:保存失败");
                }
            } catch (Exception e) {
                log.error("反馈登记导入第{}行失败", rowNo, e);
                errors.add("第" + rowNo + "行:" + e.getMessage());
            }
        }
 
        AjaxResult result = AjaxResult.success();
        result.put("totalCount", rows.size());
        result.put("successCount", success);
        result.put("failCount", rows.size() - success);
        result.put("errors", errors);
        result.put("msg", "导入完成:成功 " + success + " 条,失败 " + (rows.size() - success) + " 条");
        return result;
    }
 
    private String trim(String value) {
        return value == null ? null : value.trim();
    }
 
    private Customer findCustomerByName(String name, Long tenantId) {
        List<Customer> customers = customerMapper.selectList(new LambdaQueryWrapper<Customer>()
                .eq(Customer::getCustomerName, name));
        if (CollectionUtils.isEmpty(customers)) {
            return null;
        }
        if (tenantId != null) {
            for (Customer c : customers) {
                if (tenantId.equals(c.getTenantId())) {
                    return c;
                }
            }
        }
        return customers.get(0);
    }
 
    private String dictValueByLabel(String dictType, String label) {
        if (StringUtils.isEmpty(label)) {
            return null;
        }
        List<SysDictData> datas = sysDictDataMapper.selectDictDataByType(dictType);
        if (CollectionUtils.isEmpty(datas)) {
            return null;
        }
        for (SysDictData d : datas) {
            if (label.equals(d.getDictLabel())) {
                return d.getDictValue();
            }
        }
        return null;
    }
}