<?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.BrokerageMarketerRatioMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.BrokerageMarketerRatio">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="item_cate" property="itemCate" />
        <result column="item_source" property="itemSource" />
        <result column="ratio" property="ratio" />
        <result column="remark" property="remark" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        item_cate, item_source, ratio, remark
    </sql>

    <select id="xPage" resultType="com.dayou.vo.MarketerRatioVO" parameterType="com.dayou.entity.BrokerageMarketerRatio">
        SELECT
            *
        FROM
            (
                SELECT
                    id AS itemCate,
                    NAME AS itemCateName
                FROM
                    dict_data
                WHERE
                    dict_type_id = ( SELECT id FROM dict_type WHERE NAME = '项目类型' )
                  AND deleted = 0
            ) cate
                JOIN (
                SELECT
                    id AS itemSource,
                    NAME AS itemSourceName
                FROM
                    dict_data
                WHERE
                    dict_type_id = ( SELECT id FROM dict_type WHERE NAME = '业务来源' )
                  AND deleted = 0
            ) source
                LEFT JOIN brokerage_marketer_ratio br ON br.item_cate = itemCate
                AND br.item_source = itemSource
            <where>
                <if test="ratio!=null and ratio.itemCate!=null">
                    and itemCate = #{ratio.itemCate}
                </if>
                <if test="ratio!=null and ratio.itemSource!=null">
                    and itemSource = #{ratio.itemSource}
                </if>
            </where>
        order by br.id desc
    </select>
</mapper>
