自动化立体仓库 - WMS系统
src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java
@@ -1,27 +1,29 @@
package com.zy.asrs.task.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.OrderDetlService;
import com.zy.asrs.service.OrderService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.service.impl.OrderDetl1ServiceImpl;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.common.constant.MesConstant;
import com.zy.common.model.MesPakinParam;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.User;
import com.zy.system.service.UserService;
import com.zy.third.entity.*;
import com.zy.third.mapper.ExdMaterialMapper;
import com.zy.third.service.ExdInstockTargetService;
import com.zy.third.service.ExdOutstockSourceService;
import com.zy.third.service.ExdOutstockTargetService;
import com.zy.third.service.ExdStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
@@ -31,66 +33,367 @@
@Service
public class OrderSyncHandler extends AbstractHandler<String> {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private ExdInstockTargetService exdInstockTargetService;
    @Autowired
    private ExdOutstockTargetService exdOutstockTargetService;
    @Autowired
    private MatService matService;
    @Autowired
    private OrderDetl1ServiceImpl orderDetl1Service;
    @Autowired
    private ExdMaterialMapper exdMaterialMapper;
    @Autowired
    private Order1Service order1Service;
    @Autowired
    private ExdOutstockSourceService exdOutstockSourceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ExdStockService exdStockService;
    @Transactional
    public ReturnT<String> start(Order order) {
        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
        MesPakinParam pakinParam = new MesPakinParam();
        pakinParam.setPakinTime(DateUtils.convert(order.getUpdateTime()));
        pakinParam.setLgortFrom("5008");
        pakinParam.setLgortTo("5006");
        for (OrderDetl orderDetl : orderDetls) {
            String serial = Cools.isEmpty(orderDetl.getBatch()) ? "" : orderDetl.getBatch();
            pakinParam.getList().add(new MesPakinParam.Detl(orderDetl.getMatnr() + (Cools.isEmpty(serial) ? "" : "-" + serial), orderDetl.getAnfme()));
        DocType docType = docTypeService.selectById(order.getDocType());
        if (null == docType) {
            return SUCCESS;
        }
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                        .setUri(MesConstant.URL)
                        .setPath(MesConstant.PAKIN_URL)
                        .setJson(JSON.toJSONString(pakinParam))
                        .build()
                        .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code").equals(200)) {
                success = true;
                // 修改订单状态 4.完成 ===>> 6.已上报
                if (!orderService.updateSettle(order.getId(), 6L, null)) {
                    throw new CoolException("服务器内部错误,请联系管理员");
        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
        if (orderDetls.isEmpty()) {
            return SUCCESS;
        }
        boolean boo=false;
        // 入库完成上报
        if (docType.getPakin() == 1) {
            ExdInstockTarget exdInstockTarge = null;
            for (OrderDetl orderDetl : orderDetls) {
                exdInstockTarge = new ExdInstockTarget();
                exdInstockTarge.setFinterid(orderDetl.getDeadWarn());
                exdInstockTarge.setFbillno(orderDetl.getOrderNo());
                Integer decType = order.getDocType().intValue();
                if(order.getDocType().intValue()==42){
                    decType = 41;
                }else if(order.getDocType().intValue()==25){
                    decType = 24;
                }else if(order.getDocType().intValue()==3){
                    decType = 1;
                }
            } else {
                log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PAKIN_URL, JSON.toJSONString(pakinParam), response);
                throw new CoolException("上报mes系统失败");
                exdInstockTarge.setFtrantype(decType);
                exdInstockTarge.setFdate(DateUtils.convert(order.getOrderTime()));
                Integer frob = 1;
                if(order.getDocType().intValue()==21||order.getDocType().intValue()==24||order.getDocType().intValue()==2||order.getDocType().intValue()==3){
                    frob = -1;
                }
                exdInstockTarge.setFrob(frob);
                //操作用户
                User id = userService.selectOne(new EntityWrapper<User>().eq("id", orderDetl.getUpdateBy()));
                exdInstockTarge.setFuserid(Integer.valueOf(id.getEmail()));
                exdInstockTarge.setWritetime(new Date());
                exdInstockTarge.setWritor("WMS");
                exdInstockTarge.setStatus(0);
                exdInstockTarge.setProdinkind(orderDetl.getItemNum());
                exdInstockTargetService.insert(exdInstockTarge);
            }
        } catch (Exception e) {
            log.error("fail", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return FAIL.setMsg(e.getMessage());
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "成品库入库上报",
                        MesConstant.URL + MesConstant.PAKIN_URL,
                        null,
                        "127.0.0.1",
                        JSON.toJSONString(pakinParam),
                        response,
                        success
                );
            } catch (Exception e) { log.error("", e); }
            if (!orderService.updateSettle(order.getId(), 6L, null)) {
                throw new CoolException("服务器内部错误,请联系管理员");
            }else {
//                boo = true;
            }
        } else if (docType.getPakout() == 1) {
            // 出库完成上报
            ExdOutstockTarget exdOutstockTarget = null;
            for (OrderDetl orderDetl : orderDetls) {
                exdOutstockTarget = new ExdOutstockTarget();
                exdOutstockTarget.setFinterid(orderDetl.getDeadWarn());
                exdOutstockTarget.setFbillno(orderDetl.getOrderNo());
                if (order.getDocType() == 42) {
                    exdOutstockTarget.setFtrantype(41);
                } else {
                    exdOutstockTarget.setFtrantype(order.getDocType().intValue());
                }
                exdOutstockTarget.setFdate(DateUtils.convert(order.getOrderTime()));
                exdOutstockTarget.setFrob(order.getDocType().intValue()==2? -1:1);
                //操作用户
                User id = userService.selectOne(new EntityWrapper<User>().eq("id", orderDetl.getUpdateBy()));
                exdOutstockTarget.setFuserid(Integer.valueOf(id.getEmail()));
                exdOutstockTarget.setWritetime(new Date());
                exdOutstockTarget.setWritor("WMS");
                exdOutstockTarget.setStatus(0);
                exdOutstockTarget.setProdinkind(orderDetl.getItemNum());
                exdOutstockTargetService.insert(exdOutstockTarget);
            }
            if (!orderService.updateSettle(order.getId(), 6L, null)) {
                throw new CoolException("服务器内部错误,请联系管理员");
            }else {
                boo = true;
            }
        }
        //普通入出订单子单完成
        if (boo){
            Order1 orderNo = order1Service.selectOne(new EntityWrapper<Order1>().like("order_no", order.getOrderNo()));
            orderNo.setSettle(6L);
            order1Service.update(orderNo,new  EntityWrapper<Order1>().eq("order_no",order.getOrderNo()));
        }
        return SUCCESS;
    }
    @Transactional
    public ReturnT<String> start1(Order1 order) {
        DocType docType = docTypeService.selectById(order.getDocType());
        if (null == docType) {
            return FAIL;
        }
        List<OrderDetl1> orderDetls = orderDetl1Service.selectByOrderId(order.getId());
        if (orderDetls.isEmpty()) {
            return FAIL;
        }
        int i = 1;
        for (OrderDetl1 orderDetl : orderDetls) {
            ExdvYanbu bacode = exdMaterialMapper.getBacode(orderDetl.getBatch());
            //获取主表明细
            OrderDetl orderDetl1 = orderDetlService.selectItem(orderDetl.getOrderNo().split("-")[0], orderDetl.getMatnr(), null);
            //上报数量
            BigDecimal anfme = new BigDecimal(orderDetl1.getAnfme());
            BigDecimal Units = new BigDecimal((Cools.isEmpty(orderDetl1.getUnits()) ? 0.0 : orderDetl1.getUnits()));
            BigDecimal result = anfme.subtract(Units);
            double count = result.doubleValue();
//            //剩余数量
//            BigDecimal Anfme = new BigDecimal(orderDetl.getAnfme());
//            BigDecimal subtract = Anfme.subtract(Qty);
            // 出库完成上报
            ExdOutstockTarget exdOutstockTarget = null;
            exdOutstockTarget = new ExdOutstockTarget();
            //订单内码
            exdOutstockTarget.setFinterid(orderDetl.getDeadWarn());
            //单号 = erp订单号+物料编号+上报次数
            exdOutstockTarget.setFbillno(order.getOrderNo());
            //单据类型
            exdOutstockTarget.setFtrantype(21);
            //单据日期
            exdOutstockTarget.setFdate(DateUtils.convert(order.getOrderTime()));
            //红蓝字
            exdOutstockTarget.setFrob(1);
            //操作用户
            User id = userService.selectOne(new EntityWrapper<User>().eq("id", orderDetl.getCreateBy()));
            exdOutstockTarget.setFuserid(Integer.valueOf(id.getEmail()));
            //部门ID
            exdOutstockTarget.setFdeptid(7502);
            //供应商/客户ID
            exdOutstockTarget.setFsupplyid(0);
            //制单用户ID
            exdOutstockTarget.setFbillerid(Integer.valueOf(id.getEmail()));
            //细表ID
            Integer c = Integer.valueOf(order.getOrderNo().split("-")[1]);
            exdOutstockTarget.setFentryid(i++);
            //物料ID
            Mat mat = matService.selectByMatnr(orderDetl.getMatnr());
            exdOutstockTarget.setFitemid(Integer.valueOf(mat.getUuid()));
            //源单数量
            exdOutstockTarget.setFqtymust(count);
            //出库数量
            exdOutstockTarget.setFqty(bacode.getQty());
            //单价
            exdOutstockTarget.setFprice(0.0);
            //批号 暂定
            exdOutstockTarget.setFbatchno(orderDetl.getBatch());
            //单位ID
            exdOutstockTarget.setFunitid(0);
            //源单号
            exdOutstockTarget.setFsourcebillno(orderDetl.getOrderNo().split("-")[0]);
            ExdOutstockSource exdOutstockSource = exdOutstockSourceService.selectOne(new EntityWrapper<ExdOutstockSource>()
                    .eq("FBillNo", orderDetl.getOrderNo().split("-")[0])
                    .eq("FItemID", mat.getUuid()));
            //源单细表ID
            exdOutstockTarget.setFsourceentryid(exdOutstockSource.getFentryid());
            //源单内码
            exdOutstockTarget.setFsourceinterid(exdOutstockSource.getFinterid());
            //源单单据类型
            exdOutstockTarget.setFsourcetrantype(81);
            //库位ID
            exdOutstockTarget.setFdcspid(0);
            //仓库ID
            Integer Fdcstockid = 19382;
            if(!Cools.isEmpty(bacode.getProdinkind())){
                ExdStock fname = exdStockService.selectOne(new EntityWrapper<ExdStock>().eq("Fname", orderDetl.getBrand()));
                Fdcstockid = Integer.valueOf(fname.getId());
            }
            exdOutstockTarget.setFdcstockid(Fdcstockid);
            //销售订单单号 暂定
            exdOutstockTarget.setForderbillno(exdOutstockSource.getForderbillno());
            //销售订单细表ID 暂定
            exdOutstockTarget.setForderentryid(exdOutstockSource.getForderentryid());
            //销售订单内码 暂定
            exdOutstockTarget.setForderinterid(exdOutstockSource.getForderinterid());
            //计划模式
            exdOutstockTarget.setFplanmode(0);
            exdOutstockTarget.setBarcode(orderDetl.getBatch());
            exdOutstockTarget.setGross(BigDecimal.valueOf(bacode.getGross()));
            exdOutstockTarget.setWidth(BigDecimal.valueOf(bacode.getWidth()));
            exdOutstockTarget.setDefects(BigDecimal.valueOf(bacode.getDefects()));
            exdOutstockTarget.setBackcode(bacode.getBackcode());
            exdOutstockTarget.setWritetime(new Date());
            exdOutstockTarget.setWritor("WMS");
            exdOutstockTarget.setStatus(0);
//
//        //仓库
            exdOutstockTarget.setProdinkind(bacode.getProdinkind());
            //写入时间
            exdOutstockTarget.setWritetime(new Date());
            //写入者
            exdOutstockTarget.setWritor("WMS");
            //状态
            exdOutstockTarget.setStatus(0);
            if (exdOutstockTargetService.insert(exdOutstockTarget)) {
            }
        }
        for (OrderDetl1 orderDetl : orderDetls) {
            OrderDetl orderDetl1 = orderDetlService.selectItem(orderDetl.getOrderNo().split("-")[0], orderDetl.getMatnr(), null);
            orderDetl1.setUnits(orderDetl.getAnfme() + (Cools.isEmpty(orderDetl1.getUnits()) ? 0.0 : orderDetl1.getUnits()));
            orderDetl1.setUpdateTime(new Date());
            orderDetlService.updateById(orderDetl1);
        }
        return SUCCESS;
    }
}
//    @Transactional
//    public ReturnT<String> start1(Order order, OrderDetl orderDetl) {
//        DocType docType = docTypeService.selectById(order.getDocType());
//        if (null == docType) {
//            return SUCCESS;
//        }
//        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
//        if (orderDetls.isEmpty()) {
//            return SUCCESS;
//        }
//
////        ExdvYanbu bacode = exdMaterialMapper.getBacode(wrkDetl.getThreeCode());
//
//        //上报数量
//        BigDecimal Qty = new BigDecimal(orderDetl.getQty());
//        BigDecimal Units = new BigDecimal((Cools.isEmpty(orderDetl.getUnits()) ? 0.0 : orderDetl.getUnits()));
//        BigDecimal result = Qty.subtract(Units);
//        double count = result.doubleValue();
//
//        //剩余数量
//        BigDecimal Anfme = new BigDecimal(orderDetl.getAnfme());
//        BigDecimal subtract = Anfme.subtract(Qty);
//
//
//        // 出库完成上报
//        ExdOutstockTarget exdOutstockTarget = null;
//        exdOutstockTarget = new ExdOutstockTarget();
//        //订单内码
//        exdOutstockTarget.setFinterid(orderDetl.getDeadWarn());
//        //单号 = erp订单号+物料编号+上报次数
//        exdOutstockTarget.setFbillno(orderDetl.getOrderNo() + "--" + orderDetl.getMatnr() + "--" + (Cools.isEmpty(orderDetl.getInspect()) ? 1 : (orderDetl.getInspect() + 1)));
//
//        //部门ID
//        exdOutstockTarget.setFdeptid(7502);
//        //供应商/客户ID
//        exdOutstockTarget.setFsupplyid(0);
//        //制单用户ID
//        exdOutstockTarget.setFbillerid(Math.toIntExact(orderDetl.getUpdateBy()));
//        //细表ID
//        exdOutstockTarget.setFentryid((Cools.isEmpty(orderDetl.getInspect()) ? 1 : (orderDetl.getInspect() + 1)));
//        //物料ID
//        Mat mat = matService.selectByMatnr(orderDetl.getMatnr());
//        exdOutstockTarget.setFitemid(Integer.valueOf(mat.getUuid()));
//        //源单数量
//        exdOutstockTarget.setFqtymust(subtract.doubleValue());
//        //出库数量
//        exdOutstockTarget.setFqty(count);
//        //单价
//        exdOutstockTarget.setFprice(0.0);
//        //批号 暂定
//        exdOutstockTarget.setFbatchno(orderDetl.getBatch());
//        //单位ID
//        exdOutstockTarget.setFunitid(0);
//        //源单号
//        exdOutstockTarget.setFsourcebillno(orderDetl.getOrderNo());
//        //源单细表ID 暂定
//        exdOutstockTarget.setFsourceentryid(0);
//        //源单内码 暂定
//        exdOutstockTarget.setFsourceinterid(0);
//        //源单单据类型
//        exdOutstockTarget.setFsourcetrantype(0);
//        //库位ID
//        exdOutstockTarget.setFdcspid(0);
//        //仓库ID 暂定
//        exdOutstockTarget.setFdcstockid(0);
//        //销售订单单号 暂定
//        exdOutstockTarget.setForderbillno(orderDetl.getOrderNo());
//        //销售订单细表ID 暂定
//        exdOutstockTarget.setForderentryid(0);
//        //销售订单内码 暂定
//        exdOutstockTarget.setForderinterid(0);
//        //计划模式
//        exdOutstockTarget.setFplanmode(0);
//
//        //单据类型
//        exdOutstockTarget.setFtrantype(21);
//
//        //单据日期
//        exdOutstockTarget.setFdate(DateUtils.convert(order.getOrderTime()));
//        //红蓝字
//        exdOutstockTarget.setFrob(orderDetl.getBeBatch());
//        //操作用户
//        exdOutstockTarget.setFuserid(Integer.getInteger(orderDetl.getColor()));
//
////        exdOutstockTarget.setBarcode(wrkDetl.getBatch());
////        exdOutstockTarget.setGross(bacode.getGross());
////        exdOutstockTarget.setWidth(bacode.getWidth());
////        exdOutstockTarget.setDefects(bacode.getDefects());
////        exdOutstockTarget.setBackcode(bacode.getBackcode());
////        exdOutstockTarget.setWritetime(new Date());
////        exdOutstockTarget.setWritor("WMS");
////        exdOutstockTarget.setStatus(0);
////
////        //仓库
////        exdOutstockTarget.setProdinkind(bacode.getProdinkind());
//
//        //写入时间
//        exdOutstockTarget.setWritetime(new Date());
//        //写入者
//        exdOutstockTarget.setWritor("WMS");
//        //状态
//        exdOutstockTarget.setStatus(0);
//        if (exdOutstockTargetService.insert(exdOutstockTarget)) {
//            orderDetl.setInspect((Cools.isEmpty(orderDetl.getInspect()) ? 1 : (orderDetl.getInspect() + 1)));
//            orderDetl.setUnits(count + (Cools.isEmpty(orderDetl.getUnits()) ? 0.0 : orderDetl.getUnits()));
//            orderDetlService.updateById(orderDetl);
//        }
//
//        return SUCCESS;
//    }