package com.uas.eis.controller;

import com.uas.eis.sdk.entity.ApiResult;
import com.uas.eis.sdk.resp.ApiResponse;
import com.uas.eis.service.MESHelperService;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaost
 * @date 2022/8/16 11:35
 **/
@RestController
@RequestMapping("/MES/helper")
public class MESHelperController {
    @Autowired
    private MESHelperService mesHelperService;

    @RequestMapping(value="/dllMain")
    public ApiResult<Map<Object,Object>> DLLMain(HttpServletRequest request, @RequestBody String data){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String accessKey = request.getHeader("AccessKey");
        String requestId = request.getHeader("RequestId");
        Map<Object,Object> map = mesHelperService.DLLMain(accessKey,requestId,data);
        return ApiResponse.successRsp(requestId,map);
    }

    //抛料数据接口
    @RequestMapping(value="/StateEventDetail")
    public Map<Object,Object> StateEventDetail(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.StateEventDetail(json);
        return map;
    }

    //抛料数据接口
    @RequestMapping(value="/SendPlaceData")
    public Map<Object,Object> SendPlaceData(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.SendPlaceData(json);
        return map;
    }

    //物料移动接口
    @RequestMapping(value="/SetupResultDataInterface")
    public Map<Object,Object> SetupResultDataInterface(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.SetupResultDataInterface(json);
        return map;
    }


    //追溯信息接口
    @RequestMapping(value="/TraceabilityDataInterface")
    public Map<Object,Object> TraceabilityDataInterface(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.TraceabilityDataInterface(json);
        return map;
    }

    //追溯信息接口
    @RequestMapping(value="/XrayTestDataSave")
    public Map<Object,Object> XrayTestDataSave(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.XrayTestDataSave(json);
        return map;
    }


    //
    //天马电子秤对接接口
    @RequestMapping(value="/Weigh")
    public Map<Object,Object> Weigh(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.Weigh(json);
        return map;
    }

    //镭雕机接口
    @RequestMapping(value="/Radium")
    public Map<Object,Object> Radium(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.Radium(json);
        return map;
    }

    //SPI接口
    @RequestMapping(value="/SPI")
    public Map<Object,Object> SPI(HttpServletRequest request,  @RequestBody String json){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Map<Object,Object> map = mesHelperService.SPI(json);
        return map;
    }

    //AOI接口
    @RequestMapping(value="/AOI")
    public String AOI(HttpServletRequest request,@RequestBody String Data,String Username,String Shiftname, String MachineNO, String LineNO, String procedure,String Moname,
                                  String Lotsn,String Side,String Result,String Defect,String elementQTY,String TestQTY,String Type,String Time) {
        Data=Data+"1";
        Element root = DocumentHelper.createElement("Data");
        Document document = DocumentHelper.createDocument(root);
        String Err=mesHelperService.AOI( Username, Shiftname,  MachineNO,  LineNO,  procedure, Moname,
                Lotsn, Side, Result, Defect, elementQTY, TestQTY, Type, Time);
        Element element1 = root.addElement("Result");
        Element element2 = root.addElement("ErrMsg");

        if(Err.equals("")||Err==null){
            element1.setText("OK");
            element2.setText("");
        }else{
            element1.setText("NG");
            element2.setText(Err);
        }

        StringWriter stringWriter = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
        try {
            xmlWriter.write(document);
        } catch (IOException  e) {
            e.printStackTrace();
        }
        String xmlString = stringWriter.toString();
        return xmlString;
    }


    //更新刮刀数据
    @RequestMapping(value="/UploadSteel",method = RequestMethod.POST)
    public String UploadSteel(HttpServletRequest request,String SteelMeshCode,String scraperCode, String SteelMeshCount, String scraperCount, String datatime) {

        Element root = DocumentHelper.createElement("Data");
        Document document = DocumentHelper.createDocument(root);
        String Err=mesHelperService.UploadSteel( SteelMeshCode, scraperCode,  SteelMeshCount,  scraperCount,  datatime);
        Element element1 = root.addElement("Result");
        Element element2 = root.addElement("ErrMsg");

        if(Err.equals("")||Err==null){
            element1.setText("OK");
            element2.setText("");
        }else{
            element1.setText("NG");
            element2.setText(Err);
        }

        StringWriter stringWriter = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
        try {
            xmlWriter.write(document);
        } catch (IOException  e) {
            e.printStackTrace();
        }
        String xmlString = stringWriter.toString();
        return xmlString;
    }

    //校验钢网是否可以用
    @RequestMapping(value="/CheckSteel")
    public String CheckSteel(HttpServletRequest request, String SteelMeshCode) {

        Element root = DocumentHelper.createElement("Data");
        Document document = DocumentHelper.createDocument(root);
        String Err=mesHelperService.CheckSteel(SteelMeshCode);
        Element element1 = root.addElement("Result");
        Element element2 = root.addElement("ErrMsg");

        if(Err.equals("")||Err==null){
            element1.setText("OK");
            element2.setText("");
        }else{
            element1.setText("NG");
            element2.setText(Err);
        }

        StringWriter stringWriter = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
        try {
            xmlWriter.write(document);
        } catch (IOException  e) {
            e.printStackTrace();
        }
        String xmlString = stringWriter.toString();
        return xmlString;
    }

    /**
     * 获取MES生成的SN条码
     * @param request
     * @param json
     * @return
     */
    @RequestMapping(value="/getMESSnCode")
    public Map<Object,Object> GetMESSnCode(HttpServletRequest request,  @RequestBody String json){
        return mesHelperService.getMESSnCode(json);
    }

    /**
     * 获取MES生成的SN条码
     * @param request
     * @param json
     * @return
     */
    @RequestMapping(value="/getMakeSnCode")
    public Map<Object,Object> getMakeSnCode(HttpServletRequest request,  @RequestBody String json){
        return mesHelperService.getMakeSnCode(json);
    }

    /**
     * 获取MES生成的SN条码
     * @param request
     * @param json
     * @return
     */
    @RequestMapping(value="/getMainAllSnCode")
    public Map<Object,Object> getMainAllSnCode(HttpServletRequest request,  @RequestBody String json){
        return mesHelperService.getMainAllSnCode(json);
    }


    /**
     * 微浦MES点料机对接
     * @param request
     * @param json
     * @return
     */
    @RequestMapping(value="/countQTY")
    public Map<Object,Object> countQTY(HttpServletRequest request,  @RequestBody String json){
        return mesHelperService.countQTY(json);
    }
}
