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.hutool.core.date.DateBetween;
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 java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 任务执行类 协查函 结果下载
 * @author chen mh
 * @date 2023/8/14 10:07
 */
@Component
@Slf4j
public class ScheduleXchJgxz {
    String region = "INTRA";
    String requestUrl = "https://api-one.digitalhainan.com.cn/apione";
    String mediaType="application/json";
    Map<String, String> queryParamsMap=new HashMap<>();

    /**
     * 协查函 定时执行接口
     */
    @Scheduled(cron = "0 0 1 * * ?")
    //@Scheduled(initialDelay = 5000,fixedDelay = 60*60*1000)
    public void Xch(){

        log.info("执行协查函及结果下载任务--{}", DateUtil.date());

        //换成自己的ak、sk
        String ak = "AK3aa44d2fc93e44bfb1089a9693a9091f";
        String sk = "SK608e9dd1360649658868199bda4611e8";
        String api = "DZZW_DSB_JDJK_QQCS_DSSJ";

        String contentJson = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server.unimas.com/\"> <soapenv:Header/> <soapenv:Body> <ser:query> <in>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; &lt;search&gt; &lt;appname&gt;query193&lt;/appname&gt; &lt;limited&gt;100&lt;/limited&gt; &lt;conditions&gt; &lt;s1f1c1&gt;" +
                DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+"&lt;/s1f1c1&gt; &lt;/conditions&gt; &lt;/search&gt;</in> </ser:query> </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();

        String date = DateUtil.format(DateUtil.yesterday(), "YYYYMMdd");
        JSONArray handle = ResultHandle.handle(response);

        if(handle == null){
            log.info(date+"日没有查询数据-协查函");
            return;
        }

        //疑似涉私寄件快递信息
        String ak_jj_xch="AKeabc1d2788944fc9a408ab5be631d0e8";
        String sk_jj_xch="SK42b1168ee774477e952ca52c795cc149";
        String apiName="gjyzj_ysssjjkdxx_xch";

        //疑似涉私收件快递信息
        String ak_sj_xch="AKc7ab48f6470444fa83f55a56c407e2be";
        String sk_sj_xch="SK60d3df8fe75f4ccf938da7fc22179eec";
        String apiName1="gjyzj_yssssjkdxx_xch";

        //异常寄件地点快递信息
        String ak_ycjj_xch="AKd66646feecea4c93adc372e3e8a3ce8a";
        String sk_ycjj_xch="SK5fde11c5510b43f6b5b3a5964349a1d9";
        String apiName3="gjyzj_ycjjddkdxx_xch";

        String ak_ycjjr_xch="AK445608f59ebb446ca51d3d4660c5f72f";
        String sk_ycjjr_xch="SK0b0d76a38f5349818d480f0310ba9346";
        String apiName4="gjyzj_ycjjrxwfxxx_xch";

        String ak_ycsj_xch="AK7b13190e791741c0a9bf518ee273a638";
        String sk_ycsj_xch="SK66e3e572fd48483c862ac00345d3662c";
        String apiName5="gjyzj_ycsjddkdxx_xch";

        String ak_ycsjr_xch="AK126a4113f3424a6a9b7e8b548da39cb1";
        String sk_ycsjr_xch="SK1da6383d3cb746818f70bd469496f576";
        String apiName6="gjyzj_ycsjrxwfxxx_xch";


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

        handle.stream().forEach(array-> {

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

            param.put("operateOrg",object.getString("JDJK_SEND.OPERATEORG"));
            param.put("operator",object.getString("JDJK_SEND.OPERATOR"));
            param.put("photoName",object.getString("JDJK_SEND.PHOTONAME"));
            param.put("photo",object.getString("JDJK_SEND.PHOTO"));

            //疑似涉私寄件快递信息_协查函
            String call1 = ApioneUtils.call(param.toJSONString(),ak_jj_xch, sk_jj_xch, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

            //疑似涉私收件快递信息_协查函
            String call2 = ApioneUtils.call(param.toJSONString(),ak_sj_xch, sk_sj_xch, apiName1, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

            //异常寄件地点快递信息_协查函
            String call3 = ApioneUtils.call(param.toJSONString(),ak_ycjj_xch, sk_ycjj_xch, apiName3, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

            //异常寄件人行为分析信息_协查函
            String call4 = ApioneUtils.call(param.toJSONString(),ak_ycjjr_xch, sk_ycjjr_xch, apiName4, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

            //异常收件地点快递信息_协查函
            String call5 = ApioneUtils.call(param.toJSONString(),ak_ycsj_xch, sk_ycsj_xch, apiName5, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);

            //异常收件人行为分析信息_协查函
            String call6 = ApioneUtils.call(param.toJSONString(),ak_ycsjr_xch, sk_ycsjr_xch, apiName6, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);


        });
    }


    /**
     * 下载结果接口定时运行
     */
    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(initialDelay = 5000,fixedDelay = 60*60*1000)
    public void Jgxz(){
        log.info("执行结果下载任务--{}", DateUtil.date());
        downloadYsssjj();
        downloadYssssj();
        downloadYcjjdd();
        downloadYcjjrxw();
        downloadYcsjdd();
        downloadYcsjrxw();
    }

    public void downloadYsssjj(){
        String ak_jj_jgxz="AKe8fbb21605e04a3f88d78e1110f63c28";
        String sk_jj_jgxz="SKd7fd481703b64387bab49ff2368d3d5d";
        String apiName="gjyzj_ysssjjkdxx_jgxz";

        Map<String, String> headerParamsMap = getHeaderParamsMap();

        com.alibaba.fastjson.JSONObject param = getParam();

        InputStream call = ApioneUtils.callByte(param.toJSONString(),ak_jj_jgxz, sk_jj_jgxz, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);
        String fileName = "疑似涉私寄件快递信息"+DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+".xlsx" ;

        writeFile(fileName,call);
    }

    public void downloadYssssj(){
        String ak_sj_jgxz="AK968863e9f3094e9a91fab5b965910fa2";
        String sk_sj_jgxz="SK5548f604797e4651a04941cfbf1704fa";
        String apiName="gjyzj_yssssjkdxx_jgxz";

        Map<String, String> headerParamsMap = getHeaderParamsMap();

        com.alibaba.fastjson.JSONObject param = getParam();

        InputStream call = ApioneUtils.callByte(param.toJSONString(),ak_sj_jgxz, sk_sj_jgxz, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);
        String fileName = "疑似涉私收件快递信息"+DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+".xlsx" ;

        writeFile(fileName,call);
    }

    public void downloadYcjjdd(){
        //异常寄件地址
        String ak_ycjj_jgxz="AKc577515132804d919c54c56026794cfe";
        String sk_ycjj_jgxz="SKed3d3d3240f142bf87bf285d733d7a12";
        String apiName="gjyzj_ycjjddkdxx_jgxz";

        Map<String, String> headerParamsMap = getHeaderParamsMap();

        com.alibaba.fastjson.JSONObject param = getParam();

        InputStream call = ApioneUtils.callByte(param.toJSONString(),ak_ycjj_jgxz, sk_ycjj_jgxz, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);
        String fileName = "异常寄件地点分析信息"+DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+".xlsx" ;

        writeFile(fileName,call);
    }

    public void downloadYcjjrxw(){
        //异常寄件人行为分析信息
        String ak_ycjjr_jgxz="AK88de3e809c1842488a142601f14b707a";
        String sk_ycjjr_jgxz="SKb10fdf3ed87f4f8c9f690066c3685643";
        String apiName="gjyzj_ycjjrxwfxxx_jgxz";

        Map<String, String> headerParamsMap = getHeaderParamsMap();

        com.alibaba.fastjson.JSONObject param = getParam();

        InputStream call = ApioneUtils.callByte(param.toJSONString(),ak_ycjjr_jgxz, sk_ycjjr_jgxz, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);
        String fileName = "异常寄件人行为分析信息"+DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+".xlsx" ;

        writeFile(fileName,call);
    }

    public void downloadYcsjdd(){
        //异常收件人行为分析信息
        String ak_ycsjr_jgxz="AKb4466349a8974dc9ae6f0bd68a99dc1b";
        String sk_ycsjr_jgxz="SK383393c73d0b4ad89b55db664e7d8449";
        String apiName="gjyzj_ycsjrxwfxxx_jgxz";

        Map<String, String> headerParamsMap = getHeaderParamsMap();

        com.alibaba.fastjson.JSONObject param = getParam();

        InputStream call = ApioneUtils.callByte(param.toJSONString(),ak_ycsjr_jgxz, sk_ycsjr_jgxz, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);
        String fileName = "异常收件地点分析信息"+DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+".xlsx" ;

        writeFile(fileName,call);
    }

    public void downloadYcsjrxw(){
        //异常寄件人行为分析信息
        String ak_ycjjr_jgxz="AK88de3e809c1842488a142601f14b707a";
        String sk_ycjjr_jgxz="SKb10fdf3ed87f4f8c9f690066c3685643";
        String apiName="gjyzj_ycjjrxwfxxx_jgxz";

        Map<String, String> headerParamsMap = getHeaderParamsMap();

        com.alibaba.fastjson.JSONObject param = getParam();

        InputStream call = ApioneUtils.callByte(param.toJSONString(),ak_ycjjr_jgxz, sk_ycjjr_jgxz, apiName, region, requestUrl, mediaType, "", headerParamsMap, queryParamsMap);
        String fileName = "异常收件人行为分析信息"+DateUtil.format(DateUtil.yesterday(), "YYYYMMdd")+".xlsx" ;

        writeFile(fileName,call);
    }

    public static void writeFile(String fileName,InputStream inputStream){
        String path = "file"+"/"+fileName;
        log.info("生成文件--{}",path);
        File outputFile = new File(path);
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(outputFile);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

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

    public  com.alibaba.fastjson.JSONObject getParam(){
        com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
        param.put("operateOrg","海南省打私办");
        param.put("operator","王鹏");
        return param;
    }

}
