package cn.teammodel.service.impl;

import cn.hutool.json.JSONObject;
import cn.teammodel.manager.wx.WechatUtilService;
import cn.teammodel.model.dto.Wechat.AppraiseReqDto;
import cn.teammodel.model.dto.Wechat.ExamReqDto;
import cn.teammodel.model.dto.Wechat.ReportReqDto;
import cn.teammodel.model.dto.Wechat.VocationalReqDto;
import cn.teammodel.model.vo.WechatResponseMsgVo;
import cn.teammodel.service.WechatService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Service
public class WechatServiceImpl implements WechatService{


    @Resource
    private WechatUtilService wechatUtilService;

    /**
     * 发送评价订阅消息
     * @param appraiseDto
     * @return
     */

    @Override
    public WechatResponseMsgVo AppraisePushMsg(AppraiseReqDto appraiseDto){
        WechatResponseMsgVo sendMsg = null;
        String openId = null;
        if (appraiseDto.getCode() != null){
            String openId2 = wechatUtilService.GetOpenId(appraiseDto.getCode());
            JSONObject jsonObject = new JSONObject(openId2);
            openId = jsonObject.getStr("openid");
        }
        if (appraiseDto.getOpenId() != null){
            openId = appraiseDto.getOpenId();
        }
        // 处理微信推送数据结构
        JSONObject mapData = new JSONObject();
        if (appraiseDto.getName() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  appraiseDto.getName());
            mapData.set("thing5", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"用户不能为空");
            return sendMsg;
        }
        if (appraiseDto.getContent() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  appraiseDto.getContent());
            mapData.set("thing2", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"评价内容");
            return sendMsg;
        }
        if (appraiseDto.getAppraiseTime() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  appraiseDto.getAppraiseTime());
            mapData.set("time6", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"评价时间不能为空");
            return sendMsg;
        }
        if (appraiseDto.getAppraiseUser() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  appraiseDto.getAppraiseUser());
            mapData.set("thing7", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"评价人不能为空");
            return sendMsg;
        }

        sendMsg = wechatUtilService.PushMsgResponse(WechatUtilService.TEMPLATE_Appraise,openId,mapData,appraiseDto.page);
        return sendMsg;
    }

    /**
     * 考核成绩发布通知
     * @param examReqDto
     * @return
     */
    public WechatResponseMsgVo ExamPushMsg(ExamReqDto examReqDto){
        WechatResponseMsgVo sendMsg = null;
        String openId = null;
        if (examReqDto.getCode() != null){
            String openId2 = wechatUtilService.GetOpenId(examReqDto.getCode());
            JSONObject jsonObject = new JSONObject(openId2);
            openId = jsonObject.getStr("openid");
        }
        if (examReqDto.getOpenId() != null){
            openId = examReqDto.getOpenId();
        }
        // 处理微信推送数据结构
        JSONObject mapData = new JSONObject();
        if (examReqDto.getThing3() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  examReqDto.getThing3());
            mapData.set("thing3", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"用户不能为空");
            return sendMsg;
        }
        if (examReqDto.getThing1() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  examReqDto.getThing1());
            mapData.set("thing1", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"评价内容");
            return sendMsg;
        }
        if (examReqDto.getNumber2() != 0){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  examReqDto.getNumber2());
            mapData.set("number2", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"考试分数");
            return sendMsg;
        }
        if (examReqDto.getTime5() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  examReqDto.getTime5());
            mapData.set("time5", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"考试时间不能为空");
            return sendMsg;
        }
        return wechatUtilService.PushMsgResponse(WechatUtilService.TEMPLATE_Eexam,openId,mapData,examReqDto.page);
    }

    /**
     * 考核成绩发布通知
     * @param reportReqDto
     * @return
     */
    public WechatResponseMsgVo ReportPushMsg(ReportReqDto reportReqDto){
        WechatResponseMsgVo sendMsg = null;
        String openId = null;
        if (reportReqDto.getCode() != null){
            String openId2 = wechatUtilService.GetOpenId(reportReqDto.getCode());
            JSONObject jsonObject = new JSONObject(openId2);
            openId = jsonObject.getStr("openid");
        }
        if (reportReqDto.getOpenId() != null){
            openId = reportReqDto.getOpenId();
        }
        // 处理微信推送数据结构
        JSONObject mapData = new JSONObject();
        if (reportReqDto.getThing8() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  reportReqDto.getThing8());
            mapData.set("thing8", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"用户不能为空");
            return sendMsg;
        }
        if (reportReqDto.getThing1() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  reportReqDto.getThing1());
            mapData.set("thing1", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"报告类型");
            return sendMsg;
        }
        if (reportReqDto.getTime2() != null){
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value",  reportReqDto.getTime2());
            mapData.set("time2", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"考试时间不能为空");
            return sendMsg;
        }
        return wechatUtilService.PushMsgResponse(WechatUtilService.TEMPLATE_Report,openId,mapData,reportReqDto.page);
    }

    /**
     * 职业测试报告通知
     * @param vocationalReqDto
     * @return
     */
    public WechatResponseMsgVo VocationalPushMsg(VocationalReqDto vocationalReqDto){
        WechatResponseMsgVo sendMsg = null;
        String openId = null;
        if (vocationalReqDto.getCode() != null){
            String openId2 = wechatUtilService.GetOpenId(vocationalReqDto.getCode());
            JSONObject jsonObject = new JSONObject(openId2);
            openId = jsonObject.getStr("openid");
        }
        if (vocationalReqDto.getOpenId() != null){
            openId = vocationalReqDto.getOpenId();
        }
        // 处理微信推送数据结构
        JSONObject mapData = new JSONObject();
        if (vocationalReqDto.getName() != null) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value", vocationalReqDto.getName());
            mapData.set("name1", map1);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"用户不能为空");
            return sendMsg;
        }
        if (vocationalReqDto.getTime2() != null) {
            Map<String, Object> map2 = new HashMap<>();
            map2.put("value", vocationalReqDto.getTime2());
            mapData.set("time2", map2);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"评测时间不能为空");
            return sendMsg;
        }
        if (vocationalReqDto.getThing3() != null) {
            Map<String, Object> map3 = new HashMap<>();
            map3.put("value", vocationalReqDto.getThing3());
            mapData.set("thing3", map3);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"测评名称不能为空");
            return sendMsg;
        }
        if (vocationalReqDto.getNumber4() != 0) {
            Map<String, Object> map4 = new HashMap<>();
            map4.put("value", vocationalReqDto.getNumber4());
            mapData.set("number4", map4);
        }else {
            sendMsg  = new WechatResponseMsgVo(400,"测评成绩不能为空");
            return sendMsg;
        }

        return wechatUtilService.PushMsgResponse(WechatUtilService.TEMPLATE_Vocational,openId,mapData,vocationalReqDto.page);
    }



}
