import { queryNotice, queryNoticeReaded } from "@/services/go/commonService";
import { QueryNoticeRsp } from '@/services/go/commonService/interface';
import timerUtil from '@/utils/timer/timerUtil';
import { ref } from "vue";

// 消息
const noticeList = ref<QueryNoticeRsp[]>([]);

export function handleNotice() {
    // 查询通知数据
    function queryNoticeAction() {
        queryNotice().then((res) => {
            noticeList.value = res;
        });
    }
    // 查询通知数据
    function getNoticeByType(type: number[]): QueryNoticeRsp[] {
        //消息类型 - 1:公告通知 2:系统消息 3:商品到期提货通知
        return noticeList.value.filter((e: QueryNoticeRsp) => type.includes(e.msgtype));
    }
    // 获取具体某个类型未读通知的个数
    function getUnReadNoticeByType(type: number[]): number {
        return getNoticeByType(type).filter(e => !e.readed).length
    }
    // 设置已读消息
    function readNotice({ readed, autoid }: QueryNoticeRsp) {
        if (!readed) {
            queryNoticeReaded(autoid).then(() => {
                queryNoticeAction()
            });
        }
    }
    // 更新通知，通知没有推送，所有需要轮询
    function updateNotice() {
        timerUtil.setInterval(queryNoticeAction, 1 * 60 * 1000, 'pollingNotice')
    }
    // 获取未读通知的消息的个数
    function getUnReadNoticeLength() {
        return noticeList.value.filter(e => !e.readed).length
    }
    return { noticeList, queryNoticeAction, getNoticeByType, readNotice, updateNotice, getUnReadNoticeLength, getUnReadNoticeByType }
}