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

import java.util.Date;

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 cn.com.ty.lift.ud.common.BasePageQuery;
import cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity;
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 UserBillQuery extends BasePageQuery {

    /**
     * 用户
     */
    @ApiModelProperty("用户")
    private Long userId;

    /**
     * 交易类型
     */
    @ApiModelProperty("交易类型")
    private Integer type;

    @ApiModelProperty("起始时间")
    private Date starDate;

    @ApiModelProperty("结束时间")
    private Date endDate;
    
    /**
     * 流水号
     */
    @ApiModelProperty("流水号")
    private String flowNum;

    @ApiModelProperty("根据年月查询")
    private Integer currentYear;
    @ApiModelProperty("根据年月查询")
    private Integer currentMonth;
    /**
     * 查询条件
     * 
     * @return
     */
    @Override
    public Wrapper condition() {
        QueryWrapper<UserBillEntity> query = new QueryWrapper<>();
        if (null != type) {
            query.eq("t.type", type);
        }
        if (null != userId) {
            query.eq("t.user_id", userId);
        }
        if (StringUtils.isNotBlank(flowNum)) {
            query.like("t.flow_num", flowNum);
        }
        if (null != starDate && null != endDate) {
            query.between("t.create_time", starDate, endDate);
        }
        if (null != currentYear) {
            query.eq("year(t.create_time)", currentYear);
        }
        if (null != currentMonth) {
            query.eq("month(t.create_time)", currentMonth);
        }
        query.eq("1", 1);
        //按创建时间倒序
        query.orderByDesc("t.create_time");
        if (null != getSort()) {
            if (getSort().getOrder().indexOf("asc") != -1 || getSort().getOrder().indexOf("ASC") != -1) {
                query.orderByAsc("t."+getSort().getOrderBy());
            } else {
                query.orderByDesc("t."+getSort().getOrderBy());
            }
        }
        
        
        return query;
    }

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

}
