package com.zy.third.erp.task; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.SnowflakeIdWorker; import com.core.exception.CoolException; import com.zy.asrs.entity.DocType; import com.zy.asrs.entity.Mat; import com.zy.asrs.entity.Order; import com.zy.asrs.entity.OrderDetl; import com.zy.asrs.service.DocTypeService; import com.zy.asrs.service.MatService; import com.zy.asrs.service.OrderDetlService; import com.zy.asrs.service.OrderService; import com.zy.common.model.DetlDto; import com.zy.third.erp.entity.InDetTB; import com.zy.third.erp.entity.InHedTB; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; @Slf4j @Component public class ERPInOrOutTBSchedulerMS { @Autowired private OrderService orderService; @Autowired private OrderDetlService orderDetlService; @Autowired private SnowflakeIdWorker snowflakeIdWorker; @Autowired private DocTypeService docTypeService; @Autowired private MatService matService; /** * @return */ @Transactional public boolean createOrder(InHedTB inHedTB, List inDetTBs) { Order order = orderService.selectByNo(inHedTB.getBillNo()); // 如果单据不存在则添加;如果单据存在,作业中无法修改,反之则修改单据 if (!Cools.isEmpty(order)) { if (order.getSettle() > 1L) { throw new CoolException(inHedTB.getBillNo() + "正在出库,无法修改单据"); } orderService.remove(order.getId()); } DocType docType = docTypeService.selectOrAdd(inHedTB.getIoKindID(), Boolean.FALSE); Date now = new Date(); // 单据主档 order = new Order( String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空] inHedTB.getBillNo(), // 订单编号 DateUtils.convert(now), // 单据日期 docType.getDocId(), // 单据类型 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, // 状态 9527L, // 添加人员 now, // 添加时间 9527L, // 修改人员 now, // 修改时间 null // 备注 ); if (!orderService.insert(order)) { throw new CoolException("生成单据主档失败,请联系管理员"); } // 单据明细档 List list = new ArrayList<>(); for (InDetTB detail : inDetTBs) { DetlDto dto = new DetlDto(detail.getBillNo(), detail.getItemBatch(), detail.getMainNum()); if (DetlDto.has(list, dto)) { DetlDto detlDto = DetlDto.find(list, dto.getMatnr(), dto.getBatch()); assert detlDto != null; detlDto.setAnfme(detlDto.getAnfme() + detail.getMainNum()); } else { list.add(dto); } } for (DetlDto detlDto : list) { Mat mat = matService.selectByMatnr(detlDto.getMatnr()); if (Cools.isEmpty(mat)) { throw new CoolException(detlDto.getMatnr() + "编号商品检索失败,请先添加商品"); } OrderDetl orderDetl = new OrderDetl(); orderDetl.sync(mat); orderDetl.setBatch(detlDto.getBatch()); orderDetl.setAnfme(detlDto.getAnfme()); orderDetl.setOrderId(order.getId()); orderDetl.setOrderNo(order.getOrderNo()); orderDetl.setCreateBy(9527L); orderDetl.setCreateTime(now); orderDetl.setUpdateBy(9527L); orderDetl.setUpdateTime(now); orderDetl.setStatus(1); orderDetl.setQty(0.0D); if (!orderDetlService.insert(orderDetl)) { throw new CoolException("生成单据明细失败,请联系管理员"); } } return true; } }