| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | 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 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; |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | @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); |