package com.zhongshu.card.server.core.dao.school.impl;

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.school.NoticeInfoViewSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.school.extend.NoticeInfoViewDaoExtend;
import com.zhongshu.card.server.core.domain.school.NoticeInfo;
import com.zhongshu.card.server.core.domain.school.NoticeInfoView;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @Author TRX
 * @CreateDate: 2023/4/12
 * @Version: 1.0
 */
public class NoticeInfoViewDaoImpl extends BaseImpl implements NoticeInfoViewDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

    @Override
    public Page<NoticeInfoView> page(Pageable pageable, NoticeInfoViewSearch param) {
        Criteria criteria = new Criteria();

        //
        if (ObjectUtils.isNotEmpty(param.getNoticeInfoId())) {
            criteria.and("noticeInfo").is(NoticeInfo.build(param.getNoticeInfoId()));
        }
        criteria.and("isDelete").is(Boolean.FALSE);
        Sort sort = Sort.by(
                Sort.Order.desc("createTime")
        );
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, NoticeInfoView.class);
    }

}
