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

import cn.iocoder.yudao.framework.common.enums.SocialTypeEnum;
import cn.iocoder.yudao.framework.common.template.dto.CheckInReminderDTO;
import cn.iocoder.yudao.framework.common.template.dto.CompanyAnnouncementNoticeDTO;
import cn.iocoder.yudao.framework.common.template.dto.MessageUnreadReminderDTO;
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 java.util.Date;

// -- 微信小程序模版转换器
public class WxMaMsgTemplateUtils {

    /**
     * 消息未读提醒
     */
    public final static String MESSAGE_UNREAD_REMINDER = "fH29xjNb8pe-7onQ-wE3QrBAC-y8aaC_oosYZKNMtzM";
    /**
     * 公司公告通知
     */
    public final static String COMPANY_ANNOUNCEMENT_NOTICE = "OF4-948Vz9rtE_VA55rmDxo4azOwmrjjk33jDpOiPiY";
    /**
     * 打卡提醒
     */
    public final static String CHECK_IN_REMINDER = "5yHyXhrsyqXi8s4hYhvaeMtz9kT5OPvDCe3h6G1T-OE";


    /**
     * 消息未读提醒
     *
     * @param dto
     * @return
     */
    public SubscribeMessageReqDTO convertMessageUnreadReminder(MessageUnreadReminderDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(MESSAGE_UNREAD_REMINDER);

        //消息类型
        MsgData noticeType = new MsgData();
        noticeType.setName("phrase8");
        noticeType.setValue(dto.getMsgType());

        message.addData(noticeType);

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

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

        //消息内容
        MsgData content = new MsgData();
        content.setName("thing2");
        String comment = dto.getComment();
        if (comment.length() > 10) {
            comment = comment.substring(0, 10) + ". . . . ";
        }
        content.setValue(comment);
        message.addData(content);

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


    public SubscribeMessageReqDTO convertCompanyAnnouncementNotice(CompanyAnnouncementNoticeDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(COMPANY_ANNOUNCEMENT_NOTICE);
        //通知类型
        MsgData noticeType = new MsgData();
        noticeType.setName("thing1");
        noticeType.setValue(dto.getNoticeType()); //这里根据业务需求填写具体的公告类型  如全员公告， xxx公告
        message.addData(noticeType);

        //公告标题
        String value = dto.getTitle();
        if (value.length() > 10) {
            value = value.substring(0, 10) + ". . . . ";
        }

        MsgData title = new MsgData();
        title.setName("thing6");
        title.setValue(value);
        message.addData(title);

        //通知内容
        MsgData content = new MsgData();
        content.setName("thing2");
        content.setValue("具体内容请进入小程序查看");
        message.addData(content);

        //发布人
        MsgData publishMan = new MsgData();
        publishMan.setName("thing9");
        publishMan.setValue(dto.getNickname());
        message.addData(publishMan);

        //通知时间
        MsgData createTime = new MsgData();
        createTime.setName("time3");
        createTime.setValue(DateUtils.dateFormat(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
        message.addData(createTime);

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

    /**
     * 打卡通知
     *
     * @param dto
     * @return
     */
    public SubscribeMessageReqDTO convertCheckInReminder(CheckInReminderDTO dto) {
        SubscribeMessageReqDTO message = new SubscribeMessageReqDTO();
        message.setToUser(dto.getOpenId());
        message.setTemplateId(CHECK_IN_REMINDER);
        MsgData data = new MsgData();
        data.setName("thing9");
        data.setValue(dto.getCheckInType());
        message.addData(data);
        data = new MsgData();
        data.setName("thing6");
        data.setValue(dto.getComment());
        message.addData(data);
        message.setPage(dto.getPage());
        message.setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType());
        return message;
    }


}
