使用oracle做的数据上传系统后台
kongdeqiang
2026-03-24 f2cbc818993dab5646c99886810b17c6c79668ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.example.service.impl;
 
import com.alibaba.excel.EasyExcel;
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;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class DataExcelServiceImpl extends ServiceImpl<DataExcelMapper, DataExcel> implements DataExcelService {
 
    @Override
    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.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)
                .orderByDesc(DataExcel::getId);
        return page(page, wrapper);
    }
 
    @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>() {
                @Override
                public void invoke(DataExcelImport data, AnalysisContext context) {
                    if (data.getUnitCode() == null || data.getUnitCode().isEmpty()) {
                        errorMsgs.add("第" + data.getSeqNo() + "行单位编码为空");
                        return;
                    }
                    DataExcel entity = new DataExcel();
                    entity.setSortNo(data.getSortNo());
                    entity.setSeqNo(data.getSeqNo());
                    entity.setSecondaryUnit(data.getSecondaryUnit());
                    entity.setUnitCode(data.getUnitCode());
                    entity.setUnitName(data.getUnitName());
                    entity.setTransactionNo(data.getTransactionNo());
                    entity.setSummary(data.getSummary());
                    entity.setAmount(data.getAmount());
                    entity.setAccountingPeriod(data.getAccountingPeriod());
                    entity.setVoucherNo(data.getVoucherNo());
                    entity.setBpContractNo(data.getBpContractNo());
                    entity.setContractCenterNo(data.getContractCenterNo());
                    entity.setBpInvoiceNo(data.getBpInvoiceNo());
                    entity.setGkInvoiceNo(data.getGkInvoiceNo());
                    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);
                            counts2[0]++;
                        } else {
                            insertList.add(entity);
                            counts[0]++;
                        }
                    } else {
                        insertList.add(entity);
                        counts[0]++;
                    }
                }
 
                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                }
            }).sheet().doRead();
        } catch (IOException e) {
            return "导入失败: " + e.getMessage();
        }
 
        if (!insertList.isEmpty()) {
            saveBatch(insertList);
        }
        if (!updateList.isEmpty()) {
            updateBatchById(updateList);
        }
 
        StringBuilder result = new StringBuilder();
        if (counts[0] > 0) {
            result.append("成功新增 ").append(counts[0]).append(" 条记录");
        }
        if (counts2[0] > 0) {
            if (result.length() > 0) {
                result.append(",");
            }
            result.append("成功更新 ").append(counts2[0]).append(" 条记录");
        }
        if (!errorMsgs.isEmpty()) {
            if (result.length() > 0) {
                result.append("。");
            }
            result.append("导入部分失败: ").append(String.join(", ", errorMsgs));
        }
 
        if (result.length() == 0) {
            return "导入失败";
        }
 
        return result.toString();
    }
}