package com.zy.asrs.common.utils;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.zy.asrs.common.domain.dto.DetlDto;
|
import com.zy.asrs.common.domain.entity.OutLocOrderExcel;
|
import com.zy.asrs.common.domain.param.OrderDomainParam;
|
import com.zy.asrs.common.wms.entity.DocType;
|
import com.zy.asrs.common.wms.entity.Mat;
|
import com.zy.asrs.common.wms.entity.Order;
|
import com.zy.asrs.common.wms.entity.OrderDetl;
|
import com.zy.asrs.common.wms.service.DocTypeService;
|
import com.zy.asrs.common.wms.service.MatService;
|
import com.zy.asrs.common.wms.service.OrderDetlService;
|
import com.zy.asrs.common.wms.service.OrderService;
|
import com.zy.asrs.framework.common.*;
|
import com.zy.asrs.framework.exception.CoolException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import java.util.*;
|
|
/**
|
* Created by vincent on 2019-11-25
|
*/
|
@Slf4j
|
public class OutLocOrderExcelListener extends AnalysisEventListener<OutLocOrderExcel> {
|
|
private int total = 0;
|
private String orderOneNo = null;
|
private String orderSign = null;
|
private Long orderSignId = null;
|
|
private int totalAnfme = 0;
|
private Long userId;
|
private Long hostId;
|
List<String[]> OutLocOrderCs = new ArrayList<>();
|
List<List<String[]>> OutLocOrderList = new ArrayList<>();
|
|
public OutLocOrderExcelListener() {
|
}
|
|
public OutLocOrderExcelListener(Long userId, Long hostId) {
|
this.userId = userId;
|
this.hostId = hostId;
|
}
|
|
/**
|
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
|
*/
|
private static final int BATCH_COUNT = 50;
|
|
private final List<OutLocOrderExcel> list = new ArrayList<>();
|
|
/**
|
* 这里会一行行的返回头
|
*/
|
@Override
|
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
}
|
|
/**
|
* 这个每一条数据解析都会来调用
|
*/
|
@Override
|
public void invoke(OutLocOrderExcel excel, AnalysisContext ctx) {
|
if (Cools.isEmpty(orderOneNo)){
|
if (!Cools.isEmpty(excel.getOrder())){
|
orderOneNo = excel.getOrder();
|
} else {
|
throw new CoolException("订单号参数为空");
|
}
|
}
|
if (Cools.isEmpty(orderSign)){
|
if (!Cools.isEmpty(excel.getSign())){
|
orderSign = excel.getSign();
|
DocTypeService docTypeService = SpringUtils.getBean(DocTypeService.class);
|
DocType docType = docTypeService.getOne(new LambdaQueryWrapper<DocType>().eq(DocType::getDocName, orderSign));
|
if (Cools.isEmpty(docType)){
|
throw new CoolException("不存在的单据类型:"+orderSign);
|
}
|
orderSignId = docType.getDocId();
|
} else {
|
throw new CoolException("入出库标记参数为空");
|
}
|
}
|
String matnr = "";
|
try{
|
matnr = excel.getMatnr();
|
if (Cools.isEmpty(matnr)){
|
throw new CoolException("品号参数为空");
|
}
|
} catch (Exception e){
|
throw new CoolException("品号参数为空");
|
}
|
MatService matService = SpringUtils.getBean(MatService.class);
|
Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, matnr));
|
if (Cools.isEmpty(mat)){
|
throw new CoolException("品号:"+matnr+"不存在,请先新增商品档案!!!");
|
}
|
String anfme = "0";
|
try{
|
String anfme1 = excel.getAnfme();
|
try{
|
Integer anfmeInt = Integer.parseInt(anfme1);
|
if (!Cools.isEmpty(anfmeInt) && anfmeInt>0){
|
anfme = anfmeInt.toString();
|
} else {
|
throw new CoolException("数量参数为空");
|
}
|
} catch (Exception e){
|
throw new CoolException("数量参数为空");
|
}
|
} catch (Exception e){
|
throw new CoolException("数量参数为空");
|
|
}
|
|
String[] s=new String[2];
|
s[0] = matnr;
|
s[1] = anfme;
|
OutLocOrderCs.add(s);
|
total++;
|
// totalAnfme = totalAnfme+Integer.parseInt(anfme);
|
}
|
|
/**
|
* 所有数据解析完成了调用
|
* 适合事务
|
*/
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext ctx) {
|
if (!Cools.isEmpty(OutLocOrderCs) && OutLocOrderCs.size() != 0) {
|
ArrayList<String> matnrSign = new ArrayList<>();
|
for (String[] outLocOrderCs : OutLocOrderCs){
|
if (!matnrSign.contains(outLocOrderCs[0])){
|
matnrSign.add(outLocOrderCs[0]);
|
} else {
|
throw new CoolException("品号参数存在重复行:"+outLocOrderCs[0]);
|
}
|
}
|
MatService matService = SpringUtils.getBean(MatService.class);
|
|
OrderDomainParam orderDomainParam = new OrderDomainParam();
|
orderDomainParam.setDocType(orderSignId);
|
orderDomainParam.setOrderNo(orderOneNo);
|
List<OrderDetl> orderDetlList = new ArrayList<>();
|
for (String[] outLocOrderCs : OutLocOrderCs){
|
OrderDetl orderDetl = new OrderDetl();
|
orderDetl.setOrderNo(orderOneNo);
|
orderDetl.setAnfme(Double.parseDouble(outLocOrderCs[1]));
|
|
Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, outLocOrderCs[0]));
|
|
orderDetl.setMatnr(mat.getMatnr());
|
orderDetl.setMaktx(mat.getMaktx());
|
// orderDetl.setBatch(mat.getBatch());
|
orderDetl.setBarcode(mat.getBarcode());
|
orderDetl.setSpecs(mat.getSpecs());
|
orderDetl.setModel(mat.getModel());
|
orderDetl.setStatus(1);
|
orderDetl.setMemo(mat.getMemo());
|
orderDetl.setHostId(hostId);
|
orderDetlList.add(orderDetl);
|
}
|
orderDomainParam.setOrderDetlList(orderDetlList);
|
boolean formAddOrder = formAddOrder(orderDomainParam);
|
if (!formAddOrder){
|
throw new CoolException("生成订单失败");
|
}
|
|
}
|
|
log.info("成功导入{}条订单明细!", total);
|
}
|
|
public int[] getTotal() {
|
return new int[]{total};
|
}
|
|
public boolean formAddOrder(OrderDomainParam param){
|
OrderService orderService = SpringUtils.getBean(OrderService.class);
|
JdbcTemplate jdbcTemplate = SpringUtils.getBean(JdbcTemplate.class);
|
SnowflakeIdWorker snowflakeIdWorker = SpringUtils.getBean(SnowflakeIdWorker.class);
|
OrderDetlService orderDetlService = SpringUtils.getBean(OrderDetlService.class);
|
|
Order order = orderService.selectByNo(param.getOrderNo(), hostId);
|
if (order != null) {
|
throw new CoolException("单据编号已存在");
|
}
|
// 判断重复订单号
|
Integer orderCount = Optional.ofNullable(jdbcTemplate.queryForObject("select count(1) from man_order where order_no = '" + param.getOrderNo() + "'", Integer.class)).orElse(0);
|
Integer orderLogCount = Optional.ofNullable(jdbcTemplate.queryForObject("select count(1) from man_order_log where order_no = '" + param.getOrderNo() + "'", Integer.class)).orElse(0);
|
if (orderCount > 0 || orderLogCount > 0) {
|
throw new CoolException("单据编号已存在");
|
}
|
Date now = new Date();
|
order = new Order(
|
String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空]
|
param.getOrderNo(), // 订单编号
|
DateUtils.convert(now), // 单据日期
|
param.getDocType(), // 单据类型
|
null, // 项目编号
|
null, //
|
null, // 调拨项目编号
|
null, // 初始票据号
|
null, // 票据号
|
null, // 客户编号
|
null, // 客户
|
null, // 联系方式
|
null, // 操作人员
|
null, // 合计金额
|
null, // 优惠率
|
null, // 优惠金额
|
null, // 销售或采购费用合计
|
null, // 实付金额
|
null, // 付款类型
|
null, // 业务员
|
null, // 结算天数
|
null, // 邮费支付类型
|
null, // 邮费
|
null, // 付款时间
|
null, // 发货时间
|
null, // 物流名称
|
null, // 物流单号
|
1L, // 订单状态
|
1, // 状态
|
userId, // 添加人员
|
now, // 添加时间
|
userId, // 修改人员
|
now, // 修改时间
|
null, // 备注
|
hostId
|
);
|
if (!orderService.save(order)) {
|
throw new CoolException("保存订单主档失败");
|
}
|
List<DetlDto> list = new ArrayList<>();
|
for (OrderDetl orderDetl : param.getOrderDetlList()) {
|
DetlDto dto = new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch());
|
if (DetlDto.has(list, dto)) {
|
OrderDetl item = orderDetlService.selectItem(order.getId(), orderDetl.getMatnr(), orderDetl.getBatch(), hostId);
|
item.setAnfme(item.getAnfme() + orderDetl.getAnfme());
|
if (!orderDetlService.updateById(item)) {
|
throw new CoolException("保存订单明细档失败");
|
}
|
} else {
|
list.add(dto);
|
orderDetl.setOrderId(order.getId());
|
orderDetl.setOrderNo(order.getOrderNo());
|
orderDetl.setCreateBy(userId);
|
orderDetl.setCreateTime(now);
|
orderDetl.setUpdateBy(userId);
|
orderDetl.setUpdateTime(now);
|
orderDetl.setStatus(1);
|
orderDetl.setQty(0.0D);
|
orderDetl.setWorkQty(0.0D);
|
if (!orderDetlService.save(orderDetl)) {
|
throw new CoolException("保存订单明细档失败");
|
}
|
}
|
}
|
return true;
|
}
|
}
|