package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesMaximoService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesMouldMappingCfg;
import cn.estsh.i3plus.pojo.mes.repository.IMesMouldMappingCfgRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesProdMouldRecordRepository;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description :Maximo 仪表读数汇总
 * @Reference :
 * @Author : Castle
 * @CreateDate : 2024/6/13 17:55
 * @Modify:
 **/
@Service
@Slf4j
public class MesMaximoServiceImpl implements IMesMaximoService {
    @Autowired
    private MesProdMouldRecordRepository prodMouldRecordRao;
    @Autowired
    private IMesMouldMappingCfgRepository mesMouldMappingCfgRao;

    @Override
    public void doMeterReading(String organizeCode,String url) {
        //1.查询表磨具映射关系维护表，查询出当前工厂需要统计的模具信息

        //1.按天、按照meter_name 和 asset_num 汇总
        DdlPackBean modulePackBean = DdlPackBean.getDdlPackBean(organizeCode);
        List<MesMouldMappingCfg> mappingCfgList = mesMouldMappingCfgRao.findByHqlWhere(modulePackBean);
        if (mappingCfgList.isEmpty()){
            log.info("当前工厂:{} 模具映射关系没有数据",organizeCode);
            return;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date date = calendar.getTime();
        String createDateTimeStart = TimeTool.getToday(date) + MesPcnExtConstWords.APPEND_ZERO_HMS;
        String createDateTimeEnd = TimeTool.getToday(date) + MesPcnExtConstWords.APPEND_24_HMS;
        for (MesMouldMappingCfg cfg : mappingCfgList) {
            //循环模具映射关系维护
            DdlPackBean recordPackBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.getStringEqualPack(cfg.getMeterName(),"meterName",recordPackBean);
            DdlPreparedPack.getStringEqualPack(cfg.getAssetNum(),"assetNum",recordPackBean);
            DdlPreparedPack.timeBuilder(createDateTimeStart,createDateTimeEnd,"createDatetime",recordPackBean,true);
            int count = prodMouldRecordRao.findByHqlWhereCount(recordPackBean);
            call(cfg.getMeterName(),cfg.getAssetNum(),organizeCode,count,url);
        }
    }


    private void call(String meterName,String assetNum,String organizeCode,int count , String url){
        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:max=\"http://www.ibm.com/maximo\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <max:CreateCUR_METERREADING creationDateTime=\"EN\" baseLanguage=\"EN\">\n" +
                "         <max:CUR_METERREADINGSet>\n" +
                "            <max:METERDATA action=\"Change\">\n" +
                "               <max:MAXINTERRORMSG>?</max:MAXINTERRORMSG>\n" +
                "               <max:ASSETNUM changed=\"?\">%s</max:ASSETNUM>\n" +
                "               <max:METERNAME changed=\"?\">%s</max:METERNAME>\n" +
                "               <max:NEWREADING changed=\"?\">%s</max:NEWREADING>\n" +
                "               <max:NEWREADINGDATE changed=\"?\">%s</max:NEWREADINGDATE>\n" +
                "               <max:SITEID changed=\"?\">%s</max:SITEID>\n" +
                "            </max:METERDATA>\n" +
                "         </max:CUR_METERREADINGSet>\n" +
                "      </max:CreateCUR_METERREADING>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        //2024-05-30T11:10:00
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter isoDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        String today = now.format(isoDateTime);
        String xmlString = String.format(xml, assetNum, meterName, count, today, organizeCode);
        log.info("当前工厂:{},报文:{}",organizeCode,xmlString);
        HttpRequest.post(url).header("Content-Type","text/xml").body(xmlString).execute().body();

    }
}
