package com.sky.ioc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.ioc.entity.domain.parking.Park;
import com.sky.ioc.entity.params.IocParam;
import com.sky.ioc.entity.params.IocTimeRange;
import com.sky.ioc.entity.params.scene.ParkParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface ParkMapper extends BaseMapper<Park> {


    @Select("<script>" +
            "SELECT count(1) FROM park where 1=1 " +
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</if>"+
            "<if test='iocParam!=null and iocParam.timeRange != null '>" +
            " <![CDATA[ and entry_time > #{iocParam.timeRange.startDate} and entry_time < #{iocParam.timeRange.endDate} ]]> " +
            "</if>"+
            "</script>")
    Integer getTotalParkByCompanyIdAndDeptIdAndEntryTime(@Param("iocParam") IocParam iocParam);

    @Select("<script>" +
            "SELECT count(1) FROM park where 1=1 " +
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</if>"+
            "<if test='iocParam!=null '>" +
            " <![CDATA[ and departure_time > #{iocParam.timeRange.startDate} and departure_time < #{iocParam.timeRange.endDate} ]]>" +
            "</if>"+
            "</script>")
    Integer getTotalParkByCompanyIdAndDeptIdAndDepartureTime(@Param("iocParam") IocParam iocParam);

    @Select("<script>" +
            "SELECT licence_plate,entry_time,departure_time  from park" +
            " where 1=1 " +
            "<if test='parkParam!=null and parkParam.startTime!=null'>" +
            " and entry_time BETWEEN  #{parkParam.startTime} AND #{parkParam.endTime}" +
            "</if>" +
            "<if test='parkParam!=null and parkParam.carCode!=\"\" and parkParam.carCode!=null'>" +
            " and licence_plate = #{parkParam.carCode} " +
            "</if>" +
            " ORDER BY id " +
            "limit #{parkParam.pageSize} OFFSET  #{parkParam.pageStart}" +
            "</script>")
    List<Map<String,Object>> pageList(@Param("parkParam")ParkParam parkParam);

    @Select("<script>" +
            "SELECT count(*)  from park" +
            " where 1=1 " +
            "<if test='parkParam!=null and parkParam.startTime!=null'>" +
            " and entry_time BETWEEN  #{parkParam.startTime} AND #{parkParam.endTime}" +
            "</if>" +
            "<if test='parkParam!=null and parkParam.carCode!=\"\" and parkParam.carCode!=null'>" +
            " and licence_plate = #{parkParam.carCode} " +
            "</if>" +
            "</script>")
    Long pageCount(@Param("parkParam")ParkParam parkParam);


    @Select("<script>" +
            "SELECT parking_numbers,sum(hours) as hours FROM park where 1=1 " +
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</if>"+
            "<if test='iocParam!=null '>" +
            " <![CDATA[ and departure_time > #{iocParam.timeRange.startDate} and departure_time < #{iocParam.timeRange.endDate} ]]>" +
            "</if>"+
            " GROUP BY parking_numbers ORDER BY hours" +
            "</script>")
    List<Park> getVacantList(@Param("iocParam") IocParam iocParam);


    @Select("<script>" +
            "SELECT count(DISTINCT parking_numbers)  from park" +
            " where 1=1 " +
            "<if test='iocParam!=null and iocParam.companyId != null '>" +
            " and company_id=#{iocParam.companyId} " +
            "</if>"+
            "<if test='iocParam!=null '>" +
            " <![CDATA[ and departure_time > #{iocParam.timeRange.startDate} and departure_time < #{iocParam.timeRange.endDate} ]]>" +
            "</if>"+
            "</script>")
    Long pageUsedCount(@Param("iocParam") IocParam iocParam);



}
