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;
|
}
|
|
}
|