|  |  | 
 |  |  | import com.alibaba.fastjson.JSONObject;
 | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 | 
 |  |  | import com.fasterxml.jackson.core.JsonProcessingException;
 | 
 |  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
 |  |  | import com.fasterxml.jackson.databind.cfg.CoercionAction;
 | 
 |  |  | import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
 | 
 |  |  | import com.zy.asrs.framework.common.R;
 | 
 |  |  | import com.zy.asrs.framework.exception.CoolException;
 | 
 |  |  | import com.zy.asrs.wms.apis.wcs.entity.domain.SystemProperties;
 | 
 |  |  | 
 |  |  | import com.zy.asrs.wms.asrs.service.*;
 | 
 |  |  | import io.jsonwebtoken.lang.Collections;
 | 
 |  |  | import lombok.extern.slf4j.Slf4j;
 | 
 |  |  | import org.slf4j.Logger;
 | 
 |  |  | import org.slf4j.LoggerFactory;
 | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
 |  |  | import org.springframework.http.HttpEntity;
 | 
 |  |  | import org.springframework.http.HttpHeaders;
 | 
 |  |  | 
 |  |  | import org.springframework.web.client.RestTemplate;
 | 
 |  |  | 
 | 
 |  |  | import javax.annotation.Resource;
 | 
 |  |  | import java.math.BigDecimal;
 | 
 |  |  | import java.util.*;
 | 
 |  |  | import java.util.stream.Collectors;
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |         if (task == null) {
 | 
 |  |  |             return R.error("任务不存在!!");
 | 
 |  |  |         }
 | 
 |  |  |         log.info("容器到达接收,任务号:{},当前状态:{},上报状态:{}",task.getTaskNo(),task.getTaskSts$(),TaskStsType.WCS_CONTAINER_RECEIVE.desc);
 | 
 |  |  |         log.info("容器到达接收,任务号:{},当前状态:{},上报状态:{}, 接收参数:{}",task.getTaskNo(),task.getTaskSts$(),TaskStsType.WCS_CONTAINER_RECEIVE.desc, JSONObject.toJSONString(arrivedParam));
 | 
 |  |  | 
 | 
 |  |  |         taskType = task.getTaskType() > 100 ? "outStock":"inStock";
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |             if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_ARRIVED.id
 | 
 |  |  |             || task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TASK_DONE.id ) {
 | 
 |  |  |                 // 到达拣选位,控制播种墙亮灯
 | 
 |  |  |                     List<CacheSite> cacheSites = cacheSiteService.list(new LambdaQueryWrapper<CacheSite>().eq(CacheSite::getBarcode, task.getBarcode()));
 | 
 |  |  |                     List<CacheSite> cacheSites = cacheSiteService.list(new LambdaQueryWrapper<CacheSite>()
 | 
 |  |  |                             .eq(CacheSite::getBarcode, task.getBarcode()));
 | 
 |  |  |                     if(cacheSites.size() == 0) {
 | 
 |  |  |                         log.error("条码:{},未找到播种墙亮灯",task.getBarcode());
 | 
 |  |  |                         throw new CoolException("未找到播种墙");
 | 
 |  |  | 
 |  |  |                     for (CacheSite cacheSite:cacheSites) {
 | 
 |  |  |                         // 获取任务明细数量
 | 
 |  |  |                         List<TaskDetl> taskDetlByTaskId = taskDetlService.getTaskDetlByTaskId(task.getId());
 | 
 |  |  |                         double sum = taskDetlByTaskId.stream().mapToDouble(TaskDetl::getAnfme).sum();
 | 
 |  |  |                         int sum = (int)taskDetlByTaskId.stream().mapToDouble(TaskDetl::getAnfme).sum();
 | 
 |  |  |                         SlapLightControlParam slapLightControlParam = new SlapLightControlParam();
 | 
 |  |  |                         slapLightControlParam
 | 
 |  |  |                                 .setControllerCode(cacheSite.getMemo())
 | 
 |  |  |                                 .setTagCode(cacheSite.getSiteNo())
 | 
 |  |  |                                 .setColor("GREEN")
 | 
 |  |  |                                 .setIndex(cacheSite.getIndex())
 | 
 |  |  |                                 .setMode("LIGHT")
 | 
 |  |  |                                 .setDisplay(String.valueOf(sum));
 | 
 |  |  |                                 .setDisplay(sum + "");
 | 
 |  |  |                         // 发起亮灯请求
 | 
 |  |  |                         log.info("任务{}到达拣选位,播种墙亮灯下发",task.getTaskNo(), JSONObject.toJSONString(slapLightControlParam));
 | 
 |  |  |                         CommonReponse response = HttpEssUtils.post("到达拣选位,播种墙亮灯", HttpEssUtils.PLT_SEND_COMMAND, slapLightControlParam);
 | 
 |  |  | 
 |  |  |      * @return
 | 
 |  |  |      */
 | 
 |  |  |     @Override
 | 
 |  |  |     public synchronized R containerConveryor(String code) {
 | 
 |  |  |         List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getBarcode, code));
 | 
 |  |  |         if (tasks.isEmpty()) {
 | 
 |  |  |             return R.error("容器码任务不存在!!");
 | 
 |  |  |         } else if (tasks.size() > 1) {
 | 
 |  |  |             return R.error("容器码任务错误!!");
 | 
 |  |  |         }
 | 
 |  |  |         tasks.forEach(task -> {
 | 
 |  |  |             try {
 | 
 |  |  |                 ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
 | 
 |  |  |                 conveyorStarParam.setSlotCode(task.getOriginSite())
 | 
 |  |  |                         .setContainerCode(task.getBarcode());
 | 
 |  |  |                 if (task.getTaskType() == 101) { //任务类型为101全盘出库,直接取下容器,传200
 | 
 |  |  |                     conveyorStarParam.setDirection("200");
 | 
 |  |  |                 } else if (task.getTaskType() == 103 || task.getTaskType() == 107) { //如果为任务类型为103,需走回库操作,传100
 | 
 |  |  |                     conveyorStarParam.setDirection("100");
 | 
 |  |  |                 }
 | 
 |  |  |                 //调用三方接口,将任务推送至ESS平台
 | 
 |  |  |                 MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
 | 
 |  |  |                 // 设置请求参数
 | 
 |  |  |                 HttpHeaders headers = new HttpHeaders();
 | 
 |  |  |                 headers.add("Content-Type", "application/json");
 | 
 |  |  |                 headers.add("api-version", "v2.0");
 | 
 |  |  |                 HttpEntity httpEntity = new HttpEntity<>(conveyorStarParam, headers);
 | 
 |  |  |                 log.info("请求地址:{},请求参数:{}", HttpEssUtils.CONVEYOR_START, JSONObject.toJSONString(conveyorStarParam));
 | 
 |  |  |                 ResponseEntity<String> exchange = restTemplate.exchange("http://192.168.2.200:9046/conveyor/moveContainer", HttpMethod.POST, httpEntity, String.class);
 | 
 |  |  | //                HttpHeaders headers = new HttpHeaders();
 | 
 |  |  | //                headers.add("Content-Type", "application/json");
 | 
 |  |  | //                HttpEntity httpEntity = new HttpEntity<>(params, headers);
 | 
 |  |  | //                // 请求
 | 
 |  |  | //                ResponseEntity<String> exchange = restTemplate.exchange(HttpEssUtils.CONVEYOR_START, HttpMethod.POST, httpEntity, String.class);
 | 
 |  |  |                 log.info("下发流动通知 返回结果:{}", exchange);
 | 
 |  |  |                 if (exchange.getBody() == null) {
 | 
 |  |  |                     throw new CoolException("下发流动通知失败!!");
 | 
 |  |  |                 } else {
 | 
 |  |  |                     CommonReponse commonReponse = JSON.toJavaObject(JSON.parseObject(exchange.getBody()), CommonReponse.class);
 | 
 |  |  |                     if (commonReponse.getCode() == 0) {
 | 
 |  |  |                         //流动通知下发完成后,修改任务状态为输送线流动中。。
 | 
 |  |  |                         if (task.getTaskType() == 103 || task.getTaskType() == 107) {
 | 
 |  |  |                             //更新库存信息,修改任务状态为4 (RCS容器流动任务已下发)
 | 
 |  |  |                             workService.pickTask(task.getId());
 | 
 |  |  |             //                taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskSts, TaskStsType.WCS_CONVEYOR_START))
 | 
 |  |  |                         } else {
 | 
 |  |  |                             taskService.update(new LambdaUpdateWrapper<Task>()
 | 
 |  |  |                                     .eq(Task::getId, task.getId())
 | 
 |  |  |                                     .set(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
 | 
 |  |  |                         }
 | 
 |  |  |                         log.info(task.getTaskNo() + "下发流动通知" + commonReponse.getMsg());
 | 
 |  |  |                     } else {
 | 
 |  |  |                         throw new CoolException("下发流动通知失败!!");
 | 
 |  |  |                     }
 | 
 |  |  |                 }
 | 
 |  |  |             } catch (Exception ex) {
 | 
 |  |  |                 log.error(ex.getMessage());
 | 
 |  |  |     @Transactional(rollbackFor = Exception.class)
 | 
 |  |  |     public synchronized R containerConveryor(String code) throws Exception{
 | 
 |  |  |         Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getBarcode, code));
 | 
 |  |  |         if (Objects.isNull(task)) {
 | 
 |  |  |             throw new CoolException("容器码任务不存在!!");
 | 
 |  |  |         }  | 
 |  |  |         try {
 | 
 |  |  |             ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
 | 
 |  |  |             conveyorStarParam.setSlotCode(task.getOriginSite())
 | 
 |  |  |                     .setContainerCode(task.getBarcode());
 | 
 |  |  |             if (task.getTaskType() == 101) { //任务类型为101全盘出库,直接取下容器,传200
 | 
 |  |  |                 conveyorStarParam.setDirection("200");
 | 
 |  |  |             } else if (task.getTaskType() == 103 || task.getTaskType() == 107) { //如果为任务类型为103,需走回库操作,传100
 | 
 |  |  |                 conveyorStarParam.setDirection("100");
 | 
 |  |  |             }
 | 
 |  |  |         });
 | 
 |  |  |             //调用三方接口,将任务推送至ESS平台
 | 
 |  |  |             MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
 | 
 |  |  |             // 设置请求参数
 | 
 |  |  |             HttpHeaders headers = new HttpHeaders();
 | 
 |  |  |             headers.add("Content-Type", "application/json");
 | 
 |  |  |             headers.add("api-version", "v2.0");
 | 
 |  |  |             HttpEntity httpEntity = new HttpEntity<>(conveyorStarParam, headers);
 | 
 |  |  |             log.info("请求地址:{},请求参数:{}", HttpEssUtils.CONVEYOR_START, JSONObject.toJSONString(conveyorStarParam));
 | 
 |  |  |             ResponseEntity<String> exchange = restTemplate.exchange("http://192.168.2.200:9046/conveyor/moveContainer", HttpMethod.POST, httpEntity, String.class);
 | 
 |  |  |             log.info("下发流动通知 返回结果:{}", exchange);
 | 
 |  |  |             if (exchange.getBody() == null) {
 | 
 |  |  |                 throw new CoolException("下发流动通知失败!!");
 | 
 |  |  |             } else {
 | 
 |  |  |                 CommonReponse commonReponse = JSON.toJavaObject(JSON.parseObject(exchange.getBody()), CommonReponse.class);
 | 
 |  |  |                 if (commonReponse.getCode() == 0) {
 | 
 |  |  |                     //流动通知下发完成后,修改任务状态为输送线流动中。。
 | 
 |  |  |                     if (task.getTaskType() == 103 || task.getTaskType() == 107) {
 | 
 |  |  |                         //更新库存信息,修改任务状态为4 (RCS容器流动任务已下发)
 | 
 |  |  |                         workService.pickTask(task.getId());
 | 
 |  |  |                     } else {
 | 
 |  |  |                         taskService.update(new LambdaUpdateWrapper<Task>()
 | 
 |  |  |                                 .eq(Task::getId, task.getId())
 | 
 |  |  |                                 .set(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
 | 
 |  |  |                     }
 | 
 |  |  |                     log.info(task.getTaskNo() + "下发流动通知" + commonReponse.getMsg());
 | 
 |  |  |                 } else {
 | 
 |  |  |                     throw new CoolException("下发流动通知失败!!");
 | 
 |  |  |                 }
 | 
 |  |  |             }
 | 
 |  |  |         } catch (Exception ex) {
 | 
 |  |  |             log.error("UNK",  ex);
 | 
 |  |  |         }
 | 
 |  |  |         return R.ok();
 | 
 |  |  |     }
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |      * 拍灯拣料逻辑,详细说明见接口调用说明
 | 
 |  |  |      *
 | 
 |  |  |      * @param request
 | 
 |  |  |      * @return
 | 
 |  |  |      */
 | 
 |  |  |     @Override
 | 
 |  |  |     @Transactional(rollbackFor = Exception.class)
 | 
 |  |  |     public void slapLightLogic(Map<String, Object> request) {
 | 
 |  |  |     public R slapLightLogic(Map<String, Object> request) {
 | 
 |  |  |         String taskNo = request.get("taskNo").toString();
 | 
 |  |  |         String orderNo = request.get("orderNo").toString();
 | 
 |  |  | 
 | 
 |  |  |         String siteNo = Objects.isNull(request.get("siteNo")) ? null : request.get("siteNo").toString();
 | 
 |  |  |         List<CacheSite> performs = cacheSiteService.list(new LambdaQueryWrapper<CacheSite>().eq(CacheSite::getOrderNo, orderNo));
 | 
 |  |  |         if (Objects.isNull(performs.stream().findFirst().get().getPlatformId())) {
 | 
 |  |  |             throw new CoolException("请打印明细,并绑定目标集货区");
 | 
 |  |  | 
 |  |  |         //* 1. 判断当前容器是否还有物料未拣,未拣完闪灯,拣完通知容器流动,并灭灯
 | 
 |  |  |         SlapLightControlParam slapParam = new SlapLightControlParam();
 | 
 |  |  |         MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
 | 
 |  |  |         //默认流动
 | 
 |  |  |         boolean converyor = false;
 | 
 |  |  |         //判断当前任务是否还有物料未拣
 | 
 |  |  |         if (!checked(orderNo, taskNo)) {
 | 
 |  |  |             log.info("未完成闪灯请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
 | 
 |  |  |             //调用三方接口,闪灯不做操作
 | 
 |  |  |             // 设置请求参数
 | 
 |  |  |             params.add("params", JSONObject.toJSONString(slapParam));
 | 
 |  |  |             log.info("未完成闪灯请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
 | 
 |  |  |             throw new CoolException("当前任务未完成,不能执行此操作!!");
 | 
 |  |  | //            params.add("params", JSONObject.toJSONString(slapParam));
 | 
 |  |  |         } else {
 | 
 |  |  |             //调用三方接口,灭灯通知容器流动, 传灭灯参数
 | 
 |  |  |             //todo 判断当前订单是否完成,完成灭灯,未完成保持拣货状态亮灯
 | 
 |  |  |             Order one = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, orderNo));
 | 
 |  |  | 
 | 
 |  |  |             if (Objects.isNull(one)) {
 | 
 |  |  |                 throw new CoolException("当前订单明细不存在!!");
 | 
 |  |  |             }
 | 
 |  |  | 
 |  |  | //            }).collect(Collectors.toList());
 | 
 |  |  | 
 | 
 |  |  |             //判断数量是否与订单明细的需求量相同,相同则订单完成
 | 
 |  |  |             if (one.getPickStatus() == OrderPickStatus.ORDER_PICK_STATUS_DONE.val) {
 | 
 |  |  |                 //播种完成,释放绑定站点
 | 
 |  |  |                 List<CacheSite> cacheSites = cacheSiteService.list(new LambdaQueryWrapper<CacheSite>().eq(CacheSite::getOrderNo, orderNo));
 | 
 |  |  |                 for (CacheSite cacheSite : cacheSites) {
 | 
 |  |  |                     if (!cacheSite.getSiteStatus().equals(CacheSiteStatusType.O.id)) {
 | 
 |  |  |                         cacheSite.setSiteStatus(CacheSiteStatusType.O.id);
 | 
 |  |  |                         cacheSite.setOrderId(null);
 | 
 |  |  |                         cacheSite.setBarcode(null);
 | 
 |  |  |                         cacheSite.setOrderNo(null);
 | 
 |  |  |                         cacheSite.setPlatformId(null);
 | 
 |  |  |                         cacheSite.setPlatformNo(null);
 | 
 |  |  |                         cacheSite.setUpdateTime(new Date());
 | 
 |  |  |                         if (!cacheSiteService.updateById(cacheSite)) {
 | 
 |  |  |                             throw new CoolException("播种站点更新失败");
 | 
 |  |  |                         }
 | 
 |  |  |             //播种完成,释放绑定站点
 | 
 |  |  |             List<CacheSite> cacheSites = cacheSiteService.list(new LambdaQueryWrapper<CacheSite>()
 | 
 |  |  |                     .eq(!Objects.isNull(siteNo), CacheSite::getSiteNo, siteNo)
 | 
 |  |  |                     .eq(CacheSite::getOrderNo, orderNo));
 | 
 |  |  |             for (CacheSite cacheSite : cacheSites) {
 | 
 |  |  |                 if (!cacheSite.getSiteStatus().equals(CacheSiteStatusType.O.id)) {
 | 
 |  |  |                     cacheSite.setSiteStatus(CacheSiteStatusType.O.id);
 | 
 |  |  |                     cacheSite.setOrderId(null);
 | 
 |  |  |                     cacheSite.setBarcode(null);
 | 
 |  |  |                     cacheSite.setOrderNo(null);
 | 
 |  |  |                     cacheSite.setPlatformId(null);
 | 
 |  |  |                     cacheSite.setPlatformNo(null);
 | 
 |  |  |                     cacheSite.setUpdateTime(new Date());
 | 
 |  |  |                     if (!cacheSiteService.updateById(cacheSite)) {
 | 
 |  |  |                         throw new CoolException("播种站点更新失败");
 | 
 |  |  |                     }
 | 
 |  |  |                 }
 | 
 |  |  |             }
 | 
 |  |  |             converyor = true;
 | 
 |  |  |             params.add("params", JSONObject.toJSONString(slapParam));
 | 
 |  |  |             log.info("完成灭灯请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
 | 
 |  |  |         }
 | 
 |  |  | 
 | 
 |  |  |         if (converyor) {
 | 
 |  |  |             Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskNo, request.get("taskNo")));
 | 
 |  |  |             if (Objects.isNull(task)) {
 | 
 |  |  |                 throw new CoolException("任务不存在!!");
 | 
 |  |  |             }
 | 
 |  |  |         Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskNo, request.get("taskNo")));
 | 
 |  |  |         if (Objects.isNull(task)) {
 | 
 |  |  |             throw new CoolException("任务不存在!!");
 | 
 |  |  |         }
 | 
 |  |  |         try {
 | 
 |  |  |             containerConveryor(task.getBarcode());
 | 
 |  |  |         } catch (Exception e) {
 | 
 |  |  |             log.error("执行异常:UNK",  e);
 | 
 |  |  |             throw new CoolException(e.getMessage());
 | 
 |  |  |         }
 | 
 |  |  | 
 | 
 |  |  | //        HttpHeaders headers = new HttpHeaders();
 | 
 |  |  | //        headers.add("Content-Type", "application/json");
 | 
 |  |  | //        HttpEntity httpEntity = new HttpEntity<>(params, headers);
 | 
 |  |  | //        // 请求
 | 
 |  |  | //        ResponseEntity<CommonReponse> exchange = restTemplate.exchange(SystemProperties.CONVEYOR_START, HttpMethod.POST, httpEntity, CommonReponse.class);
 | 
 |  |  | //        log.info("下发流动通知 返回结果:{}", exchange);
 | 
 |  |  | //        if (exchange.getBody() == null) {
 | 
 |  |  | //            throw new CoolException("下发流动通知失败!!");
 | 
 |  |  | //        } else {
 | 
 |  |  | //            CommonReponse response = exchange.getBody();
 | 
 |  |  | //            if (response.getCode() == 0) {
 | 
 |  |  | //                if (!converyor) {
 | 
 |  |  | //                    //* 2. 容器流动判断,如果料箱没有物料 code传200, 有物料传100 执行回库任务,修改任务状态为  调用containerConveryor(taskNo)方法
 | 
 |  |  | //                    containerConveryor(request.get("taskNo").toString());
 | 
 |  |  | //                }
 | 
 |  |  | //            }
 | 
 |  |  | //        }
 | 
 |  |  |         return R.ok();
 | 
 |  |  |     }
 | 
 |  |  | 
 | 
 |  |  |     /**
 |