package jnpf.personnels.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
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.personnels.po.FtbPersonnelsStaffRoster;
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.stream.Collectors;

/**
 * 劳动合同未发起消息通知
 */
@Component
@Slf4j
public class NoSendContactIMUtils {

    @Autowired
    private ImRobotApi imRobotApi;
    private static final String NOTICE_APP_NAME = "电子合同";
    private static final String NOTICE_MPID = "__UNI__8684C05";
    private static final String NOTICE_APP_LOGO = "https://jnpf-resource-1304460613.cos.ap-chengdu.myqcloud.com/jnpf-resources/UserAvatar/668ba53ae4b0621053741b0e.png";
    private static final String NOTICE_BUTTON_NAME = "查看详细内容 >";
    private static final String NOTICE_BUTTON_LINK = "/pages/announcement/components/info?id=%s";//跳转页面链接


    /**
     * 发送消息
     *
     * @param tenantId      租户
     * @param msgRosterList 接收人
     * @param toRosterList  接收人
     * @return
     */
    public Boolean sendMsg(String tenantId, List<FtbPersonnelsStaffRoster> msgRosterList, List<FtbPersonnelsStaffRoster> toRosterList) {

        if (CollUtil.isEmpty(msgRosterList) || CollUtil.isEmpty(toRosterList)) {
            return true;
        }
        List<String> collect = toRosterList.stream().map(FtbPersonnelsStaffRoster::getUserId).collect(Collectors.toList());

        for (FtbPersonnelsStaffRoster ftbPersonnelsStaffRoster : msgRosterList) {
            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("您有条劳务合同未发起,请登录到后台发起");
            robotNoticeDataForm.setContent(buildNoticeContent(ftbPersonnelsStaffRoster));
            form.setMessageAttributionRobotMpId(NOTICE_MPID);

//            robotNoticeDataForm.setJumpUrlList(buildBtnUrl(roster));
            form.setRobotNoticeDataForm(robotNoticeDataForm);

            form.setToUserIds(collect);
            ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
//                log.error("send msg ContractSign={},result={}",JSONUtil.toJsonStr(form),JSONUtil.toJsonStr(actionResult));
            if (actionResult == null || !actionResult.getCode().equals(200)) {
//                    log.error("合同未发起通知消息,send msg req={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
            }

        }
        return true;
    }

    private String buildNoticeContent(FtbPersonnelsStaffRoster roster) {
        StringBuilder sb = new StringBuilder();
        sb.append("姓名：");
        sb.append(roster.getName());
        sb.append("<br>");

        sb.append("入职日期：");
        if (null != roster.getActualStartDate()) {
            sb.append(DateUtil.format(roster.getActualStartDate(), "yyyy.MM.dd"));
        }
        return sb.toString();

    }


    private LinkedList<JumpUrlListModel> buildBtnUrl(FtbPersonnelsStaffRoster roster) {
        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(String.format(NOTICE_BUTTON_LINK, roster.getUserId()));
        btn.add(urlModel);
        return btn;
    }

}
