package com.hzya.frame.plugin.seeyonExt.plugin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.seeyon.service.ISeeYonInterFace;
import com.hzya.frame.sysnew.integtationTaskLivingDetails.entity.IntegrationTaskLivingDetailsEntity;
import com.hzya.frame.sysnew.integtationTaskLivingDetails.service.IIntegrationTaskLivingDetailsService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author xiangerlin
 * @Date 2024/5/15 08:34
 **/
public class SeeyonExtPluginInitializer extends PluginBaseEntity {
    Logger logger = LoggerFactory.getLogger(SeeyonExtPluginInitializer.class);
    @Autowired
    private IIntegrationTaskLivingDetailsService taskLivingDetailsService;
    @Autowired
    private ISeeYonInterFace seeyInterFace;
    /***
     * 插件初始化方法
     * @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 "SeeyonExtPlugin";
    }

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

    /****
     * 插件的显示值
     * @author 👻👻👻👻👻👻👻👻 gjh
     * @date 2023-08-02 10:48
     * @return void
     **/
    @Override
    public String getPluginLabel() {
        return "seeyonExt插件";
    }

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

    /***
     * seeyonExt 重试专用插件
     * @Since 3.0
     * @Author 👻👻👻👻👻👻👻👻 gjh
     * @Date 2023-08-07 11:20
     * @param requestJson  执行业务代码的参数
     * @return void
     **/
    @Override
    public JsonResultEntity executeBusiness(JSONObject requestJson) throws Exception {
        try {
            //重试方法
            if (null != requestJson){
                //JSONObject jsonStr = requestJson.getJSONObject("jsonStr");
                //如果这个id不为空，说明是重试的
                String id = requestJson.getString("integration_task_living_details_id");
                if (StrUtil.isNotEmpty(id)){
                    //查询日志表
                    IntegrationTaskLivingDetailsEntity taskDetailEntity = taskLivingDetailsService.get(id);
                    if (null != taskDetailEntity && JSONUtil.isTypeJSON(taskDetailEntity.getRootAppPk())){
                        //拿到这张表的源系统ID
                        JSONObject jsonObject = JSONObject.parseObject(taskDetailEntity.getRootAppPk());
                        Map<String,String>extData = new HashMap<>();
                        extData.put("integration_task_living_details_id",id);//把日志id放到hzyaExtData中，下游方法从这个对象里取
                        jsonObject.put("hzyaExtData",extData);
                        JSONObject param = new JSONObject();
                        param.put("jsonStr", JSON.toJSONString(jsonObject));
                        //调用seeyon标准重试方法
                        seeyInterFace.thirdInterfaceSeeYonDefinitionRePush(param);
                    }
                }
            }
            logger.info("执行成功");
            return BaseResult.getSuccessMessageEntity("执行成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return BaseResult.getSuccessMessageEntity("执行成功");
    }
}
