package com.zsc.edu.gateway.modules.message.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.modules.message.entity.Bulletin;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 系统公告Query
 *
 * @author harry_yao
 */
@Data
@AllArgsConstructor
@NoArgsConstructor

public class BulletinQuery {

    private String title;
    private Bulletin.State state;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime publishTimeBegin;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime publishTimeEnd;

    public LambdaQueryWrapper<Bulletin> wrapper() {
        LambdaQueryWrapper<Bulletin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(this.title), Bulletin::getTitle, this.title);
        queryWrapper.eq(Objects.nonNull(this.state), Bulletin::getState, this.state);
        if (publishTimeBegin != null && publishTimeEnd != null) {
            queryWrapper.between(Bulletin::getPublishTime, this.publishTimeBegin, this.publishTimeEnd);
        }
        return queryWrapper;
    }
}
