package cn.com.taiji.dataService.task;

import cn.com.digitalhainan.apione.sdk.ContentBody;
import cn.com.digitalhainan.apione.sdk.HttpCaller;
import cn.com.digitalhainan.apione.sdk.HttpParameters;
import cn.com.digitalhainan.apione.sdk.HttpReturn;
import cn.com.taiji.dataService.utils.ApioneUtils;
import cn.com.taiji.dataService.utils.ResultHandle;
import cn.com.taiji.dataService.utils.StringProducer;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计任务接口
 *
 */
@Component
@Slf4j
public class ScheduleTJjk {


    @Resource
    private StringProducer producer;

    @Scheduled(cron = "0 20 3 * * ?")
    //@Scheduled(initialDelay = 5000,fixedDelay = 5*60*1000)
    public void task(){

        log.info("执行统计任务---{}",DateUtil.date());
        String region = "INTRA";
        String requestUrl = "https://api-one.digitalhainan.com.cn/apione";
        String mediaType="application/json";

        String ak_dsb = "AK50976835f38d41bc94349a7bce75fcc8";
        String sk_dsb = "SKc08527f892ac475f81e2f5ba440b283b";
        String api_dsb = "DZZW_DSB_JDJK_TJLQQCS_DSJJ";


        //国家邮政局_内件品名热门统计接口
        String ak = "AKefa11a3e59f1448a8e3da49af4c260d5";
        String sk = "SK6b6285acbcff49ee922fb3c27e90163f";
        String api = "gjyzj_njpmrmtjjk";

        //国家邮政局_市县业务量统计接口
        String ak1 = "AK06e53764285a45a7abe2493b5daf1604";
        String sk1 = "SK63a1a49fac9f4a969c049770be2d97db";
        String api1 = "gjyzj_sxywltjjk";

        //国家邮政局_收寄件热门城市接口
        String ak2 = "AK0bc6087e0e9a4ad597ed26d434c51ec4";
        String sk2 = "SKba3f5cf136e04073ae2fcd0f5da9d27f";
        String api2 = "gjyzj_sjjrmcsjk";

        Map<String, String> headerParamsMap=new HashMap<>();
        headerParamsMap.put("Authorization","Bearer 7bee4ad2-1d1f-36e0-b9ab-625a7e495de2");
        Map<String, String> queryParamsMap=new HashMap<>();

        String date = DateUtil.format(DateUtil.yesterday(), "YYYYMMdd");
        String response = DSBJK(ak_dsb, sk_dsb, api_dsb, "query207", "gjyzj_njpmrmtjjk", date);
        String response1 = DSBJK(ak_dsb, sk_dsb, api_dsb, "query207", "gjyzj_sxywltjjk", date);
        String response2 = DSBJK(ak_dsb, sk_dsb, api_dsb, "query207", "gjyzj_sjjrmcsjk", date);

        JSONArray handle = ResultHandle.handle(response);
        if(handle!=null){
            handle.stream().forEach(obj->{

                com.alibaba.fastjson.JSONObject object = JSON.parseObject(obj.toString());
                com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();

                param.put("date",DateUtil.format(DateUtil.parse(object.getString("JDJK_STATISTICS.DATE_TIME")),"YYYY-MM-dd"));
                param.put("top",object.getString("JDJK_STATISTICS.TOP"));
                param.put("tjType",object.getString("JDJK_STATISTICS.TJTYPE"));
                //国家邮政局_内件品名热门统计接口
                String call = ApioneUtils.call(param.toJSONString(),ak, sk, api, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

                NJPMRMTJDataResult njpmrmtjDataResult = JSON.parseObject(call, NJPMRMTJDataResult.class);
                if(njpmrmtjDataResult.getCode()==0){
                    RecSen1 data = njpmrmtjDataResult.getData().getData();
                    List<Rec1> rec = data.getRec();
                    for(Rec1 r:rec){
                        producer.sendMessage("gjyzj_njpmrmtjjk",JSON.toJSONString(r));
                    }
                    List<Rec1> sen = data.getSen();
                    for(Rec1 r:sen){
                        producer.sendMessage("gjyzj_njpmrmtjjk",JSON.toJSONString(r));
                    }
                }
            });
        }


        JSONArray handle1 = ResultHandle.handle(response1);
        if(handle1!=null){
            handle1.stream().forEach(obj->{

                com.alibaba.fastjson.JSONObject object = JSON.parseObject(obj.toString());
                com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
                param.put("date",DateUtil.format(DateUtil.parse(object.getString("JDJK_STATISTICS.DATE_TIME")),"YYYY-MM-dd"));
                //国家邮政局_市县业务量统计接口
                String call = ApioneUtils.call(param.toJSONString(),ak1, sk1, api1, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

                SXYWLTJDataResult sxywltjDataResult = JSON.parseObject(call, SXYWLTJDataResult.class);
                if(sxywltjDataResult.getCode()==0){
                    List<DataSub2> data1 = sxywltjDataResult.getData().getData();
                    for (DataSub2 sub2 : data1){
                        List<Info> infoList = sub2.getInfoList();
                        for (Info info : infoList){
                            producer.sendMessage("gjyzj_sxywltjjk",JSON.toJSONString(info));
                        }
                    }
                }
            });
        }

        JSONArray handle2 = ResultHandle.handle(response2);
        if(handle2!=null){
            handle2.stream().forEach(obj->{

                com.alibaba.fastjson.JSONObject object = JSON.parseObject(obj.toString());
                com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();

                param.put("date",DateUtil.format(DateUtil.parse(object.getString("JDJK_STATISTICS.DATE_TIME")),"YYYY-MM-dd"));
                param.put("top",object.getString("JDJK_STATISTICS.TOP"));
                //国家邮政局_收寄件热门城市接口
                String call = ApioneUtils.call(param.toJSONString(),ak2, sk2, api2, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

                SJJRMCSDataResult sjjrmcsDataResult = JSON.parseObject(call, SJJRMCSDataResult.class);
                if(sjjrmcsDataResult.getCode()==0){
                    RecSen data = sjjrmcsDataResult.getData().getData();
                    List<Rec> rec = data.getRec();
                    for(Rec r:rec){
                        producer.sendMessage("gjyzj_sjjrmcsjk",JSON.toJSONString(r));
                    }
                    List<Rec> sen = data.getSen();
                    for(Rec r:sen){
                        producer.sendMessage("gjyzj_sjjrmcsjk",JSON.toJSONString(r));
                    }
                }
            });
        }


    }


    /**
     * 打私办请求接口
     * @param ak
     * @param sk
     * @param api
     * @param queryID
     * @param tableName
     * @param date
     * @return 接口返回值
     */
    public String DSBJK(String ak,String sk,String api,String queryID,String tableName,String date){
        String region = "INTRA";
        String requestUrl = "https://api-one.digitalhainan.com.cn/apione";

        String contentJson = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server.unimas.com/\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ser:query>\n" +
                "         <in>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n" +
                "               &lt;search&gt;\n" +
                "               &lt;appname&gt;"+queryID+"&lt;/appname&gt;\n" +
                "               &lt;limited&gt;100&lt;/limited&gt;\n" +
                "               &lt;conditions&gt;\n" +
                "                  &lt;s1f1c2&gt;"+tableName+"&lt;/s1f1c2&gt;\n" +
                "                  &lt;s1f1c1&gt;"+date+"&lt;/s1f1c1&gt;\n" +
                "               &lt;/conditions&gt;\n" +
                "               &lt;/search&gt;</in>\n" +
                "      </ser:query>\n" +
                "   </soapenv:Body>";

        ContentBody contentBody = new ContentBody(contentJson);


        //拼装业务信息
        HttpParameters parameters = HttpParameters.builder()
                .api(api)
                .region(region)
                // 如果有query参数需要设置
                //.queryParamsMap(query)
                // 如果有path参数需要设置
                //.path(path)
                .accessKey(ak)
                .secretKey(sk)
                .contentBody(contentBody)
                .requestUrl(requestUrl)
                .build();

        // 请求服务接口，获取response
        HttpReturn call = HttpCaller.getInstance().call(parameters);
        String response = call.getResponse();

        return response;
    }
}
