package com.jianshui.invoice.task.yuande;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.jianshui.invoice.domain.yuande.CorpBody;
import com.jianshui.invoice.service.yuande.IInvoiceFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component("yuanDeInformPushShuYuTask")
@Slf4j
public class YuanDeInformPushShuYuTask {

    @Autowired
    private IInvoiceFormService invoiceFormService;

    /**
     * @Author 路明慧
     * @Description 将开票信息表一次性发送
     * @Date  2023/4/10
     **/
    public void infoPushAllMap(){
        //请求接口路径
        String url= "https://open.fxiaoke.com/cgi/corpAccessToken/get/V2";
        //请求参数封装
        JSONObject paramMap = new JSONObject();
        paramMap.put("appId","FSAID_131be20");
        paramMap.put("appSecret","78db04ad63c645609d0b05127fe73715");
        paramMap.put("permanentCode","C1A6DFC2DFDEC79CECC207D00658DBF3");
        //发送请求
        String respRaw = HttpUtil.createPost(url).body(paramMap.toString()).execute().body();
        //返回报文封装实体
        CorpBody corpBody = JSONUtil.toBean(respRaw, CorpBody.class);
        //获取要传输的报文信息
        JSONObject pushInfo = invoiceFormService.getPushInfo(corpBody);
        //调用对方接口完成信息推送
        String url1 = "https://open.fxiaoke.com/cgi/crm/custom/v2/data/create";
        HttpUtil.createPost(url1).body(pushInfo.toString()).execute().body();
    }

    /**
     * @Author 路明慧
     * @Description 将开票信息表每次一条记录
     * @Date  2023/4/10
     **/
    public void infoPush(){
        log.info("开始执行推送远得数据的定时任务");
        //请求接口路径
        String url1= "https://open.fxiaoke.com/cgi/corpAccessToken/get/V2";
        //请求参数封装
        JSONObject paramMap = new JSONObject();
        paramMap.put("appId","FSAID_131be20");
        paramMap.put("appSecret","78db04ad63c645609d0b05127fe73715");
        paramMap.put("permanentCode","C1A6DFC2DFDEC79CECC207D00658DBF3");
        //发送请求
        String respRaw = HttpUtil.createPost(url1).body(paramMap.toString()).execute().body();
        //返回报文封装实体
        CorpBody corpBody = JSONUtil.toBean(respRaw, CorpBody.class);
        if ("success".equals(corpBody.getErrorMessage())){
            log.info("获取应用授权成功");
        }else {
            log.info("获取应用授权失败:"+corpBody.getErrorMessage());
        }
        //信息传输接口
        String url2 = "https://open.fxiaoke.com/cgi/crm/custom/v2/data/create";
        //获取要传输的报文信息
        List<JSONObject> pushInfos = invoiceFormService.getPushInfos(corpBody);
        //判断参数是否为空
        if (!Objects.isNull(pushInfos)){
            for (int i = 0; i < pushInfos.size(); i++) {
                //调用对方接口完成信息推送
                HttpUtil.createPost(url2).body(pushInfos.get(i).toString()).execute().body();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    log.info("线程休眠异常");
                }
            }
            log.info("任务完成");
        }
    }

    public void infoPushLimit(int pageSize){
        log.info("开始执行推送远得数据的定时任务");
        //获取应用授权的url
        String url1 = "https://open.fxiaoke.com/cgi/corpAccessToken/get/V2";
        //封装认证所需要的参数
        JSONObject paramMap = new JSONObject();
        paramMap.put("appId","FSAID_131be20");
        paramMap.put("appSecret","78db04ad63c645609d0b05127fe73715");
        paramMap.put("permanentCode","C1A6DFC2DFDEC79CECC207D00658DBF3");
        //获取应用授权
        String respRaw = HttpUtil.createPost(url1).body(paramMap.toString()).execute().body();
        //反序列化为实体
        CorpBody corpBody = JSONUtil.toBean(respRaw, CorpBody.class);
        if ("success".equals(corpBody.getErrorMessage())){
            log.info("获取应用授权成功");
        }else {
            log.info("获取应用授权失败:"+corpBody.getErrorMessage());
        }
        //获取推送信息集合
        List<JSONObject> jsonObjects = invoiceFormService.getPushInfo(corpBody, pageSize);
        //判断是否为空
        if (!jsonObjects.isEmpty()) {
            log.info("任务开始执行");
            //信息传输接口
            String url2 = "https://open.fxiaoke.com/cgi/crm/custom/v2/data/create";
            for (JSONObject jsonObject: jsonObjects) {
                //调用对方接口完成信息推送
                HttpUtil.createPost(url2).body(jsonObject.toString()).execute().body();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    log.info("线程休眠异常");
                }
            }
        }
        log.info("任务完成");
    }

}
