package jnpf.cultivate.utils;

import cn.hutool.json.JSONUtil;
import jnpf.ImRobotApi;
import jnpf.base.ActionResult;
import jnpf.entity.cultivate.CultivateIdentifyApply;
import jnpf.from.ImRobotTypeEnum;
import jnpf.from.JumpUrlListModel;
import jnpf.from.SendRobotNoticeDataForm;
import jnpf.from.SingleSendRobotNoticeForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * 培训鉴定提醒
 */
@Component
@Slf4j
public class CultivateIdentifyIMUtils {

    @Autowired
    private ImRobotApi imRobotApi;
    private static final String NOTICE_APP_NAME = "培训";
    private static final String TITLE = "鉴定通知";
    private static final String NOTICE_APP_LOGO = "https://jnpf-resource-1304460613.cos.ap-chengdu.myqcloud.com/jnpf-resources/UserAvatar/66457c71e4b05b749193397b.png";
    private static final String NOTICE_BUTTON_NAME = "点击查看详情";
    private static final String NOTICE_BUTTON_LINK = "/pages/qualificationManagement/authenticateInfo/index?menuId=461212890154671237&fullName=我的鉴定&id=";//跳转页面链接

    private static final String CULTIVATE_MP_ID = "__UNI__74A05F6";//__UNI__74A05F6 培训管理

    /**
     * 发送消息
     *
     * @param apply    鉴定信息
     * @param tenantId 租户ID
     * @return
     */
    public Boolean sendMsg(CultivateIdentifyApply apply, String tenantId) {

        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setTenantId(tenantId);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        //固定图片
        robotNoticeDataForm.setLogo(NOTICE_APP_LOGO);
        robotNoticeDataForm.setAppName(NOTICE_APP_NAME);

        robotNoticeDataForm.setTitle(TITLE);
        robotNoticeDataForm.setContent(buildNoticeContent(apply));

        robotNoticeDataForm.setJumpUrlList(buildBtnUrl(apply));
        form.setRobotNoticeDataForm(robotNoticeDataForm);
        form.setToUserIds(List.of(apply.getBeIdentifyUserId()));
        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        log.info("CultivateIdentifyIMUtils send msg ={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
        if (actionResult == null || !actionResult.getCode().equals(200)) {
            log.error("CultivateIdentifyIMUtils,send msg req={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
        }
        return true;
    }

    /**
     * 构建消息内容
     *
     * @param taskName 任务名称
     * @return
     */
    private String buildNoticeContent(CultivateIdentifyApply apply) {
        return "您有一条新的鉴定结果，请查看！";
    }

    /**
     * 构建按钮链接
     *
     * @param taskId 任务ID
     * @return
     */
    private LinkedList<JumpUrlListModel> buildBtnUrl(CultivateIdentifyApply apply) {
        LinkedList<JumpUrlListModel> btn = new LinkedList<>();
        JumpUrlListModel urlModel = new JumpUrlListModel();
        urlModel.setDisplayMethodEnum(JumpUrlListModel.DisplayMethodEnum.TRUE);
        urlModel.setButtonName(NOTICE_BUTTON_NAME);
        urlModel.setReqMethod(JumpUrlListModel.ReqMethodEnum.GET);
        urlModel.setType(1);//1-跳转链接  2-表单提交
        urlModel.setUrl(NOTICE_BUTTON_LINK + apply.getId());
        urlModel.setMpId(CULTIVATE_MP_ID);
        btn.add(urlModel);
        return btn;
    }

}
