gaoluyang
15 小时以前 9cd4074a456fd5c422506279191b6015836347e5
src/main/java/com/ruoyi/device/service/impl/DeviceLedgerServiceImpl.java
@@ -4,6 +4,7 @@
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.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
@@ -23,6 +24,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@@ -45,6 +47,11 @@
    public IPage<DeviceLedgerDto> queryPage(Page page, DeviceLedgerDto deviceLedger) {
        return deviceLedgerMapper.queryPage(page, deviceLedger);
    }
    @Override
    public List<DeviceLedgerDto> selectDeviceOptions(Long areaId) {
        return deviceLedgerMapper.selectDeviceOptions(areaId);
    }
    @Override
@@ -138,29 +145,59 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean importData(MultipartFile file) throws IOException {
        ExcelUtil<DeviceLedgerExeclDto> util = new ExcelUtil<DeviceLedgerExeclDto>(DeviceLedgerExeclDto.class);
        List<DeviceLedgerExeclDto> userList = util.importExcel(file.getInputStream());
        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());
        if (userList == null || userList.isEmpty()) {
            // 表头与模板不一致时解析不到任何数据,明确提示,避免前端只看到“导入成功”却没有任何数据
            throw new ServiceException("未读取到可导入的数据,请确认文件表头与导入模板一致后重试");
        }
        int importCount = 0;
        for (int i = 0; i < userList.size(); i++) {
            DeviceLedgerExeclDto c = userList.get(i);
            if (c == null) {
                continue;
            }
            BeanUtils.copyProperties(c,deviceLedger);
            // 通过含税单价、数量、税率计算含税总价,不含税总价
            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));
            DeviceLedger deviceLedger;
            try {
                deviceLedger = buildDeviceLedger(c);
            } catch (Exception e) {
                throw new ServiceException("第" + (i + 2) + "行数据有误:" + e.getMessage());
            }
            deviceLedgerMapper.insert(deviceLedger);
        });
            try {
                deviceLedgerMapper.insert(deviceLedger);
            } catch (Exception e) {
                throw new ServiceException("第" + (i + 2) + "行保存失败:" + e.getMessage());
            }
            importCount++;
        }
        if (importCount == 0) {
            throw new ServiceException("未读取到可导入的数据,请确认文件表头与导入模板一致后重试");
        }
        return true;
    }
    /**
     * 将导入行转换为设备台账实体
     */
    private DeviceLedger buildDeviceLedger(DeviceLedgerExeclDto 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);
        // 通过含税单价、数量、税率计算含税总价,不含税总价
        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));
        }
        return deviceLedger;
    }
}