skyouc
6 天以前 e046dba174365eb8934ee1e4206f09821145e876
zy-asrs-wms/src/main/java/com/zy/asrs/wms/apis/wcs/schedule/ScheduleJobs.java
@@ -15,10 +15,13 @@
import com.zy.asrs.wms.apis.wcs.entity.request.TaskDescribe;
import com.zy.asrs.wms.apis.wcs.entity.request.TaskParam;
import com.zy.asrs.wms.apis.wcs.entity.response.CommonReponse;
import com.zy.asrs.wms.asrs.entity.Task;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.dto.OrderOutMergeDto;
import com.zy.asrs.wms.asrs.entity.enums.LocStsType;
import com.zy.asrs.wms.asrs.entity.enums.TaskStsType;
import com.zy.asrs.wms.asrs.service.TaskService;
import com.zy.asrs.wms.asrs.service.WorkService;
import com.zy.asrs.wms.asrs.service.*;
import com.zy.asrs.wms.asrs.service.impl.LocServiceImpl;
import com.zy.asrs.wms.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
@@ -33,6 +36,7 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@@ -48,7 +52,22 @@
    private RestTemplate restTemplate;
    @Autowired
    private WorkService workService;
    @Resource
    private SystemProperties properties;
    @Autowired
    private LocServiceImpl locService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private TaskDetlService taskDetlService;
    @Autowired
    private LocDetlFieldService locDetlFieldService;
    @Autowired
    private TaskDetlFieldService taskDetlFieldService;
    @Autowired
    private CacheSiteService cacheSiteService;
    @Autowired
    private OperationPortService operationPortService;
    /***
     * 入库任务---通知ESS输送线流动
@@ -57,17 +76,19 @@
    @Transactional(rollbackFor = Exception.class)
    public void conveyorStart() {
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getTaskSts, TaskStsType.WCS_CONTAINER_RECEIVE.id).eq(Task::getStatus, 1));
                .eq(Task::getTaskSts, TaskStsType.WCS_CONTAINER_RECEIVE.id)
                .eq(Task::getStatus, 1));
        tasks.forEach(task -> {
            try {
                ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
                conveyorStarParam.setSlotCode(task.getOriginLoc())
                conveyorStarParam.setSlotCode(task.getOriginSite())
                        .setContainerCode(task.getBarcode())
                        .setDirection("100");
                // 原始报文
                log.info("请求地址:{},请求参数:{}", "http://192.168.2.200:9046/conveyor/moveContainer", JSONObject.toJSONString(conveyorStarParam));
                log.info("通知ESS输送线流动请求地址:{},请求参数:{}", "http://192.168.2.200:9046/conveyor/moveContainer", JSONObject.toJSONString(conveyorStarParam));
                HttpHeaders headers = new HttpHeaders();
                headers.add("Content-Type", "application/json");
                headers.add("api-version", "v2.0");
                HttpEntity httpEntity = new HttpEntity<>(conveyorStarParam, headers);
                // 调用三方接口,将任务推送至ESS平台
                ResponseEntity<String> exchange = restTemplate.exchange("http://192.168.2.200:9046/conveyor/moveContainer", HttpMethod.POST, httpEntity, String.class);
@@ -83,21 +104,88 @@
                        taskService.update(new LambdaUpdateWrapper<Task>()
                                .eq(Task::getId, task.getId())
                                .set(Task::getTaskSts, TaskStsType.WCS_CONVEYOR_START.id));
                        log.info(task.getTaskNo() + "下发流动通知" + commonReponse.getMsg());
                    } else {
                        throw new CoolException("下发流动通知失败!!");
                    }
                }
            } catch (Exception ex) {
                log.error(ex.getMessage());
            } finally {
                //如果异常修改禁用状态
                taskService.update(new LambdaUpdateWrapper<Task>().set(Task::getStatus, 0).eq(Task::getId, task.getId()));
            }
        });
    }
    /**
     * 生成自动盘点任务
     */
