package com.hzya.frame.dingtalk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.dingtalk.service.IDingTalkExtService;
import com.hzya.frame.dingtalk.util.DingTalkAccessToken;
import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiEntity;
import com.hzya.frame.sysnew.application.apiPara.dao.ISysApplicationApiParaDao;
import com.hzya.frame.sysnew.application.apiPara.entity.SysApplicationApiParaEntity;
import com.hzya.frame.sysnew.application.apiPara.service.ISysApplicationApiParaService;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description 钉钉集成扩展类
 * @Author xiangerlin
 * @Date 2024/8/28 14:25
 **/
@Service(value = "dingTalkExtService")
public class DingTalkExtServiceImpl implements IDingTalkExtService {


    @Resource
    private ISysApplicationApiParaDao sysApplicationApiParaDao;
    private final ConcurrentHashMap<String, JSONObject> dingTalkMap = new ConcurrentHashMap<>();
    /**
     * 调用这个方法初始化钉钉参数
     *
     * @param entity
     * @return
     */
    @Override
    public SysExtensionApiEntity init(SysExtensionApiEntity entity) {
        Map<String, String> headers = entity.getHeaders();
        if (null == headers){
            headers = new HashMap<>();
        }
        SysApplicationEntity receiveApp = entity.getReceiveApp();
        //查询应用上配置的参数
        JSONObject dingTalkConfig = getDingTalkConfig(receiveApp);
        //给token赋值
        entity.setQuerys("access_token="+DingTalkAccessToken.getAccessToken(dingTalkConfig.getString("appKey"),dingTalkConfig.getString("appSecret")));
        return entity;
    }

    /**
     * 查询配置在应用上的钉钉参数
     *
     * @param sysApplication
     * @return
     */
    @Override
    public JSONObject getDingTalkConfig(SysApplicationEntity sysApplication) {
        if (null != sysApplication && StrUtil.isNotEmpty(sysApplication.getId()) && null != sysApplication.getAppId()){
            JSONObject jsonObject = new JSONObject();
            String key = sysApplication.getAppId()+"dingTalk";
            if (null != dingTalkMap.get(key)){
                return dingTalkMap.get(key);
            }else {
                //查询应用上配置的参数
                SysApplicationApiParaEntity paraEntity = new SysApplicationApiParaEntity();
                paraEntity.setAppId(sysApplication.getId());
                List<SysApplicationApiParaEntity> paraList = sysApplicationApiParaDao.query(paraEntity);
                if (CollectionUtils.isNotEmpty(paraList)) {
                    List<SysApplicationApiParaEntity> appKeyList = paraList.stream().filter(p -> p.getInterfaceKey().equals("appKey")).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(appKeyList)) {
                        jsonObject.put("appKey", appKeyList.get(0).getInterfaceValue());
                    }
                    List<SysApplicationApiParaEntity> appSecretList = paraList.stream().filter(p -> p.getInterfaceKey().equals("appSecret")).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(appSecretList)) {
                        jsonObject.put("appSecret", appSecretList.get(0).getInterfaceValue());
                    }
                    dingTalkMap.put(key,jsonObject);
                    return dingTalkMap.get(key);
                }
            }
        }
        return null;
    }

    /**
     * 清空配置缓存
     */
    @Override
    public void clearDingTalkConfigCatch() {
        dingTalkMap.clear();
    }
}
