package com.hzya.frame.plugin.kekao.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.plugin.kekao.dao.ICreditauditDao;
import com.hzya.frame.plugin.kekao.entity.CreditauditEntity;
import com.hzya.frame.plugin.kekao.service.ICreditauditService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.nio.charset.StandardCharsets;

public class CreditauditServiceImpl extends BaseService<CreditauditEntity, String> implements ICreditauditService {
    @Value("${zt.url}")
    private String url;

    private ICreditauditDao creditauditDao;

    @Autowired
    public void setCreditauditDao(ICreditauditDao dao) {
        this.creditauditDao = dao;
        this.dao = dao;
    }

    /**
     * @param requestJson
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 信用额度审批单保存提交同步
     * @Date 2:32 下午 2025/8/5
     **/
    @Override
    public JsonResultEntity sendCreditauditData(JSONObject requestJson) {
        logger.error("同步信用额度审批单保存提交入参" + requestJson.toJSONString());
        JSONObject jsonStr = requestJson.getJSONObject("jsonStr");
        if (jsonStr == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        JSONObject businessDataStr = jsonStr.getJSONObject("businessDataStr");
        if (businessDataStr == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        JSONObject mainData = businessDataStr.getJSONObject("formmain_9555");
        if (mainData == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //组装数据
        //获取token
        String token = null; //todo
        JSONObject tokenData = new JSONObject();
        tokenData.put("userName","beis");
        tokenData.put("password","64de4de7-23ce-4946-ac91-dd9e322d86e5");
        tokenData.put("loginName","seeyon");
        String tokenResult = HttpRequest.post(url).
                header("appId", "800051").
                header("apiCode", "8000510005").
                header("publicKey", "ZJYAM2YFEIsIaI1e4wMagkS2Q7SFgGk0RvsPhEES45m/OVFCe7eDIk+3zDUT+v578prj").
                header("secretKey", "lR6+uf94mvNMclAB43oIwmhJSMDzQaViTkfXLeNvmGdpLfbFiUvbgbU+p43zO25Zj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=").
                body(tokenData.toJSONString()).
                execute().
                body();
        logger.error("BIP Token：" + tokenResult);
        JSONObject resoutJson = JSONObject.parseObject(tokenResult);
        if (resoutJson.getBoolean("flag")) {
            token = resoutJson.getJSONObject("attribute").getString("id");
        } else {
            return BaseResult.getFailureMessageEntity("获取BIP token失败");
        }
        JSONObject sendData = setSendCreditauditData(mainData);
        logger.error("同步信用额度审批单保存提交单据发送参数：" + sendData.toJSONString());
        String sendResult = HttpRequest.post(url).//todo
                header("appId", "800051").
                header("apiCode", "8000510001").
                header("token", token).
                header("publicKey", "ZJYAM2YFEIsIaI1e4wMagkS2Q7SFgGk0RvsPhEES45m/OVFCe7eDIk+3zDUT+v578prj").
                header("secretKey", "lR6+uf94mvNMclAB43oIwmhJSMDzQaViTkfXLeNvmGdpLfbFiUvbgbU+p43zO25Zj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=").
                body(sendData.toJSONString()).
                execute().
                body();
        logger.error("同步信用额度审批单保存提交单据发送返回：" + sendResult);
        JSONObject sendJson = JSONObject.parseObject(sendResult);
        if (sendJson.getBoolean("flag")) {
            return BaseResult.getSuccessMessageEntity("同步信用额度审批单保存提交单据发送成功");
        } else {
            return BaseResult.getFailureMessageEntity("同步信用额度审批单保存提交单据发送错误："+sendJson.getString("msg"));
        }
    }

    /**
     * @Author lvleigang
     * @Description  组装参数
     * @Date 2:47 下午 2025/8/5
     * @param mainData
     * @return com.alibaba.fastjson.JSONObject
     **/
    private JSONObject setSendCreditauditData(JSONObject mainData) {
        JSONObject sendData = new JSONObject();

        // 构建headVO对象
        JSONObject headVO = new JSONObject();
        headVO.put("pk_org", "COCO_CS");
        headVO.put("ctrantypeid", "4320-01");
        headVO.put("vtrantypecode", "4320-01");
        headVO.put("capplyerid", "subo");
        headVO.put("vbillcode", "wxz08051210");
        headVO.put("dbilldate", "2025-08-05 09:30:19");
        headVO.put("vnote", "wxz备注");
        headVO.put("creator", "sub");
        headVO.put("vdef1", "wxzvdef1");

        // 构建bodyVOList数组中的第一个对象
        JSONObject bodyVO = new JSONObject();
        bodyVO.put("climittypeid", "COCO_01");
        bodyVO.put("ccurrencyid", "CNY");
        bodyVO.put("nlimitmny", "12.00");
        bodyVO.put("dtodate", "2099-12-31 23:59:59");
        bodyVO.put("dfromdate", "2025-08-05 00:00:00");
        bodyVO.put("csaleorgid", "001");
        bodyVO.put("ccustid", "01020017");
        bodyVO.put("dbilldate", "2025-08-05 09:36:37");
        bodyVO.put("vnote", "wxzB备注");
        bodyVO.put("vbdef1", "wxzvbdef1");
        bodyVO.put("pk_saleorg", "001");
        bodyVO.put("pk_income", "1010");

        // 创建JSON数组并添加bodyVO对象
        JSONArray bodyVOList = new JSONArray();
        bodyVOList.add(bodyVO);

        // 将headVO和bodyVOList添加到外层对象
        sendData.put("headVO", headVO);
        sendData.put("bodyVOList", bodyVOList);

        return sendData;
    }




    /**
     * @param requestJson
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 销售返利同步
     * @Date 2:32 下午 2025/8/5
     **/
    @Override
    public JsonResultEntity sendSalesRebateData(JSONObject requestJson) {
        logger.error("同步销售返利入参" + requestJson.toJSONString());
        JSONObject jsonStr = requestJson.getJSONObject("jsonStr");
        if (jsonStr == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        JSONObject businessDataStr = jsonStr.getJSONObject("businessDataStr");
        if (businessDataStr == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        JSONObject mainData = businessDataStr.getJSONObject("formmain_9555");
        if (mainData == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //组装数据
        //获取token
        String token = null; //todo
        JSONObject tokenData = new JSONObject();
        tokenData.put("userName","beis");
        tokenData.put("password","64de4de7-23ce-4946-ac91-dd9e322d86e5");
        tokenData.put("loginName","seeyon");
        String tokenResult = HttpRequest.post(url).
                header("appId", "800051").
                header("apiCode", "8000510005").
                header("publicKey", "ZJYAM2YFEIsIaI1e4wMagkS2Q7SFgGk0RvsPhEES45m/OVFCe7eDIk+3zDUT+v578prj").
                header("secretKey", "lR6+uf94mvNMclAB43oIwmhJSMDzQaViTkfXLeNvmGdpLfbFiUvbgbU+p43zO25Zj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=").
                body(tokenData.toJSONString()).
                execute().
                body();
        logger.error("BIP Token：" + tokenResult);
        JSONObject resoutJson = JSONObject.parseObject(tokenResult);
        if (resoutJson.getBoolean("flag")) {
            token = resoutJson.getJSONObject("attribute").getString("id");
        } else {
            return BaseResult.getFailureMessageEntity("获取BIP token失败");
        }
        JSONObject sendData = setSalesRebateData(mainData);
        logger.error("同步销售返利发送参数：" + sendData.toJSONString());
        String sendResult = HttpRequest.post(url).//todo
                header("appId", "800051").
                header("apiCode", "8000510001").
                header("token", token).
                header("publicKey", "ZJYAM2YFEIsIaI1e4wMagkS2Q7SFgGk0RvsPhEES45m/OVFCe7eDIk+3zDUT+v578prj").
                header("secretKey", "lR6+uf94mvNMclAB43oIwmhJSMDzQaViTkfXLeNvmGdpLfbFiUvbgbU+p43zO25Zj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=").
                body(sendData.toJSONString()).
                execute().
                body();
        logger.error("同步销售返利发送返回：" + sendResult);
        JSONObject sendJson = JSONObject.parseObject(sendResult);
        if (sendJson.getBoolean("flag")) {
            return BaseResult.getSuccessMessageEntity("同步销售返利发送成功");
        } else {
            return BaseResult.getFailureMessageEntity("同步销售返利发送错误："+sendJson.getString("msg"));
        }
    }

    /**
     * @Author lvleigang
     * @Description  组装参数
     * @Date 2:47 下午 2025/8/5
     * @param mainData
     * @return com.alibaba.fastjson.JSONObject
     **/
    private JSONObject setSalesRebateData(JSONObject mainData) {
        JSONObject sendData = new JSONObject();



        return sendData;
    }
}
