<?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.bonus.canteen.core.supermarket.mapper.SupermarketProductMapper">
    <resultMap type="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" id="SupermarketProductResult">
        <result property="productId"    column="product_id"    />
        <result property="supermarketId"    column="supermarket_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="putawayState"    column="putaway_state"    />
        <result property="ifOnline"    column="if_online"    />
        <result property="salePrice"    column="sale_price"    />
        <result property="prefPrice"    column="pref_price"    />
        <result property="personLimit"    column="person_limit"    />
        <result property="oneDayLimit"    column="one_day_limit"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="inventoryNum"    column="inventory_num"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSupermarketProductVo">
        select product_id, supermarket_id, material_id, putaway_state, if_online, sale_price, pref_price,
               person_limit, one_day_limit, img_url, inventory_num, create_by, create_time, update_by, update_time
        from supermarket_product
    </sql>

    <select id="selectSupermarketProductList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        <where>  
            <if test="supermarketId != null "> and supermarket_id = #{supermarketId}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="putawayState != null "> and putaway_state = #{putawayState}</if>
            <if test="ifOnline != null "> and if_online = #{ifOnline}</if>
            <if test="salePrice != null "> and sale_price = #{salePrice}</if>
            <if test="prefPrice != null "> and pref_price = #{prefPrice}</if>
            <if test="personLimit != null "> and person_limit = #{personLimit}</if>
            <if test="oneDayLimit != null "> and one_day_limit = #{oneDayLimit}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and img_url = #{imgUrl}</if>
            <if test="inventoryNum != null "> and inventory_num = #{inventoryNum}</if>
        </where>
    </select>
    
    <select id="selectSupermarketProductByProductId" parameterType="Long" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        where product_id = #{productId}
    </select>

    <select id="selectSupermarketProductByMaterialIds" parameterType="String" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        where material_id in
        <foreach item="materialId" collection="array" open="(" separator="," close=")">
            #{materialId}
        </foreach>
    </select>
        
    <insert id="insertSupermarketProduct" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" useGeneratedKeys="true" keyProperty="productId">
        insert into supermarket_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="supermarketId != null">supermarket_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="putawayState != null">putaway_state,</if>
            <if test="ifOnline != null">if_online,</if>
            <if test="salePrice != null">sale_price,</if>
            <if test="prefPrice != null">pref_price,</if>
            <if test="personLimit != null">person_limit,</if>
            <if test="oneDayLimit != null">one_day_limit,</if>
            <if test="imgUrl != null">img_url,</if>
            <if test="inventoryNum != null">inventory_num,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="supermarketId != null">#{supermarketId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="putawayState != null">#{putawayState},</if>
            <if test="ifOnline != null">#{ifOnline},</if>
            <if test="salePrice != null">#{salePrice},</if>
            <if test="prefPrice != null">#{prefPrice},</if>
            <if test="personLimit != null">#{personLimit},</if>
            <if test="oneDayLimit != null">#{oneDayLimit},</if>
            <if test="imgUrl != null">#{imgUrl},</if>
            <if test="inventoryNum != null">#{inventoryNum},</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>
         </trim>
    </insert>

    <update id="updateSupermarketProduct" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct">
        update supermarket_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="supermarketId != null">supermarket_id = #{supermarketId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="putawayState != null">putaway_state = #{putawayState},</if>
            <if test="ifOnline != null">if_online = #{ifOnline},</if>
            <if test="salePrice != null">sale_price = #{salePrice},</if>
            <if test="prefPrice != null">pref_price = #{prefPrice},</if>
            <if test="personLimit != null">person_limit = #{personLimit},</if>
            <if test="oneDayLimit != null">one_day_limit = #{oneDayLimit},</if>
            <if test="imgUrl != null">img_url = #{imgUrl},</if>
            <if test="inventoryNum != null">inventory_num = #{inventoryNum},</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>
        </trim>
        where product_id = #{productId}
    </update>

    <delete id="deleteSupermarketProductByProductId" parameterType="Long">
        delete from supermarket_product where product_id = #{productId}
    </delete>

    <delete id="deleteSupermarketProductByProductIds" parameterType="String">
        delete from supermarket_product where product_id in 
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </delete>
</mapper>