<?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.ym.mec.biz.dal.dao.TenantProxyDividendDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.TenantProxyDividend">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="proxy_id_" jdbcType="INTEGER" property="proxyId"/>
        <result column="tenant_id_" jdbcType="INTEGER" property="tenantId"/>
        <result column="total_month_" jdbcType="INTEGER" property="totalMonth"/>
        <result column="active_time_" jdbcType="TIMESTAMP" property="activeTime"/>
        <result column="order_id_" jdbcType="INTEGER" property="orderId"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , proxy_id_, tenant_id_, total_month_, active_time_, order_id_
    </sql>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.TenantProxyDividend">
        insert into tenant_proxy_dividend(proxy_id_, tenant_id_, total_month_, active_time_, order_id_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.proxyId}, #{entity.tenantId}, #{entity.totalMonth}, #{entity.activeTime}, #{entity.orderId})
        </foreach>
    </insert>

    <select id="queryProxyDividendInfo"  parameterType="map" resultType="com.ym.mec.biz.dal.vo.ProxyDividendInfoVo">
        select a.proxy_id_ as proxyId,
        b.proxy_name_ as proxyName,
        c.id_ as tenantId,
        c.name_ as tenantName,
        a.active_time_ as activeDate,
        a.total_month_ as activeCount,
        d.real_name_ as recommenderName
        from tenant_proxy_dividend as a
        left join tenant_proxy_info as b on a.proxy_id_ = b.id_
        left join tenant_info as c on a.tenant_id_ = c.id_
        left join sys_user as d on c.recommender_ = d.id_
        <where>
            <if test="param.proxyId != null">
               AND a.proxy_id_ = #{param.proxyId}
            </if>
            <if test="param.recommenderId != null">
                AND c.recommender_ = #{param.recommenderId}
            </if>
            <if test="param.startData != null and param.startData != ''">
                AND a.active_time_ <![CDATA[ >= ]]> #{param.startData}
            </if>
            <if test="param.endData != null and param.endData != ''">
                AND a.active_time_ <![CDATA[ <= ]]> #{param.endData}
            </if>
        </where>
    </select>

    <select id="queryProxyDividendCount" parameterType="map" resultType="int">
        select IFNULL(SUM(a.total_month_), 0)  as activeCount
        from tenant_proxy_dividend as a
        left join tenant_proxy_info as b on a.proxy_id_ = b.id_
        left join tenant_info as c on a.tenant_id_ = c.id_
        left join sys_user as d on c.recommender_ = d.id_
        <where>
            <if test="param.proxyId != null">
                AND a.proxy_id_ = #{param.proxyId}
            </if>
        </where>
    </select>

</mapper>
