| package com.zy.asrs.task.handler; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.core.common.Cools; | 
| import com.core.common.DateUtils; | 
| import com.core.exception.CoolException; | 
| import com.zy.asrs.entity.DocType; | 
| import com.zy.asrs.entity.Order; | 
| import com.zy.asrs.entity.OrderDetl; | 
| import com.zy.asrs.service.ApiLogService; | 
| import com.zy.asrs.service.DocTypeService; | 
| import com.zy.asrs.service.OrderDetlService; | 
| import com.zy.asrs.service.OrderService; | 
| import com.zy.asrs.task.AbstractHandler; | 
| import com.zy.asrs.task.core.ReturnT; | 
| import com.zy.asrs.utils.OrderInAndOutUtil; | 
| import com.zy.common.constant.MesConstant; | 
| import com.zy.common.model.DetlDto; | 
| import com.zy.common.model.MesPakinParam; | 
| import com.zy.common.model.MesPakoutParam; | 
| import com.zy.common.utils.HttpHandler; | 
| 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 java.util.ArrayList; | 
| import java.util.List; | 
|   | 
| /** | 
|  * Created by vincent on 2020/7/7 | 
|  */ | 
| @Slf4j | 
| @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; | 
|   | 
|     @Transactional | 
|     public ReturnT<String> startOrderIssuedOnceMore(Order order) { | 
|         DocType docType = docTypeService.selectById(order.getDocType()); | 
|         if (null == docType) { | 
|             return SUCCESS; | 
|         } | 
|   | 
|         Order order1 = OrderInAndOutUtil.selectByNo(order.getPakinPakoutStatus$(), order.getOrderNo()); | 
|         if (!Cools.isEmpty(order1)){ | 
|             return FAIL.setMsg("请等待...订单执行档案正在转历史档案!!!"); | 
|         } | 
|         List<OrderDetl> orderDetls = OrderInAndOutUtil.selectByOrderId(order.getPakinPakoutStatus$(), order.getId()); | 
|         if (!Cools.isEmpty(orderDetls) || !orderDetls.isEmpty()) { | 
|             return FAIL.setMsg("请等待...订单执行明细档案正在转历史档案!!!"); | 
|         } | 
|   | 
|         // 单据明细档 | 
|         List<OrderDetl> orderDetlList = orderDetlService.selectByOrderId(order.getId()); | 
|         for (OrderDetl orderDetl : orderDetlList){ | 
|             if (orderDetl.getQty().equals(0D)){ | 
|                 continue; | 
|             } | 
|             orderDetl.setWorkQty(orderDetl.getWorkQty()+orderDetl.getQty()); | 
| //            orderDetl.setAnfme(orderDetl.getAnfme()-orderDetl.getQty()); | 
|             orderDetl.setAnfme(orderDetl.getAnfme()); | 
|             orderDetl.setQty(0D); | 
|             orderDetlService.updateById(orderDetl); | 
|         } | 
|   | 
|         // 修改订单状态 99.再次作业 ===>> 1.待作业 | 
|         if (!orderService.updateSettle(order.getId(), 1L, null)) { | 
|             throw new CoolException("服务器内部错误,请联系管理员"); | 
|         } | 
|         return SUCCESS; | 
|     } | 
|   | 
|     @Transactional | 
|     public ReturnT<String> startOrderIssued(Order order) { | 
|         DocType docType = docTypeService.selectById(order.getDocType()); | 
|         if (null == docType) { | 
|             return SUCCESS; | 
|         } | 
|         List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId()); | 
|   | 
|         OrderInAndOutUtil.insertOrder(order.getPakinPakoutStatus$(),order); | 
|         // 单据明细档 | 
|         List<DetlDto> list = new ArrayList<>(); | 
|         List<OrderDetl> orderDetlList = new ArrayList<>(); | 
|   | 
|         for (OrderDetl detail : orderDetls) { | 
|             detail.setAnfme(detail.getAnfme() - detail.getWorkQty()); | 
|             if (detail.getAnfme().equals(0D)){ | 
|                 continue; | 
|             } | 
|             DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(),detail.getBrand(),detail.getStandby1(),detail.getStandby2(),detail.getStandby3(), | 
|                     detail.getBoxType1(),detail.getBoxType2(),detail.getBoxType3(), detail.getAnfme()); | 
|             if (DetlDto.has(list, dto)) { | 
|                 DetlDto detlDto = DetlDto.find(list, dto.getMatnr(), dto.getBatch(),dto.getBrand(),dto.getStandby1(),dto.getStandby2(),dto.getStandby3(),dto.getBoxType1(),dto.getBoxType2(),dto.getBoxType3()); | 
|                 assert detlDto != null; | 
|                 detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme()); | 
|   | 
|                 OrderDetl orderDetl = OrderDetl.find(orderDetlList, dto.getMatnr(), dto.getBatch(),dto.getBrand(),dto.getStandby1(),dto.getStandby2(),dto.getStandby3(),dto.getBoxType1(),dto.getBoxType2(),dto.getBoxType3()); | 
|                 assert orderDetl != null; | 
|                 orderDetl.setAnfme(orderDetl.getAnfme() + detail.getAnfme()); | 
|             } else { | 
|                 list.add(dto); | 
|                 orderDetlList.add(detail); | 
|             } | 
|         } | 
|         if (orderDetlList.size()==0){ | 
|             // 修改订单状态 1.未作业 ===>> 2.作业中 | 
|             if (!orderService.updateSettle(order.getId(), 4L, null)) { | 
|                 throw new CoolException("服务器内部错误,请联系管理员"); | 
|             } | 
|         } | 
|         for (OrderDetl orderDetl : orderDetlList){ | 
|             orderDetl.setWorkQty(0D); | 
|             OrderInAndOutUtil.insertOrderDetl(order.getPakinPakoutStatus$(),order,orderDetl); | 
|         } | 
|         // 修改订单状态 1.未作业 ===>> 2.作业中 | 
|         if (!orderService.updateSettle(order.getId(), 2L, null)) { | 
|             throw new CoolException("服务器内部错误,请联系管理员"); | 
|         } | 
|         return SUCCESS; | 
|     } | 
|   | 
|     @Transactional | 
|     public ReturnT<String> startOrderInAndOutUtil(Order order) { | 
|         DocType docType = docTypeService.selectById(order.getDocType()); | 
|         if (null == docType) { | 
|             return SUCCESS; | 
|         } | 
|         Order orderSou = orderService.selectByNo(order.getOrderNo()); | 
|         if (!orderSou.getSettle().equals(2L)){ | 
|             return FAIL.setMsg("订单状态不正确,请联系管理员"); | 
|         } | 
|         List<OrderDetl> orderDetlList = orderDetlService.selectByOrderId(orderSou.getId()); | 
|   | 
|         List<OrderDetl> orderDetls = OrderInAndOutUtil.selectByOrderId(order.getPakinPakoutStatus$(), order.getId()); | 
|   | 
|         for (OrderDetl orderDetlSou : orderDetlList){ | 
|             for (OrderDetl orderDetl : orderDetls){ | 
|                 if (orderDetl.getQty().equals(0D)){ | 
|                     continue; | 
|                 } | 
|                 if (orderDetlSou.beSimilar(orderDetl)){ | 
|                     double anfme = orderDetlSou.getAnfme() - orderDetlSou.getWorkQty(); | 
|                     double v = anfme - orderDetlSou.getQty(); | 
|                     if (v<0D){ | 
|                         v = 0D; | 
|                     } | 
|                     if (v<orderDetl.getQty() || orderDetl.getQty().equals(v)){ | 
|                         orderDetlSou.setQty(anfme); | 
|                         orderDetl.setQty(orderDetl.getQty() - v); | 
|                         break; | 
|                     } else { | 
|                         orderDetlSou.setQty(orderDetlSou.getQty()+orderDetl.getQty()); | 
|                         orderDetl.setQty(0D); | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|         for (OrderDetl orderDetlSou : orderDetlList){ | 
|             orderDetlService.updateById(orderDetlSou); | 
|         } | 
|         // 修改订单状态 2.作业中 ===>> 4.待上报 | 
|         if (!orderService.updateSettle(orderSou.getId(), 4L, null)) { | 
|             throw new CoolException("服务器内部错误,请联系管理员"); | 
|         } | 
|         // 修改订单状态 4.完成 ===>> 6.结束 | 
|         OrderInAndOutUtil.updateOrder(order.getPakinPakoutStatus$(),order.getId(),6L,null); | 
|         return SUCCESS; | 
|     } | 
|     @Transactional | 
|     public ReturnT<String> startOrderReport(Order order) { | 
|         DocType docType = docTypeService.selectById(order.getDocType()); | 
|         if (null == docType) { | 
|             return SUCCESS; | 
|         } | 
|         long settle = 6L; | 
|         List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId()); | 
|         // 入库完成上报 | 
|         if (docType.getPakin() == 1) { | 
|             MesPakinParam pakinParam = new MesPakinParam(); | 
|             pakinParam.setPakinTime(DateUtils.convert(order.getUpdateTime())); | 
|             pakinParam.setLgortFrom("5008"); | 
|             pakinParam.setLgortTo("5006"); | 
|             for (OrderDetl orderDetl : orderDetls) { | 
|                 double anfme = orderDetl.getAnfme() - orderDetl.getWorkQty(); | 
|                 if (anfme<=0D){ | 
|                     continue; | 
|                 } | 
|                 if (!orderDetl.getQty().equals(orderDetl.getAnfme())){ | 
|                     settle = 99L;//二次生成单据 | 
|                 } | 
|                 String serial = Cools.isEmpty(orderDetl.getBatch()) ? "" : orderDetl.getBatch(); | 
|                 pakinParam.getList().add(new MesPakinParam.Detl(orderDetl.getMatnr() + (Cools.isEmpty(serial) ? "" : "-" + serial), orderDetl.getQty())); | 
|             } | 
|   | 
|             String response = ""; | 
|             boolean success = false; | 
|             try { | 
|                 if (pakinParam.getList().size()==0){ | 
|                     success = true; | 
|                     // 修改订单状态 4.完成 ===>> 6.已上报 | 
|                     if (!orderService.updateSettle(order.getId(), settle, null)) { | 
|                         throw new CoolException("服务器内部错误,请联系管理员"); | 
|                     } | 
|                 } else { | 
| //                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)) { | 
|                     if (true) { | 
|                         success = true; | 
|                         // 修改订单状态 4.完成 ===>> 6.已上报 | 
|                         if (!orderService.updateSettle(order.getId(), settle, null)) { | 
|                             throw new CoolException("服务器内部错误,请联系管理员"); | 
|                         } | 
|                     } else { | 
|                         log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PAKIN_URL, JSON.toJSONString(pakinParam), response); | 
|                         throw new CoolException("上报mes系统失败"); | 
|                     } | 
|                 } | 
|             } 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 (docType.getPakout() == 1) { | 
|             MesPakoutParam pakoutParam = new MesPakoutParam(); | 
|             pakoutParam.setTag(!order.getDocType$().equalsIgnoreCase("手动出库单")); | 
|             pakoutParam.setPakoutTime(DateUtils.convert(order.getUpdateTime())); | 
|             pakoutParam.setLgortFrom("5006"); | 
|             pakoutParam.setLgortTo("1111"); | 
|             if (!pakoutParam.isTag()) { | 
|                 pakoutParam.setKunnr("C1000"); | 
|             } | 
|             pakoutParam.setOrderNo(order.getOrderNo()); | 
|             for (OrderDetl orderDetl : orderDetls) { | 
|                 if (!orderDetl.getQty().equals(orderDetl.getAnfme())){ | 
|                     settle = 99L;//二次生成单据 | 
|                 } | 
|                 String serial = Cools.isEmpty(orderDetl.getBatch()) ? "" : orderDetl.getBatch(); | 
|                 pakoutParam.getList().add(new MesPakoutParam.Detl(orderDetl.getMatnr() + (Cools.isEmpty(serial) ? "" : "-" + serial), orderDetl.getAnfme())); | 
|             } | 
|             String response = ""; | 
|             boolean success = false; | 
|             try { | 
| //                response = new HttpHandler.Builder() | 
| //                        .setUri(MesConstant.URL) | 
| //                        .setPath(MesConstant.PAKOUT_URL) | 
| //                        .setJson(JSON.toJSONString(pakoutParam)) | 
| //                        .build() | 
| //                        .doPost(); | 
| //                JSONObject jsonObject = JSON.parseObject(response); | 
| //                if (jsonObject.getInteger("code").equals(200)) { | 
|                 if (true) { | 
|                     success = true; | 
|                     // 修改订单状态 4.完成 ===>> 6.已上报 | 
|                     if (!orderService.updateSettle(order.getId(), settle, null)) { | 
|                         throw new CoolException("服务器内部错误,请联系管理员"); | 
|                     } | 
|                 } else { | 
|                     log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PAKOUT_URL, JSON.toJSONString(pakoutParam), response); | 
|                     throw new CoolException("上报mes系统失败"); | 
|                 } | 
|             } catch (Exception e) { | 
|                 log.error("fail", e); | 
| //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); | 
|                 return FAIL.setMsg(e.getMessage()); | 
|             } finally { | 
|                 try { | 
|                     // 保存接口日志 | 
|                     apiLogService.save( | 
|                             "成品库出库上报", | 
|                             MesConstant.URL + MesConstant.PAKOUT_URL, | 
|                             null, | 
|                             "127.0.0.1", | 
|                             JSON.toJSONString(pakoutParam), | 
|                             response, | 
|                             success | 
|                     ); | 
|                 } catch (Exception e) { log.error("", e); } | 
|             } | 
|         } | 
|         return SUCCESS; | 
|     } | 
|   | 
| } |