<?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.crmcontract.CrmContractMapper">

    <!--
        一般情况下，尽可能使用 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.crmcontract.vo.CrmContractRespVO">
        select a.*,
        t3.name as customerName,
        t1.nickname as orderAdminName,
        t2.nickname as creatorName
        from crm_contract as a
        left join crm_customer as t3 on t3.id = a.customer_id
        left join system_users as t1 on a.order_admin_id = t1.id
        left join system_users as t2 on a.creator = t2.id
        <where>
            a.deleted = 0
            and t1.deleted = 0
            and t2.deleted = 0
            and t3.deleted = 0
            <if test="dto.customerName != null and dto.customerName != ''">
                and t3.name like concat('%', #{dto.customerName}, '%')
            </if>
            <if test="dto.number != null and dto.number != ''">
                and a.number like concat('%', #{dto.number}, '%')
            </if>
            <if test="dto.name != null and dto.name != ''">
                and a.name like concat('%', #{dto.name}, '%')
            </if>
            <if test="dto.customerId != null">
                and a.customer_id = #{dto.customerId}
            </if>
            <if test="dto.checkStatus != null">
                and a.check_status = #{dto.checkStatus}
            </if>
            <if test="dto.expireHandle != null">
                and a.expire_handle = #{dto.expireHandle}
            </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.orderAdminName != null and dto.orderAdminName != ''">
                and t1.nickname like concat('%', #{dto.orderAdminName}, '%')
            </if>
            <if test="dto.creatorName != null and dto.creatorName != ''">
                and t2.nickname like concat('%', #{dto.creatorName}, '%')
            </if>
            <if test="dto.getIsCheck() != null">
                AND FIND_IN_SET ('${dto.loginAdminId}',a.flow_admin_id)
            </if>
        </where>
    </select>

    <select id="selectContractTop" resultType="cn.iocoder.yudao.module.crm.controller.admin.crmanalysis.vo.ContractVO">
        SELECT
            t.user_id AS id,
            COUNT( t.id ) AS count,
	        SUM( t.contract_money ) AS money,
	        u.nickname AS nickname
        FROM
            bpm_oa_contract t
            LEFT JOIN system_users u ON ( u.id = t.user_id )
            AND u.deleted = 0
        WHERE
            t.deleted = 0
            AND t.result = 2
            AND t.contract_type = 1
        <if test="vo.times != null and vo.times.length > 0">
            <if test="vo.times[0] != null">
                and t.signing_date &gt;= #{vo.times[0]}
            </if>
            <if test="vo.times[1] != null">
                and t.signing_date &lt;= #{vo.times[1]}
            </if>
        </if>
        GROUP BY
            t.user_id
        ORDER BY
            money DESC
            LIMIT 10
    </select>
    <select id="getProductCount"
            resultType="cn.iocoder.yudao.module.product.api.storeproductattrvalue.vo.UserProductCountVO">
        select
        a.owner_user_id as userId,
        b.nums AS nums,
        b.product_id as productId,
        b.name as name,
        b.product_attr_unique as productAttrUnique,
        b.price as price,
        b.discount as discount,
        b.subtotal as subtotal,
        b.remarks as remarks
        from crm_contract as a
        left join crm_contract_product as b on a.id = b.contract_id
        <where>
            a.deleted = 0
            and b.deleted = 0
            <if test="userIds != null and userIds.size() > 0">
                AND a.owner_user_id in
                <foreach collection="userIds" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="createTime != null and createTime.length > 0">
                <if test="createTime[0] != null">
                    and a.create_time &gt;= #{createTime[0]}
                </if>
                <if test="createTime[1] != null">
                    and a.create_time &lt;= #{createTime[1]}
                </if>
            </if>
        </where>
    </select>
    <select id="getContractProductSettlement"
            resultType="cn.iocoder.yudao.module.hrm.api.crmcontract.dto.CrmContractProductSettlementDTO">
        SELECT
            b.user_id AS userId,
            b.id AS contractId,
            a.product_id AS productId,
            a.price AS price,
            a.nums AS nums,
            CASE WHEN bu.is_channel = 1 THEN a.nums ELSE 0 END AS channelNums,
            a.discount AS discount,
            a.subtotal AS subtotal
        FROM
            crm_contract_product AS a
            LEFT JOIN bpm_oa_contract AS b ON a.contract_id = b.id
            LEFT JOIN crm_business bu ON bu.id = b.business_id
        WHERE
            a.deleted = 0
            and b.deleted = 0
            and b.result = 2
            and b.return_money &gt; 0
            and b.settlement_flag = 0
    </select>
</mapper>
