<?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">

    <!-- 分页获取所有 -->
    <select id="selectAllList" resultType="com.bonus.canteen.core.menu.vo.MenuMaterialPageVO">
        select
        mn.nutrition_id nutritionId,
        mn.nutrition_name nutritionName,
        mn.big_type bigType,
        mn.little_type littleType,
        mn.edible,
        mn.calories,
        mn.protein,
        mn.fat,
        mn.carbohydrate,
        mn.ash,
        mn.dietary_fiber dietaryFiber,
        mn.cholesterol,
        mn.vitamin_a vitaminA,
        mn.vitamin_c vitaminC,
        mn.vitamin_d vitaminD,
        mn.vitamin_e vitaminE,
        mn.thiamine,
        mn.riboflavin,
        mn.folic,
        mn.niacin,
        mn.carotene,
        mn.calcium,
        mn.phosphorus,
        mn.kalium,
        mn.sodium,
        mn.magnesium,
        mn.iron,
        mn.zinc,
        mn.cuprum,
        mn.manganese,
        mn.selenium,
        mm.material_id materialId,
        mm.material_name materialName,
        mm.material_code materialCode,
        mm.image_url imageUrl,
        mm.category_id categoryId,
        mmc.category_name categoryName,
        mm.bar_code,
        mm.sale_price,
        mm.unit_price,
        mm.material_type,
        mm.unit_id,
        du.unit_name,
        mm.sales_mode,
        mm.crby,
        mm.crtime,
        mm.upby,
        mm.uptime,
        mm.shelf_life_type,
        mm.shelf_life_days,
        mm.synopsis,
        mm.pur_price_ceiling,
        mm.area_id,
        aa.area_name,
        mm.cert_id,
        dc.cert_name,
        mm.size,
        mm.tax_rate,
        mm.convert_grams,
        mm.disable_status
        from
        menu_material mm
        left join menu_nutrition mn on mm.nutrition_id = mn.nutrition_id
        left join menu_material_category mmc on mm.category_id = mmc.category_id
        left join drp_unit du on du.unit_id = mm.unit_id
        left join alloc_area aa on mm.area_id = aa.area_id
        left join drp_certificate dc on dc.cert_id = mm.cert_id
        where
        mm.del_flag = #{delFlag}
        <if test="content.areaId != null">
            and (mm.area_id = #{content.areaId} or mm.area_id is null)
        </if>
        <!--区域权限-->
        <if test="content.areaAuth != null and content.areaAuth.size() > 0">
            and (mm.area_id in
            <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
            or mm.area_id is null)
        </if>
        <if test="content.materialName != null and content.materialName != ''">
            and (
                mm.material_name like concat(concat('%', #{content.materialName}), '%')
                or mm.pinyin_initials like concat(concat('%', #{content.materialName}), '%')
                or mm.pinyin_full like concat(concat('%', #{content.materialName}), '%')
                )
        </if>
        <if test="categoryIdList != null and categoryIdList.size() > 0">
            and mm.category_id in
            <foreach close=")" collection="categoryIdList" item="categoryId" open="(" separator=",">
                #{categoryId}
            </foreach>
        </if>
        <if test="content.ifHaveNutrition != null and content.ifHaveNutrition == 1">
            and mn.id is not null
        </if>
        <if test="content.barCode != null and content.barCode != ''">
            and mm.bar_code = #{content.barCode}
        </if>
        <if test="content.materialType != null">
            and mm.material_type = #{content.materialType}
        </if>
        <if test="content.disableStatus  != null">
            and mm.disable_status = #{content.disableStatus}
        </if>
        order by
        mm.crtime desc, mm.material_code desc
    </select>

    <!-- 根据菜品id获取菜品原料 -->
    <select id="selectListByDishesId" resultType="com.bonus.canteen.core.menu.vo.MenuMaterialDishesPageVO">
        select mm.material_id    materialId,
               mm.material_name  materialName,
               mmd.weight,
               mmd.material_type materialType
        from menu_material mm
                 left join menu_material_dishes mmd on mm.material_id = mmd.material_id
        where mm.del_flag = #{delFlag}
          and mmd.dishes_id = #{dishesId}
    </select>
    <select id="getMaterialByName" resultType="com.bonus.canteen.core.menu.vo.MaterialSomeInfoVO">
        SELECT
            a.material_id,
            a.material_name,
            a.material_code,
            b.category_id,
            b.category_name
        FROM
            menu_material a
            LEFT JOIN menu_material_category b ON a.category_id = b.category_id
            LEFT JOIN drp_unit c ON c.unit_id = a.unit_id
        WHERE
            a.material_name = #{materialName}
            and c.unit_name = #{unitName}
            and a.del_flag = #{delFlag}
    </select>
    <select id="pageAndroidMaterial" resultType="com.bonus.canteen.core.drp.vo.AndroidMaterialPageVO">
        SELECT
            a.id,
            a.material_id,
            a.material_name,
            a.material_code,
            a.image_url,
            a.category_id,
            b.category_name,
            a.material_type,
            a.bar_code,
            a.unit_id,
            d.unit_name,
            a.sale_price,
            a.sales_mode
        FROM
            menu_material a
            LEFT JOIN menu_material_category b ON b.category_id = a.category_id
            LEFT JOIN drp_unit d ON d.unit_id = a.unit_id
        <where>
            a.material_type = #{content.materialType}
            and a.del_flag = #{delFlag}
            <if test="content.NameOrBarCode != null and content.NameOrBarCode != ''" >
                and (a.material_name like #{content.NameOrBarCode} or a.bar_code like #{content.NameOrBarCode})
            </if>
            order by a.crtime desc
        </where>

    </select>
    <select id="checkMaterialNameAndUnit" resultType="java.lang.Long">
        SELECT
            count(*)
        FROM
            menu_material a
            LEFT JOIN drp_unit b ON b.unit_id = a.unit_id
        WHERE
            a.del_flag = 2
            and a.material_name = #{materialName}
            AND b.unit_name = #{unitName}
        <if test="areaId != null">
            and a.area_id = #{areaId} and (b.area_id = #{areaId} or b.area_id is null)
        </if>
        <if test="areaId == null">
            and a.area_id is null
        </if>
    </select>
    <select id="getMaterialByNameAndUnit" resultType="com.bonus.canteen.core.menu.entity.MenuMaterial">
        SELECT
            a.*
        FROM
            menu_material a
            LEFT JOIN drp_unit b ON b.unit_id = a.unit_id
        WHERE
            a.material_name = #{materialName}
            AND b.unit_name = #{unitName}
            AND a.del_flag = #{delFlag}
        <if test="areaId != null">
            and a.area_id = #{areaId} and (b.area_id = #{areaId} or b.area_id is null)
        </if>
        <if test="areaId == null">
            and a.area_id is null
        </if>
    </select>
    <select id="selectMaterialList" resultType="java.lang.String">
        select material_name from menu_material where del_flag = #{delFlag}
    </select>
  <select id="getAllTypes" resultType="com.bonus.canteen.core.drp.vo.DrpAppAllMaterialTypesVO">
      SELECT
          DISTINCT m.category_id,
                   c.category_name
      FROM menu_material m
               INNER JOIN menu_material_category c ON m.category_id = c.category_id
      WHERE
          m.del_flag = 2
        AND c.del_flag = 2
  </select>
    <select id="getMaterialByMaterialId" resultType="com.bonus.canteen.core.menu.vo.MenuMaterialVO">
        SELECT a.id,
               a.material_id,
               a.material_name,
               a.material_code,
               a.pinyin_initials,
               a.pinyin_full,
               a.image_url,
               a.nutrition_id,
               a.category_id,
               b.category_name,
               a.material_type,
               a.bar_code,
               a.unit_id,
               c.unit_name,
               a.sale_price,
               a.unit_price,
               a.sales_mode,
               a.reserve_rate,
               a.shelf_life_type,
               a.shelf_life_days,
               a.synopsis,
               a.del_flag,
               a.crby,
               a.crtime,
               a.upby,
               a.uptime,
               d.big_type,
               d.little_type,
               a.cert_id,
               e.cert_name
        FROM menu_material a
                 LEFT JOIN menu_material_category b ON b.category_id = a.category_id
                 LEFT JOIN drp_unit c ON c.unit_id = a.unit_id
                 LEFT JOIN menu_nutrition d ON d.nutrition_id = a.nutrition_id
                 LEFT JOIN drp_certificate e ON e.cert_id = a.cert_id
        WHERE a.material_id = #{materialId}
    </select>
    <select id="listMaterial" resultType="com.bonus.canteen.core.menu.vo.MenuMaterialListVO">
        SELECT
            material_id,
            material_name
        FROM
            menu_material
        WHERE
            del_flag = #{delFlag}
            <if test="content.materialName != null and content.materialName != ''">
                and material_name like #{content.materialName}
            </if>
    </select>
    <select id="countCategoryMaterialNum"
            resultType="com.bonus.canteen.core.drp.po.CategoryMaterialNumModel">
        SELECT
            a.category_id,
            count( a.id ) materialNum
        FROM
            menu_material a left join menu_material_category b on b.category_id = a.category_id
        WHERE
            a.del_flag = #{delFlag} and b.parent_id != -1
            <if test="categoryIdList != null and categoryIdList.size() > 0">
                and a.category_id in
                <foreach collection="categoryIdList" item="item" open="(" separator="," close=")" >
                    #{item}
                </foreach>
            </if>
            <if test="content.areaId != null">
                and (a.area_id = #{content.areaId} or a.area_id is null)
            </if>
        GROUP BY
            a.category_id
    </select>
    <!-- 批量插入原料信息 -->
    <insert id="insertMaterialBatch" parameterType="list">
        insert into
        menu_material(material_id,material_code, material_name, pinyin_initials, pinyin_full, nutrition_id, category_id,unit_id,sales_mode,
        crby)
        values
        <foreach collection="list" item="material" separator="," close=";">
            (#{material.materialId},#{material.materialCode}, #{material.materialName}, #{material.pinyinInitials},
            #{material.pinyinFull},
            #{material.nutritionId}, #{material.categoryId},#{material.unitId},#{material.salesMode}, #{material.crby})
        </foreach>
    </insert>

    <select id="getMenuMaterialAndUnitByMaterialIdList" resultType="com.bonus.canteen.core.drp.vo.MenuMaterialUnitVO">
        SELECT
        mm.material_id,
        mm.material_name,
        mm.unit_id,
        du.unit_name,
        du.rate
        FROM
        menu_material mm
        LEFT JOIN drp_unit du ON mm.unit_id = du.unit_id
        <where>
            <if test="materialIdList != null and materialIdList.size() > 0">
                and mm.material_id in
                <foreach close=")" collection="materialIdList" item="item" open="(" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="syncMenuMaterial" resultType="com.bonus.canteen.core.drp.vo.AndroidMenuMaterialSyncVO">
        SELECT
            *
        FROM
            (
                SELECT
                    mm.*,
                    du.unit_name,
                    mmc.category_name,
                    concat(
                            UNIX_TIMESTAMP( mm.uptime ),
                        RIGHT ( mm.material_id, 15 )) AS updateId
                FROM
                    menu_material mm
                    LEFT JOIN drp_unit du ON du.unit_id = mm.unit_id
                    left join menu_material_category mmc on mm.category_id = mmc.category_id
                where
                    mm.area_id = #{content.areaId} or mm.area_id is null
            ) t1
        WHERE
            t1.updateId > #{content.updateId}
        ORDER BY
            updateId
    </select>
    <select id="listCollectNotRegisterMaterial" resultType="com.bonus.canteen.core.drp.v2.po.MaterialListBean">
        SELECT DISTINCT
            mm.material_id,
            mm.material_name
        FROM
            menu_material mm
                LEFT JOIN menu_material_register mmr ON mm.material_id = mmr.material_id
                LEFT JOIN menu_material_register_pic mmrp ON mmrp.material_id = mm.material_id
        WHERE
            ( mmr.if_success IS NULL OR mmr.if_success = 0 )
          AND mmrp.id IS NOT NULL
          AND mm.del_flag = #{delFlag}
    </select>
    <select id="getMaterialByExcel" resultType="com.bonus.canteen.core.menu.vo.DrpImportMaterialVO">
        select mm.material_id,
        mm.material_name,
        mm.material_code,
        mm.category_id,
        mm.unit_id,
        mm.unit_price,
        mm.tax_rate,
        mm.size,
        mm.bar_code,
        mm.image_url,
        mmc.category_name,
        du.unit_name
        from menu_material mm
        left join menu_material_category mmc on mm.category_id = mmc.category_id
        left join drp_unit du on mm.unit_id = du.unit_id
        where mm.del_flag = 2 and mm.disable_status = 1
        <if test="materialCodeList != null and materialCodeList.size() > 0">
            AND mm.material_code in
            <foreach collection="materialCodeList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>
