package com.youchain.modules.quartz.task;

import com.alibaba.fastjson.JSONObject;
import com.github.s7connector.api.S7Connector;
import com.youchain.businessdata.rest.MoController;
import com.youchain.businessdata.rest.PlcController;
import com.youchain.businessdata.service.MoService;
import com.youchain.businessdata.service.TaskService;
import com.youchain.exception.handler.ApiResult;
import com.youchain.modules.quartz.domain.QuartzLog;
import com.youchain.modules.quartz.repository.QuartzLogRepository;
import com.youchain.service.LogService;
import com.youchain.utils.S7ConnectorUtils;
import com.youchain.utils.UrlApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springframework.http.HttpStatus.BAD_REQUEST;


@Slf4j
@Service
public class plcTask {

    private final PlcController plcController;
    private final MoService moService;
    private final TaskService taskService;
    private final QuartzLogRepository quartzLogRepository;
    public LogService logService;
    @Autowired
    public plcTask(PlcController plcController,MoService moService,TaskService taskService,QuartzLogRepository quartzLogRepository) {
        this.plcController = plcController;
        this.moService = moService;
        this.taskService = taskService;
        this.quartzLogRepository = quartzLogRepository;
    }

    public synchronized void plcTask() {
        S7ConnectorUtils.connect();
        int a=0;
        JSONObject jsonObject=new JSONObject();
//        String mo="LB]A0006897]240625]12125300000145]200]240625104089]]";
//        String lx="05010010";
//        LB]A0006907]240522]12125300001741]50.00]240522554318]]
//        LB]A0006897]240625]1212504010001300000145]200]240625104089]]
        String mo = S7ConnectorUtils.readMo(S7ConnectorUtils.s7Connector,12);
        String lx = S7ConnectorUtils.readMo(S7ConnectorUtils.s7Connector,0);
        if (mo.length()>0&&lx.length()>0){
            jsonObject.put("mo",mo);
            jsonObject.put("lx",lx);
            try {
                JSONObject json = jsonObject;
                String mo1 = json.getString("mo");// MO票
                String lx1 = json.getString("lx");// 容器
                String[] mos = mo1.split("]");
                String it = mos[3];// 料号
                String labelNo = mos[5];// 标签
                // 验证标签是否已入库
                if (taskService.findByLabelNo(labelNo).size() > 0) {
                    a=1;
                }else {
                    moService.scanMo(it, lx1, labelNo);
                    S7ConnectorUtils.write(S7ConnectorUtils.s7Connector, 2);
                }
            } catch(Exception e){
                String ex="入库失败,MO票" + mo+",料箱码"+lx+",错误信息:"+e.getMessage();
                List<QuartzLog> quartzLogList=quartzLogRepository.findByJobName("访问PLC");
                if (quartzLogList.size()>0){
                    /* 重复异常*/
                    QuartzLog quartzLog=quartzLogList.get(0);
                    if (quartzLog.getJobName().equals("访问PLC")
                            &&quartzLog.getIsSuccess().equals(false)
                            &&quartzLog.getExceptionDetail().equals(ex)){
                        /* 重复异常*/
                    }else {
                        S7ConnectorUtils.write(S7ConnectorUtils.s7Connector, 1);
                    }
                }
                throw new RuntimeException(ex);
            }
            if (a==1){
                throw new RuntimeException("入库失败,MO票" + mo+",料箱码"+lx+",错误信息:任务重复");
            }
        }else {
            throw new RuntimeException("数据格式错误,MO票" + mo+",料箱码"+lx+",错误信息:");
        }
        /*try {
            S7ConnectorUtils.connect();
            JSONObject jsonObject=new JSONObject();
            String mo = S7ConnectorUtils.readMo(S7ConnectorUtils.s7Connector,12);
            String lx = S7ConnectorUtils.readMo(S7ConnectorUtils.s7Connector,0);
            if (mo.length()>0&&lx.length()>0){
                jsonObject.put("mo",mo);
                jsonObject.put("lx",lx);
                plcController.scanMo(jsonObject+"");
            }else {
                throw new RuntimeException("数据格式错误:"+mo+"|"+lx);
            }
        } catch (Exception e) {
            throw new RuntimeException("异常信息" + e.getMessage());
        }*/
    }
}
