package com.zy.common.service.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.*; import com.zy.asrs.service.*; import com.zy.asrs.task.AbstractHandler; import com.zy.common.service.erp.ErpService; import com.zy.common.service.erp.dto.VoucherDto; import com.zy.common.service.erp.entity.Goods; import com.zy.common.service.erp.entity.Voucher; import com.zy.common.service.erp.entity.VoucherDetail; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * erp任务控制器 * Created by vincent on 2020/11/27 */ @Slf4j @Component public class ErpScheduler extends AbstractHandler { @Autowired private ErpService erpService; @Autowired private TagService tagService; @Autowired private MatService matService; @Autowired private OrderService orderService; @Autowired private DocTypeService docTypeService; @Autowired private OrderDetlService orderDetlService; @Autowired private SnowflakeIdWorker snowflakeIdWorker; @Scheduled(cron = "0/5 * * * * ? ") @Synchronized @Transactional public synchronized void syncMat() { Tag top = tagService.getTop(); List goods = erpService.selectGoods(0); Date now = new Date(); if (!Cools.isEmpty(goods)) { for (Goods good : goods) { Mat mat = matService.selectByMatnr(good.getBarCode()); if (mat == null) { mat = new Mat(); mat.setTagId(top.getId()); mat.setMatnr(good.getBarCode()); mat.setMaktx(good.getMaterialNO()); mat.setSpecs(good.getProdSpec()); mat.setModel(good.getBatch()); mat.setWeight(good.getNWT()); mat.setUnits(good.getNumOfBobbins()==null?null:good.getNumOfBobbins().doubleValue()); mat.setManuDate(good.getProdDate()); mat.setCreateTime(now); mat.setSku(good.getLocation()); if (!Cools.isEmpty(good.getLastUpdatedDate())) { mat.setUpdateTime(DateUtils.convert(good.getLastUpdatedDate().substring(0, 19))); } if (!matService.insert(mat)) { throw new CoolException(good.getBarCode() + "商品同步失败"); } else { int state = 1; if (!erpService.updateStateForGoods(good.getBarCode(), state)) { throw new CoolException(good.getBarCode() + "商品修改State为"+state+"失败"); } } } } } } @Scheduled(cron = "0/5 * * * * ? ") @Synchronized @Transactional public synchronized void syncOrder() { List list = erpService.selectOrder(0); for (VoucherDto dto : list) { Voucher voucher = dto.getVoucher(); Order order = orderService.selectByNo(voucher.getVoucherID()); if (Cools.isEmpty(order)) { String regEx = "[^0-9]"; Pattern compile = Pattern.compile(regEx); Matcher matcher = compile.matcher(voucher.getMT()); String docTypeVal = matcher.replaceAll("").trim(); DocType docType = docTypeService.selectOrAdd(docTypeVal, null); Date now = new Date(); // 单据主档 order = new Order( String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空] voucher.getVoucherID(), // 订单编号 DateUtils.convert(now), // 单据日期 docType.getDocId(), // 单据类型 null, // 项目编号 null, // null, // 调拨项目编号 voucher.getBatch(), // 初始票据号 voucher.getMaterialNO(), // 票据号 null, // 客户编号 voucher.getCustomer(), // 客户 voucher.getCenterID(), // 联系方式 voucher.getPlant(), // 操作人员 voucher.getTotalNum(), // 合计金额 ******************** null, // 优惠率 null, // 优惠金额 null, // 销售或采购费用合计 null, // 实付金额 null, // 付款类型 voucher.getLoc(), // 业务员 voucher.getTotalCount(), // 结算天数 ******************** null, // 邮费支付类型 null, // 邮费 null, // 付款时间 null, // 发货时间 null, // 物流名称 null, // 物流单号 1L, // 订单状态 1, // 状态 9527L, // 添加人员 now, // 添加时间 9527L, // 修改人员 now, // 修改时间 null // 备注 ); if (!orderService.insert(order)) { throw new CoolException(order.getOrderNo() + "生成单据主档失败,请联系管理员"); } for (VoucherDetail detail : dto.getDetails()) { Mat mat = matService.selectByMatnr(detail.getBarcode()); if (mat == null) { throw new CoolException(detail.getBarcode() + "单据号不存在"); } OrderDetl orderDetl = new OrderDetl(); orderDetl.sync(mat); // orderDetl.setBatch(detlDto.getBatch()); orderDetl.setAnfme(1.0D); 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(order.getOrderNo() + "生成单据明细失败,请联系管理员"); } } // erp 同步 int state = 1; if (!erpService.updateStateForVoucher(voucher.getVoucherID(), 1)) { throw new CoolException(voucher.getVoucherID() + "订单修改State为"+state+"失败"); } else { erpService.updateTimeForVoucherDetail(voucher.getVoucherID()); } } else { log.warn("{}订单已存在", voucher.getVoucherID()); } } } }