<?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.menu.mapper.MenuMaterialMapper">
    <resultMap type="com.bonus.canteen.core.menu.domain.MenuMaterial" id="MenuMaterialResult">
        <result property="id"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialCode"    column="material_code"    />
        <result property="pinyinInitials"    column="pinyin_initials"    />
        <result property="pinyinFull"    column="pinyin_full"    />
        <result property="imageUrl"    column="image_url"    />
        <result property="nutritionId"    column="nutrition_id"    />
        <result property="categoryId"    column="category_id"    />
        <result property="materialType"    column="material_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="reserveRate"    column="reserve_rate"    />
        <result property="shelfLifeType"    column="shelf_life_type"    />
        <result property="shelfLifeDays"    column="shelf_life_days"    />
        <result property="synopsis"    column="synopsis"    />
        <result property="revision"    column="revision"    />
        <result property="purPriceCeiling"    column="pur_price_ceiling"    />
        <result property="certId"    column="cert_id"    />
        <result property="bigCategoryId"    column="big_category_id"    />
        <result property="size"    column="size"    />
        <result property="taxRate"    column="tax_rate"    />
        <result property="disableStatus"    column="disable_status"    />
        <result property="convertGrams"    column="convert_grams"    />
    </resultMap>

    <sql id="selectMenuMaterialVo">

<!--        select id, area_id, material_id, material_name, material_code, pinyin_initials, pinyin_full, image_url, nutrition_id, category_id, material_type, bar_code, unit_id, sale_price, unit_price, sales_mode, reserve_rate, shelf_life_type, shelf_life_days, synopsis, revision, pur_price_ceiling, cert_id, big_category_id, size, tax_rate, disable_status, convert_grams from menu_material-->

        SELECT
        mm.id,
        mm.area_id,
        aa.area_name,
        mm.material_id,
        mm.material_name,
        mm.material_code,
        mm.pinyin_initials,
        mm.pinyin_full,
        mm.image_url,
        mm.nutrition_id,
        mm.category_id,
        mmc.category_name,
        mm.material_type,
        mm.bar_code,
        mm.unit_id,
        du.unit_name,
        mm.sale_price,
        mm.unit_price,
        mm.sales_mode,
        mm.reserve_rate,
        mm.shelf_life_type,
        mm.shelf_life_days,
        mm.synopsis,
        mm.revision,
        mm.pur_price_ceiling,
        mm.cert_id,
        mm.big_category_id,
        mm.size,
        mm.tax_rate,
        mm.disable_status,
        mm.convert_grams
        FROM
        menu_material mm
        LEFT JOIN alloc_area aa on aa.area_id = mm.area_id
        LEFT JOIN menu_material_category mmc on mmc.category_id = mm.category_id
        LEFT JOIN drp_unit du on du.unit_id = mm.unit_id

    </sql>

    <select id="selectMenuMaterialList" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterial" resultMap="MenuMaterialResult">
        <include refid="selectMenuMaterialVo"/>
        <where>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialCode != null  and materialCode != ''"> and material_code like concat('%', #{materialCode}, '%')</if>
            <if test="barCode != null"> and bar_code like concat('%', #{barCode}, '%')</if>
        </where>
    </select>

    <select id="selectMenuMaterialById" parameterType="Long" resultMap="MenuMaterialResult">
        <include refid="selectMenuMaterialVo"/>
        where id = #{id}
    </select>

    <insert id="insertMenuMaterial" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterial" useGeneratedKeys="true" keyProperty="id">
        insert into menu_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialCode != null and materialCode != ''">material_code,</if>
            <if test="pinyinInitials != null and pinyinInitials != ''">pinyin_initials,</if>
            <if test="pinyinFull != null and pinyinFull != ''">pinyin_full,</if>
            <if test="imageUrl != null and imageUrl != ''">image_url,</if>
            <if test="nutritionId != null">nutrition_id,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="materialType != null">material_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="reserveRate != null">reserve_rate,</if>
            <if test="shelfLifeType != null">shelf_life_type,</if>
            <if test="shelfLifeDays != null">shelf_life_days,</if>
            <if test="synopsis != null">synopsis,</if>
            <if test="revision != null">revision,</if>
            <if test="purPriceCeiling != null">pur_price_ceiling,</if>
            <if test="certId != null">cert_id,</if>
            <if test="bigCategoryId != null">big_category_id,</if>
            <if test="size != null">size,</if>
            <if test="taxRate != null">tax_rate,</if>
            <if test="disableStatus != null">disable_status,</if>
            <if test="convertGrams != null">convert_grams,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialCode != null and materialCode != ''">#{materialCode},</if>
            <if test="pinyinInitials != null and pinyinInitials != ''">#{pinyinInitials},</if>
            <if test="pinyinFull != null and pinyinFull != ''">#{pinyinFull},</if>
            <if test="imageUrl != null and imageUrl != ''">#{imageUrl},</if>
            <if test="nutritionId != null">#{nutritionId},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="materialType != null">#{materialType},</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="reserveRate != null">#{reserveRate},</if>
            <if test="shelfLifeType != null">#{shelfLifeType},</if>
            <if test="shelfLifeDays != null">#{shelfLifeDays},</if>
            <if test="synopsis != null">#{synopsis},</if>
            <if test="revision != null">#{revision},</if>
            <if test="purPriceCeiling != null">#{purPriceCeiling},</if>
            <if test="certId != null">#{certId},</if>
            <if test="bigCategoryId != null">#{bigCategoryId},</if>
            <if test="size != null">#{size},</if>
            <if test="taxRate != null">#{taxRate},</if>
            <if test="disableStatus != null">#{disableStatus},</if>
            <if test="convertGrams != null">#{convertGrams},</if>
         </trim>
    </insert>

    <update id="updateMenuMaterial" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterial">
        update menu_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="materialCode != null and materialCode != ''">material_code = #{materialCode},</if>
            <if test="pinyinInitials != null and pinyinInitials != ''">pinyin_initials = #{pinyinInitials},</if>
            <if test="pinyinFull != null and pinyinFull != ''">pinyin_full = #{pinyinFull},</if>
            <if test="imageUrl != null and imageUrl != ''">image_url = #{imageUrl},</if>
            <if test="nutritionId != null">nutrition_id = #{nutritionId},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="materialType != null">material_type = #{materialType},</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="reserveRate != null">reserve_rate = #{reserveRate},</if>
            <if test="shelfLifeType != null">shelf_life_type = #{shelfLifeType},</if>
            <if test="shelfLifeDays != null">shelf_life_days = #{shelfLifeDays},</if>
            <if test="synopsis != null">synopsis = #{synopsis},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="purPriceCeiling != null">pur_price_ceiling = #{purPriceCeiling},</if>
            <if test="certId != null">cert_id = #{certId},</if>
            <if test="bigCategoryId != null">big_category_id = #{bigCategoryId},</if>
            <if test="size != null">size = #{size},</if>
            <if test="taxRate != null">tax_rate = #{taxRate},</if>
            <if test="disableStatus != null">disable_status = #{disableStatus},</if>
            <if test="convertGrams != null">convert_grams = #{convertGrams},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMenuMaterialById" parameterType="Long">
        delete from menu_material where id = #{id}
    </delete>

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

    <select id="getLastId" resultType="int">
        select max(id) from menu_material
    </select>

    <select id="getOne" resultMap="MenuMaterialResult" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterial">
        select * from menu_material where material_name = #{materialName} and area_id = #{areaId}
    </select>
</mapper>
