package cn.iocoder.yudao.module.bpm.convert.message;

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAWorkTaskDO;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import cn.iocoder.yudao.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import cn.iocoder.yudao.module.system.api.subscribe.dto.MsgData;
import cn.iocoder.yudao.module.system.api.subscribe.dto.SubscribeMessageReqDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.Date;
import java.util.Map;

@Mapper
public interface BpmMessageConvert {

    BpmMessageConvert INSTANCE = Mappers.getMapper(BpmMessageConvert.class);

    @Mapping(target = "mobile", ignore = true)
    @Mapping(source = "userId", target = "userId")
    @Mapping(source = "templateCode", target = "templateCode")
    @Mapping(source = "templateParams", target = "templateParams")
    SmsSendSingleToUserReqDTO convert(Long userId, String templateCode, Map<String, Object> templateParams);

    @Mapping(source = "userId", target = "userId")
    @Mapping(source = "templateCode", target = "templateCode")
    @Mapping(source = "templateParams", target = "templateParams")
    NotifySendSingleToUserReqDTO convert1(Long userId, String templateCode, Map<String, Object> templateParams);


    /**
     * @param openId              微信小程序唯一id
     * @param processInstanceName 流程名称
     * @param result              审批结果
     * @param reason              审批原因
     * @param miniProgramState    小程序的状态
     * @return
     */
    default SubscribeMessageReqDTO convertApprovalResultNotification(String openId, String processInstanceName,
                                                                     String time, String result, String reason, String processInstanceId,
                                                                     String miniProgramState) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(openId);
        message.setTemplateId("OnJjp5pdjG1PHMoELYaqp3Xq8jWZ5E6ndO0clEIQ4tk");
        //审批类型
        MsgData processType = new MsgData();
        processType.setName("thing1");
        processType.setValue(processInstanceName);
        message.addData(processType);

        //发起时间
        MsgData createTime = new MsgData();
        createTime.setName("time2");
        createTime.setValue(time);
        message.addData(createTime);

        //审批时间
        MsgData approvalTime = new MsgData();
        approvalTime.setName("time7");
        approvalTime.setValue(DateUtils.dateFormat(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
        message.addData(approvalTime);

        //审批结果
        MsgData approvalResult = new MsgData();
        approvalResult.setName("phrase3");
        approvalResult.setValue(result);
        message.addData(approvalResult);

        if (reason != null) {
            MsgData approvalReason = new MsgData();
            if (reason.length() > 10) {
                reason = reason.substring(0,10) + ". . . . ";
            }

            approvalReason.setName("thing12");
            approvalReason.setValue(reason);
            message.addData(approvalReason);
        }

        message.setMiniprogramState(miniProgramState);
        message.setPage("pages/bpm/task/todo/examineApprove?id=" + processInstanceId + "&isDetail=1");
        return message;
    }

    /**
     * @param openId              微信小程序唯一id
     * @param processInstanceName 流程名称
     * @param startUserNickname   申请人
     * @param time                申请时间
     * @param schedule            流程进度
     * @param miniProgramState    小程序的状态
     * @return
     */
    default SubscribeMessageReqDTO convertProcessToDoReminder(String openId, String processInstanceName,
                                                              String startUserNickname, String time, String schedule, String processInstanceId, String miniProgramState) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(openId);
        message.setTemplateId("3cP4btlFSSiZk65qVewN_WoT_bh0OfUkYzzTsADOrR4");
        //待办标题
        MsgData processType = new MsgData();
        processType.setName("thing1");
        processType.setValue("您收到了一条新的待办任务:" + processInstanceName);
        message.addData(processType);

        //申请人
        MsgData applicant = new MsgData();
        applicant.setName("thing4");
        applicant.setValue(startUserNickname);
        message.addData(applicant);

        //申请时间
        MsgData createTime = new MsgData();
        createTime.setName("time5");
        createTime.setValue(time);
        message.addData(createTime);

        //当前进度
        MsgData currentSchedule = new MsgData();
        currentSchedule.setName("thing6");
        currentSchedule.setValue(schedule);
        message.addData(currentSchedule);

        message.setMiniprogramState(miniProgramState);
        message.setPage("pages/bpm/task/todo/examineApprove?id=" + processInstanceId);
        return message;
    }

    /**
     * @param openId              微信小程序唯一id
     * @param workTaskDO          任务对象
     * @param nickname            发布人姓名
     * @param miniProgramState    小程序的状态
     * @return
     */
    default SubscribeMessageReqDTO convertWorkTask(String openId, String processInstanceName, BpmOAWorkTaskDO workTaskDO,
                                                   String nickname, String time, String miniProgramState, Boolean isFlag) {

        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(openId);
        message.setTemplateId("fH29xjNb8pe-7onQ-wE3QrBAC-y8aaC_oosYZKNMtzM");

        //消息类型
        MsgData noticeType = new MsgData();
        noticeType.setName("phrase8");
        noticeType.setValue(processInstanceName);
        message.addData(noticeType);

        //发送人
        MsgData publishMan = new MsgData();
        publishMan.setName("thing16");
        publishMan.setValue(nickname);
        message.addData(publishMan);

        //发送时间
        MsgData createTime = new MsgData();
        createTime.setName("time3");
        createTime.setValue(time);
        message.addData(createTime);

        //消息内容
        MsgData content = new MsgData();
        content.setName("thing2");
        if (isFlag) {
            content.setValue("你分配的任务已完成！");
        }else {
            content.setValue("你收到了一个新的任务！");
        }
        message.addData(content);

        message.setMiniprogramState(miniProgramState);
        if (isFlag) {
            message.setPage("subPages/task/taskAssignment?userId=" + workTaskDO.getReceiverUserId());
        }else {
            message.setPage("subPages/task/taskDispose?id=" + workTaskDO.getId());
        }

        return message;
    }
}
