package com.gree.mall.manager.logic.user;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.gree.mall.manager.config.wx.WxConfiguration;
import com.gree.mall.manager.logic.common.WechatLogic;
import com.gree.mall.manager.plus.entity.User;
import com.gree.mall.manager.plus.service.UserService;
import com.gree.mall.manager.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息订阅
 */
@Slf4j
@Service
public class MsgSubscriptLogic {


    //环境
    @Value("${spring.profiles.active}")
    public String env;

    @Autowired
    UserService userService;

    @Autowired
    WechatLogic wechatLogic;


    /**
     * 发货通知
     * @param openId
     * @param orderId 订单号
     * @param orderAmount 订单金额
     * @param expressCompany 快递公司
     * @param expressNo 快递号
     */
    public void sendFhNotice(String userId, String openId, String orderId, BigDecimal orderAmount, String expressCompany, String expressNo){

        //封装模版数据
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, String> keyword1 = new HashMap<String, String>();
        keyword1.put("value", orderId);
        keyword1.put("color", "#173177");
        Map<String, String> keyword2 = new HashMap<String, String>();
        keyword2.put("value", orderAmount.toString());
        keyword2.put("color", "#173177");
        Map<String, String> keyword3 = new HashMap<String, String>();
        keyword3.put("value", expressCompany);
        keyword3.put("color", "#173177");
        Map<String, String> keyword4 = new HashMap<String, String>();
        keyword4.put("value", expressNo);
        keyword4.put("color", "#173177");

        if(StringUtils.equals(env,"prd")){
            data.put("character_string1", keyword1);
            data.put("amount15", keyword2);
            data.put("name3", keyword3);
            data.put("character_string4", keyword4);
        }else {
            data.put("character_string2", keyword1);
            data.put("amount7", keyword2);
            data.put("thing4", keyword3);
            data.put("character_string5", keyword4);
        }
        User user = userService.getById(userId);
        String template = WxConfiguration.temFhNotice.get(user.getCompanyWechatId());
        String res = sendWxMsg(openId, data, template, "pages/mine/order/detail?orderId="+orderId,user.getCompanyWechatId());
        log.info("-----------------完成发货通知消息返回：" + res);
    }


    /**
     * 推送消息
     */
    private String sendWxMsg(String openId, Map<String,Object> data, String templateId, String page,String companyWechatId) {
        try {

            String token = wechatLogic.getAccessToken(companyWechatId);//获取token
            String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + token;

            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("access_token", token);
            paramMap.put("touser", openId);//openid
            paramMap.put("template_id", templateId);//账户变动通知模版ID
            paramMap.put("page", page);//页面
            //paramMap.put("form_id",uf.getFormId()); //formId
            paramMap.put("data", data);//数据
            // paramMap.put("emphasis_keyword","keyword1.DATA");
            String param = JSON.toJSONString(paramMap);

            String s = HttpUtils.requestPostBody(url, param, null);

            log.info("----请求推送消息返回：" + s);
            return s;
        }catch(Exception e){
            log.error("小程序服务通知失败:{}",e.getMessage());
        }
        return "";
    }

}
