package com.agileboot.domain.collaboration.record.query;

import cn.hutool.core.util.StrUtil;
import com.agileboot.common.core.page.AbstractPageQuery;
import com.agileboot.common.utils.time.DatePickUtil;
import com.agileboot.domain.collaboration.record.db.CollaborationRecordEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author codex
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class CollaborationRecordQuery extends AbstractPageQuery<CollaborationRecordEntity> {

    private String brand;

    private String goods;

    private String cooperationPlatform;

    private String settlementStatus;

    private String taskStatus;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date purchaseBeginTime;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date purchaseEndTime;

    @Override
    public QueryWrapper<CollaborationRecordEntity> addQueryCondition() {
        QueryWrapper<CollaborationRecordEntity> queryWrapper = new QueryWrapper<CollaborationRecordEntity>()
            .like(StrUtil.isNotEmpty(brand), "brand", brand)
            .like(StrUtil.isNotEmpty(goods), "goods", goods)
            .eq(StrUtil.isNotEmpty(cooperationPlatform), "cooperation_platform", cooperationPlatform)
            .ge(purchaseBeginTime != null, "purchase_date", DatePickUtil.getBeginOfTheDay(purchaseBeginTime))
            .le(purchaseEndTime != null, "purchase_date", DatePickUtil.getEndOfTheDay(purchaseEndTime));

        if (StrUtil.isEmpty(this.getOrderColumn())) {
            this.setOrderColumn("deadline");
            this.setOrderDirection("descending");
        }
        this.setTimeRangeColumn("deadline");
        return queryWrapper;
    }

}
