package com.zy.asrs.task; 
 | 
  
 | 
import com.alibaba.fastjson.JSON; 
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.baomidou.mybatisplus.mapper.EntityWrapper; 
 | 
import com.baomidou.mybatisplus.plugins.Page; 
 | 
import com.core.common.Cools; 
 | 
import com.core.common.DateUtils; 
 | 
import com.core.exception.CoolException; 
 | 
import com.zy.asrs.domain.enums.TaskStatusType; 
 | 
import com.zy.asrs.domain.enums.WorkNoType; 
 | 
import com.zy.asrs.entity.LocMast; 
 | 
import com.zy.asrs.entity.TaskWrk; 
 | 
import com.zy.asrs.entity.TaskWrkReport; 
 | 
import com.zy.asrs.entity.TaskWrkReportLog; 
 | 
import com.zy.asrs.entity.param.TaskCreateParam; 
 | 
import com.zy.asrs.entity.wms.WmsFeedback; 
 | 
import com.zy.asrs.mapper.TaskWrkMapper; 
 | 
import com.zy.asrs.service.*; 
 | 
import com.zy.common.service.CommonService; 
 | 
import com.zy.common.utils.HttpHandler; 
 | 
import com.zy.common.utils.Synchro; 
 | 
import com.zy.core.DevpThread; 
 | 
import com.zy.core.cache.MessageQueue; 
 | 
import com.zy.core.cache.SlaveConnection; 
 | 
import com.zy.core.enums.SlaveType; 
 | 
import com.zy.core.model.Task; 
 | 
import com.zy.core.model.protocol.StaProtocol; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.beans.factory.annotation.Value; 
 | 
import org.springframework.scheduling.annotation.Scheduled; 
 | 
import org.springframework.stereotype.Component; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
  
 | 
import javax.annotation.Resource; 
 | 
import java.io.IOException; 
 | 
import java.util.ArrayList; 
 | 
import java.util.Date; 
 | 
import java.util.List; 
 | 
  
 | 
/** 
 | 
 * 定时将任务(完成、取消)转成日志 
 | 
 */ 
 | 
@Slf4j 
 | 
@Component 
 | 
public class TaskReportScheduler { 
 | 
  
 | 
    @Autowired 
 | 
    private TaskWrkReportService taskWrkReportService; 
 | 
  
 | 
    @Autowired 
 | 
    private TaskWrkReportLogService taskWrkReportLogService; 
 | 
  
 | 
    @Autowired 
 | 
    private ApiLogService apiLogService; 
 | 
  
 | 
  
 | 
    @Value("${wms.url}") 
 | 
    private String wmsUrl; 
 | 
  
 | 
    @Value("${wms.taskExecCallback}") 
 | 
    private String TaskExecCallback; 
 | 
  
 | 
    @Value("${wms.completeDevpTask}") 
 | 
    private String completeDevpTask; 
 | 
  
 | 
    @Resource 
 | 
    private TaskWrkMapper taskWrkMapper; 
 | 
  
 | 
    @Resource 
 | 
    private LocMastService locMastService; 
 | 
  
 | 
    @Resource 
 | 
    private CommonService commonService; 
 | 
  
 | 
    @Resource 
 | 
    private OpenService openService; 
 | 
  
 | 
    // 监听外置输送线 
 | 
    public static final ArrayList<Integer> staNos = new ArrayList<Integer>() {{ 
 | 
        add(301);add(302);add(303);add(304);add(305);add(306);add(307);add(308); 
 | 
    }}; 
 | 
  
