package com.sky.ioc.mapper.operation;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.ioc.entity.domain.operation.Investment;
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;

@Mapper
public interface InvestmentMapper extends BaseMapper<Investment> {

    @Select("<script> " +
            "SELECT sum(money) as money,field from investment where 1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and start_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 field ORDER BY money desc  limit 1" +
            "</script>")
    Investment  getTopOne(@Param("iocParam")IocParam iocParam);
    @Select("<script> " +
            "SELECT  name,sum(money) as money from investment   where 1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and start_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 name </script>")
    List<Investment> listByTime(@Param("iocParam")IocParam iocParam);


    @Select("<script> " +
            "SELECT COALESCE(avg(rate_of_return),0) from investment  where 1=1 " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and start_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 avgReturnByTime(@Param("iocParam")IocParam iocParam);

    @Select("<script> " +
            "SELECT  name,sum(money) as money from investment   where name = #{name} " +
            "<if test='iocParam!=null and iocParam.timeRange!=null '>" +
            "and start_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 name </script>")
    Investment getByTimeAndName(@Param("iocParam")IocParam iocParam,@Param("name") String name);
}
