package com.bs.web.utils;

import com.alibaba.fastjson2.JSONObject;
import com.bs.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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


/**
 * @author: gfk
 * @create: 2023/6/23
 * @Description:
 * @FileName: WXSenMsgUtil
 * @History:
 * @自定义内容：
 */
@Slf4j
@Component
public class WXSenMsgUtil {

    @Resource
    private ISysConfigService sysConfigService;
    @Resource
    private WeiXinUtil weiXinUtil;

    public  boolean sendOpenIdToTemplateMessage(String templateKey,String openId,String path, JSONObject data) {
        String templateId = getSysTemplate(templateKey);
        if (!StringUtils.isEmpty(openId)) {
            return weiXinUtil.sendMessage(data,openId,templateId,path);
        }
        return false;
    }


    /**
     * <br>
     * 描述：模板消息发送 <br>
     * 接口文档：https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
     *
     * @param templateKey  从本类的静态属性取
     * @param openId   openId，微信登录返回的openId
     * @param path   path，跳转小程序页面
     * @param values   values，消息通知的值，从上到下按顺序下来
     * @return
     * @throws Exception
     * @date: 2019年9月5日 下午2:45:19
     * @author Tjw
     */
    public  boolean sendOpenIdToTemplateMessage(String templateKey,String openId,String path, List<String> values)
          {
        String templateId = getSysTemplate(templateKey);
        int size = values.size();
        JSONObject data = new JSONObject();
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                JSONObject first = new JSONObject();
                first.put("value",values.get(0));
                data.put("first",first);
            } else if (i == size-1) {
                JSONObject remark = new JSONObject();
                remark.put("value",values.get(0));
                data.put("remark",remark);
            } else {
                JSONObject keyword = new JSONObject();
                keyword.put("value",values.get(i));
                data.put("keyword"+i,keyword);
            }
        }
        if (!StringUtils.isEmpty(openId)) {
            return weiXinUtil.sendMessage(data,openId,templateId,path);
        }
        return false;
    }



    /**
     * 获取模板的配置信息
     * @param templateCongifKey 模板key
     * @return
     * @throws Exception
     */
    private  String getSysTemplate(String templateCongifKey){
        String value = sysConfigService.selectConfigByKey(templateCongifKey);
        if(StringUtils.isEmpty(value)){
            return null;
        }
        return value;
    }




    public static void main(String[] args) throws Exception {
        List<String> list = new ArrayList<>();
        list.add("123");
        list.add("456");
        list.add("789");
        list.add("10");
        list.add("abc");
        //sendOpenIdToTemplateMessage("kc6ADrWzxuFxVsvgKqM5V255LGCMe1DXg40S1CmL3Uc","otHaO4u_ISeXjLrTr3TZ5H_t0ZBw","",list);
    }

}
