<?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.SupermarketMaterialMapper">
    <resultMap type="com.bonus.canteen.core.supermarket.domain.SupermarketMaterial" id="SupermarketMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_name"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialCode"    column="material_code"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="materialTypeId"    column="material_type_id"    />
        <result property="materialTypeName"    column="material_type_name"    />
        <result property="barCode"    column="bar_code"    />
        <result property="unitId"    column="unit_id"    />
        <result property="unitName"    column="unit_name"    />
        <result property="salePrice"    column="sale_price"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="salesMode"    column="sales_mode"    />
        <result property="shelfLifeType"    column="shelf_life_type"    />
        <result property="shelfLifeDays"    column="shelf_life_days"    />
        <result property="purPriceCeiling"    column="pur_price_ceiling"    />
        <result property="bigCategoryId"    column="big_category_id"    />
        <result property="size"    column="size"    />
        <result property="description"    column="description"    />
        <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="selectSupermarketMaterialVo">
        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
    </sql>

    <select id="selectSupermarketMaterialList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketMaterial" resultMap="SupermarketMaterialResult">
        <include refid="selectSupermarketMaterialVo"/>
        <where>  
            <if test="areaId != null "> and sm.area_id = #{areaId}</if>
            <if test="materialName != null  and materialName != ''"> and sm.material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialCode != null  and materialCode != ''"> and sm.material_code = #{materialCode}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and sm.img_url = #{imgUrl}</if>
            <if test="materialTypeIds != null and materialTypeIds.length > 0">
                and sm.material_type_id in
                <foreach collection="materialTypeIds" item="item" index="index" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="barCode != null  and barCode != ''"> and sm.bar_code = #{barCode}</if>
            <if test="unitId != null "> and sm.unit_id = #{unitId}</if>
            <if test="salePrice != null "> and sm.sale_price = #{salePrice}</if>
            <if test="unitPrice != null "> and sm.unit_price = #{unitPrice}</if>
            <if test="salesMode != null "> and sm.sales_mode = #{salesMode}</if>
            <if test="shelfLifeType != null "> and sm.shelf_life_type = #{shelfLifeType}</if>
            <if test="shelfLifeDays != null "> and sm.shelf_life_days = #{shelfLifeDays}</if>
            <if test="purPriceCeiling != null "> and sm.pur_price_ceiling = #{purPriceCeiling}</if>
            <if test="bigCategoryId != null "> and sm.big_category_id = #{bigCategoryId}</if>
            <if test="size != null  and size != ''"> and sm.`size` = #{size}</if>
            <if test="description != null  and description != ''"> and sm.description = #{description}</if>
        </where>
    </select>
    
    <select id="selectSupermarketMaterialByMaterialId" parameterType="Long" resultMap="SupermarketMaterialResult">
        <include refid="selectSupermarketMaterialVo"/>
        where sm.material_id = #{materialId}
    </select>
        
    <insert id="insertSupermarketMaterial" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketMaterial" useGeneratedKeys="true" keyProperty="materialId">
        insert into supermarket_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialCode != null and materialCode != ''">material_code,</if>
            <if test="imgUrl != null and imgUrl != ''">img_url,</if>
            <if test="materialTypeId != null">material_type_id,</if>
            <if test="barCode != null">bar_code,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="salePrice != null">sale_price,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="salesMode != null">sales_mode,</if>
            <if test="shelfLifeType != null">shelf_life_type,</if>
            <if test="shelfLifeDays != null">shelf_life_days,</if>
            <if test="purPriceCeiling != null">pur_price_ceiling,</if>
            <if test="bigCategoryId != null">big_category_id,</if>
            <if test="size != null">`size`,</if>
            <if test="description != null">description,</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="areaId != null">#{areaId},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialCode != null and materialCode != ''">#{materialCode},</if>
            <if test="imgUrl != null and imgUrl != ''">#{imgUrl},</if>
            <if test="materialTypeId != null">#{materialTypeId},</if>
            <if test="barCode != null">#{barCode},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="salePrice != null">#{salePrice},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="salesMode != null">#{salesMode},</if>
            <if test="shelfLifeType != null">#{shelfLifeType},</if>
            <if test="shelfLifeDays != null">#{shelfLifeDays},</if>
            <if test="purPriceCeiling != null">#{purPriceCeiling},</if>
            <if test="bigCategoryId != null">#{bigCategoryId},</if>
            <if test="size != null">#{size},</if>
            <if test="description != null">#{description},</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="updateSupermarketMaterial" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketMaterial">
        update supermarket_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="materialCode != null and materialCode != ''">material_code = #{materialCode},</if>
            <if test="imgUrl != null and imgUrl != ''">img_url = #{imgUrl},</if>
            <if test="materialTypeId != null">material_type_id = #{materialTypeId},</if>
            <if test="barCode != null">bar_code = #{barCode},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="salePrice != null">sale_price = #{salePrice},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="salesMode != null">sales_mode = #{salesMode},</if>
            <if test="shelfLifeType != null">shelf_life_type = #{shelfLifeType},</if>
            <if test="shelfLifeDays != null">shelf_life_days = #{shelfLifeDays},</if>
            <if test="purPriceCeiling != null">pur_price_ceiling = #{purPriceCeiling},</if>
            <if test="bigCategoryId != null">big_category_id = #{bigCategoryId},</if>
            <if test="size != null">`size` = #{size},</if>
            <if test="description != null">description = #{description},</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 material_id = #{materialId}
    </update>

    <delete id="deleteSupermarketMaterialByMaterialId" parameterType="Long">
        delete from supermarket_material where material_id = #{materialId}
    </delete>

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

    <select id="getSupermarketMaterialCountByMaterialTypes" resultType="Integer">
        select count(1)
        from supermarket_material
        where material_type_id in
        <foreach item="materialTypeId" collection="array" open="(" separator="," close=")">
            #{materialTypeId}
        </foreach>
    </select>
</mapper>