package cn.iocoder.yudao.framework.common.template;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.SocialTypeEnum;
import cn.iocoder.yudao.framework.common.template.dto.ApprovalResultNotificationMessageDTO;
import cn.iocoder.yudao.framework.common.template.dto.PerformanceResultConfirmationReminderDTO;
import cn.iocoder.yudao.framework.common.template.dto.ProcessToDoReminderDTO;
import cn.iocoder.yudao.framework.common.template.dto.StaffCertificateInformationDTO;
import cn.iocoder.yudao.framework.common.template.vo.MsgData;
import cn.iocoder.yudao.framework.common.template.vo.SubscribeMessageReqDTO;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;

// -- 微信公众号模版转换器
public class WxMpMsgTemplateUtils {

    /**
     * 审批结果通知
     */
    String APPROVAL_RESULT_NOTIFICATION = "xeV5M36qHGIup512JnfmFjoJhLT4-gGY6uqz2cgfFlM";
    /**
     * OA流程待办提醒
     */
    String OA_PROCESS_TO_DO_REMINDER = "rV94N8PbUOz4EQyjpJcucPQOTrBPx2icOZ5F2KNgD40";
    /**
     * 绩效结果确认提醒
     */
    String PERFORMANCE_RESULT_CONFIRMATION_REMINDER = "56VcBVdG__KGmYQzq2q3KUbijUlKwz6KaaK0mzfPQtc";


    /**
     * 转换审批结果通知
     *
     * @param dto
     * @return
     */
    public SubscribeMessageReqDTO convertApprovalResultNotification(ApprovalResultNotificationMessageDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(APPROVAL_RESULT_NOTIFICATION);
        //审批类型
        MsgData processType = new MsgData();
        processType.setName("thing1");
        processType.setValue(dto.getProcessInstanceName());
        message.addData(processType);

        //发起时间
        MsgData createTime = new MsgData();
        createTime.setName("time7");
        createTime.setValue(dto.getTime());
        message.addData(createTime);

        //审批时间
        MsgData approvalTime = new MsgData();
        approvalTime.setName("time4");
        approvalTime.setValue(DateUtils.dateFormat(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
        message.addData(approvalTime);
        //审批结果
        MsgData approvalResult = new MsgData();
        approvalResult.setName("const6");
        approvalResult.setValue(dto.getResult());
        message.addData(approvalResult);
        String reason = dto.getReason();
        if (StrUtil.isNotEmpty(reason)) {
            MsgData approvalReason = new MsgData();
            if (reason.length() > 10) {
                reason = reason.substring(0, 10) + ". . . . ";
            }
            approvalReason.setName("thing5");
            approvalReason.setValue(reason);
            message.addData(approvalReason);
        }
        message.setJumpWxMaFlag(true);
        message.setMiniProgramState(dto.getMiniProgramState());
        message.setPage(dto.getPage());
        message.setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType());
        return message;
    }


    /**
     * OA流程待办提醒
     *
     * @param dto
     * @return
     */
    public SubscribeMessageReqDTO convertProcessToDoReminder(ProcessToDoReminderDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(OA_PROCESS_TO_DO_REMINDER);
        //待办标题
        MsgData processType = new MsgData();
        processType.setName("thing2");
        processType.setValue(dto.getProcessType());
        message.addData(processType);

        //申请人
        MsgData applicant = new MsgData();
        applicant.setName("phrase10");
        applicant.setValue(dto.getStartUserNickname());
        message.addData(applicant);

        //申请时间
        MsgData createTime = new MsgData();
        createTime.setName("time15");
        createTime.setValue(dto.getTime());
        message.addData(createTime);

        //当前进度
        MsgData currentSchedule = new MsgData();
        currentSchedule.setName("thing9");
        currentSchedule.setValue(dto.getSchedule());
        message.addData(currentSchedule);

        message.setJumpWxMaFlag(true);
        message.setMiniProgramState(dto.getMiniProgramState());
        message.setPage(dto.getPage());
        message.setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType());
        return message;
    }

    /**
     * 绩效结果确认提醒
     *
     * @param dto
     * @return
     */
    public SubscribeMessageReqDTO convertPerformanceResultConfirmationReminder(PerformanceResultConfirmationReminderDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(PERFORMANCE_RESULT_CONFIRMATION_REMINDER);
        //待办标题
        MsgData processType = new MsgData();
        processType.setName("thing1");
        processType.setValue(dto.getUserName());
        message.addData(processType);

        //申请人
        MsgData applicant = new MsgData();
        applicant.setName("time2");
        applicant.setValue(dto.getTime());
        message.addData(applicant);

        message.setJumpWxMaFlag(true);
        message.setMiniProgramState(dto.getMiniProgramState());
        message.setPage(dto.getPage());
        message.setSocialType(SocialTypeEnum.WECHAT_MINI_APP_CRM.getType());
        return message;
    }


    /**
     * 叉车年检过期提醒
     *
     * @param dto
     * @return
     */
    public SubscribeMessageReqDTO forkliftAnnualInspectionExpiredReminder(StaffCertificateInformationDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(PERFORMANCE_RESULT_CONFIRMATION_REMINDER);
        //证件类型
        MsgData certificateName = new MsgData();
        certificateName.setName("thing1");
        certificateName.setValue(dto.getCertificateName());
        message.addData(certificateName);

        //到期时间
        MsgData time = new MsgData();
        time.setName("time2");
        time.setValue(dto.getTime());
        message.addData(time);

        //姓名
        MsgData name = new MsgData();
        name.setName("thing4");
        name.setValue(dto.getTime());
        message.addData(name);

        //备注
        MsgData remarks = new MsgData();
        remarks.setName("thing3");
        remarks.setValue(dto.getTime());
        message.addData(remarks);

        message.setJumpWxMaFlag(false);
//        message.setMiniProgramState(dto.getMiniProgramState());
//        message.setPage(dto.getPage());
        message.setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType());
        return message;
    }

}
