package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.message.SendCardMessageDTO;
import com.cool.store.enums.FeiShuNoticeMsgEnum;
import com.cool.store.enums.MessageTypeEnum;
import com.cool.store.http.ISVHttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

/**
 * @Author: young.yu
 * @Date: 2023-07-19  17:06
 * @Description:
 */
@Slf4j
@Service
public class NoticeService {
    @Value("${feishu.notice.link.url}")
    private String linkUrl;
    @Autowired
    private ISVHttpRequest isvHttpRequest;
    public void sendFeiShuNotice(FeiShuNoticeMsgEnum feiShuNoticeMsgEnum, List<String> userIds,Object... objects) {
        try{
            if (CollectionUtils.isEmpty(userIds)){
                log.info("sendFeiShuNotice_feiShuNoticeMsgEnum:{} userIds:{},objects:{}",feiShuNoticeMsgEnum.getTitle(), JSONObject.toJSONString(userIds),JSONObject.toJSONString(objects));
                return;
            }
            SendCardMessageDTO sendCardMessageDTO = new SendCardMessageDTO();
            sendCardMessageDTO.setUserIds(userIds);
            sendCardMessageDTO.setMessageType(MessageTypeEnum.SCHEDULE_REMINDER);
            sendCardMessageDTO.setMessageUrl(linkUrl);
            sendCardMessageDTO.setTitle(feiShuNoticeMsgEnum.getTitle());
            sendCardMessageDTO.setContent(MessageFormat.format(feiShuNoticeMsgEnum.getContent(),objects));
            isvHttpRequest.sendFeiShuCardMessage(sendCardMessageDTO);
        }catch (Exception e){
            log.info("发送飞书通知失败");
        }
    }
}
