<?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="cn.iocoder.yudao.module.bpm.dal.mysql.oa.BpmOAReceiptMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="selectStatistics" resultType="cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.ReceiptStatisticsVO">
        SELECT
            COALESCE ( SUM( CASE WHEN DATE( a.create_time ) = CURDATE() THEN a.money END ), 0 ) AS todayAddMoney,
            COALESCE ( SUM( CASE WHEN DATE( a.create_time ) = CURDATE() - INTERVAL 1 DAY THEN a.money END ), 0 ) AS yesterdayAddMoney
        FROM
            bpm_oa_receipt a
            <if test="relation == 'sub'">
                INNER JOIN (
                    SELECT DISTINCT
                        u.id
                    FROM
                        system_users u
                    INNER JOIN system_dept d ON d.leader_user_id = #{userId}
                    INNER JOIN system_dept d1 ON d1.flag LIKE CONCAT( '%', d.id, '-' )
                    OR d1.flag LIKE CONCAT( '%-', d.id, '-%' )
                    OR d1.flag LIKE CONCAT( '-', d.id, '%' )
                    WHERE
                        u.dept_id = d1.id
                        AND u.STATUS = 0
                ) subordinate ON subordinate.id = a.user_id
            </if>
        WHERE
            a.result = 2
            <if test="relation == 'my'">
                AND a.user_id = #{userId}
            </if>
    </select>

    <select id="selectReceiptStatisticsListByUserIds" resultType="cn.iocoder.yudao.module.bpm.api.oa.vo.receipt.ReceiptStatisticsDTO">
        SELECT
            user_id AS userId,
            SUM(money) AS money
        FROM
            bpm_oa_receipt
        WHERE
            deleted = 0
            AND result = 2
            AND user_id IN
            <foreach item="userId" collection="userIds" separator="," open="(" close=")" index="">
                #{userId}
            </foreach>
            <if test="createTime != null and createTime.length > 0">
                <if test="createTime[0] != null">
                    and create_time &gt;= #{createTime[0]}
                </if>
                <if test="createTime[1] != null">
                    and create_time &lt;= #{createTime[1]}
                </if>
            </if>
        GROUP BY user_id
    </select>
    <select id="getReceiptSettlement"
            resultType="cn.iocoder.yudao.module.bpm.api.oa.vo.receipt.ReceiptSettlementDTO">
        SELECT
            a.user_id AS userId,
            SUM( a.money ) AS money,
            SUM( CASE WHEN b.is_channel = 1 THEN a.money ELSE 0 END ) AS channelAmount
        FROM
            bpm_oa_receipt a
            INNER JOIN bpm_oa_contract c on a.contract_id = c.id
            INNER JOIN crm_business b on b.id = c.business_id
        WHERE
            deleted = 0
            AND result = 2
            <if test="vo.createTime != null and vo.createTime.length > 0">
                <if test="vo.createTime[0] != null">
                    and create_time &gt;= #{vo.createTime[0]}
                </if>
                <if test="vo.createTime[1] != null">
                    and create_time &lt;= #{vo.createTime[1]}
                </if>
            </if>
        GROUP BY user_id
    </select>
</mapper>
