<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cf.imes.module.system.dal.mysql.incomeandexpense.IncomeExpenseDetailsMapper">
    <sql id="dateFormat">
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(create_time), '-', QUARTER(create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(create_time), '-', MONTH(create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(create_time), '-', MONTH(create_time), '-', WEEK(create_time, 1)) AS date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(create_time), '-', MONTH(create_time), '-', DAY(create_time)) as date
        </if>
    </sql>

    <select id="selectOrganIncomeExpenseTrendGroupByCreateTime"
            resultType="com.cf.imes.module.system.controller.admin.funds.organamount.vo.OrganAmountGroupByCreateTimeRespVO">
        select COALESCE(sum(amount) ,0) as amount
        <include refid="dateFormat"/>
        from income_expense_details
        where create_time between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        and deleted = false
        <if test="organId != null">
            and organ_id = #{organId}
        </if>
        GROUP BY date
    </select>

    <select id="selectTotalSalesTrendGroupByCreateTime"
            resultType="com.cf.imes.module.system.dal.dataobject.funds.statistics.FundsTradeTypeGroupStatisticsDO">
        select COALESCE(sum(amount) ,0) as amount
        <include refid="dateFormat"/>
        from income_expense_details
        where create_time between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        and deleted = false
        GROUP BY date
    </select>
</mapper>