package com.iotechn.unimall.admin.config;

import com.alibaba.fastjson.JSONObject;
import com.iotechn.unimall.biz.service.user.UserBizService;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.mapper.AdminMapper;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SendWxMsg {
    private static Logger logger = LoggerFactory.getLogger(SendWxMsg.class);

    /**
     * 办公流程审核通知
     * @param sender
     * @param userDO
     * @param accessToken
     */
    public static void sendSubscribeMessageSH(String sender, UserDO userDO,String accessToken){
        try{
            OkHttpClient okHttpClient = new OkHttpClient();
            MediaType mediaType = MediaType.parse("application/json");
            JSONObject param = new JSONObject();
            JSONObject data = new JSONObject();
            JSONObject thing1 = new JSONObject();
            JSONObject name2 = new JSONObject();
            JSONObject name3 = new JSONObject();
            JSONObject date5 = new JSONObject();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            thing1.put("value","审核任务");
            name2.put("value",sender);
            name3.put("value",userDO.getUserName());
            date5.put("value",sdf.format(new Date()));
            data.put("thing1",thing1);
            data.put("name2",name2);
            data.put("name3",name3);
            data.put("date5",date5);
            param.put("touser", userDO.getOpenId());
            param.put("template_id", "xL_uZEqJz4QP1FbnlzQWAiO2zVBn3Qeu8JwLVOU3hU0");
            param.put("data", data);
            param.put("page","pages/user/task");
            RequestBody body = RequestBody.create(mediaType, param.toJSONString());
            Request request = new Request.Builder()
                    .url("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken)
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            JSONObject jsonObject = JSONObject.parseObject(okHttpClient.newCall(request).execute().body().string());
            Integer errcode = jsonObject.getInteger("errcode");
            if (errcode == null || errcode == 0) {
                logger.info("[审核任务] 已成功向用户："+userDO.getPhone()+" 发送订阅消息");
            }
            else{
                logger.info("[审核任务] 订阅消息发送失败 ：errcode = "+errcode);
            }
        }
        catch (Exception e){
            logger.error("sendSubscribeMessageSH exception"+e.getMessage());
        }
    }

    /**
     * 审核结果通知
     * @param goodsName
     * @param sender
     * @param type
     * @param userDO
     * @param accessToken
     */
    public static void sendSubscribeMessageTZ(String goodsName,String sender,String type, UserDO userDO, String accessToken){
        try{
            OkHttpClient okHttpClient = new OkHttpClient();
            MediaType mediaType = MediaType.parse("application/json");
            JSONObject param = new JSONObject();
            JSONObject data = new JSONObject();
            JSONObject phrase2 = new JSONObject();
            JSONObject date4 = new JSONObject();
            JSONObject thing3 = new JSONObject();
            JSONObject thing9 = new JSONObject();
            JSONObject thing13 = new JSONObject();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            phrase2.put("value","已通过");
            date4.put("value",sdf.format(new Date()));
            thing3.put("value",goodsName);
            thing9.put("value",type);
            thing13.put("value",sender);
            data.put("phrase2",phrase2);
            data.put("date4",date4);
            data.put("thing3",thing3);
            data.put("thing9",thing9);
            data.put("thing13",thing13);
            param.put("touser", userDO.getOpenId());
            param.put("template_id", "8cVkckXi_8zfHeScXRHhjN6cgZFYYCWIMPDTiPWagXY");
            param.put("data", data);
            param.put("page","pages/user/task");

            RequestBody body = RequestBody.create(mediaType, param.toJSONString());
            Request request = new Request.Builder()
                    .url("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken)
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            JSONObject jsonObject = JSONObject.parseObject(okHttpClient.newCall(request).execute().body().string());
            Integer errcode = jsonObject.getInteger("errcode");
            if (errcode == null || errcode == 0) {
                logger.info("[审核"+type+"信息] 已成功向用户："+userDO.getPhone()+" 发送订阅消息");
            }
            else{
                logger.info("[审核"+type+"信息] 订阅消息发送失败 ：errcode = "+errcode);
            }
        }
        catch (Exception e){
            logger.error("sendSubscribeMessageSH exception"+e.getMessage());
        }
    }

}
