package com.ruoyi.device.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.utils.DateUtils; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.device.dto.DeviceLedgerDto; import com.ruoyi.device.execl.DeviceLedgerExeclDto; import com.ruoyi.device.mapper.DeviceLedgerMapper; import com.ruoyi.device.pojo.DeviceLedger; import com.ruoyi.basic.dto.StorageAttachmentDTO; import com.ruoyi.basic.enums.RecordTypeEnum; import com.ruoyi.basic.service.StorageAttachmentService; import com.ruoyi.device.service.IDeviceLedgerService; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.project.system.domain.SysUser; import com.ruoyi.project.system.mapper.SysUserMapper; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.time.format.DateTimeFormatter; import java.time.LocalDate; @Service @RequiredArgsConstructor @Slf4j public class DeviceLedgerServiceImpl extends ServiceImpl implements IDeviceLedgerService { private final DeviceLedgerMapper deviceLedgerMapper; private final SysUserMapper sysUserMapper; private final StorageAttachmentService storageAttachmentService; @Override public IPage queryPage(Page page, DeviceLedgerDto deviceLedger) { return deviceLedgerMapper.queryPage(page, deviceLedger); } @Override public AjaxResult saveDeviceLedger(DeviceLedgerDto deviceLedgerDto) { LambdaQueryWrapper deviceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>(); deviceLedgerLambdaQueryWrapper.eq(DeviceLedger::getDeviceName,deviceLedgerDto.getDeviceName()); if (this.count(deviceLedgerLambdaQueryWrapper) > 0) { return AjaxResult.error("设备名称已存在"); } DeviceLedger deviceLedger = new DeviceLedger(); BeanUtils.copyProperties(deviceLedgerDto, deviceLedger); if (StringUtils.isEmpty(deviceLedger.getUniqueCode())) { String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.likeRight(DeviceLedger::getUniqueCode, "SB" + dateStr); queryWrapper.orderByDesc(DeviceLedger::getUniqueCode); queryWrapper.last("limit 1"); DeviceLedger lastRecord = this.getOne(queryWrapper); int sequence = 1; if (lastRecord != null && StringUtils.isNotEmpty(lastRecord.getUniqueCode()) && lastRecord.getUniqueCode().length() >= 13) { String lastSeqStr = lastRecord.getUniqueCode().substring(10); try { sequence = Integer.parseInt(lastSeqStr) + 1; } catch (NumberFormatException ignored) {} } deviceLedger.setUniqueCode("SB" + dateStr + String.format("%03d", sequence)); } if (this.count(new LambdaQueryWrapper().eq(DeviceLedger::getUniqueCode, deviceLedger.getUniqueCode())) > 0) { return AjaxResult.error("设备编号已存在:" + deviceLedger.getUniqueCode()); } boolean save = this.save(deviceLedger); if (save){ if (deviceLedgerDto.getStorageBlobDTOs() != null) { StorageAttachmentDTO attachmentDTO = new StorageAttachmentDTO(); attachmentDTO.setApplication("image"); attachmentDTO.setRecordType(RecordTypeEnum.DEVICE_LEDGER.getType()); attachmentDTO.setRecordId(deviceLedger.getId()); attachmentDTO.setStorageBlobDTOs(deviceLedgerDto.getStorageBlobDTOs()); storageAttachmentService.saveStorageAttachment(attachmentDTO); } return AjaxResult.success(); } return AjaxResult.error(); } @Override public AjaxResult updateDeviceLedger(DeviceLedgerDto deviceLedgerDto) { if (StringUtils.isNotEmpty(deviceLedgerDto.getUniqueCode())) { if (this.count(new LambdaQueryWrapper() .eq(DeviceLedger::getUniqueCode, deviceLedgerDto.getUniqueCode()) .ne(DeviceLedger::getId, deviceLedgerDto.getId())) > 0) { return AjaxResult.error("设备编号已存在:" + deviceLedgerDto.getUniqueCode()); } } DeviceLedger deviceLedger = new DeviceLedger(); BeanUtils.copyProperties(deviceLedgerDto, deviceLedger); if (this.updateById(deviceLedger)) { if (deviceLedgerDto.getStorageBlobDTOs() != null) { StorageAttachmentDTO attachmentDTO = new StorageAttachmentDTO(); attachmentDTO.setApplication("image"); attachmentDTO.setRecordType(RecordTypeEnum.DEVICE_LEDGER.getType()); attachmentDTO.setRecordId(deviceLedger.getId()); attachmentDTO.setStorageBlobDTOs(deviceLedgerDto.getStorageBlobDTOs()); storageAttachmentService.saveStorageAttachment(attachmentDTO); } return AjaxResult.success(); } return AjaxResult.error(); } @Override public DeviceLedgerDto getDeviceLedgerDetail(Long id) { DeviceLedger deviceLedger = this.getById(id); if (deviceLedger != null) { DeviceLedgerDto deviceLedgerDto = new DeviceLedgerDto(); BeanUtils.copyProperties(deviceLedger, deviceLedgerDto); StorageAttachmentDTO dto = new StorageAttachmentDTO(); dto.setRecordType(RecordTypeEnum.DEVICE_LEDGER.getType()); dto.setRecordId(id); dto.setApplication("image"); deviceLedgerDto.setStorageBlobVOs(storageAttachmentService.list(dto)); return deviceLedgerDto; } return null; } @Override public void export(HttpServletResponse response, Long[] ids) { if (ids == null || ids.length == 0) { List supplierManageList = this.list(); ArrayList deviceLedgerExeclDtos = new ArrayList<>(); supplierManageList.stream().forEach(deviceLedger -> { DeviceLedgerExeclDto deviceLedgerExeclDto = new DeviceLedgerExeclDto(); BeanUtils.copyProperties(deviceLedger,deviceLedgerExeclDto); deviceLedgerExeclDto.setCreateUser(sysUserMapper.selectUserById(Long.valueOf(deviceLedger.getCreateUser().toString())).getUserName()); deviceLedgerExeclDtos.add(deviceLedgerExeclDto); }); ExcelUtil util = new ExcelUtil(DeviceLedgerExeclDto.class); util.exportExcel(response, deviceLedgerExeclDtos, "设备台账导出"); }else { ArrayList arrayList = new ArrayList<>(); Arrays.stream(ids).map(arrayList::add); List supplierManageList = deviceLedgerMapper.selectBatchIds(arrayList); ArrayList deviceLedgerExeclDtos = new ArrayList<>(); supplierManageList.stream().forEach(deviceLedger -> { DeviceLedgerExeclDto deviceLedgerExeclDto = new DeviceLedgerExeclDto(); BeanUtils.copyProperties(deviceLedger,deviceLedgerExeclDto); deviceLedgerExeclDto.setCreateUser(sysUserMapper.selectUserById(Long.valueOf(deviceLedger.getCreateUser().toString())).getUserName()); deviceLedgerExeclDtos.add(deviceLedgerExeclDto); }); ExcelUtil util = new ExcelUtil(DeviceLedgerExeclDto.class); util.exportExcel(response, deviceLedgerExeclDtos, "设备台账导出"); } } @Override public Boolean importData(MultipartFile file) throws IOException { ExcelUtil util = new ExcelUtil(DeviceLedgerExeclDto.class); List userList = util.importExcel(file.getInputStream()); String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.likeRight(DeviceLedger::getUniqueCode, "SB" + dateStr); queryWrapper.orderByDesc(DeviceLedger::getUniqueCode); queryWrapper.last("limit 1"); DeviceLedger lastRecord = this.getOne(queryWrapper); // 应对Java lambda表达式内变量必须是final的问题,用数组或类的成员包裹 final int[] maxSequence = {0}; if (lastRecord != null && StringUtils.isNotEmpty(lastRecord.getUniqueCode()) && lastRecord.getUniqueCode().length() >= 13) { String lastSeqStr = lastRecord.getUniqueCode().substring(10); try { maxSequence[0] = Integer.parseInt(lastSeqStr); } catch (NumberFormatException ignored) {} } List importUniqueCodes = new ArrayList<>(); userList.forEach(c -> { DeviceLedger deviceLedger = new DeviceLedger(); SysUser sysUser = sysUserMapper.selectUserByUserName(c.getCreateUser()); if (sysUser!=null) { deviceLedger.setCreateUser(sysUser.getUserId().intValue()); }else { deviceLedger.setCreateUser(SecurityUtils.getUserId().intValue()); } BeanUtils.copyProperties(c,deviceLedger); if (StringUtils.isEmpty(deviceLedger.getUniqueCode())) { maxSequence[0]++; deviceLedger.setUniqueCode("SB" + dateStr + String.format("%03d", maxSequence[0])); } if (importUniqueCodes.contains(deviceLedger.getUniqueCode())) { throw new RuntimeException("导入文件中存在重复的设备编号:" + deviceLedger.getUniqueCode()); } if (this.count(new LambdaQueryWrapper().eq(DeviceLedger::getUniqueCode, deviceLedger.getUniqueCode())) > 0) { throw new RuntimeException("设备编号已存在:" + deviceLedger.getUniqueCode()); } importUniqueCodes.add(deviceLedger.getUniqueCode()); // 通过含税单价、数量、税率计算含税总价,不含税总价 deviceLedger.setTaxIncludingPriceTotal(c.getTaxIncludingPriceUnit()); deviceLedger.setNumber(BigDecimal.ONE); deviceLedger.setPlanRuntimeTime(DateUtils.toLocalDate(c.getPlanRuntimeTime())); // 计算不含税总价,处理空值情况 if (deviceLedger.getTaxIncludingPriceTotal() != null && c.getTaxRate() != null) { deviceLedger.setUnTaxIncludingPriceTotal(deviceLedger.getTaxIncludingPriceTotal().divide(BigDecimal.ONE.add(c.getTaxRate()), 2, RoundingMode.HALF_UP)); } if (c.getPurchaseDate() != null) { deviceLedger.setPurchaseDate(DateUtils.toLocalDate(c.getPurchaseDate())); } if (c.getActivationDate() != null) { deviceLedger.setActivationDate(DateUtils.toLocalDate(c.getActivationDate())); } if (c.getFilingDate() != null) { deviceLedger.setFilingDate(DateUtils.toLocalDate(c.getFilingDate())); } deviceLedgerMapper.insert(deviceLedger); }); return true; } }