<?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.qnfhq.modules.c2c.dao.C2cAdDao">

    <resultMap type="com.qnfhq.modules.c2c.entity.C2cAdEntity" id="c2cAdMap">
        <result property="id" column="id"/>
        <result property="adNo" column="ad_no"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="direction" column="direction"/>
        <result property="legalCoin" column="legal_coin"/>
        <result property="symbol" column="symbol"/>
        <result property="num" column="num"/>
        <result property="availableNum" column="available_num"/>
        <result property="priceType" column="price_type"/>
        <result property="price" column="price"/>
        <result property="minAmount" column="min_amount"/>
        <result property="maxAmount" column="max_amount"/>
        <result property="payway" column="payway"/>
        <result property="remark" column="remark"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="reason" column="reason"/>
        <result property="version" column="version"/>
        <result property="tag" column="tag"/>

    </resultMap>

    <update id="updateC2cAdAvailableNumById" parameterType="java.util.Map">
        update c2c_ad set
            available_num = available_num-#{symbolNum},version=version+1,
            <if test="maxAmount != null ">max_amount=price*available_num,</if>
            <if test="status != null ">status=#{status},</if>
            update_time = now()
            where id = #{id} and version=#{version} and available_num>=#{symbolNum}
    </update>

    <update id="addC2cAdAvailableNumById" parameterType="java.util.Map">
        update c2c_ad set
        available_num = available_num+#{symbolNum},version=version+1,
        status=1,
        <if test="maxAmount != null ">max_amount=price*available_num,</if>
        update_time = now()
        where id = #{id} and version=#{version}
    </update>

</mapper>