package com.mortals.xhx.base.system.message.impl;

import com.mortals.framework.common.Rest;
import com.mortals.xhx.base.system.message.MessageService;
import com.mortals.xhx.common.code.AppealResultEnum;
import com.mortals.xhx.common.code.SubAddTypeEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.dingding.CheckDingMsg;
import com.mortals.xhx.module.dingding.personal.model.req.workmsg.Link;
import com.mortals.xhx.module.dingding.personal.model.req.workmsg.Msg;
import com.mortals.xhx.module.dingding.personal.model.req.workmsg.Text;
import com.mortals.xhx.module.dingding.personal.model.req.workmsg.WorkMsgReq;
import com.mortals.xhx.module.dingding.personal.service.IDingPersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service("messageService")
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Value("${webUrl:''}")
    private String webUrl;
    @Autowired
    private IDingPersonService dingPersonService;

    @Override
    public Rest<String> sendDingTalkWorkMsg(CheckDingMsg dingMsg) {
        WorkMsgReq workMsgReq = new WorkMsgReq();
        Msg msg = new Msg();
        msg.setMsgtype("link");
        Link link = new Link();
        link.setPicUrl("");
        link.setMessageUrl("webUrl" + dingMsg.getPath());

        if (SubAddTypeEnum.增加.getValue() == dingMsg.getSubAddType()) {
            link.setText(String.format("你好，经人工核实后你在%%-%s" +
                    "，系统自动增加绩效%s分，如有情况说明请及时向管理人员提交申诉处理，感谢你的配合。", dingMsg.getDateTime(), dingMsg.getRuleStr(), dingMsg.getScore()));
        } else {
            link.setText(String.format("你好，经人工核实后你在%s违反%s" +
                    "，系统自动扣除绩效%s分，如有情况说明请及时向管理人员提交申诉处理，感谢你的配合。", dingMsg.getDateTime(), dingMsg.getRuleStr(), dingMsg.getScore()));
        }

        link.setTitle(String.format("%s考核核查通知", dingMsg.getPerformTypeStr()));
        msg.setLink(link);

        workMsgReq.setUseridList(dingMsg.getUserIds());
        workMsgReq.setMsg(msg);

        Rest<String> sendWorkRest = dingPersonService.sendWorkMsg(workMsgReq);
        if (YesNoEnum.YES.getValue() == sendWorkRest.getCode()) {
            String requestId = sendWorkRest.getData();
            return Rest.ok(requestId);
        } else {
            log.error("发送工作消息失败,msg:{}", sendWorkRest.getMsg());
            return Rest.fail(sendWorkRest.getMsg());
        }
    }

    @Override
    public Rest<String> sendDingTalkAppealMsg(CheckDingMsg dingMsg) {
        WorkMsgReq workMsgReq = new WorkMsgReq();
        Msg msg = new Msg();
        msg.setMsgtype("text");
        Text text = new Text();
        if (AppealResultEnum.通过.getValue() == dingMsg.getAppealResult()) {
            text.setContent(String.format("你好，你申诉的在%s违反%s，经人工核查后情况属实，本次申诉成功，将撤销之前的扣除绩效%s。", dingMsg.getDateTime(), dingMsg.getRuleStr(), dingMsg.getScore()));
        } else {
            text.setContent(String.format("你好，你申诉的在%s违反%s，经人工核查后情况仍继续扣除%s。本次申诉失败，原因：%s，如有疑问请联系相关负责人。", dingMsg.getDateTime(), dingMsg.getRuleStr(), dingMsg.getScore(), dingMsg.getAppealDesc()));
        }
        msg.setText(text);
        workMsgReq.setUseridList(dingMsg.getUserIds());
        workMsgReq.setMsg(msg);
        Rest<String> sendWorkRest = dingPersonService.sendWorkMsg(workMsgReq);
        if (YesNoEnum.YES.getValue() == sendWorkRest.getCode()) {
            String requestId = sendWorkRest.getData();
            return Rest.ok(requestId);
        } else {
            log.error("发送工作消息失败,msg:{}", sendWorkRest.getMsg());
            return Rest.fail(sendWorkRest.getMsg());
        }
    }
}