导入文件并插入数据
This commit is contained in:
@@ -0,0 +1,122 @@
|
|||||||
|
package com.zmzm.finance.common.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.zmzm.finance.common.entity.Import;
|
||||||
|
import com.zmzm.finance.common.entity.Payment;
|
||||||
|
import com.zmzm.finance.common.service.IImportService;
|
||||||
|
import com.zmzm.finance.common.service.IPaymentService;
|
||||||
|
import com.zmzm.finance.util.ExcelUtil;
|
||||||
|
import com.zmzm.finance.util.FileUtil;
|
||||||
|
import com.zmzm.finance.util.R;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.apache.commons.lang.time.DateUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 导入文件表 前端控制器
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author baomidou
|
||||||
|
* @since 2025-12-15
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/common/import")
|
||||||
|
public class ImportController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IImportService importService;
|
||||||
|
@Autowired
|
||||||
|
private IPaymentService paymentService;
|
||||||
|
|
||||||
|
//导入文件列表
|
||||||
|
@RequestMapping("/getImportList")
|
||||||
|
public R getImportList(@RequestBody Map<String,Object> params){
|
||||||
|
List<Import> importList = importService.list(new LambdaQueryWrapper<Import>()
|
||||||
|
.eq(StringUtils.isNotEmpty(params.get("year").toString()),Import::getYear,params.get("year"))
|
||||||
|
.eq(StringUtils.isNotEmpty(params.get("month").toString()),Import::getMonth,params.get("month")));
|
||||||
|
return R.ok().putData("importList",importList);
|
||||||
|
}
|
||||||
|
|
||||||
|
//导入数据
|
||||||
|
@RequestMapping("/importData")
|
||||||
|
@Transactional
|
||||||
|
public R importData(@RequestParam("file") MultipartFile file, @RequestParam("type") int type,
|
||||||
|
@RequestParam("year") int year, @RequestParam("month") int month) {
|
||||||
|
//删除之前上的的数据
|
||||||
|
Import imp = importService.getOne(new LambdaQueryWrapper<Import>()
|
||||||
|
.eq(Import::getYear, year).eq(Import::getMonth, month));
|
||||||
|
if(imp != null){
|
||||||
|
List<Payment> oldPayments = paymentService.list(new LambdaQueryWrapper<Payment>()
|
||||||
|
.eq(Payment::getImportId,imp.getId()).eq(Payment::getType, type));
|
||||||
|
paymentService.removeBatchByIds(oldPayments);
|
||||||
|
}else {
|
||||||
|
imp = new Import();
|
||||||
|
imp.setYear(year);
|
||||||
|
imp.setMonth(month);
|
||||||
|
}
|
||||||
|
//上传文件
|
||||||
|
FileUtil fileUtil = new FileUtil();
|
||||||
|
String url = fileUtil.uploadFile(file);
|
||||||
|
if(0==type){
|
||||||
|
imp.setWechatFile(url);
|
||||||
|
}else if(1==type){
|
||||||
|
imp.setAlipayFile(url);
|
||||||
|
}else if(2==type){
|
||||||
|
imp.setBankFile(url);
|
||||||
|
}
|
||||||
|
importService.saveOrUpdate(imp);
|
||||||
|
//导入新数据
|
||||||
|
int num = 0;
|
||||||
|
try (InputStream fis = file.getInputStream()) {
|
||||||
|
//解析数据
|
||||||
|
ExcelUtil example = new ExcelUtil();
|
||||||
|
example.processOneSheet(fis);
|
||||||
|
LinkedHashMap<String, String> map = example.getRowContents();
|
||||||
|
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
|
||||||
|
//处理数据
|
||||||
|
int count = 0;
|
||||||
|
List<Payment> list = new ArrayList<>();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
Map.Entry<String, String> entry = it.next();
|
||||||
|
String pos = entry.getKey();
|
||||||
|
String rowNo = pos.replaceAll("[a-zA-Z]", "");
|
||||||
|
count = Integer.parseInt(rowNo);
|
||||||
|
}
|
||||||
|
for (int i=2;i<=count;i++){
|
||||||
|
Payment payment = new Payment();
|
||||||
|
payment.setImportId(imp.getId());
|
||||||
|
payment.setType(map.containsKey("A"+i)?Integer.valueOf(map.get("A"+i)):0);
|
||||||
|
payment.setFinanceSn(map.getOrDefault("B" + i, ""));
|
||||||
|
payment.setTransactionSn(map.getOrDefault("C" + i, ""));
|
||||||
|
payment.setRelationSn(map.getOrDefault("D" + i, ""));
|
||||||
|
payment.setFee(new BigDecimal(map.get("E" + i)));
|
||||||
|
payment.setCtime(DateUtils.parseDate(map.get("F"+i), new String[]{"yyyy-MM-dd HH:mm:ss"}));
|
||||||
|
payment.setPaymentName(map.getOrDefault("G" + i, ""));
|
||||||
|
payment.setPaymentType(map.getOrDefault("H" + i, ""));
|
||||||
|
payment.setRemark(map.getOrDefault("I" + i, ""));
|
||||||
|
list.add(payment);
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
paymentService.saveBatch(list);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||||
|
return R.error(e.getMessage());
|
||||||
|
}
|
||||||
|
return R.ok("导入成功"+num+"条");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user