<?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.xingxi.business.Carts.mapper.CartsMapper">
    <resultMap type="Carts" id="CartsResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="mercId"    column="mercId"    />
        <result property="prodId"    column="prodId"    />
        <result property="prodAttrId"    column="prodAttrId"    />
        <result property="quantity"    column="quantity"    />
        <result property="checkFlag"    column="checkFlag"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectCarts">
        select id, user_id, mercId, prodId, prodAttrId, quantity, checkFlag, delFlag, createUser, createTime, updateUser, updateTime from t_carts
    </sql>

    <sql id="selectCartsExt">
        select
            ${tableAlias}.id,
            ${tableAlias}.user_id,
            ${tableAlias}.mercId,
            ${tableAlias}.prodId,
            ${tableAlias}.prodAttrId,
            ${tableAlias}.quantity,
            ${tableAlias}.checkFlag,
            ${tableAlias}.delFlag,
            ${tableAlias}.createUser,
            ${tableAlias}.createTime,
            ${tableAlias}.updateUser,
            ${tableAlias}.updateTime
        from t_carts ${tableAlias}
    </sql>

    <select id="selectCartsList" parameterType="Carts" resultMap="CartsResult">
        <include refid="selectCarts"/>
        <where>  
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="mercId != null "> and mercId = #{mercId}</if>
            <if test="prodId != null "> and prodId = #{prodId}</if>
            <if test="prodAttrId != null "> and prodAttrId = #{prodAttrId}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="checkFlag != null  and checkFlag != ''"> and checkFlag = #{checkFlag}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </select>
    
    <select id="selectCartsById" parameterType="Long" resultMap="CartsResult">
        <include refid="selectCarts"/>
        where id = #{id}
    </select>
        
    <insert id="insertCarts" parameterType="Carts" useGeneratedKeys="true" keyProperty="id">
        insert into t_carts
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="mercId != null">mercId,</if>
            <if test="prodId != null">prodId,</if>
            <if test="prodAttrId != null">prodAttrId,</if>
            <if test="quantity != null">quantity,</if>
            <if test="checkFlag != null">checkFlag,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateUser != null">updateUser,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="mercId != null">#{mercId},</if>
            <if test="prodId != null">#{prodId},</if>
            <if test="prodAttrId != null">#{prodAttrId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="checkFlag != null">#{checkFlag},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertCarts">
        insert into t_carts( user_id, mercId, prodId, prodAttrId, quantity, checkFlag, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.userId}, #{item.mercId}, #{item.prodId}, #{item.prodAttrId}, #{item.quantity}, #{item.checkFlag}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateCarts" parameterType="Carts">
        update t_carts
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="mercId != null">mercId = #{mercId},</if>
            <if test="prodId != null">prodId = #{prodId},</if>
            <if test="prodAttrId != null">prodAttrId = #{prodAttrId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="checkFlag != null">checkFlag = #{checkFlag},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCartsById" parameterType="Long">
        delete from t_carts where id = #{id}
    </delete>

    <delete id="deleteCartsByIds" parameterType="String">
        delete from t_carts where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="logicDeleteCartsById" parameterType="Long">
        update t_carts
            set delFlag = '1'
        where id = #{id}
    </update>

    <update id="logicDeleteCartsByIds" parameterType="String">
        update t_carts
        set delFlag = '1'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="logicDeleteCartsByCondition" parameterType="Carts">
        update t_carts
        set delFlag = '1'
        <where>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="mercId != null "> and mercId = #{mercId}</if>
            <if test="prodId != null "> and prodId = #{prodId}</if>
            <if test="prodAttrId != null "> and prodAttrId = #{prodAttrId}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="checkFlag != null  and checkFlag != ''"> and checkFlag = #{checkFlag}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </update>
</mapper>