package com.elephant.framework.galaxy.aroad.module.roadcondition.service.impl;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elephant.framework.galaxy.aroad.module.roadcondition.entity.RoadConditionEvent;
import com.elephant.framework.galaxy.aroad.module.roadcondition.service.GetRoadPlatformService;
import com.elephant.framework.galaxy.aroad.module.roadcondition.service.RoadConditionEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import java.text.SimpleDateFormat;
import java.util.Map;

/** 普通国省道路况接口 */
@Slf4j
@Service
public class GetRoadPlatformImpl implements GetRoadPlatformService {
  @Autowired private RoadConditionEventService roadConditionEventService;
  /** 定时任务间隔1分钟获取接口数据 */
  @Override
  @Scheduled(cron = "0 0/5 * * * ?")
  public void scheduledSaveRoadInfo() {
    log.info("执行定时任务获取路况平台路况事件时间: {}", DateTime.now());
    try {
      String endPoint = "http://103.239.155.114:17001/lkxxfw/RoadInfoService?wsdl";
      String nameSpace = "http://services.team.com/";
      org.apache.axis.client.Service service = new org.apache.axis.client.Service();
      javax.xml.rpc.Call call = service.createCall();
      call.setTargetEndpointAddress(new java.net.URL(endPoint).toString());
      call.setOperationName(new QName(nameSpace, "getLkInfo"));
      call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
      call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); // 设置返回类型
      String res = (String) call.invoke(new Object[] {"B"});
      Map mapTypes = JSON.parseObject(res);
      if(mapTypes!=null){
        roadConditionEventService.deleteUnit();
      }
      for (Object obj : mapTypes.keySet()) {
        JSONArray ojb = (JSONArray) mapTypes.get("ROADINFO");
        Object[] array = ojb.toArray();
        for (Object o : array) {
          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Integer rowNum = null;
          rowNum =
              roadConditionEventService.getRoadMaintainEventByRoadPlatform(
                  //                  ((JSONObject) o).get("infid").toString(),
                  ((JSONObject) o).get("inftypename").toString(),
                  ((JSONObject) o).get("inftitle").toString(),
                  ((JSONObject) o).get("infcontent").toString(),
                  ((JSONObject) o).get("infwrinter").toString(),
                  formatter.parse(((JSONObject) o).get("writerdate").toString()),
                  ((JSONObject) o).get("infchecker").toString(),
                  formatter.parse(((JSONObject) o).get("checkdate").toString()),
                   formatter.parse(((JSONObject)o).get("removedate").toString().equals("null")?"1900-01-01 00:00:00":((JSONObject) o).get("removedate").toString()),
//                  ((JSONObject) o).get("remark").toString(),
                  ((JSONObject) o).get("glno").toString(),
                  ((JSONObject) o).get("writerno").toString(),
                  ((JSONObject) o).get("loctx").toString(),
                  ((JSONObject) o).get("locty").toString(),
                  ((JSONObject) o).get("zhuanghao").toString(),
                  ((JSONObject) o).get("daolu").toString(),
                  // ((JSONObject) o).get("removereson").toString(),
                  //                  ((JSONObject) o).get("departname").toString(),
                  //                  ((JSONObject) o).get("removedepart").toString(),
                  //                  formatter.parse(((JSONObject) o).get("expdate").toString()),
                  //                  ((JSONObject) o).get("impact").toString(),
                  //                  ((JSONObject) o).get("zhfrom").toString(),
                  //                  ((JSONObject) o).get("zhto").toString(),
                  ((JSONObject) o).get("yccd").toString());
          if (rowNum > 0) {
            continue;
          } else {
            roadConditionEventService.save(
                RoadConditionEvent.builder()
                    .eventId(((JSONObject) o).get("infid").toString())
                    .eventType(((JSONObject) o).get("inftypename").toString())
                    .eventTitle(((JSONObject) o).get("inftitle").toString())
                    .eventContent(((JSONObject) o).get("infcontent").toString())
                    .startPerson(((JSONObject) o).get("infwrinter").toString())
                    .startTime(formatter.parse(((JSONObject) o).get("writerdate").toString()))
                    .checkPerson(((JSONObject) o).get("infchecker").toString())
                    .checkTime(formatter.parse(((JSONObject) o).get("checkdate").toString()))
                     .endTime(formatter.parse(((JSONObject)o).get("removedate").toString().equals("null")?"1900-01-01 00:00:00":((JSONObject) o).get("removedate").toString()))
                    .startBz(((JSONObject) o).get("remark").toString())
                    .roadCode(((JSONObject) o).get("glno").toString())
                    .startUnitNo(((JSONObject) o).get("writerno").toString())
                    .longitude(((JSONObject) o).get("loctx").toString())
                    .latitude(((JSONObject) o).get("locty").toString())
                    .stoem(((JSONObject) o).get("zhuanghao").toString())
                    .roadName(((JSONObject) o).get("daolu").toString())
                    .endReason(((JSONObject) o).get("removereson").toString().equals("null")?"":((JSONObject) o).get("removereson").toString())
                    .maintainUnit(((JSONObject) o).get("departname").toString())
                    .endUnit(((JSONObject) o).get("removedepart").toString().equals("null")?"":((JSONObject) o).get("removedepart").toString())
                    .preEndTime(formatter.parse(((JSONObject) o).get("expdate").toString()))
                    .influence(((JSONObject) o).get("impact").toString())
                    .startm(((JSONObject) o).get("zhfrom").toString())
                    .endm(((JSONObject) o).get("zhto").toString())
                    .carsLength(((JSONObject) o).get("yccd").toString())
                        .solution("")
                        .startUnit("")
                        .endUnitNo("")
                        .endBz("")
                        .endPerson("")
                    .build());
          }
        }
      }
      if(mapTypes!=null){
        roadConditionEventService.saveUnit();
      }
    } catch (Exception e) {
      log.info("调用路况平台 exception: {}", e);
    }
  }
}
