使用oracle做的数据上传系统后台
kongdeqiang
2026-03-24 f2cbc818993dab5646c99886810b17c6c79668ee
src/main/java/com/example/service/impl/DataExcelServiceImpl.java
@@ -4,10 +4,12 @@
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.example.entity.DataExcel;
import com.example.entity.Department;
import com.example.excel.DataExcelImport;
import com.example.mapper.DataExcelMapper;
import com.example.service.DataExcelService;
@@ -22,12 +24,12 @@
public class DataExcelServiceImpl extends ServiceImpl<DataExcelMapper, DataExcel> implements DataExcelService {
    @Override
    public IPage<DataExcel> getDataExcelPage(Long current, Long size, String unitName, String transactionNo, String accountingPeriod, List<String> permissionDeptCodes) {
    public IPage<DataExcel> getDataExcelPage(Long current, Long size, String unitCode, String transactionNo, String accountingPeriod, List<String> permissionDeptCodes) {
        Page<DataExcel> page = new Page<>(current, size);
        page.setOptimizeJoinOfCountSql(false);
        page.setOptimizeCountSql(false);
        LambdaQueryWrapper<DataExcel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(unitName != null && !unitName.isEmpty(), DataExcel::getUnitName, unitName)
        wrapper.eq(unitCode != null && !unitCode.isEmpty(), DataExcel::getUnitCode, unitCode)
                .like(transactionNo != null && !transactionNo.isEmpty(), DataExcel::getTransactionNo, transactionNo)
                .eq(accountingPeriod != null && !accountingPeriod.isEmpty(), DataExcel::getAccountingPeriod, accountingPeriod)
                .in(permissionDeptCodes != null && !permissionDeptCodes.isEmpty(), DataExcel::getUnitCode, permissionDeptCodes)
@@ -36,11 +38,41 @@
    }
    @Override
    public IPage<DataExcel> getDataExcelPageNew(Long current, Long size, String unitCode, String transactionNo, String accountingPeriod) {
        Page<DataExcel> page = new Page<>(current, size);
        page.setOptimizeJoinOfCountSql(false);
        page.setOptimizeCountSql(false);
        LambdaQueryWrapper<DataExcel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(unitCode != null && !unitCode.isEmpty(), DataExcel::getUnitCode, unitCode)
                .like(transactionNo != null && !transactionNo.isEmpty(), DataExcel::getTransactionNo, transactionNo)
                .eq(accountingPeriod != null && !accountingPeriod.isEmpty(), DataExcel::getAccountingPeriod, accountingPeriod)
                .orderByDesc(DataExcel::getId);
        return page(page, wrapper);
    }
    @Override
    public List<Department> getDepartmentTree() {
        QueryWrapper<DataExcel> wrapper = new QueryWrapper<>();
        // 关键:两个字段去重(DISTINCT 作用于这两个字段的组合)
       wrapper.select("distinct unit_code, unit_name");
        List<DataExcel> list = baseMapper.selectList(wrapper);
        List<Department> resultList = new ArrayList<>();
        for (DataExcel dataExcel : list) {
            Department department = new Department();
            department.setDeptCode(dataExcel.getUnitCode());
            department.setDeptName(dataExcel.getUnitName());
            resultList.add(department);
        }
        return resultList;
    }
    @Override
    public String importData(MultipartFile file, List<String> permissionDeptCodes) {
        List<DataExcel> insertList = new ArrayList<>();
        List<DataExcel> updateList = new ArrayList<>();
        List<String> errorMsgs = new ArrayList<>();
        int[] counts = new int[2];
        int[] counts2 = new int[2];
        try {
            EasyExcel.read(file.getInputStream(), DataExcelImport.class, new ReadListener<DataExcelImport>() {
@@ -50,11 +82,6 @@
                        errorMsgs.add("第" + data.getSeqNo() + "行单位编码为空");
                        return;
                    }
                    if (!permissionDeptCodes.contains(data.getUnitCode())) {
                        errorMsgs.add("第" + data.getSeqNo() + "行无权限导入部门编码: " + data.getUnitCode());
                        return;
                    }
                    DataExcel entity = new DataExcel();
                    entity.setSortNo(data.getSortNo());
                    entity.setSeqNo(data.getSeqNo());
@@ -73,13 +100,14 @@
                    entity.setBusinessRelation(data.getBusinessRelation());
                    entity.setRemark(data.getRemark());
                    entity.setStatus(1);
                    entity.setPkVoucher(data.getPkVoucher());
                    if (data.getSortNo() != null) {
                        DataExcel existData = baseMapper.selectBySortNo(data.getSortNo());
                        if (existData != null) {
                            entity.setId(existData.getId());
                            updateList.add(entity);
                            counts[1]++;
                            counts2[0]++;
                        } else {
                            insertList.add(entity);
                            counts[0]++;
@@ -109,11 +137,11 @@
        if (counts[0] > 0) {
            result.append("成功新增 ").append(counts[0]).append(" 条记录");
        }
        if (counts[1] > 0) {
        if (counts2[0] > 0) {
            if (result.length() > 0) {
                result.append(",");
            }
            result.append("成功更新 ").append(counts[1]).append(" 条记录");
            result.append("成功更新 ").append(counts2[0]).append(" 条记录");
        }
        if (!errorMsgs.isEmpty()) {
            if (result.length() > 0) {
@@ -121,11 +149,11 @@
            }
            result.append("导入部分失败: ").append(String.join(", ", errorMsgs));
        }
        if (result.length() == 0) {
            return "导入失败";
        }
        return result.toString();
    }
}