5 小时以前 55d694aaae4e421b5e55cd941500e08f85cb5d4d
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
package com.ruoyi.measuringinstrumentledger.service.impl;
 
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.common.enums.FileNameType;
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 com.ruoyi.measuringinstrumentledger.dto.MeasuringInstrumentLedgerImportDto;
import com.ruoyi.measuringinstrumentledger.dto.MeasuringInstrumentLedgerDto;
import com.ruoyi.measuringinstrumentledger.mapper.MeasuringInstrumentLedgerMapper;
import com.ruoyi.measuringinstrumentledger.mapper.MeasuringInstrumentLedgerRecordMapper;
import com.ruoyi.measuringinstrumentledger.pojo.MeasuringInstrumentLedger;
import com.ruoyi.measuringinstrumentledger.pojo.MeasuringInstrumentLedgerRecord;
import com.ruoyi.measuringinstrumentledger.service.MeasuringInstrumentLedgerService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.sales.mapper.CommonFileMapper;
import com.ruoyi.sales.pojo.CommonFile;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/8/5 9:23
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MeasuringInstrumentLedgerServiceImpl extends ServiceImpl<MeasuringInstrumentLedgerMapper, MeasuringInstrumentLedger> implements MeasuringInstrumentLedgerService {
 
    private final MeasuringInstrumentLedgerMapper measuringInstrumentLedgerMapper;
    private final MeasuringInstrumentLedgerRecordMapper measuringInstrumentLedgerRecordMapper;
    private final SysUserMapper sysUserMapper;
 
    @Override
    public IPage<MeasuringInstrumentLedger> listPage(Page page, MeasuringInstrumentLedger measuringInstrumentLedger) {
        IPage<MeasuringInstrumentLedger> measuringInstrumentLedgerIPage = measuringInstrumentLedgerMapper.listPage(page, measuringInstrumentLedger);
        List<Integer>  types = new ArrayList<>();
        types.add(FileNameType.MEASURING.getValue());
        types.add(FileNameType.MEASURINGRecord.getValue());
        measuringInstrumentLedgerIPage.getRecords().forEach(item -> {
            LambdaQueryWrapper<MeasuringInstrumentLedgerRecord> measuringInstrumentLedgerRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            measuringInstrumentLedgerRecordLambdaQueryWrapper.eq(MeasuringInstrumentLedgerRecord::getMeasuringInstrumentLedgerId, item.getId());
            List<MeasuringInstrumentLedgerRecord> measuringInstrumentLedgerRecords = measuringInstrumentLedgerRecordMapper.selectList(measuringInstrumentLedgerRecordLambdaQueryWrapper);
            List<Long> collect = new ArrayList<>();
            if(!CollectionUtils.isEmpty(measuringInstrumentLedgerRecords)){
                collect = measuringInstrumentLedgerRecords.stream().map(MeasuringInstrumentLedgerRecord::getId).collect(Collectors.toList());
            }
            collect.add(item.getId());
        });
        return measuringInstrumentLedgerIPage;
    }
 
    @Override
    public boolean verifying(MeasuringInstrumentLedgerDto req) throws IOException {
        MeasuringInstrumentLedger measuringInstrumentLedger = measuringInstrumentLedgerMapper.selectById(req.getId());
        if(measuringInstrumentLedger == null) {
            return false;
        }
        SysUser sysUser = sysUserMapper.selectUserById(measuringInstrumentLedger.getUserId());
        measuringInstrumentLedger.setValid(req.getValid());
        measuringInstrumentLedger.setMostDate(req.getRecordDate());
        measuringInstrumentLedger.setNextDate(new Date(req.getRecordDate().getTime() + 1000L * 60 * 60 * 24 * req.getValid()));
        MeasuringInstrumentLedgerRecord measuringInstrumentLedgerRecord = new MeasuringInstrumentLedgerRecord();
        if(measuringInstrumentLedgerMapper.updateById(measuringInstrumentLedger) > 0) {
            measuringInstrumentLedgerRecord.setMeasuringInstrumentLedgerId(req.getId());
            measuringInstrumentLedgerRecord.setRecordDate(req.getRecordDate());
            measuringInstrumentLedgerRecord.setEntryDate(req.getEntryDate());
            measuringInstrumentLedgerRecord.setValid(req.getValid());
            measuringInstrumentLedgerRecord.setUserId(req.getUserId());
            measuringInstrumentLedgerRecord.setUserName(sysUser.getUserName());
            measuringInstrumentLedgerRecordMapper.insert(measuringInstrumentLedgerRecord);
            // 台账绑定一次
//            if(!CollectionUtils.isEmpty(req.getTempFileIds())){
//                migrateTempFilesToFormal(measuringInstrumentLedger.getId(), req.getTempFileIds(), FileNameType.MEASURING.getValue());
//            }
            return true;
        }
        return false;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("请上传 Excel 文件");
        }
        String filename = file.getOriginalFilename();
        if (filename == null || !(filename.endsWith(".xls") || filename.endsWith(".xlsx"))) {
            return AjaxResult.error("只支持导入 xls 或 xlsx 文件");
        }
        try {
            ExcelUtil<MeasuringInstrumentLedgerImportDto> util = new ExcelUtil<>(MeasuringInstrumentLedgerImportDto.class);
            List<MeasuringInstrumentLedgerImportDto> rows = util.importExcel(file.getInputStream());
            if (CollectionUtils.isEmpty(rows)) {
                return AjaxResult.error("Excel 中没有可导入的数据");
            }
            Set<String> codes = new HashSet<>();
            Map<Integer, String> errors = new LinkedHashMap<>();
            List<MeasuringInstrumentLedger> ledgers = new ArrayList<>();
            Long tenantId = SecurityUtils.getLoginUser().getTenantId();
            Long userId = SecurityUtils.getUserId();
            String userName = SecurityUtils.getLoginUser().getNickName();
            for (int i = 0; i < rows.size(); i++) {
                MeasuringInstrumentLedgerImportDto row = rows.get(i);
                int excelRow = i + 2;
                String code = StringUtils.trim(row.getCode());
                String name = StringUtils.trim(row.getName());
                if (!StringUtils.hasText(code)) {
                    errors.put(excelRow, "计量器具编号不能为空");
                    continue;
                }
                if (!StringUtils.hasText(name)) {
                    errors.put(excelRow, "计量器具名称不能为空");
                    continue;
                }
                if (!codes.add(code)) {
                    errors.put(excelRow, "计量器具编号在 Excel 中重复");
                    continue;
                }
                if (row.getValid() != null && row.getValid() <= 0) {
                    errors.put(excelRow, "检定有效期必须大于 0");
                    continue;
                }
                MeasuringInstrumentLedger ledger = new MeasuringInstrumentLedger();
                ledger.setCode(code);
                ledger.setName(name);
                ledger.setModel(StringUtils.trim(row.getModel()));
                ledger.setMostDate(row.getMostDate());
                ledger.setValid(row.getValid());
                ledger.setRecordDate(row.getRecordDate() == null ? new Date() : row.getRecordDate());
                ledger.setUnit(StringUtils.trim(row.getUnit()));
                ledger.setDeptId(row.getDeptId());
                ledger.setInstationLocation(StringUtils.trim(row.getInstationLocation()));
                ledger.setCycle(StringUtils.trim(row.getCycle()));
                ledger.setUserId(userId);
                ledger.setUserName(userName);
                ledger.setTenantId(tenantId);
                if (row.getMostDate() != null && row.getValid() != null) {
                    ledger.setNextDate(new Date(row.getMostDate().getTime() + 86400000L * row.getValid()));
                }
                ledgers.add(ledger);
            }
            if (!codes.isEmpty()) {
                for (String code : measuringInstrumentLedgerMapper.selectExistingCodes(tenantId, codes)) {
                    errors.put(-1, "计量器具编号已存在:" + code);
                }
            }
            if (!errors.isEmpty()) {
                return AjaxResult.error("导入校验失败:" + errors.values().stream().limit(100).collect(Collectors.joining(";")));
            }
            saveBatch(ledgers);
            return AjaxResult.success("成功导入 " + ledgers.size() + " 条数据");
        } catch (Exception e) {
            log.error("计量器具台账导入失败", e);
            return AjaxResult.error("导入失败,请检查 Excel 内容");
        }
    }
 
    @Override
    public void downloadTemplate(HttpServletResponse response) {
        ExcelUtil<MeasuringInstrumentLedgerImportDto> util = new ExcelUtil<>(MeasuringInstrumentLedgerImportDto.class);
        util.importTemplateExcel(response, "计量器具台账导入模板");
    }
 
    @Override
    public void export(HttpServletResponse response) {
        List<MeasuringInstrumentLedger> list = measuringInstrumentLedgerMapper.listPage(new MeasuringInstrumentLedger());
        ExcelUtil<MeasuringInstrumentLedger> util = new ExcelUtil<MeasuringInstrumentLedger>(MeasuringInstrumentLedger.class);
        util.exportExcel(response, list , "客户档案数据");
    }
 
    @Override
    public boolean add(MeasuringInstrumentLedger measuringInstrumentLedger) throws IOException {
        SysUser sysUser = sysUserMapper.selectUserById(measuringInstrumentLedger.getUserId());
        if (sysUser == null) {
            return false;
        }
        measuringInstrumentLedger.setUserName(sysUser.getUserName());
        measuringInstrumentLedgerMapper.insert(measuringInstrumentLedger);
        return true;
    }
 
}