package com.sky.ioc.mapper.carbon;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.ioc.entity.domain.carbon.CarCarbon;
import com.sky.ioc.entity.params.IocParam;
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 CarCarbonMapper extends BaseMapper<CarCarbon> {

    @Select("<script> " +
            "select coalesce(sum(discharge),0) from car_carbon where  1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and time between #{iocParam.timeRange.startDate} and #{iocParam.timeRange.endDate} " +
            "</if>" +
            "<if test='iocParam!=null and iocParam.deptId!=null '>" +
            "and dept_id = #{iocParam.deptId} " +
            "</if>" +
            "</script>")
    Double getDischargeByTime(@Param("iocParam")IocParam iocParam);

    @Select("<script> " +
            "select coalesce(sum(mileage),0) from car_carbon where  1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and time between #{iocParam.timeRange.startDate} and #{iocParam.timeRange.endDate} " +
            "</if>" +
            "<if test='iocParam!=null and iocParam.deptId!=null '>" +
            "and dept_id = #{iocParam.deptId} " +
            "</if>" +
            "</script>")
    long getMileageByTime(@Param("iocParam")IocParam iocParam);

    @Select("<script> " +
            "select coalesce(sum(hours),0) from car_carbon where  1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and time between #{iocParam.timeRange.startDate} and #{iocParam.timeRange.endDate} " +
            "</if>" +
            "<if test='iocParam!=null and iocParam.deptId!=null '>" +
            "and dept_id = #{iocParam.deptId} " +
            "</if>" +
            "</script>")
    long getHoursByTime(@Param("iocParam")IocParam iocParam);

    @Select("<script> " +
            "select coalesce(sum(discharge),0) as total,dept_id,department.name  from car_carbon " +
            "LEFT JOIN department on car_carbon.dept_id= department.id " +
            "where  1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and time between #{iocParam.timeRange.startDate} and #{iocParam.timeRange.endDate} " +
            "</if>" +
            "<if test='iocParam!=null and iocParam.deptId!=null '>" +
            "and dept_id = #{iocParam.deptId} " +
            "</if>" +
            " GROUP BY dept_id,department.name ORDER BY total desc </script>")
    List<Map<String,Object>> getDischargeOrderByTime(@Param("iocParam")IocParam iocParam);

}
