package com.sky.ioc.mapper.carbon;

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

    @Select("<script> " +
            "select coalesce(sum(discharge),0) from print_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(paper),0) from print_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 getPaperByTime(@Param("iocParam")IocParam iocParam);

    @Select("<script> " +
            "select coalesce(sum(inkbox),0) from print_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 getInkboxByTime(@Param("iocParam")IocParam iocParam);

    @Select("<script> " +
            "select coalesce(sum(discharge),0) as total,dept_id,department.name  from print_carbon " +
            "LEFT JOIN department on print_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);


    @Select("<script> " +
            "select coalesce(count(DISTINCT user_id),0) from print_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>")
    Integer getUserByTime(@Param("iocParam")IocParam iocParam);
}
