package com.sky.ioc.mapper.carbon;

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

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

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

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

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

    @Select("<script> " +
            "select coalesce(sum(minus_discharge),0) as total,dept_id,department.name  from photovoltaic " +
            "LEFT JOIN department on photovoltaic.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>> getMinusDischargeOrderByTime(@Param("iocParam")IocParam iocParam);
}
