云
2026-09-24 48e8e81666b2d0f49b931cb7b93729355758f2ae
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
package com.ruoyi.aftersalesservice.service.impl;
 
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.AfterSalesNearExpiryImportDto;
import com.ruoyi.aftersalesservice.mapper.AfterSalesNearExpiryMapper;
import com.ruoyi.aftersalesservice.pojo.AfterSalesNearExpiry;
import com.ruoyi.aftersalesservice.service.AfterSalesNearExpiryService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * <br>
 * 临期售后管理接口实现类
 * </br>
 *
 * @author deslrey
 * @version 1.0
 * @since 2026/03/02 14:46
 */
@Slf4j
@Service
public class AfterSalesNearExpiryServiceImpl extends ServiceImpl<AfterSalesNearExpiryMapper, AfterSalesNearExpiry> implements AfterSalesNearExpiryService {
 
    @Override
    public void add(AfterSalesNearExpiry entity) {
        if (entity == null) {
            throw new ServiceException("添加失败,数据不能为空");
        }
        if (entity.getProductName() == null || entity.getProductName().trim().isEmpty()) {
            throw new ServiceException("产品名称不能为空");
        }
        if (entity.getBatchNumber() == null || entity.getBatchNumber().trim().isEmpty()) {
            throw new ServiceException("产品批号不能为空");
        }
        if (entity.getExpireDate() == null) {
            throw new ServiceException("临期日期不能为空");
        }
 
        entity.setId(null);
        entity.setStatus(1);
        entity.setDisposeUserId(null);
        entity.setDisposeNickName(null);
        entity.setDisDate(null);
        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateUser(SecurityUtils.getLoginUser().getUserId());
        entity.setUpdateUser(null);
        entity.setUpdateTime(null);
        entity.setTenantId(SecurityUtils.getLoginUser().getTenantId());
 
        int result = baseMapper.insert(entity);
        if (result <= 0) {
            throw new ServiceException("新增失败");
        }
    }
 
    @Override
    public void update(AfterSalesNearExpiry entity) {
        if (entity == null || entity.getId() == null) {
            throw new ServiceException("更新失败,数据不完整");
        }
        entity.setStatus(2);
        entity.setUpdateUser(SecurityUtils.getLoginUser().getUserId());
        entity.setUpdateTime(LocalDateTime.now());
        int result = baseMapper.updateById(entity);
        if (result <= 0) {
            throw new ServiceException("更新失败");
        }
    }
 
    @Override
    public void delete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new ServiceException("请选择要删除的数据");
        }
        int result = baseMapper.deleteBatchIds(Arrays.asList(ids));
        if (result <= 0) {
            throw new ServiceException("删除失败");
        }
    }
 
    @Override
    public IPage<AfterSalesNearExpiry> listPage(Page<AfterSalesNearExpiry> page, AfterSalesNearExpiry entity) {
        return baseMapper.listPage(page, entity);
    }
 
    @Override
    public AjaxResult importData(MultipartFile file) {
        List<AfterSalesNearExpiryImportDto> rows;
        try {
            ExcelUtil<AfterSalesNearExpiryImportDto> util = new ExcelUtil<>(AfterSalesNearExpiryImportDto.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("导入文件无数据");
        }
        int success = 0;
        List<String> errors = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            AfterSalesNearExpiryImportDto row = rows.get(i);
            int rowNo = i + 2; // 首行为表头
            try {
                if (StringUtils.isEmpty(trim(row.getProductName()))) {
                    errors.add("第" + rowNo + "行:临期产品名称为空");
                    continue;
                }
                if (StringUtils.isEmpty(trim(row.getBatchNumber()))) {
                    errors.add("第" + rowNo + "行:产品批号为空");
                    continue;
                }
                if (row.getExpireDate() == null) {
                    errors.add("第" + rowNo + "行:临期日期为空");
                    continue;
                }
                if (row.getStockQuantity() == null) {
                    errors.add("第" + rowNo + "行:库存数量为空");
                    continue;
                }
                if (StringUtils.isEmpty(trim(row.getCustomerName()))) {
                    errors.add("第" + rowNo + "行:客户名称为空");
                    continue;
                }
 
                AfterSalesNearExpiry entity = new AfterSalesNearExpiry();
                entity.setProductName(trim(row.getProductName()));
                entity.setBatchNumber(trim(row.getBatchNumber()));
                entity.setExpireDate(row.getExpireDate());
                entity.setStockQuantity(row.getStockQuantity());
                entity.setCustomerName(trim(row.getCustomerName()));
                entity.setContactPhone(trim(row.getContactPhone()));
                entity.setDisRes(row.getDisRes());
 
                add(entity);
                success++;
            } 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();
    }
}