package cn.com.ty.lift.ud.schoolVideo.controller.query;

import cn.com.ty.lift.ud.common.BasePageQuery;
import cn.com.ty.lift.ud.schoolVideo.mapper.entity.SchoolVideoEntity;
import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 电梯学堂 查询条件
 * 
 * @author Xson
 * @date 2020-04-23
 */
@Data
@ApiModel(description = "电梯学堂 查询条件")
public final class SchoolVideoQuery extends BasePageQuery {

    public static String APP = "app";

    /**
     * 标题
     */
    @ApiModelProperty("标题")
    private String title;

    /**
     * 发布人
     */
    @ApiModelProperty("发布人")
    private String name;

    /**
     * 品牌
     */
    @ApiModelProperty("品牌")
    private Long brandId;

    /**
     * 专家
     */
    @ApiModelProperty("专家")
    private Long userId;

    /**
     * 热门推荐 0是1否
     */
    @ApiModelProperty("热门推荐 1是0否")
    private Integer hotFlag;

    /**
     * 状态 0:正常1:下架2:未审核
     */
    @ApiModelProperty("状态 1:正常0:下架")
    private Integer statuz;

    /**
     * 状态 0:正常1:下架2:未审核
     */
    @ApiModelProperty("状态 1:通过0:驳回2待审核")
    private Integer checkFlag;

    /**
     * 平台
     */
    @ApiModelProperty("平台")
    private String platform;

    /**
     * 查询条件
     * 
     * @return
     */
    @Override
    public Wrapper condition() {
        QueryWrapper<SchoolVideoEntity> query = new QueryWrapper<>();
        if (StringUtils.isNotBlank(title)) {
            query.like("a.title", title);
        }
        if (StringUtils.isNotBlank(name)) {
            query.like("b.name", name);
        }
        if (null != brandId) {
            query.eq("a.brand_id", brandId);
        }
        if (null != checkFlag) {
            query.eq("a.check_flag", checkFlag);
        }
        if (null != hotFlag) {
            query.eq("a.hot_flag", hotFlag);
        }
        if (null != userId) {
            query.eq("a.user_id", userId);
        }
        if (null != statuz) {
            query.eq("a.statuz", statuz);
        }
        query.eq("1", 1);
        if (APP.equals(platform)) {
            query.eq("a.statuz", 1);
            query.eq("a.check_flag", 1);
            // 先按按置顶排序，再按浏览量
            query.orderByDesc("a.hot_flag")
                    .orderByDesc("a.browse_num")
                    .orderByDesc("a.create_time");
        } else {
            // 按创建时间倒序
            query.orderByDesc("a.create_time");
        }
        return query;
    }

    /**
     * 分页条件
     * 
     * @return
     */
    @Override
    public IPage page() {
        Page<SchoolVideoEntity> page = new Page<>(getPageNum(), getPageSize());
        return page;
    }

}
