package org.jeecg.modules.orderplatform.util;

import cn.com.ctop.common.module.mapper.MailLogMapper;
import cn.com.ctop.common.module.utils.CorpWexinUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.orderplatform.service.IPlatformMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class ApprovalNodeNoticeHandler {

    @Autowired
    MailLogMapper mailLogMapper;

    @Autowired
    IPlatformMaterialService platformMaterialService;
    @Autowired
    private CorpWexinUtils corpWexinUtils;

    /**
     * 企业微信通知
     */
    public void NOTICE_WE_CHAT_ORDER(String operate, String orderCode) {
        Map<String, JSONObject> relatePerson = relatePerson(orderCode, null);
        message(operate, null, relatePerson.get("orderRelatePerson"));
    }

    public void NOTICE_WE_CHAT_MATERIAL(String operate, String materialCode) {
        Map<String, JSONObject> relatePerson = relatePerson(null, materialCode);
        message(operate, relatePerson.get("materialRelatePerson"), null);
    }

    /**
     * 查询消息接收者
     */
    private Map<String, JSONObject> relatePerson(String orderCode, String materialCode) {
        Map<String, JSONObject> result = new HashMap<>();
        if (null == materialCode) {
            result.put("orderRelatePerson", platformMaterialService.getOrderRelatePersonByMaterialCode(orderCode));
        } else {
            result.put("materialRelatePerson", platformMaterialService.getMaterialRelatePersonByMaterialCode(materialCode));
        }
        return result;
    }

    /**
     * 消息模板
     */
    private void message(String operate, JSONObject materialRelatePerson, JSONObject orderRelatePerson) {
        switch (operate) {
            //运营下单，通知设计组长接单派单
            case "PAYOUT":
                sendWeChat(orderRelatePerson.getString("design"),
                        String.format("您收到一条新的素材订单，请及时查看！\n订单名称：%s\n 订单ID：%s\n下单人：%s",
                                orderRelatePerson.getString("orderTitle"),
                                orderRelatePerson.getLong("orderId"),
                                orderRelatePerson.getString("operatorName")));
                break;

            //设计组长接单，发送通知给
            case "ACCEPT_ORDER":
                sendWeChat(orderRelatePerson.getString("operator"),
                        String.format("您的订单“%s”已接单，请及时查看！\n 订单ID：%s\n操作人：%s",
                                orderRelatePerson.getString("orderTitle"),
                                orderRelatePerson.getLong("orderId"),
                                orderRelatePerson.getString("designName")));
                break;

            //设计组长驳回订单，通知下单运营订单已被驳回
            case "REJECT_ORDER":
                sendWeChat(orderRelatePerson.getString("operator"),
                        String.format("您的订单“%s”被驳回，请及时查看！\n 订单ID：%s\n操作人：%s\n驳回原因：%s",
                                orderRelatePerson.getString("orderTitle"),
                                orderRelatePerson.getLong("orderId"),
                                orderRelatePerson.getString("designName"),
                                orderRelatePerson.getString("orderAdvice")));
                break;

            //销毁订单
            case "DESTROY_ORDER":
                sendWeChat(orderRelatePerson.getString("design"),
                        String.format("您的订单“%s”已被撤回\n 订单ID：%s\n操作人：%s",
                                orderRelatePerson.getString("orderTitle"),
                                orderRelatePerson.getLong("orderId"),
                                orderRelatePerson.getString("operatorName")));
                break;

            //订单完结
            case "ORDER_END":
                sendWeChat(orderRelatePerson.getString("design"),
                        String.format("您的订单“%s”物料已全部制作完成，订单已被系统自动完结\n 订单ID：%s\n审核人：%s",
                                orderRelatePerson.getString("orderTitle"),
                                orderRelatePerson.getLong("orderId"),
                                orderRelatePerson.getString("operatorName")));
                break;

            //脚本上传成功,通知运营审核
            case "SCRIPT":
                sendWeChat(materialRelatePerson.getString("operator"),
                        String.format("您的订单“%s”收到一条新的脚本，请及时审核！\n 订单ID：%s\n上传人：%s",
                                materialRelatePerson.getString("orderTitle"),
                                materialRelatePerson.getLong("orderId"),
                                materialRelatePerson.getString("planName")));
                break;

            //视频上传成功,通知运营审核
            case "VIDEO":
                sendWeChat(materialRelatePerson.getString("operator"),
                        String.format("您的订单“%s”收到一条新的视频，请及时审核！\n 订单ID：%s\n上传人：%s",
                                materialRelatePerson.getString("orderTitle"),
                                materialRelatePerson.getLong("orderId"),
                                materialRelatePerson.getString("clipName")));
                break;

            //脚本审批通过，通知上传人
            case "ACCEPT_SCRIPT":
                sendWeChat(materialRelatePerson.getString("plan"),
                        String.format("您的脚本“%s”已审核通过\n 所属订单：%s\n 订单ID：%s\n审核人：%s",
                                materialRelatePerson.getString("scriptName"),
                                materialRelatePerson.getString("orderTitle"),
                                materialRelatePerson.getLong("orderId"),
                                materialRelatePerson.getString("operatorName")));
                break;

            //视频通过暂不通知
            case "ACCEPT_VIDEO":
                sendWeChat(materialRelatePerson.getString("clip"),
                        String.format("您的视频“%s”已审核通过\n 所属订单：%s\n 订单ID：%s\n审核人：%s",
                                materialRelatePerson.getString("videoName"),
                                materialRelatePerson.getString("orderTitle"),
                                materialRelatePerson.getLong("orderId"),
                                materialRelatePerson.getString("operatorName")));
                break;

            //脚本被驳回，通知编导重新上传脚本
            case "REJECT_SCRIPT":
                sendWeChat(materialRelatePerson.getString("plan"),
                        String.format("您的脚本“%s”被驳回，请及时查看！\n 所属订单：%s\n 订单ID：%s\n审核人：%s\n驳回原因：%s",
                                materialRelatePerson.getString("scriptName"),
                                materialRelatePerson.getString("orderTitle"),
                                materialRelatePerson.getLong("orderId"),
                                materialRelatePerson.getString("operatorName"),
                                materialRelatePerson.getString("scriptAdvise")));
                break;

            //视频被驳回，通知剪辑重新上传视频
            case "REJECT_VIDEO":
                sendWeChat(materialRelatePerson.getString("clip"),
                        String.format("您的视频“%s”被驳回，请及时查看！\n 所属订单：%s\n 订单ID：%s\n审核人：%s\n驳回原因：%s",
                                materialRelatePerson.getString("videoName"),
                                materialRelatePerson.getString("orderTitle"),
                                materialRelatePerson.getLong("orderId"),
                                materialRelatePerson.getString("operatorName"),
                                materialRelatePerson.getString("videoAdvise")));

        }
    }

    /**
     * 发送通知消息到多人企业微信
     */
    private void sendWeChat(Set<String> users, String message) {
        users.forEach(userId -> {
            JSONObject weChatId = mailLogMapper.getWChatIdByUserId(userId);
            if (weChatId.isEmpty()) {
                log.error(String.format("User WeChat not found. userId =%s", userId));
                //TODO 通知管理员添加邮箱
            } else {
                corpWexinUtils.sendMessageByWeChatId(weChatId, message);
            }
        });
    }

    /**
     * 发送通知消息到多人企业微信
     */
    private void sendWeChat(String userId, String message) {
        JSONObject weChatId = mailLogMapper.getWChatIdByUserId(userId);
        if (weChatId.isEmpty()) {
            log.error(String.format("User WeChat not found. userId =%s", userId));
            //TODO 通知管理员添加邮箱
        } else {
            corpWexinUtils.sendMessageByWeChatId(weChatId, message);
        }
    }
}
