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

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
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();
            approvalReason.setName("thing12");
            approvalReason.setValue(reason);
            message.addData(approvalReason);
        }

        message.setMiniprogramState(miniProgramState);
        message.setPage("pages/home/index?id=" + processInstanceId);
        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;
    }
}
