package com.nmgs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.nmgs.entity.HttpParamDto;
import com.nmgs.mapper.Mapper;
import com.nmgs.util.HttpClientUtil;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PubTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MessagePushServiceImpl {
    @Autowired
    public Mapper mapperI;

    @Async("integralManagerThreadPool")
    public void pushMsg(String openId, String integralType, String consumeIntegral, String enabledIntegral) {
        try {
            Thread.sleep(300);
            String templete_id = "";
            String url = "";
            String page = "";
            String sqlTemp = "SELECT\n" +
                    "\tips.ID,\n" +
                    "\tips.PARAMCODE,\n" +
                    "\tips.BAK1,\n" +
                    "\tips.BAK2,\n" +
                    "\tips.BAK3,\n" +
                    "\tips.BAK4,\n" +
                    "\tips.BAK5,\n" +
                    "\tips.ENABLED \n" +
                    "FROM\n" +
                    "\tINTEGRAL_PUBLIC_SET ips \n" +
                    "WHERE\n" +
                    "\t1 = 1 \n" +
                    "\tAND PARAMCODE = 'CHANGE_MSG_PUSH' \n" +
                    "\tAND ENABLED = 'Y'";
            List selectforlist = this.mapperI.selectforlist(sqlTemp);
            if (selectforlist != null && selectforlist.size() > 0) {
                Map<String, Object> map = (Map<String, Object>) selectforlist.get(0);
                templete_id = map.get("BAK1").toString();
                page = map.get("BAK2").toString();
                url = map.get("BAK3").toString();
            }
            if (!PubTools.isNull(templete_id) && !PubTools.isNull(page) && !PubTools.isNull(url)) {
                LogUtil.WriteLog_Info("小程序消息推送接口执行===templete_id===" + templete_id + "=====page===" + page + "====url===" + url, "MessagePushServiceImpl");
                HttpParamDto dto = new HttpParamDto(integralType, PubTools.getCurrentDate(), consumeIntegral, enabledIntegral, "进入小程序查看积分详情。");
                dto.setTemplate_id(templete_id);
                dto.setPage(page);
                String s = HttpClientUtil.doPostJson(url + openId, JSON.toJSONString(dto));
                if (!PubTools.isNull(s)) {
                    JSONObject retJo = (JSONObject) JSONObject.parse(s);
                    String retCode = retJo.get("code").toString();
                    if (!"200".equals(retCode)) {
                        Object message = retJo.get("message");
                        LogUtil.WriteLog_Error("小程序消息推送接口执行失败===code===" + retCode + "=====message===" + message, "MessagePushServiceImpl");
                    }
                }
                System.out.println(s);
            } else {
                LogUtil.WriteLog_Error("小程序消息推送接口未配置", "MessagePushServiceImpl");
            }
            System.out.println("更新任务线程,线程名：" + Thread.currentThread().getName());
        } catch (Exception e) {
            LogUtil.WriteLog_Error("小程序消息推送接口报错====" + e.getMessage(), "MessagePushServiceImpl");
        }
    }
}
