<?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.xinelu.manage.mapper.goodsAttributeDetails.GoodsAttributeDetailsMapper">

    <resultMap type="GoodsAttributeDetails" id="GoodsAttributeDetailsResult">
        <result property="id" column="id"/>
        <result property="goodsAttributeId" column="goods_attribute_id"/>
        <result property="attributeDetailsName" column="attribute_details_name"/>
        <result property="goodsStock" column="goods_stock"/>
        <result property="goodsPrice" column="goods_price"/>
        <result property="attributePitureUrl" column="attribute_piture_url"/>
        <result property="sort" column="sort"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="integralExchangeFlag" column="integral_exchange_flag"/>
        <result property="integralExchangeSill" column="integral_exchange_sill"/>
        <result property="integralExchangeCount" column="integral_exchange_count"/>
    </resultMap>

    <sql id="selectGoodsAttributeDetailsVo">
        select id,
               goods_attribute_id,
               attribute_details_name,
               goods_stock,
               goods_price,
               attribute_piture_url,
               sort,
               create_by,
               create_time,
               update_by,
               update_time,
               integral_exchange_flag,
               integral_exchange_sill,
               integral_exchange_count
        from goods_attribute_details
    </sql>

    <select id="selectGoodsAttributeDetailsList" parameterType="GoodsAttributeDetails"
            resultMap="GoodsAttributeDetailsResult">
        <include refid="selectGoodsAttributeDetailsVo"/>
        <where>
            <if test="goodsAttributeId != null ">
                and goods_attribute_id = #{goodsAttributeId}
            </if>
            <if test="attributeDetailsName != null  and attributeDetailsName != ''">
                and attribute_details_name like concat('%', #{attributeDetailsName}, '%')
            </if>
            <if test="goodsStock != null ">
                and goods_stock = #{goodsStock}
            </if>
            <if test="goodsPrice != null ">
                and goods_price = #{goodsPrice}
            </if>
            <if test="attributePitureUrl != null  and attributePitureUrl != ''">
                and attribute_piture_url = #{attributePitureUrl}
            </if>
            <if test="sort != null ">
                and sort = #{sort}
            </if>
            <if test="integralExchangeFlag != null ">
                and integral_exchange_flag = #{integralExchangeFlag}
            </if>
            <if test="integralExchangeSill != null ">
                and integral_exchange_sill = #{integralExchangeSill}
            </if>
            <if test="integralExchangeCount != null ">
                and integral_exchange_count = #{integralExchangeCount}
            </if>
        </where>
    </select>

    <select id="selectGoodsAttributeDetailsById" parameterType="Long"
            resultMap="GoodsAttributeDetailsResult">
        <include refid="selectGoodsAttributeDetailsVo"/>
        where id = #{id}
    </select>

    <insert id="insertGoodsAttributeDetails" parameterType="GoodsAttributeDetails" useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_attribute_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsAttributeId != null">goods_attribute_id,
            </if>
            <if test="attributeDetailsName != null">attribute_details_name,
            </if>
            <if test="goodsStock != null">goods_stock,
            </if>
            <if test="goodsPrice != null">goods_price,
            </if>
            <if test="attributePitureUrl != null">attribute_piture_url,
            </if>
            <if test="sort != null">sort,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="integralExchangeFlag != null">integral_exchange_flag,
            </if>
            <if test="integralExchangeSill != null">integral_exchange_sill,
            </if>
            <if test="integralExchangeCount != null">integral_exchange_count,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsAttributeId != null">#{goodsAttributeId},
            </if>
            <if test="attributeDetailsName != null">#{attributeDetailsName},
            </if>
            <if test="goodsStock != null">#{goodsStock},
            </if>
            <if test="goodsPrice != null">#{goodsPrice},
            </if>
            <if test="attributePitureUrl != null">#{attributePitureUrl},
            </if>
            <if test="sort != null">#{sort},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="integralExchangeFlag != null">#{integralExchangeFlag},
            </if>
            <if test="integralExchangeSill != null">#{integralExchangeSill},
            </if>
            <if test="integralExchangeCount != null">#{integralExchangeCount},
            </if>
        </trim>
    </insert>

    <update id="updateGoodsAttributeDetails" parameterType="GoodsAttributeDetails">
        update goods_attribute_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsAttributeId != null">goods_attribute_id =
                #{goodsAttributeId},
            </if>
            <if test="attributeDetailsName != null">attribute_details_name =
                #{attributeDetailsName},
            </if>
            <if test="goodsStock != null">goods_stock =
                #{goodsStock},
            </if>
            <if test="goodsPrice != null">goods_price =
                #{goodsPrice},
            </if>
            <if test="attributePitureUrl != null">attribute_piture_url =
                #{attributePitureUrl},
            </if>
            <if test="sort != null">sort =
                #{sort},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="integralExchangeFlag != null">integral_exchange_flag =
                #{integralExchangeFlag},
            </if>
            <if test="integralExchangeSill != null">integral_exchange_sill =
                #{integralExchangeSill},
            </if>
            <if test="integralExchangeCount != null">integral_exchange_count =
                #{integralExchangeCount},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGoodsAttributeDetailsById" parameterType="Long">
        delete
        from goods_attribute_details
        where id = #{id}
    </delete>

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


    <insert id="insertGoodsAttributeDetailsList" parameterType="java.util.List">
        insert into goods_attribute_details(
        goods_attribute_id,
        attribute_details_name,
        goods_stock,
        goods_price,
        attribute_piture_url,
        sort,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="goodAttributeDetailsLists" index="index" collection="goodAttributeDetailsLists" separator=",">
            (
            #{goodAttributeDetailsLists.goodsAttributeId},
            #{goodAttributeDetailsLists.attributeDetailsName},
            #{goodAttributeDetailsLists.goodsStock},
            #{goodAttributeDetailsLists.goodsPrice},
            #{goodAttributeDetailsLists.attributePitureUrl},
            #{goodAttributeDetailsLists.sort},
            #{goodAttributeDetailsLists.createBy},
            #{goodAttributeDetailsLists.createTime},
            #{goodAttributeDetailsLists.updateBy},
            #{goodAttributeDetailsLists.updateTime}
            )
        </foreach>
    </insert>


    <select id="selectGoodsAttributeDetailsByIdList" parameterType="Long"
            resultType="Long">
        select id
        from goods_attribute_details
        <where>
            <if test="goodsAttributeId != null ">
                and goods_attribute_id = #{goodsAttributeId}
            </if>
        </where>
    </select>


    <insert id="addGoodsAttributeDetails" parameterType="com.xinelu.manage.dto.goodsInfo.GoodsAttributeDTO"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_attribute_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsAttributeId != null">goods_attribute_id,
            </if>
            <if test="attributeDetailsName != null">attribute_details_name,
            </if>
            <if test="goodsStock != null">goods_stock,
            </if>
            <if test="goodsPrice != null">goods_price,
            </if>
            <if test="attributePitureUrl != null">attribute_piture_url,
            </if>
            <if test="attributeDetailsSort != null">sort,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="integralExchangeFlag != null">integral_exchange_flag,
            </if>
            <if test="integralExchangeSill != null">integral_exchange_sill,
            </if>
            <if test="integralExchangeCount != null">integral_exchange_count,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsAttributeId != null">#{goodsAttributeId},
            </if>
            <if test="attributeDetailsName != null">#{attributeDetailsName},
            </if>
            <if test="goodsStock != null">#{goodsStock},
            </if>
            <if test="goodsPrice != null">#{goodsPrice},
            </if>
            <if test="attributePitureUrl != null">#{attributePitureUrl},
            </if>
            <if test="attributeDetailsSort != null">#{attributeDetailsSort},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="integralExchangeFlag != null">#{integralExchangeFlag},
            </if>
            <if test="integralExchangeSill != null">#{integralExchangeSill},
            </if>
            <if test="integralExchangeCount != null">#{integralExchangeCount},
            </if>
        </trim>
    </insert>

    <update id="updateGoodsAttributeDetailss" parameterType="com.xinelu.manage.dto.goodsInfo.GoodsAttributeDTO">
        update goods_attribute_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsAttributeId != null">goods_attribute_id =
                #{goodsAttributeId},
            </if>
            <if test="attributeDetailsName != null">attribute_details_name =
                #{attributeDetailsName},
            </if>
            <if test="goodsStock != null">goods_stock =
                #{goodsStock},
            </if>
            <if test="goodsPrice != null">goods_price =
                #{goodsPrice},
            </if>
            <if test="attributePitureUrl != null">attribute_piture_url =
                #{attributePitureUrl},
            </if>
            <if test="attributeDetailsSort != null">sort =
                #{attributeDetailsSort},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            integral_exchange_flag = #{integralExchangeFlag},
            integral_exchange_sill = #{integralExchangeSill},
            integral_exchange_count = #{integralExchangeCount}
        </trim>
        where id = #{attributeDetailsId}
    </update>

    <update id="addGoodsStockCount">
        update goods_attribute_details set goods_stock = goods_stock + #{goodsCount}, update_time = now() where id = #{id}
    </update>

    <update id="reduceGoodsStockCount">
        update goods_attribute_details set goods_stock = goods_stock - #{goodsCount}, update_time = now() where id = #{id} and goods_stock &gt; 0
    </update>
</mapper>
