<?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.crm.dal.mysql.crmcontractreceivables.CrmContractReceivablesMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.yixiang.co/MyBatis/x-plugins/
     -->

    <select id="selectPageList"
            resultType="cn.iocoder.yudao.module.crm.controller.admin.crmcontractreceivables.vo.CrmContractReceivablesRespVO">
        select
        a.*,
        b.name as customerName,
        c.name as contractName,
        c.number as contractNumber,
        d.nickname as creatorName
        from crm_contract_receivables as a
        left join crm_customer as b on a.customer_id = b.id
        left join crm_contract as c on a.contract_id = c.id
        left join system_users as d on d.creator = d.id
        <where>
            a.deleted = 0
            <if test="dto.customerName != null and dto.customerName != ''">
                and b.name like concat('%', #{dto.customerName}, '%')
            </if>
            <if test="dto.customerId != null">
                and a.customer_id = #{dto.customerId}
            </if>
            <if test="dto.contractName != null and dto.contractName != ''">
                and c.name = #{dto.contractName}
            </if>
            <if test="dto.checkStatus != null">
                and a.check_status = #{dto.checkStatus}
            </if>
            <if test="dto.number != null and dto.number != ''">
                and a.number like concat('%', #{dto.number}, '%')
            </if>
            <if test="dto.contractId != null">
                and a.contract_id = #{dto.contractId}
            </if>
            <if test="ids != null and ids.size() > 0">
                and a.owner_user_id in
                <foreach item="ownerUserId" collection="ids" separator="," open="(" close=")" index="">
                    #{ownerUserId}
                </foreach>
            </if>
            <if test="dto.creatorName != null and dto.creatorName != ''">
                and d.nickname like concat('%', #{dto.creatorName}, '%')
            </if>
            <if test="dto.getIsCheck()">
                AND FIND_IN_SET ('${dto.loginAdminId}',a.flow_admin_id)
            </if>
        </where>
    </select>

    <select id="selectReceivablesTop" resultType="cn.iocoder.yudao.module.crm.controller.admin.crmanalysis.vo.ContractVO">
        SELECT
            t.user_id AS id,
            COUNT( t.id ) AS count,
	        SUM( t.money ) AS money,
	        u.nickname AS nickname
        FROM
            bpm_oa_receipt t
            LEFT JOIN system_users u ON ( u.id = t.user_id )
            AND u.deleted = 0
        WHERE
            t.deleted = 0
          AND t.result = 2
            <if test="vo.times != null and vo.times.length > 0">
                <if test="vo.times[0] != null">
                    and t.return_time &gt;= #{vo.times[0]}
                </if>
                <if test="vo.times[1] != null">
                    and t.return_time &lt;= #{vo.times[1]}
                </if>
            </if>
        GROUP BY
            t.user_id
        ORDER BY
            money DESC
            LIMIT 10
    </select>
</mapper>