 | 
    // WMS分配入库库位异常上报 
 | 
    @Scheduled(cron = "0/5 * * * * ? ") 
 | 
    @Transactional 
 | 
    public synchronized void locationAssignmentExceptionEscalation() { 
 | 
        List<TaskWrk> taskWrkList = taskWrkMapper.selectList(new EntityWrapper<TaskWrk>().eq("wrk_sts", 5)); 
 | 
  
 | 
        for(TaskWrk taskWrk : taskWrkList) { 
 | 
            Date now = new Date(); 
 | 
            WmsFeedback wmsFeedback = new WmsFeedback(); 
 | 
            wmsFeedback.setTaskNo(taskWrk.getTaskNo()); 
 | 
            wmsFeedback.setIoType(taskWrk.getIoType()); 
 | 
            wmsFeedback.setBarcode(taskWrk.getBarcode()); 
 | 
            wmsFeedback.setStatus(-1); // 入库库位分配异常 
 | 
            wmsFeedback.setReportTime(DateUtils.convert(now)); 
 | 
  
 | 
            String response = ""; 
 | 
            boolean bool = false; 
 | 
            try { 
 | 
                log.info("入库wms分配库位有误上报wms={}", taskWrk); 
 | 
                response = new HttpHandler.Builder() 
 | 
                        // .setHeaders(headParam) 
 | 
                        .setUri(wmsUrl) 
 | 
                        .setPath(TaskExecCallback) 
 | 
                        .setJson(JSON.toJSONString(wmsFeedback)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                if (!Cools.isEmpty(response)) { 
 | 
                    JSONObject jsonObject = JSON.parseObject(response); 
 | 
                    if (Integer.parseInt(String.valueOf(jsonObject.get("code"))) == 200) { 
 | 
                        bool = true; 
 | 
                        taskWrk.setWrkSts(6); 
 | 
                        taskWrk.setModiTime(now); 
 | 
                        taskWrkMapper.updateById(taskWrk); 
 | 
                    } 
 | 
                } 
 | 
  
 | 
            } catch (Exception e) { 
 | 
                log.error("入库wms分配库位有误上报wms失败{},返回值={}", taskWrk, response); 
 | 
            } finally { 
 | 
                apiLogService.save("wms分配库位有误上报" 
 | 
                        , wmsUrl + TaskExecCallback 
 | 
                        , null 
 | 
                        , "127.0.0.1" 
 | 
                        , JSON.toJSONString(wmsFeedback) 
 | 
                        , response 
 | 
                        , bool 
 | 
                ); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @Scheduled(cron = "0/5 * * * * ? ") 
 | 
    public synchronized void execute() throws IOException { 
 | 
        Page<TaskWrkReport> taskWrkReportPage = taskWrkReportService.selectPage(new Page<>(0, 100), new EntityWrapper<TaskWrkReport>().orderBy("create_time", true)); 
 | 
        if (taskWrkReportPage.getRecords() != null) { 
 | 
            //新增一个过滤机制 
 | 
            for (TaskWrkReport taskWrk : taskWrkReportPage.getRecords()) { 
 | 
                WmsFeedback wmsFeedback = getWmsFeedback(taskWrk); 
 | 
                String response = ""; 
 | 
                boolean bool = false; 
 | 
                try { 
 | 
                    log.info("wcs手动完成任务上报wms={}", taskWrk); 
 | 
                    response = new HttpHandler.Builder() 
 | 
                            // .setHeaders(headParam) 
 | 
                            .setUri(wmsUrl) 
 | 
                            .setPath(TaskExecCallback) 
 | 
                            .setJson(JSON.toJSONString(wmsFeedback)) 
 | 
                            .build() 
 | 
                            .doPost(); 
 | 
                    if (!Cools.isEmpty(response)) { 
 | 
                        JSONObject jsonObject = JSON.parseObject(response); 
 | 
                        if (Integer.parseInt(String.valueOf(jsonObject.get("code"))) == 200) { 
 | 
                            TaskWrkReportLog taskWrkLog = new TaskWrkReportLog(); 
 | 
                            Synchro.Copy(taskWrk, taskWrkLog); 
 | 
                            taskWrkLog.setId(null); 
 | 
                            if (!taskWrkReportLogService.insert(taskWrkLog)) { 
 | 
                                throw new CoolException("转历史档失败" + taskWrkLog); 
 | 
                            } 
 | 
                            if (!taskWrkReportService.deleteById(taskWrk)) { 
 | 
                                throw new CoolException("任务档删除失败" + taskWrkLog); 
 | 
                            } 
 | 
                            bool = true; 
 | 
                        } 
 | 
                    } 
 | 
  
 | 
                } catch (Exception e) { 
 | 
                    log.error("wcs手动完成任务上报wms失败{},返回值={}", taskWrk, response); 
 | 
                } finally { 
 | 
                    apiLogService.save("任务状态上报" 
 | 
                            , wmsUrl + TaskExecCallback 
 | 
                            , null 
 | 
                            , "127.0.0.1" 
 | 
                            , JSON.toJSONString(wmsFeedback) 
 | 
                            , response 
 | 
                            , bool 
 | 
                    ); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
    // 立体库跑库程序,生产勿用 
 | 
//    @Scheduled(cron = "0/5 * * * * ? ") 
 | 
    public synchronized void autoMoveLoc() { 
 | 
        int flag = 1; // 1.跑23号堆垛机前7列,1号堆垛机1,9层 2.跑23号堆垛机后8-12列,1号堆垛机2-8层 
 | 
        for(int i = 1; i <= 3; i++ ){ 
 | 
            // 获取堆垛机对应任务 
 | 
            Integer count = taskWrkMapper.selectCount(new EntityWrapper<TaskWrk>().eq("crn_no", i)); 
 | 
            if (count == 0) { 
 | 
                // 获取堆垛机巷道在库库位 
 | 
                LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_sts", "F").eq("crn_no", i)); 
 | 
                if (locMast != null) { 
 | 
                    // 50次出一次库 
 | 
                    int count1 = locMastService.selectCount(new EntityWrapper<LocMast>().eq("crn_no", i).eq("status", 1)); 
 | 
                    if (count1 % 25 == 0) { 
 | 
                        TaskCreateParam taskCreateParam = new TaskCreateParam(); 
 | 
                        taskCreateParam.setTaskNo(locMast.getLocNo()+"0"); 
 | 
                        taskCreateParam.setStartPoint(locMast.getLocNo()); 
 | 
                        taskCreateParam.setIoType(2); 
 | 
                        taskCreateParam.setTaskPriority(11); 
 | 
                        taskCreateParam.setBarcode(locMast.getBarcode()); 
 | 
                        taskCreateParam.setTargetPoint(i == 1 ? "114" : i == 2 ? "111" : "105"); 
 | 
                        openService.taskCreate(taskCreateParam); 
 | 
                        return; 
 | 
                    } 
 | 
                    // 获取一个移动的目标库位 
 | 
                    LocMast locMast1; 
 | 
                    if (flag == 1) { 
 | 
                        if (i == 1) { 
 | 
                            locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("crn_no", i).eq("status", "0") 
 | 
                                    .eq("loc_sts","O").orderBy("lev1").orderBy("bay1").orderBy("row1")); 
 | 
                        } else if(i==2) { 
 | 
                            locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("crn_no", i).eq("status", "0") 
 | 
                                    .eq("loc_sts","O").orderBy("lev1").orderBy("bay1").orderBy("row1").gt("bay1",7)); 
 | 
                        }else { 
 | 
                            locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("crn_no", i).eq("status", "0") 
 | 
                                    .eq("loc_sts","O").orderBy("lev1").orderBy("bay1").orderBy("row1").gt("bay1",7)); 
 | 
                        } 
 | 
  
 | 
                    } else { 
 | 
                        if (i == 1) { 
 | 
                            locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("crn_no", i).eq("status", "0") 
 | 
                                    .eq("loc_sts","O").orderBy("lev1").orderBy("bay1").orderBy("row1").in("lev1",2,3,4,5,6,7,8)); 
 | 
                        } else { 
 | 
                            locMast1 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("crn_no", i).eq("status", "0") 
 | 
                                    .eq("loc_sts","O").orderBy("lev1").orderBy("bay1").orderBy("row1").gt("bay1",7)); 
 | 
                        } 
 | 
  
 | 
                    } 
 | 
  
 | 
                    if (locMast1 != null) { 
 | 
                        // 创建移库任务 
 | 
                        TaskWrk taskWrk = new TaskWrk(); 
 | 
                        Date now = new Date(); 
 | 
                        taskWrk.setTaskNo(locMast1.getLocNo());//任务号 
 | 
                        taskWrk.setWrkNo(commonService.getWorkNo(WorkNoType.PAKIN.type)); 
 | 
                        taskWrk.setStatus(TaskStatusType.RECEIVE.id);//任务状态:接收 
 | 
                        taskWrk.setCreateTime(now); 
 | 
                        taskWrk.setAssignTime(now); 
 | 
                        taskWrk.setIoType(3);//任务类型 
 | 
                        taskWrk.setIoPri(11);//优先级 
 | 
                        taskWrk.setBarcode(locMast.getBarcode());//条码 
 | 
                        taskWrk.setCrnNo(locMast.getCrnNo()); 
 | 
                        taskWrk.setWrkSts(11); 
 | 
                        taskWrk.setStartPoint(locMast.getLocNo());//起点 
 | 
                        taskWrk.setTargetPoint(locMast1.getLocNo()); 
 | 
                        taskWrkMapper.insert(taskWrk); 
 | 
                        locMast1.setStatus("1"); 
 | 
                        locMastService.updateById(locMast1); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    // 监听外置输送线,有任务号和可出信号,上报成功清除任务号目标站 
 | 
    @Scheduled(cron = "0/3 * * * * ? ") 
 | 
    public synchronized void completeDevpTask() { 
 | 
        DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, 2); 
 | 
        if (devpThread != null) { 
 | 
            for(Integer site : staNos) { 
 | 
                StaProtocol staProtocol = devpThread.getStation().get(site); 
 | 
                if (staProtocol == null) { 
 | 
                    continue; 
 | 
                } else { 
 | 
                    staProtocol = staProtocol.clone(); 
 | 
                } 
 | 
                if (staProtocol.isOutEnable() && staProtocol.getWorkNo() != 0 && staProtocol.isAutoing() && staProtocol.isLoading()) { 
 | 
                    String response = ""; 
 | 
                    boolean bool = false; 
 | 
                    JSONObject request = new JSONObject(); 
 | 
                    request.put("site",site); 
 | 
                    try { 
 | 
                        log.info("外置输送线流动到位上报wms={}", site); 
 | 
                        response = new HttpHandler.Builder() 
 | 
                                .setUri(wmsUrl) 
 | 
                                .setPath(completeDevpTask) 
 | 
                                .setJson(request.toJSONString()) 
 | 
                                .build() 
 | 
                                .doPost(); 
 | 
                        if (!Cools.isEmpty(response)) { 
 | 
                            JSONObject jsonObject = JSON.parseObject(response); 
 | 
                            if (Integer.parseInt(String.valueOf(jsonObject.get("code"))) == 200) { 
 | 
                                bool = true; 
 | 
                                staProtocol.setWorkNo((short) 0); 
 | 
                                staProtocol.setStaNo((short) 0); 
 | 
                                boolean result = MessageQueue.offer(SlaveType.Devp, 2, new Task(2, staProtocol)); 
 | 
                                if (result) { 
 | 
                                    log.info("外置输送线流动到位上报wms,清除输送线站点{}命令下发成功!",site); 
 | 
                                } else { 
 | 
                                    log.error("外置输送线流动到位上报wms,清除输送线站点{}命令下发失败!" ,site); 
 | 
                                } 
 | 
                            } 
 | 
                        } 
 | 
                    } catch (Exception e) { 
 | 
                        log.error("外置输送线流动到位上报wms异常{},返回值={}", request, response); 
 | 
                    } finally { 
 | 
                        apiLogService.save("外置输送线流动到位上报wms" 
 | 
                                , wmsUrl + completeDevpTask 
 | 
                                , null 
 | 
                                , "127.0.0.1" 
 | 
                                , request.toJSONString() 
 | 
                                , response 
 | 
                                , bool 
 | 
                        ); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    private WmsFeedback getWmsFeedback(TaskWrkReport taskWrk) { 
 | 
        WmsFeedback feedback = new WmsFeedback(); 
 | 
        feedback.setBarcode(taskWrk.getBarcode()); 
 | 
        feedback.setReportTime(DateUtils.convert(new Date())); 
 | 
        feedback.setIoType(taskWrk.getIoType()); 
 | 
        feedback.setTaskNo(taskWrk.getTaskNo()); 
 | 
        feedback.setStatus(taskWrk.getStatus()); 
 | 
        return feedback; 
 | 
    } 
 | 
} 
 |