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.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.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 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;
|
}
|
// if (!permissionDeptCodes.contains(data.getUnitCode())) {
|
// errorMsgs.add("第" + data.getSeqNo() + "行无权限导入部门编码: " + data.getUnitCode());
|
// 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();
|
}
|
}
|