<?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.cook.mapper.CookMaterialMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookMaterial" id="CookMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialCode"    column="material_code"    />
        <result property="imgUrl"    column="img_url"    />
        <result property="nutritionId"    column="nutrition_id"    />
        <result property="materialTypeId"    column="material_type_id"    />
        <result property="nutritionTypeId"    column="nutrition_type_id"    />
        <result property="goodsType"    column="goods_type"    />
        <result property="barCode"    column="bar_code"    />
        <result property="unitId"    column="unit_id"    />
        <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"    />
        <result property="nutritionName"    column="nutrition_name"    />
        <result property="materialTypeName"    column="material_type_name"    />
        <result property="areaName"    column="area_name"    />
        <result property="nutritionTypeName"    column="nutrition_type_name"    />
        <result property="unitName"    column="unit_name"    />
    </resultMap>

    <sql id="selectCookMaterialVo">
        select material_id, material_name, material_code,cm.img_url,
               cm.goods_type, bar_code,
               sale_price, unit_price, sales_mode, shelf_life_type, shelf_life_days,
               pur_price_ceiling, big_category_id, size, cm.description,cm.create_by,
               cm.create_time, cm.update_by, cm.update_time,
               cm.nutrition_id,cn.nutrition_name,
               cmt.material_type_id, cmt.material_type_name,
               a.area_id, a.area_name,
               cm.nutrition_type_id,  cnt.nutrition_type_name,
               spu.unit_name,cm.unit_id
               from cook_material cm
        left join cook_material_type cmt on cm.material_type_id = cmt.material_type_id
        left join cook_nutrition_type cnt on cm.nutrition_type_id = cnt.nutrition_type_id
        left join cook_nutrition cn on cm.nutrition_id = cn.nutrition_id
        left join supply_product_unit spu on cm.unit_id = spu.unit_id
        left join basic_area a on cm.area_id = a.area_id
    </sql>

    <select id="selectCookMaterialList" parameterType="com.bonus.canteen.core.cook.domain.CookMaterial" resultMap="CookMaterialResult">
        <include refid="selectCookMaterialVo"/>
        <where>
            <if test="searchValue != null and searchValue != ''">
                and (cm.material_name like concat('%', #{searchValue}, '%')
                or cm.material_code like concat('%', #{searchValue}, '%'))
            </if>
            <if test="materialTypeIds != null and materialTypeIds.size() > 0">
                and cm.material_type_id in
                <foreach item="materialTypeId" collection="materialTypeIds" open="(" separator="," close=")">
                    #{materialTypeId}
                </foreach>
            </if>
            <if test="areaId != null "> and cm.area_id = #{areaId}</if>
            <if test="materialName != null  and materialName != ''"> and cm.material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialCode != null  and materialCode != ''"> and cm.material_code = #{materialCode}</if>
            <if test="imgUrl != null  and imgUrl != ''"> and cm.img_url = #{imgUrl}</if>
            <if test="nutritionId != null "> and cm.nutrition_id = #{nutritionId}</if>
            <if test="materialTypeId != null "> and cm.material_type_id = #{materialTypeId}</if>
            <if test="nutritionTypeId != null  and nutritionTypeId != ''"> and cm.nutrition_type_id = #{nutritionTypeId}</if>
            <if test="goodsType != null "> and cm.goods_type = #{goodsType}</if>
            <if test="barCode != null  and barCode != ''"> and cm.bar_code = #{barCode}</if>
            <if test="unitId != null "> and cm.unit_id = #{unitId}</if>
            <if test="salePrice != null "> and cm.sale_price = #{salePrice}</if>
            <if test="unitPrice != null "> and cm.unit_price = #{unitPrice}</if>
            <if test="salesMode != null "> and cm.sales_mode = #{salesMode}</if>
            <if test="shelfLifeType != null "> and cm.shelf_life_type = #{shelfLifeType}</if>
            <if test="shelfLifeDays != null "> and cm.shelf_life_days = #{shelfLifeDays}</if>
            <if test="purPriceCeiling != null "> and cm.pur_price_ceiling = #{purPriceCeiling}</if>
            <if test="bigCategoryId != null "> and cm.big_category_id = #{bigCategoryId}</if>
            <if test="size != null  and size != ''"> and cm.size = #{size}</if>
            <if test="description != null  and description != ''"> and cm.description = #{description}</if>
        </where>
        order by cm.create_time desc
    </select>
    
    <select id="selectCookMaterialByMaterialId" parameterType="Long" resultMap="CookMaterialResult">
        <include refid="selectCookMaterialVo"/>
        where cm.material_id = #{materialId}
    </select>
    <select id="checkIsExistByCode" resultType="java.lang.Integer">
        select count(1) from cook_material where material_code = #{materialCode}
        <if test="materialId != null and materialId != ''"> and material_id != #{materialId}</if>
    </select>
    <select id="checkIsExistByName" resultType="java.lang.Integer">
        select count(1) from cook_material where material_name = #{materialName}
        <if test="materialId != null and materialId != ''"> and material_id != #{materialId}</if>
    </select>
    <select id="checkIsUse" resultType="java.lang.Integer">
        select count(1) from cook_dishes_material where material_id
        in
        <foreach item="materialId" collection="materialIds" open="(" separator="," close=")">
            #{materialId}
        </foreach>
    </select>

    <insert id="insertCookMaterial" parameterType="com.bonus.canteen.core.cook.domain.CookMaterial" useGeneratedKeys="true" keyProperty="materialId">
        insert into cook_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="nutritionId != null">nutrition_id,</if>
            <if test="materialTypeId != null">material_type_id,</if>
            <if test="nutritionTypeId != null">nutrition_type_id,</if>
            <if test="goodsType != null">goods_type,</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="nutritionId != null">#{nutritionId},</if>
            <if test="materialTypeId != null">#{materialTypeId},</if>
            <if test="nutritionTypeId != null">#{nutritionTypeId},</if>
            <if test="goodsType != null">#{goodsType},</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="updateCookMaterial" parameterType="com.bonus.canteen.core.cook.domain.CookMaterial">
        update cook_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="nutritionId != null">nutrition_id = #{nutritionId},</if>
            <if test="materialTypeId != null">material_type_id = #{materialTypeId},</if>
            <if test="nutritionTypeId != null">nutrition_type_id = #{nutritionTypeId},</if>
            <if test="goodsType != null">goods_type = #{goodsType},</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="deleteCookMaterialByMaterialId" parameterType="Long">
        delete from cook_material where material_id = #{materialId}
    </delete>

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