package com.hzya.frame.plugin.contract.plugin;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.plugin.contract.entity.ContractAuditEntity;
import com.hzya.frame.plugin.contract.service.IContractAuditService;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @Description 合同评审，超过60天流程未审批的，把流程终止掉
 * @Author xiangerlin
 * @Date 2025/3/31 11:07
 **/
public class ContractAuditPluginInitializer extends PluginBaseEntity {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IContractAuditService contractAuditService;
    /***
     * 插件初始化方法
     * @Author 👻👻👻👻👻👻👻👻 gjh
     * @Date 2023-08-02 10:48
     * @Param []
     * @return void
     **/
    @Override
    public void initialize() {
        logger.info(getPluginLabel() + "執行初始化方法initialize()");
    }

    /****
     * 插件销毁方法
     * @author 👻👻👻👻👻👻👻👻 gjh
     * @date 2023-08-02 10:48
     * @return void
     **/
    @Override
    public void destroy() {
        logger.info(getPluginLabel() + "執行銷毀方法destroy()");
    }

    /****
     * 插件的ID
     * @author 👻👻👻👻👻👻👻👻 gjh
     * @date 2023-08-02 10:48
     * @return void
     **/
    @Override
    public String getPluginId() {
        return "ContractAuditPluginInitializer";
    }

    /****
     * 插件的名称
     * @author 👻👻👻👻👻👻👻👻 gjh
     * @date 2023-08-02 10:48
     * @return void
     **/
    @Override
    public String getPluginName() {
        return "OA合同评审流程检测插件";
    }

    /****
     * 插件的显示值
     * @author 👻👻👻👻👻👻👻👻 gjh
     * @date 2023-08-02 10:48
     * @return void
     **/
    @Override
    public String getPluginLabel() {
        return "OA合同评审流程检测插件";
    }

    /***
     * 插件类型 1、场景插件
     * @Author 👻👻👻👻👻👻👻👻 gjh
     * @Date 2023-08-02 14:01
     * @Param []
     * @return java.lang.String
     **/
    @Override
    public String getPluginType() {
        return "1";
    }

    /***
     * 执行业务代码
     * @Author 👻👻👻👻👻👻👻👻 gjh
     * @Date 2023-08-07 11:20
     * @param requestJson  执行业务代码的参数
     * @return void
     **/
    @Override
    public JsonResultEntity executeBusiness(JSONObject requestJson) throws Exception {
        logger.info("======开始执行合同评审流程检测插件定时任务======");
        //1、查询超期的合同
        ContractAuditEntity contractAuditEntity = new ContractAuditEntity();
        contractAuditEntity.setDataSourceCode(requestJson.getString("sourceCode"));
        //contractAuditEntity.setField0168("PS202503270198");
        List<ContractAuditEntity> contractAuditList = contractAuditService.queryOverdueList(contractAuditEntity);
        if (CollectionUtils.isNotEmpty(contractAuditList)){
            String token = getToken();
            for (ContractAuditEntity auditEntity : contractAuditList) {
                //2、更新流程状态为终止状态
                JSONObject jsonObject =  new JSONObject();
                jsonObject.put("affairId",auditEntity.getAffairId());//待办id
                jsonObject.put("member",auditEntity.getLogin_name());//当前待办登录名
                //jsonObject.put("affairId","9022465059383233743");//待办id
                //jsonObject.put("member","yonyou");//当前待办登录名
                String reqParams = jsonObject.toString();
                logger.info("合同评审流程终止接口请求参数：{}",reqParams);
                //调用wms接口
                Map<String, String> headerMap = MapBuilder.<String, String>create(true)
                        .put("apiCode", "8000590002")
                        .put("publicKey","ZJYAWb7lhAUTYqekPkU+uHJv1/ObJxb7dT7sD8HPRDGAgyhCe7eDIk+3zDUT+v578prj")
                        .put("secretKey","fviZnLBsQUAGF8w8FSOdJi7XlIm/XAZclMxRagDLfTyJFlvnIBF3w66Hrpfzs8cYj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=")
                        .put("appId","800059")
                        .put("token",token)
                        .build();
                String body = HttpRequest.post("http://127.0.0.1:10086/kangarooDataCenterV3/entranceController/externalCallInterface").addHeaders(headerMap).body(reqParams).timeout(60000).execute().body();
                logger.info("合同评审流程终止接口返回参数：{}",body);
            }
        }
        return null;
    }

    //获取token
    private String getToken(){
        String token = null;
        JSONObject tokenObj = new JSONObject();
        tokenObj.put("userName","rest0331");
        tokenObj.put("password","27db12a6-e09d-488f-90fb-143d9f45938d");
        tokenObj.put("loginName","yonyou");
        String tokenReqParams = tokenObj.toString();
        Map<String, String> tokenHeaderMap = MapBuilder.<String, String>create(true)
                .put("apiCode", "8000590001")
                .put("publicKey","ZJYAWb7lhAUTYqekPkU+uHJv1/ObJxb7dT7sD8HPRDGAgyhCe7eDIk+3zDUT+v578prj")
                .put("secretKey","fviZnLBsQUAGF8w8FSOdJi7XlIm/XAZclMxRagDLfTyJFlvnIBF3w66Hrpfzs8cYj3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=")
                .put("appId","800059")
                .build();
        String tokenBody = HttpRequest.post("http://127.0.0.1:10086/kangarooDataCenterV3/entranceController/externalCallInterface").addHeaders(tokenHeaderMap).body(tokenReqParams).timeout(60000).execute().body();
        if (StrUtil.isNotEmpty(tokenBody)){
            JsonResultEntity jsonResultEntity = JSONObject.parseObject(tokenBody, JsonResultEntity.class);
            JSONObject jsonObject = (JSONObject) jsonResultEntity.getAttribute();
            token = jsonObject.getString("id");
        }
        return token;
    }
}
