<?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.dayou.mapper.BrokeragePostRatioMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.BrokeragePostRatio">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="post_id" property="postId" />
        <result column="brokerage_ratio" property="brokerageRatio" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        post_id, brokerage_ratio
    </sql>

    <select id="getLeadersRatio" parameterType="java.util.Set" resultType="com.dayou.bo.LeaderRatioBO">
        SELECT
            up.user_id,
            up.post_id,
            p.NAME AS postName,
            bpr.brokerage_ratio AS ratio,
            bpr.brokerage_rule as brokerageRule
        FROM
            user_post up
                LEFT JOIN brokerage_post_ratio bpr ON bpr.post_id = up.post_id
                LEFT JOIN post p ON p.id = up.post_id
        WHERE
            up.deleted = 0
          AND bpr.deleted = 0
          AND p.deleted = 0
          and up.user_id != #{userId}
            and up.post_id in
         <foreach collection="parentPostIds" index="index" item="item" open="(" close=")" separator=",">
                #{item}
         </foreach>
    </select>

    <select id="page" resultType="com.dayou.vo.PostRatioVO">
        SELECT
            r.id,
            p.id AS postId,
            p.NAME AS postName,
            d.NAME AS departmentName,
            r.brokerage_ratio,
            r.brokerage_rule
        FROM
            post p
                LEFT JOIN department d ON p.department_id = d.id
                LEFT JOIN brokerage_post_ratio r ON r.post_id = p.id
        WHERE
            p.deleted = 0
          AND d.deleted = 0
        <if test="ratio!=null and ratio.departmentId!=null">
            and d.id = #{ratio.departmentId}
        </if>
        ORDER BY
            r.id DESC;
    </select>

    <select id="getLeaderRatioBO" parameterType="java.util.Set" resultType="com.dayou.bo.LeaderRatioBO">
        SELECT
            up.user_id,
            up.post_id,
            p.NAME AS postName,
            bpr.brokerage_ratio AS ratio,
            bpr.brokerage_rule AS brokerageRule
        FROM
            user_post up
                left join brokerage_post_ratio bpr on bpr.post_id = up.post_id
                left join post p on p.id = up.post_id
        where
            up.deleted = 0
          AND bpr.deleted = 0
          AND p.deleted = 0
        and up.user_id in (
        <foreach collection="leaderIds" index="index" item="item" separator=",">
            #{item}
        </foreach>
            )
    </select>

    <select id="marketManagerRatioBO" resultType="com.dayou.bo.LeaderRatioBO">
        SELECT
            up.user_id,
            up.post_id,
            p.NAME AS postName,
            bpr.brokerage_ratio AS ratio,
            bpr.brokerage_rule AS brokerageRule
        FROM
            user_post up
                left join brokerage_post_ratio bpr on bpr.post_id = up.post_id
                left join post p on p.id = up.post_id
        where
            up.deleted = 0
          AND bpr.deleted = 0
          AND p.deleted = 0

          and p.name = '市场部经理'
    </select>
</mapper>
