package com.clx.performance.sqlProvider;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clx.performance.param.app.integral.DriverPageIntegralTruckRankParam;
import com.clx.performance.param.pc.PageIntegralTruckParam;
import com.clx.performance.param.pc.PageIntegralTruckRankParam;
import com.clx.performance.vo.pc.IntegralTruckVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

public class IntegralTruckSqlProvider {

    public String pageByParam(@Param("page") Page<IntegralTruckVO> page, @Param("param") PageIntegralTruckParam param) {
        return new SQL(){{

            SELECT(" id, truck_no, " +
                    " driver_name, driver_mobile, " +
                    " echelon, integral, rank, " +
                    " date_format(create_time, '%Y-%m-%d %H:%i:%s') as createTime, " +
                    " date_format(modified_time, '%Y-%m-%d %H:%i:%s') as modifiedTime ");

            FROM("integral_truck");

            if (StringUtils.isNotBlank(param.getTruckNo())) {WHERE("truck_no = #{param.truckNo}");}
            if (StringUtils.isNotBlank(param.getDriverMobile())) {WHERE("driver_mobile = #{param.driverMobile}");}
            if (!Objects.isNull(param.getEchelon())) {WHERE("echelon = #{param.echelon}");}

            if (StringUtils.isNotBlank(param.getBeginTime())) {WHERE("create_time >= #{param.beginTime}");}
            if (StringUtils.isNotBlank(param.getEndTime())) {WHERE("create_time <= #{param.endTime}");}

            ORDER_BY("create_time desc");
        }}.toString();
    }

    public String pageTruckRank(@Param("page") Page<IntegralTruckVO> page) {
        return new SQL(){{

            SELECT(" id, truck_no, " +
                    " driver_name, driver_mobile, " +
                    " echelon, integral, rank, " +
                    " date_format(create_time, '%Y-%m-%d %H:%i:%s') as createTime, " +
                    " date_format(modified_time, '%Y-%m-%d %H:%i:%s') as modifiedTime ");

            FROM("integral_truck");
            ORDER_BY("integral desc, create_time desc");
        }}.toString();
    }

    public String driverPageTruckRank(@Param("page") Page<IntegralTruckVO> page, @Param("param")DriverPageIntegralTruckRankParam param) {
        return new SQL(){{

            SELECT(" a.id, a.truck_no, " +
                    " a.driver_name, a.driver_mobile, " +
                    " a.echelon, b.integral, b.rank, " +
                    " date_format(a.create_time, '%Y-%m-%d %H:%i:%s') as createTime, " +
                    " date_format(a.modified_time, '%Y-%m-%d %H:%i:%s') as modifiedTime ");

            FROM("integral_truck a");
            LEFT_OUTER_JOIN("integral_statistics b on a.truck_no = b.truck_no and b.statistics_date = #{param.statisticsDate}");


            WHERE("a.truck_no in (\""+ StringUtils.join(param.getTruckNoList(), "\",\"") +"\") ");
            WHERE("a.create_time < #{param.time}");

            ORDER_BY("b.integral desc, a.create_time desc");
        }}.toString();
    }

}
