<?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="productName"    column="product_name"    />
        <result property="barCode"    column="bar_code"    />
        <result property="salePrice"    column="sale_price"    />
        <result property="salesMode"    column="sales_mode"    />
        <result property="areaId"    column="area_id"    />
        <result property="unitId"    column="unit_id"    />
        <result property="productType"    column="product_type"    />
        <result property="prefPrice"    column="pref_price"    />
        <result property="qualityType"    column="quality_type"    />
        <result property="qualityNum"    column="quality_num"    />
        <result property="supplyCertificate"    column="supply_certificate"    />
        <result property="productRemark"    column="product_remark"    />
        <result property="putawayState"    column="putaway_state"    />
        <result property="ifOnline"    column="if_online"    />
        <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="revision"    column="revision"    />
        <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="productCode"    column="product_code"    />
        <result property="state"    column="state"    />
    </resultMap>

    <sql id="selectSupermarketProductVo">
        select product_id, supermarket_id, product_name, bar_code, sale_price, sales_mode, area_id, unit_id, product_type, pref_price, quality_type, quality_num, supply_certificate, product_remark, putaway_state, if_online, person_limit, one_day_limit, img_url, inventory_num, revision, create_by, create_time, update_by, update_time, product_code, state 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="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="barCode != null  and barCode != ''"> and bar_code = #{barCode}</if>
            <if test="salePrice != null "> and sale_price = #{salePrice}</if>
            <if test="salesMode != null  and salesMode != ''"> and sales_mode = #{salesMode}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="unitId != null "> and unit_id = #{unitId}</if>
            <if test="productType != null "> and product_type = #{productType}</if>
            <if test="prefPrice != null "> and pref_price = #{prefPrice}</if>
            <if test="qualityType != null  and qualityType != ''"> and quality_type = #{qualityType}</if>
            <if test="qualityNum != null "> and quality_num = #{qualityNum}</if>
            <if test="supplyCertificate != null  and supplyCertificate != ''"> and supply_certificate = #{supplyCertificate}</if>
            <if test="productRemark != null  and productRemark != ''"> and product_remark = #{productRemark}</if>
            <if test="putawayState != null "> and putaway_state = #{putawayState}</if>
            <if test="ifOnline != null "> and if_online = #{ifOnline}</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>
            <if test="revision != null "> and revision = #{revision}</if>
            <if test="productCode != null  and productCode != ''"> and product_code = #{productCode}</if>
            <if test="state != null  and state != ''"> and state = #{state}</if>
        </where>
    </select>
    
    <select id="selectSupermarketProductByProductId" parameterType="Long" resultMap="SupermarketProductResult">
        <include refid="selectSupermarketProductVo"/>
        where product_id = #{productId}
    </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="productName != null">product_name,</if>
            <if test="barCode != null">bar_code,</if>
            <if test="salePrice != null">sale_price,</if>
            <if test="salesMode != null">sales_mode,</if>
            <if test="areaId != null">area_id,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="productType != null">product_type,</if>
            <if test="prefPrice != null">pref_price,</if>
            <if test="qualityType != null">quality_type,</if>
            <if test="qualityNum != null">quality_num,</if>
            <if test="supplyCertificate != null">supply_certificate,</if>
            <if test="productRemark != null">product_remark,</if>
            <if test="putawayState != null">putaway_state,</if>
            <if test="ifOnline != null">if_online,</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="revision != null">revision,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="productCode != null">product_code,</if>
            <if test="state != null">state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="supermarketId != null">#{supermarketId},</if>
            <if test="productName != null">#{productName},</if>
            <if test="barCode != null">#{barCode},</if>
            <if test="salePrice != null">#{salePrice},</if>
            <if test="salesMode != null">#{salesMode},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="productType != null">#{productType},</if>
            <if test="prefPrice != null">#{prefPrice},</if>
            <if test="qualityType != null">#{qualityType},</if>
            <if test="qualityNum != null">#{qualityNum},</if>
            <if test="supplyCertificate != null">#{supplyCertificate},</if>
            <if test="productRemark != null">#{productRemark},</if>
            <if test="putawayState != null">#{putawayState},</if>
            <if test="ifOnline != null">#{ifOnline},</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="revision != null">#{revision},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="productCode != null">#{productCode},</if>
            <if test="state != null">#{state},</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="productName != null">product_name = #{productName},</if>
            <if test="barCode != null">bar_code = #{barCode},</if>
            <if test="salePrice != null">sale_price = #{salePrice},</if>
            <if test="salesMode != null">sales_mode = #{salesMode},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="productType != null">product_type = #{productType},</if>
            <if test="prefPrice != null">pref_price = #{prefPrice},</if>
            <if test="qualityType != null">quality_type = #{qualityType},</if>
            <if test="qualityNum != null">quality_num = #{qualityNum},</if>
            <if test="supplyCertificate != null">supply_certificate = #{supplyCertificate},</if>
            <if test="productRemark != null">product_remark = #{productRemark},</if>
            <if test="putawayState != null">putaway_state = #{putawayState},</if>
            <if test="ifOnline != null">if_online = #{ifOnline},</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="revision != null">revision = #{revision},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="productCode != null">product_code = #{productCode},</if>
            <if test="state != null">state = #{state},</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>

    <select id="getProductCountOfSupermarket" parameterType="String">
        select count(1) from supermarket_product where supermarket_id in
        <foreach item="supermarketId" collection="array" open="(" separator="," close=")">
            #{supermarketId}
        </foreach>
    </select>
</mapper>