package jnpf.cultivate.utils;

import cn.hutool.json.JSONUtil;
import jnpf.ImRobotApi;
import jnpf.base.ActionResult;
import jnpf.from.ImRobotTypeEnum;
import jnpf.from.JumpUrlListModel;
import jnpf.from.SendRobotNoticeDataForm;
import jnpf.from.SingleSendRobotNoticeForm;
import jnpf.model.cultivate.dto.learn.NeedAlertUserDto;
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;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务提醒
 */
@Component
@Slf4j
public class CultivateLearnTaskIMUtils {

    @Autowired
    private ImRobotApi imRobotApi;
    private static final String NOTICE_APP_NAME = "培训";
    private static final String CERTIFICATE_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/myTask/index?menuId=461212890154671237&fullName=我的培训任务";//跳转页面链接

    private static final String CULTIVATE_MP_ID = "__UNI__74A05F6";//__UNI__74A05F6 培训管理
    /**
     * 发送消息
     *
     * @param alertUserList 提醒人员列表
     * @param tenantId      租户ID
     * @return
     */
    public Boolean sendMsg(List<NeedAlertUserDto> alertUserList, String tenantId) {
        Map<String, List<NeedAlertUserDto>> batchMaps = alertUserList.stream()
                .collect(Collectors.groupingBy(a -> a.getTaskId() + "#" + a.getTaskName()));
        batchMaps.forEach((k, v) -> {
            String[] split = k.split("#");
            List<String> userIds = v.stream().map(NeedAlertUserDto::getUserId).collect(Collectors.toList());
            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(CERTIFICATE_TITLE);
            robotNoticeDataForm.setContent(buildNoticeContent(split[1]));

            robotNoticeDataForm.setJumpUrlList(buildBtnUrl(split[0]));
            form.setRobotNoticeDataForm(robotNoticeDataForm);
            form.setToUserIds(userIds);
            ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
            log.info("NeedAlertUserDto send msg ={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
            if (actionResult == null || !actionResult.getCode().equals(200)) {
                log.error("NeedAlertUserDto,send msg req={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
            }
        });
        return true;
    }

    /**
     * 构建消息内容
     * @param taskName 任务名称
     * @return
     */
    private String buildNoticeContent(String taskName) {
        return "您有一项新的任务,可点击下方按钮前往\"培训任务\"进行学习！任务名称：" + taskName;
    }

    /**
     * 构建按钮链接
     * @param taskId 任务ID
     * @return
     */
    private LinkedList<JumpUrlListModel> buildBtnUrl(String taskId) {
        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);
        urlModel.setMpId(CULTIVATE_MP_ID);
        btn.add(urlModel);
        return btn;
    }

}