//    @Scheduled(cron = "0/50 * * * * ?")
//    @Transactional(rollbackFor = Exception.class)
    public void genCheckTask() {
        Loc locs = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocStsId, LocStsType.F.val()).orderByAsc(Loc::getLocNo).last("limit 1"));
        if (Objects.isNull(locs)) {
            return;
        }
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().select(Task::getId).eq(Task::getTaskType, 107L));
        if (!tasks.isEmpty() && tasks.size() >= 4) {
            //默认生成四个盘点任务,超过4个,停止生成盘点任务;
            return;
        }
        OperationPort operationPort = operationPortService.getOne(new LambdaQueryWrapper<OperationPort>().last("ORDER BY RAND() LIMIT 1;"));
        if (operationPort == null || Objects.isNull(operationPort)) {
            throw new CoolException("作业口不存在");
        }
        Task task = new Task();
        task.setTaskNo(workService.generateTaskNo(TaskStsType.GENERATE_OUT.id));
        task.setTaskSts(TaskStsType.GENERATE_OUT.id);
        task.setTaskType(107L); //默认107盘点任务,盘点任务没有,按103处理
        task.setIoPri(workService.generateIoPri(TaskStsType.GENERATE_OUT.id));
        task.setOriginLoc(locs.getLocNo());
        task.setTargetSite(operationPort.getFlag());
        task.setBarcode(locs.getBarcode());
        if (!taskService.save(task)) {
            throw new CoolException("任务生成失败!!");
        }
        List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, locs.getId()));
        if (locDetls.isEmpty()) {
            throw new CoolException("明细不存在!!");
        }
        for (LocDetl locDetl : locDetls) {
            TaskDetl taskDetl = new TaskDetl();
            taskDetl.sync(locDetl);
            taskDetl.setId(null);
            taskDetl.setTaskId(task.getId());
            taskDetl.setTaskNo(task.getTaskNo());
            taskDetl.setAnfme(locDetl.getAnfme());
            taskDetl.setStock(locDetl.getAnfme());
            taskDetl.setWaveId(null);
            taskDetl.setWaveNo(null);
            taskDetl.setOrderId(null);
            taskDetl.setOrderNo(null);
            if (!taskDetlService.save(taskDetl)) {
                throw new CoolException("保存工作档明细失败");
            }
            List<LocDetlField> locDetlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()));
            for (LocDetlField locDetlField : locDetlFields) {
                TaskDetlField taskDetlField = new TaskDetlField();
                taskDetlField.sync(locDetlField);
                taskDetlField.setId(null);
                taskDetlField.setDetlId(taskDetl.getId());
                if (!taskDetlFieldService.save(taskDetlField)) {
                    throw new CoolException("明细扩展生成失败");
                }
            }
        }
        if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getId, locs.getId())
                .set(Loc::getUpdateTime, new Date())
                .set(Loc::getLocStsId, LocStsType.R.val()))) {
            throw new CoolException("库位状态修改失败!!");
        }
    }
    /***
     * 入库任务---下发入库任务
@@ -136,9 +224,6 @@
            describe.setContainerCode(task.getBarcode())
                    .setToLocationCode(formatLocNo)
                    .setToStationCode(task.getTargetSite());
//            .setFromLocationCode("")
//            .setContainerFace("")
//            .setContainerType("").setStorageTag("");
            param.setTaskDescribe(describe);
            params.add(param);
@@ -147,13 +232,10 @@
        tasksParam.setTaskType("putaway");
        // TODO 多任务多订单,统一调度,是否会出现部分成功,部分失败的情况
        //调用三方接口,将任务推送至ESS平台
//        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        // 设置请求参数
//        params.add("params", tasksParam);
        log.info("请求地址:{},请求参数:{}", "http://192.168.2.200:9046/task/create", JSONObject.toJSONString(tasksParam));
        log.info("下发入库任务请求地址:{},请求参数:{}", "http://192.168.2.200:9046/task/create", JSONObject.toJSONString(tasksParam));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("api-version", "v2.0");
        HttpEntity httpEntity = new HttpEntity<>(tasksParam, headers);
        // 请求
        ResponseEntity<String> exchange = restTemplate.exchange("http://192.168.2.200:9046/task/create", HttpMethod.POST, httpEntity, String.class);
@@ -163,7 +245,6 @@
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            CommonReponse reponse = objectMapper.readValue(exchange.getBody(), CommonReponse.class);
//            CommonReponse reponse = (CommonReponse) JSON.parse(exchange.getBody());
            if (reponse.getCode() == 0) {
                //  请求成功后,统一修改所有任务档状态为入库执行中。
                tasks.forEach(task -> {
@@ -217,7 +298,7 @@
//                } else {
//                    CommonReponse commonReponse = JSON.toJavaObject(JSON.parseObject(exchange.getBody()), CommonReponse.class);
//                    if (commonReponse.getCode() == 0) {
                        //流动通知下发完成后,修改任务状态为输送线流动中。。
            //流动通知下发完成后,修改任务状态为输送线流动中。。
//                        taskService.update(new LambdaUpdateWrapper<Task>()
//                                .eq(Task::getId, task.getId())
//                                .set(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
@@ -250,19 +331,19 @@
    public void waveToTask() throws JsonProcessingException {
        //获取当前任务档中,所有为待出库状态的任务档,按时间升序排列
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                        .ge(Task::getTaskType, 101) //TODO 如何确认是101,还是103
                        .eq(Task::getTaskSts, TaskStsType.GENERATE_OUT.id));
                .ge(Task::getTaskType, 101) //TODO 如何确认是101,还是103
                .eq(Task::getTaskSts, TaskStsType.GENERATE_OUT.id));
        if (tasks.isEmpty()) {
            return;
        }
        tasks.stream().sorted(Comparator.comparing(Task::getTaskSts))
                .collect(Collectors.toList());
//        tasks.stream().sorted(Comparator.comparing(Task::getTaskSts))
//                .collect(Collectors.toList());
        // 数据组装
        PublishTasksParam tasksParam = new PublishTasksParam();
        List<TaskParam> params = new ArrayList<>();
        //TODO 确认是否需要单任务多容器码的需求,目前系统都是单容器码生成单任务,多任务明细(物料混装)
        tasks.forEach(task -> {
            List<TaskParam> params = new ArrayList<>();
            TaskParam param = new TaskParam();
            //设置容器编码
            param.setTaskCode(task.getTaskNo());
@@ -273,19 +354,16 @@
                    .setToStationCode(task.getTargetSite());
            param.setTaskDescribe(describe);
            params.add(param);
            tasksParam.setTasks(params);
        });
        tasksParam.setTasks(params);
        tasksParam.setTaskType("carry");
        // TODO 多任务多订单,统一调度,是否会出现部分成功,部分失败的情况
        SystemProperties properties = new SystemProperties();
        log.info("请求地址:{},请求参数:{}", properties.getBaseHost() + properties.getEssReceiveTask(), JSONObject.toJSONString(tasksParam));
        log.info("出库任务下发请求地址:{},请求参数:{}", properties.getBaseHost() + properties.getEssReceiveTask(), JSONObject.toJSONString(tasksParam));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(tasksParam, headers);
        // 请求
        ResponseEntity<String> exchange = restTemplate.exchange(properties.getBaseHost() + properties.getEssReceiveTask(), HttpMethod.POST, httpEntity, String.class);
        log.info("下发任务 返回结果:{}", exchange);
        log.info("出库任务下发 返回结果:{}", exchange);
        if (exchange.getBody() == null) {
            throw new CoolException("下发任务失败!!");
        } else {