package com.elephant.framework.galaxy.aroad.module.common.getInterfaceData;


import com.elephant.framework.galaxy.aroad.module.zhjs.entity.ZhjsGcht;
import com.elephant.framework.galaxy.aroad.module.zhjs.entity.ZhjsGcxm;
import com.elephant.framework.galaxy.aroad.module.zhjs.entity.ZhjsZqzfzsMx;
import com.elephant.framework.galaxy.aroad.module.zhjs.service.ZhjsGchtService;
import com.elephant.framework.galaxy.aroad.module.zhjs.service.ZhjsGcxmService;
import com.elephant.framework.galaxy.aroad.module.zhjs.entity.ZhjsZqzfzs;
import com.elephant.framework.galaxy.aroad.module.zhjs.service.ZhjsZqzfzsMxService;
import com.elephant.framework.galaxy.aroad.module.zhjs.service.ZhjsZqzfzsService;
import lombok.AllArgsConstructor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Iterator;


@Component
@Configuration
@EnableScheduling
@AllArgsConstructor
public class GetZhjsInterfaceData {

    HttpClient httpClient;
    private final ZhjsGcxmService zhjsGcxmService;
    private final ZhjsGchtService zhjsGchtService;
    private final ZhjsZqzfzsService zhjsZqzfzsService;
    private final ZhjsZqzfzsMxService zhjsZqzfzsMxService;

    @Scheduled(cron = "59 59 23 ? * *")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void scheduledSaveEvent() throws Exception{
        String token = zhjsGcxmService.getTokenData();
        String createDate = getLastTimestamp().toString();

//        String createDate = "0";

        addGcxmData(getXmData(token,createDate));
        addGcHtData(getHtData(token, createDate));
        addZqjlzfData(getZqjlzfData(token, createDate));
    }

    //获取昨晚0点时间戳
    public static Long getLastTimestamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)-1,0,0,0);
        long tt = calendar.getTime().getTime()/1000;
        return tt;
    }

    //获取计量支付信息
    public String getZqjlzfData(String token, String createDate){
        return httpClient.callOtherInterface("getMeteringPayListTestInterfaces.action",token,createDate);
    }

    //处理计量支付信息,添加到数据库
    public void addZqjlzfData(String jlzfxx) {
        if(jlzfxx==null ||  "".equals(jlzfxx)) {
            return;
        }
        String data = JSONObject.fromObject(jlzfxx).getString("data");
        JSONArray jsonArray = JSONArray.fromObject(data);
        JSONArray interimPaymentList = JSONArray.fromObject(JSONObject.fromObject(JSONArray.fromObject(jsonArray.get(0)).get(0)).getString("interimPaymentList"));
        for(int i=0; i<interimPaymentList.size(); i++) {
            ZhjsZqzfzs zhjsZqzfzs = (ZhjsZqzfzs)JSONObject.toBean(transObject(JSONObject.fromObject(interimPaymentList.get(i))), ZhjsZqzfzs.class);

            try{
                zhjsZqzfzsService.save(zhjsZqzfzs);
                System.out.println("ID为："+zhjsZqzfzs.getId()+"计量支付信息添加成功。");
            }catch (Exception e) {
                System.out.println("ID为："+zhjsZqzfzs.getId()+"计量支付信息添加失败！！");
            }
        }
        JSONArray interimPaymentItemList = JSONArray.fromObject(JSONObject.fromObject(JSONArray.fromObject(jsonArray.get(1)).get(0)).getString("interimPaymentItemList"));
        for(int i=0; i<interimPaymentItemList.size(); i++) {
            ZhjsZqzfzsMx zhjsZqzfzsMx = (ZhjsZqzfzsMx)JSONObject.toBean(transObject(JSONObject.fromObject(interimPaymentItemList.get(i))), ZhjsZqzfzsMx.class);

            try{
                zhjsZqzfzsMxService.save(zhjsZqzfzsMx);
                System.out.println("ID为："+zhjsZqzfzsMx.getId()+"计量支付详细信息添加成功。");
            }catch (Exception e) {
                System.out.println("ID为："+zhjsZqzfzsMx.getId()+"计量支付详细信息添加失败！！");
            }
        }
    }

    //获取项目信息
    public String getXmData(String token, String createDate) {
        return httpClient.callOtherInterface("getProListTestInterfaces.action",token,createDate);
    }

    //处理项目信息数据，添加到数据库
    public void addGcxmData(String xmxx) {
        JSONArray jsonArray = JSONArray.fromObject(JSONObject.fromObject(xmxx).getString("data"));

        for(int i=0; i<jsonArray.size(); i++) {
            ZhjsGcxm zhjsGcxm = (ZhjsGcxm)JSONObject.toBean(transObject(JSONObject.fromObject(jsonArray.get(i))), ZhjsGcxm.class);
            try {
                zhjsGcxmService.save(zhjsGcxm);
                System.out.println("ID 为："+zhjsGcxm.getId()+" 的项目信息添加成功。");
            }catch (Exception e) {
                System.out.println("ID 为："+zhjsGcxm.getId()+" 的项目信息添加失败！！");
            }
        }
    }

    //获取合同信息
    public String getHtData(String token, String createDate) {
        return httpClient.callOtherInterface("getConListTestInterfaces.action",token,createDate);
    }

    //处理合同信息，添加到数据库
    public void addGcHtData(String htxx) {
        JSONArray jsonArray = JSONArray.fromObject(JSONObject.fromObject(htxx).getString("data"));

        for(int i=0; i<jsonArray.size(); i++) {
            ZhjsGcht zhjsGcht = (ZhjsGcht)JSONObject.toBean(transObject(JSONObject.fromObject(jsonArray.get(i))), ZhjsGcht.class);
            try {
                zhjsGchtService.save(zhjsGcht);
                System.out.println("ID 为："+zhjsGcht.getId()+" 的合同信息添加成功。");
            }catch (Exception e) {
                System.out.println("ID 为："+zhjsGcht.getId()+" 的合同信息添加失败！！");
            }
        }
    }

    //JSONObject key值转小写
    public static JSONObject transObject(JSONObject o1){
        JSONObject o2=new JSONObject();
        Iterator it = o1.keys();
        while (it.hasNext()) {
            String key = (String) it.next();
            Object object = o1.get(key);

            if(key.toLowerCase().indexOf("_")>-1) {
                String keyLower = key.toLowerCase();
                String startKey = keyLower.substring(0, keyLower.indexOf("_"));
                String endKey = keyLower.substring(keyLower.indexOf("_")+1);
                String endKey2 = endKey.substring(0, 1).toUpperCase() + endKey.substring(1);
                o2.accumulate(startKey+endKey2, object);
            }else {
                o2.accumulate(key.toLowerCase(), object);
            }
        }
        if(o2.get("xy")!=null && !"".equals(o2.get("xy"))) {
            o2.put("xy", "'"+o2.get("xy").toString()+"'");
        }
        return o2;
    }
}
