<?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.android.mapper.DeviceMapper">
    <select id="getDeviceInfo" resultType="com.bonus.canteen.core.android.vo.DeviceInfoVo">
        SELECT di.device_number                           AS deviceNum,
               di.device_name                           AS deviceName,
               dr.recipe_id                             AS recipeId,
               dr.recipe_name                           AS recipeName,
               CONCAT(aaa.area_name, '/', aa.area_name) AS areaName,
               aa.area_id                               AS areaId,
               ac.canteen_name                          AS canteenName,
               ac.canteen_id                            AS canteenId,
               ast.stall_name                           AS stallName,
               ast.stall_id                             AS stallId,
               di.device_pwd                            as devicePwd
        FROM device_info di
                 LEFT JOIN device_bind db ON di.device_id = db.device_id
                 left join basic_area aa on db.area_id = aa.area_id
                 left join basic_area aaa on aaa.area_id = aa.parent_id
                 LEFT JOIN basic_canteen ac ON db.canteen_id = ac.canteen_id
                 LEFT JOIN basic_stall ast ON db.stall_id = ast.stall_id
                 LEFT JOIN (SELECT dr.device_id, mr.recipe_name,mr.recipe_id
                            FROM cook_recipe_bind_device dr
                                     LEFT JOIN cook_recipe mr ON dr.recipe_id = mr.recipe_id) dr
                           ON di.device_id = dr.device_id
        where di.device_sn = #{deviceSn}
        GROUP BY di.device_id
    </select>
    <select id="getAppVersion" resultType="com.bonus.canteen.core.android.vo.AppVersionVo">
        select
            version as id,
            version as version,
            version_name as versionName,
            apk_name as apkName,
            apk_path as apkPath,
            update_content as updateContent,
            deploy_time as deployTime,
            deploy_user as deployUser
        from device_app_version
        where canteen_type = #{type} and version > #{id}
    </select>
    <select id="getParamInfo" resultType="com.bonus.canteen.core.android.vo.ParamVO">
        SELECT
            item_name AS paramKey,
            item_value AS paramValue
        FROM basic_setting
        WHERE usage_type = '3'
    </select>

    <select id="getRecipeIdBySn" resultType="java.lang.Long">
        select ifnull(recipe_id,0) as recipeId
        from cook_recipe_bind_device
        where device_id = (select device_id from device_info where device_sn = #{machineSn} limit 1) limit 1
    </select>

    <select id="getOneById" resultType="com.bonus.canteen.core.cook.domain.CookRecipe">
        select recipe_id as recipeId,
               plan_id as planId,
               recipe_name as recipeName,
               recipe_type as recipeType,
               effect_date as effectDate,
               expire_date as expireDate,
               canteen_id as canteenId,
               stall_id as stallId,
               create_by as createBy,
               create_time as createTime,
               update_by as updateBy,
               update_time as updateTime
        from cook_recipe
        where recipe_id =  #{recipeId}
    </select>

    <select id="selectMenuRecipeDetailLists" resultType="com.bonus.canteen.core.android.vo.CookMenuAndroidRecipeDetailVO">
        select
            recipe_detail_id as detailId,
            mealtime_type as mealTimeType,
            apply_date as applyData
        from cook_recipe_detail where recipe_id= #{recipeId} and apply_date >= Date(NOW())
    </select>

    <select id="selectRecipeDishList" resultType="com.bonus.canteen.core.android.vo.CookRecipeDetailCompressVO">
        select
        recipe_detail_id as detailId,
        dishes_id as dishesId,
        price as salePrice,
        sale_price as prefPrice,
        supply_num as supplyNum,
        remanent_num as surplusNum,
        limit_num as restrictNum
        from cook_recipe_dishes where recipe_detail_id in
        <foreach collection="detailIds" item="detailId" separator="," open="(" close=")">
            #{detailId}
        </foreach>
    </select>

    <select id="getMenuRecipeListByDishesIds" resultType="com.bonus.canteen.core.android.vo.CookRecipeMealCompressVO">
        select
            md.dishes_id as dishesId,
            md.custom_id as customId,
            md.dishes_name as productName,
            md.price as originalPrice,
            md.sales_mode as salesMode,
            md.weight_deviation as weightDeviation,
            md.weight,
            mdt.dishes_type_id as typeId,
            mdt.dishes_type_name as typeName,
            md.purine,
            md.iodine,
            md.initial_score as initialScore,
            md.calories,
            md.protein,
            md.fat,
            md.carbohydrate,
            md.dietary_fiber as dietaryFiber,
            md.cholesterol,
            md.calcium,
            md.sodium,
            md.intro
        from cook_dishes md
                 left join cook_dishes_type mdt on md.type_id = mdt.dishes_type_id
        where dishes_id in
        <foreach collection="dishesIds" item="dishesId" separator="," open="(" close=")">
            #{dishesId}
        </foreach>
    </select>

    <select id="getMealTimeList" resultType="com.bonus.canteen.core.android.vo.CookMealTimeVO">
        SELECT
            mealtime_type AS mealTimeType,
            mealtime_name AS mealTimeName,
            start_time AS startTime,
            end_time AS endTime
        FROM basic_stall_mealtime
        WHERE stall_id =  #{stallId}
    </select>

    <select id="getBasicsTimeIntervals" resultType="com.bonus.canteen.core.android.vo.DeviceMealtimeVO">
        select
            mealtime_type as intervalId,
            mealtime_name as intervalName,
            start_time as startTime,
            end_time as endTime,
            '1' as ifUse
        FROM basic_stall_mealtime
        where stall_id = #{stallId}
    </select>
</mapper>

