|  |  |  | 
|---|
|  |  |  | 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.entity.AllLocDetl; | 
|---|
|  |  |  | import com.zy.asrs.entity.InventoryVariance; | 
|---|
|  |  |  | import com.zy.asrs.service.AllLocDetlService; | 
|---|
|  |  |  | import com.zy.asrs.service.InventoryVarianceService; | 
|---|
|  |  |  | import com.zy.asrs.service.MatService; | 
|---|
|  |  |  | import com.zy.asrs.service.TagService; | 
|---|
|  |  |  | 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 com.zy.common.service.erp.entity.WlzhVStRd; | 
|---|
|  |  |  | 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.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.regex.Matcher; | 
|---|
|  |  |  | import java.util.regex.Pattern; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * erp任务控制器 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MatService matService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OrderService orderService; | 
|---|
|  |  |  | private AllLocDetlService allLocDetlService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DocTypeService docTypeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OrderDetlService orderDetlService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  | private InventoryVarianceService inventoryVarianceService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Scheduled(cron = "0/5 * * * * ? ") | 
|---|
|  |  |  | @Synchronized | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public synchronized void syncMat() { | 
|---|
|  |  |  | Tag top = tagService.getTop(); | 
|---|
|  |  |  | List<Goods> goods = erpService.selectGoods(0); | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  | if (!Cools.isEmpty(goods)) { | 
|---|
|  |  |  | for (Goods good : goods) { | 
|---|
|  |  |  | Mat mat = matService.selectByMatnr(good.getMaterialNO()); | 
|---|
|  |  |  | if (mat == null) { | 
|---|
|  |  |  | mat = new Mat(); | 
|---|
|  |  |  | mat.setTagId(top.getId()); | 
|---|
|  |  |  | mat.setMatnr(good.getMaterialNO()); | 
|---|
|  |  |  | //                    mat.setMaktx(good.getBarCode()); | 
|---|
|  |  |  | 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))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //@Scheduled(cron = "0 0 2 * * ? ") | 
|---|
|  |  |  | public void syncLocDetl(){ | 
|---|
|  |  |  | log.info("库存差异信息清除"); | 
|---|
|  |  |  | inventoryVarianceService.deleteAll(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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+"失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | log.info("与ERP比对开始"); | 
|---|
|  |  |  | int pageSize = 500; | 
|---|
|  |  |  | int pageNumber = 0; | 
|---|
|  |  |  | while (true){ | 
|---|
|  |  |  | List<InventoryVariance> inventoryVariances = new ArrayList<>(); | 
|---|
|  |  |  | List<WlzhVStRd> wlzhVStRds = erpService.selectPage(pageSize, pageNumber); | 
|---|
|  |  |  | if(wlzhVStRds.size() < pageSize){ | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //ERP库存与立库库存比对 | 
|---|
|  |  |  | compileStock(wlzhVStRds,inventoryVariances); | 
|---|
|  |  |  | pageNumber ++; | 
|---|
|  |  |  | inventoryVarianceService.insertBatch(inventoryVariances); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Scheduled(cron = "0/5 * * * * ? ") | 
|---|
|  |  |  | @Synchronized | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public synchronized void syncOrder() { | 
|---|
|  |  |  | List<VoucherDto> list = erpService.selectOrder(0); | 
|---|
|  |  |  | for (VoucherDto dto : list) { | 
|---|
|  |  |  | Voucher voucher = dto.getVoucher(); | 
|---|
|  |  |  | Order order = orderService.selectByNo(voucher.getVoucherID()); | 
|---|
|  |  |  | if (Cools.isEmpty(order)) { | 
|---|
|  |  |  | private void compileStock(List<WlzhVStRd> wlzhVStRds, List<InventoryVariance> inventoryVariances){ | 
|---|
|  |  |  | wlzhVStRds.forEach(wlzhVStRd -> { | 
|---|
|  |  |  | String matnr = wlzhVStRd.getCinvcode(); | 
|---|
|  |  |  | String csocode = wlzhVStRd.getCsocode(); | 
|---|
|  |  |  | String isoseq = wlzhVStRd.getIsoseq(); | 
|---|
|  |  |  | Double iQuantity = wlzhVStRd.getIquantity(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String regEx = "[^0-9]"; | 
|---|
|  |  |  | Pattern compile = Pattern.compile(regEx); | 
|---|
|  |  |  | Matcher matcher = compile.matcher(voucher.getMT()); | 
|---|
|  |  |  | String docTypeVal = matcher.replaceAll("").trim(); | 
|---|
|  |  |  | Double anfme = 0.0; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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,    // 邮费支付类型 | 
|---|
|  |  |  | voucher.getExpectedWeight(),    // 邮费 | 
|---|
|  |  |  | null,    // 付款时间 | 
|---|
|  |  |  | null,    // 发货时间 | 
|---|
|  |  |  | null,    // 物流名称 | 
|---|
|  |  |  | null,    // 物流单号 | 
|---|
|  |  |  | 1L,    // 订单状态 | 
|---|
|  |  |  | 1,    // 状态 | 
|---|
|  |  |  | 9527L,    // 添加人员 | 
|---|
|  |  |  | now,    // 添加时间 | 
|---|
|  |  |  | 9527L,    // 修改人员 | 
|---|
|  |  |  | now,    // 修改时间 | 
|---|
|  |  |  | null    // 备注 | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | if (!orderService.insert(order)) { | 
|---|
|  |  |  | throw new CoolException(order.getOrderNo() + "生成单据主档失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Mat mat = matService.selectByMatnr(voucher.getMaterialNO()); | 
|---|
|  |  |  | if (mat == null) { | 
|---|
|  |  |  | throw new CoolException(voucher.getMaterialNO() + "物料号不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | OrderDetl orderDetl = new OrderDetl(); | 
|---|
|  |  |  | orderDetl.sync(mat); | 
|---|
|  |  |  | StringBuilder sb = new StringBuilder(); | 
|---|
|  |  |  | if (!Cools.isEmpty(dto.getDetails())) { | 
|---|
|  |  |  | for (VoucherDetail detail : dto.getDetails()) { | 
|---|
|  |  |  | sb.append(detail.getBarcode()).append(","); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderDetl.setBatch(sb.toString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderDetl.setAnfme(voucher.getExpectedWeight()); | 
|---|
|  |  |  | 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() + "生成单据明细失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                else { | 
|---|
|  |  |  | //                    for (VoucherDetail detail : dto.getDetails()) { | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        Mat mat = matService.selectByMatnr(voucher.getMaterialNO()); | 
|---|
|  |  |  | //                        if (mat == null) { | 
|---|
|  |  |  | //                            throw new CoolException(voucher.getMaterialNO() + "物料号不存在"); | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        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(), state)) { | 
|---|
|  |  |  | throw new CoolException(voucher.getVoucherID() + "订单修改State为"+state+"失败"); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | erpService.updateTimeForVoucherDetail(voucher.getVoucherID()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | log.warn("{}订单已存在", voucher.getVoucherID()); | 
|---|
|  |  |  | List<AllLocDetl> allLocDetlList = allLocDetlService.getByMatnrAndCsocodeAndIsoseq(matnr, csocode, isoseq); | 
|---|
|  |  |  | for(AllLocDetl allLocDetl : allLocDetlList){ | 
|---|
|  |  |  | anfme += allLocDetl.getAnfme(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int compare = Double.compare(iQuantity, anfme); | 
|---|
|  |  |  | if(compare != 0){ | 
|---|
|  |  |  | InventoryVariance inventoryVarianceParam = new InventoryVariance(matnr,csocode,isoseq,iQuantity,anfme); | 
|---|
|  |  |  | inventoryVariances.add(inventoryVarianceParam); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|