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

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

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

import cn.com.ty.lift.ud.common.BasePageQuery;
import cn.com.ty.lift.ud.shopping.mapper.entity.ShoppingEntity;
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 ShoppingQuery extends BasePageQuery {

    public static String APP = "app";

    /**
     * 商品名称
     */
    @ApiModelProperty("商品名称")
    private String name;

    /**
     * 价格
     */
    @ApiModelProperty("价格")
    private BigDecimal price;

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

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

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

    /**
     * 制造商
     */
    @ApiModelProperty("制造商")
    private String manufacturer;

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

    /**
     * 查询条件
     * 
     * @return
     */
    @Override
    public Wrapper condition() {
        LambdaQueryWrapper<ShoppingEntity> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            query.like(ShoppingEntity::getName, name);
        }
        if (StringUtils.isNotBlank(manufacturer)) {
            query.like(ShoppingEntity::getManufacturer, manufacturer);
        }
        if (null != brandId) {
            query.eq(ShoppingEntity::getBrandId, brandId);
        }
        if (null != statuz) {
            query.eq(ShoppingEntity::getStatuz, statuz);
        }
        if (null != hotFlag) {
            query.eq(ShoppingEntity::getHotFlag, hotFlag);
        }
        if (APP.equals(platform)) {
            query.eq(ShoppingEntity::getStatuz, 1);
            // 先按按置顶排序，再按浏览量
            query.orderByDesc(ShoppingEntity::getHotFlag)
                    .orderByDesc(ShoppingEntity::getBrowseNum)
                    .orderByDesc(ShoppingEntity::getCreateTime);
        } else {
            // 按创建时间倒序
            query.orderByDesc(ShoppingEntity::getCreateTime);
        }
        // if (null != getSort()) {
        // if (getSort().getOrder().indexOf("asc") != -1 || getSort().getOrder().indexOf("ASC") != -1) {
        // query.orderByAsc(ShoppingEntity::getCreateTime);
        // } else {
        // query.orderByDesc(ShoppingEntity::getCreateTime);
        // }
        // }
        return query;
    }

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

}
