<?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.mcwl.communityCenter.mapper.IncomeInfoMapper">


    <select id="communityIncome" resultType="com.mcwl.communityCenter.domain.vo.IncomeAmountVo">
        SELECT SUM(IF(DATE(create_time) = CURDATE(), amount, 0))                  AS today_income,
               SUM(IF(DATE(create_time) = CURDATE() - INTERVAL 1 DAY, amount, 0)) AS yesterday_income
        FROM cc_income_info
        WHERE tenant_id = #{userId}
          AND type = 0
          AND del_flag = '0'
    </select>

    <select id="questionIncome" resultType="com.mcwl.communityCenter.domain.vo.IncomeAmountVo">
        SELECT SUM(IF(DATE(create_time) = CURDATE(), amount, 0))                  AS today_income,
               SUM(IF(DATE(create_time) = CURDATE() - INTERVAL 1 DAY, amount, 0)) AS yesterday_income
        FROM cc_income_info
        WHERE tenant_id = #{userId}
          AND type = 1
          AND del_flag = '0'
    </select>
    <select id="totalIncome" resultType="java.lang.Double">
        SELECT SUM(amount)
        FROM cc_income_info
        WHERE del_flag = '0'
    </select>
    <select id="incomeList" resultType="com.mcwl.communityCenter.domain.vo.IncomeInfoListVo">
        SELECT u.nick_name as user_name,
        u.avatar as avatar,
        c.community_name as community_name,
        ii.type as type,
        ii.amount as amount,
        ii.create_time as create_time
        FROM cc_income_info ii
        left join cc_community c
        on ii.tenant_id = c.tenant_id
        and ii.community_id = c.id
        left join sys_user u on c.tenant_id = u.user_id
        where ii.tenant_id = #{userId}
        <if test="incomeInfoListPageRes.type != null ">
            and ii.type = #{incomeInfoListPageRes.type}
        </if>
        <if test="incomeInfoListPageRes.searchContent != null and incomeInfoListPageRes.searchContent != ''">
            and (u.nick_name like concat('%', #{incomeInfoListPageRes.searchContent}, '%')
            or c.community_name like concat('%', #{incomeInfoListPageRes.searchContent}, '%'))
        </if>
        and ii.del_flag = '0'
        order by ii.create_time desc
    </select>
</mapper>
