package jnpf.personnels.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
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.notice.dto.FtbNoticeAnnouncementsDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 发送im 试岗消息
 */
@Component
@Slf4j
public class PersonnelPreTrailIMUtils {

    @Autowired
    private ImRobotApi imRobotApi;
    private static final String APP_NAME = "人事管理";
    private static final String APP_LOGO = "https://jnpf-resource-1304460613.cos.ap-chengdu.myqcloud.com/jnpf-resources/UserAvatar/665ecb69e4b0ae5df114c87a.png";
    private static final String MSG_TEMPLATE = "您所管辖的组织中，以下人员将于明日结束试岗期：%s，若人员不符合标准，您可以在花名册中找到该人员，点击\"驳回试岗\"；若该人员符合标准，您不需要做任何操作。";


    /**
     * 发送消息
     *
     * @param tenantId     租户
     * @param toUserIds    接收人
     * @param userNameList 用户姓名列表
     * @return
     */
    public Boolean sendMsg(String tenantId, List<String> toUserIds, List<String> userNameList) {

        SingleSendRobotNoticeForm form = new SingleSendRobotNoticeForm();
        form.setRobotTypeEnum(ImRobotTypeEnum.FTB_XZS);
        form.setToUserIds(toUserIds);
        form.setTenantId(tenantId);
        SendRobotNoticeDataForm robotNoticeDataForm = new SendRobotNoticeDataForm();
        robotNoticeDataForm.setLogo(APP_LOGO);//固定图片
        robotNoticeDataForm.setAppName(APP_NAME);

        robotNoticeDataForm.setTitle("试岗即将到期提醒");
        String userNameStr = String.join("、", userNameList);
        robotNoticeDataForm.setContent(String.format(MSG_TEMPLATE, userNameStr));
        form.setRobotNoticeDataForm(robotNoticeDataForm);


        ActionResult actionResult = imRobotApi.sendSingleRobotNotice(form);
        if (actionResult == null || !actionResult.getCode().equals(200)) {
            log.error("试岗快结束im发送失败,send msg req={},result={}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(actionResult));
        }

        return true;
    }



}
