<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.advertising.dao.impl.AdvertisingSettlementMethodDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingSettlementMethod">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="master_id" property="masterId" jdbcType="BIGINT"/>
        <result column="ad_id" property="adId" jdbcType="BIGINT"/>
        <result column="settlement_method" property="settlementMethod" jdbcType="VARCHAR"/>
        <result column="price_each_time" property="priceEachTime" jdbcType="DECIMAL"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="AdvertisingSettlementMethodDTO" type="com.pcloud.book.advertising.dto.AdvertisingSettlementMethodDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="master_id" property="masterId" jdbcType="BIGINT"/>
        <result column="ad_id" property="adId" jdbcType="BIGINT"/>
        <result column="settlement_method" property="settlementMethod" jdbcType="VARCHAR"/>
        <result column="price_each_time" property="priceEachTime" jdbcType="DECIMAL"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, master_id, ad_id, settlement_method, price_each_time, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_settlement_method
        where id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="java.lang.Long">
        delete from advertising_settlement_method
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <delete id="deleteByMasterId" parameterType="java.lang.Long">
        delete from advertising_settlement_method
        where master_id = #{masterId,jdbcType=BIGINT}
        and ad_id is null
    </delete>

    <select id="getByMasterId" parameterType="java.lang.Long" resultMap="AdvertisingSettlementMethodDTO">
        select
        <include refid="Base_Column_List"/>
        from advertising_settlement_method
        where master_id = #{masterId,jdbcType=BIGINT}
        and ad_id is null
    </select>

    <select id="getByMasterIdAndAdId" parameterType="map" resultMap="AdvertisingSettlementMethodDTO">
        select
        <include refid="Base_Column_List"/>
        from advertising_settlement_method
        where master_id = #{masterId,jdbcType=BIGINT}
        and ad_id = #{adId,jdbcType=BIGINT}
        order by create_time desc
        limit 1
    </select>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingSettlementMethod"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_settlement_method
        <trim prefix="(" suffix=")" suffixOverrides=",">
            master_id,
            ad_id,
            settlement_method,
            price_each_time,
            create_time,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{masterId,jdbcType=BIGINT},
            #{adId,jdbcType=BIGINT},
            #{settlementMethod,jdbcType=VARCHAR},
            #{priceEachTime,jdbcType=DECIMAL},
            NOW(),
            NOW(),
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.advertising.entity.AdvertisingSettlementMethod"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_settlement_method (
            master_id,
            ad_id,
            settlement_method,
            price_each_time,
            create_time,
            update_time
        ) values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.masterId,jdbcType=BIGINT},
            #{item.adId,jdbcType=BIGINT},
            #{item.settlementMethod,jdbcType=VARCHAR},
            #{item.priceEachTime,jdbcType=DECIMAL},
            NOW(),
            NOW())
        </foreach>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingSettlementMethod">
        update advertising_settlement_method
        <set>
            <if test="masterId != null">
                master_id = #{masterId,jdbcType=BIGINT},
            </if>
            <if test="adId != null">
                ad_id = #{adId,jdbcType=BIGINT},
            </if>
            <if test="settlementMethod != null">
                settlement_method = #{settlementMethod,jdbcType=VARCHAR},
            </if>
            <if test="priceEachTime != null">
                price_each_time = #{priceEachTime,jdbcType=DECIMAL},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
</mapper>