package top.baogutang.admin.schedule;

import com.fasterxml.jackson.core.type.TypeReference;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import top.baogutang.admin.dao.entity.NoticeLogEntity;
import top.baogutang.admin.domain.AnnouncementsDto;
import top.baogutang.admin.services.INoticeLogService;
import top.baogutang.admin.utils.DingTalkMsgPushUtils;
import top.baogutang.common.constants.NoticeTypeEnum;
import top.baogutang.common.domain.PageUtil;
import top.baogutang.common.domain.Results;
import top.baogutang.common.utils.JacksonUtil;
import top.baogutang.common.utils.OkHttpUtil;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @description:
 * @author: nikooh
 * @date: 2024/06/14 : 16:18
 */
@Slf4j
@Component
@RefreshScope
public class NoticeScheduleHandler extends IJobHandler {

    @Resource
    private DingTalkMsgPushUtils dingTalkMsgPushUtils;

    @Resource
    private INoticeLogService noticeLogService;

    @Override
    @XxlJob("noticeScheduleHandler")
    public ReturnT<String> execute(String params) throws Exception {
        Arrays.stream(NoticeTypeEnum.values())
                .forEach(noticeType -> {
                    Results<PageUtil<AnnouncementsDto>> results = OkHttpUtil.get(noticeType.getListUrl(), null, null, new TypeReference<Results<PageUtil<AnnouncementsDto>>>() {
                    });
                    log.info(">>>>>>>>>>请求获取:{}公告返回数据：{}<<<<<<<<<<", noticeType.getDesc(), JacksonUtil.toJson(results));
                    if (Objects.isNull(results)) {
                        return;
                    }
                    if (!Boolean.TRUE.equals(results.isSuccess())) {
                        return;
                    }
                    if (Objects.isNull(results.getData()) || CollectionUtils.isEmpty(results.getData().getList())) {
                        return;
                    }
                    AnnouncementsDto announcementsDto = results.getData().getList().get(0);
                    List<NoticeLogEntity> noticeLogList = noticeLogService.queryLogByConditions(announcementsDto.getId(), noticeType);
                    if (CollectionUtils.isNotEmpty(noticeLogList)) {
                        log.info(">>>>>>>>>>notice :{} has already pushed<<<<<<<<<<", announcementsDto.getId());
                        return;
                    }
                    String content = noticeType.getTitle() + announcementsDto.getTitle() + "\n\n![](" + announcementsDto.getCover() + ")\n\n>";
                    dingTalkMsgPushUtils.robotMarkdownMsgPush(announcementsDto.getTitle(), "查看详情", noticeType.getDetailUrl() + announcementsDto.getId(), content);
                    noticeLogService.saveNotice(noticeType, announcementsDto.getId(), announcementsDto.getType(), announcementsDto.getTitle(), announcementsDto.getCover(), noticeType.getDetailUrl() + announcementsDto.getId(), announcementsDto.getCreator(), announcementsDto.getCreatedAt());
                });
        return ReturnT.SUCCESS;
    }
}
