<?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"    />
        <result property="materialName"    column="material_name"    />
        <result property="unitName"    column="unit_name"    />
    </resultMap>

    <sql id="selectSupermarketProductVo">
        select sp.product_id, sp.supermarket_id, sp.material_id, sp.putaway_state, sp.if_online, sp.sale_price,
               sp.pref_price, sp.person_limit, sp.one_day_limit, sp.img_url, sp.inventory_num, sp.create_by,
               sp.create_time, sp.update_by, sp.update_time, sm.material_name,smt.material_type_name, spu.unit_name
        from supermarket_product sp
        left join supermarket_material sm on sm.material_id = sp.material_id
        left join supply_product_unit spu on spu.unit_id = sm.unit_id
        left join supermarket_material_type smt on smt.material_type_id = sm.material_type_id
    </sql>

    <select id="selectSupermarketProductList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        <where>
            <if test="barCode != null"> and sm.bar_code like concat('%', #{barCode}, '%')</if>
            <if test="materialName != null"> and sm.material_name like concat('%', #{materialName}, '%')</if>
            <if test="supermarketId != null "> and sp.supermarket_id = #{supermarketId}</if>
            <if test="materialType != null "> and sm.material_type_id = #{materialType}</if>
            <if test="materialId != null "> and sp.material_id = #{materialId}</if>
            <if test="putawayState != null "> and sp.putaway_state = #{putawayState}</if>
            <if test="ifOnline != null "> and sp.if_online = #{ifOnline}</if>
            <if test="salePrice != null "> and sp.sale_price = #{salePrice}</if>
            <if test="prefPrice != null "> and sp.pref_price = #{prefPrice}</if>
            <if test="personLimit != null "> and sp.person_limit = #{personLimit}</if>
            <if test="oneDayLimit != null "> and sp.one_day_limit = #{oneDayLimit}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and sp.img_url = #{imgUrl}</if>
            <if test="inventoryNum != null "> and sp.inventory_num = #{inventoryNum}</if>
        </where>
    </select>


    <select id="selectSupermarketAddProductList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" resultType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct">
        select sm.material_id, sm.area_id, sm.material_name, sm.material_code, sm.img_url, sm.material_type_id,
               sm.bar_code, sm.unit_id, sm.sale_price, sm.unit_price, sm.sales_mode, sm.shelf_life_type,
               sm.shelf_life_days, sm.pur_price_ceiling, sm.big_category_id, sm.`size`, sm.description,
               sm.create_by, sm.create_time, sm.update_by, sm.update_time,
               ba.area_name, smt.material_type_name, spu.unit_name
        from supermarket_material sm
                 left join basic_area ba on ba.area_id = sm.area_id
                 left join supermarket_material_type smt on smt.material_type_id = sm.material_type_id
                 left join supply_product_unit spu on spu.unit_id = sm.unit_id
                 left join supermarket_product sp on sm.material_id=sp.material_id
        where sp.material_id is null
        <if test="areaId != null  and areaId != ''"> and sm.area_id = #{areaId}</if>
        <if test="materialName != null  and materialName != ''"> and sm.material_name like concat('%', #{materialName}, '%')</if>
        <if test="barCode != null  and barCode != ''"> and sm.bar_code = #{barCode}</if>
        <if test="materialIds != null  and materialIds != ''"> and sm.material_id in (${materialIds}) </if>
    </select>

    <select id="selectSupermarketProductByProductId" parameterType="Long" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        where sp.product_id = #{productId}
    </select>

    <select id="selectSupermarketProductByMaterialIds" parameterType="String" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        where sp.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>
    <delete id="deleteSupermarketProductByProductIdss" parameterType="String">
        delete from supermarket_product where product_id in
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </delete>
    <delete id="onlineSupermarketProductByProductIdss" parameterType="String">
        update supermarket_product set if_online="1" where product_id in
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </delete>
</mapper>
