<?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.willalp.canteen.mapper.HsCanteenMenuRegisterMapper">

    <resultMap type="HsCanteenMenuRegister" id="HsCanteenMenuRegisterResult">
        <result property="registerId" column="register_id"/>
        <result property="menuNumber" column="menu_number"/>
        <!--        <result property="periodId"    column="period_id"    />-->
        <!--        <result property="patternId"    column="pattern_id"    />-->
        <!--        <result property="consumerType"    column="consumer_type"    />-->
        <result property="cuisineNumber" column="cuisine_number"/>
        <result property="cuisineName" column="cuisine_name"/>
        <result property="price" column="price"/>
        <result property="integral" column="integral"/>
        <result property="stock" column="stock"/>
        <result property="surplus" column="surplus"/>
        <result property="maximum" column="maximum"/>
        <result property="jgid" column="jgid"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <sql id="selectHsCanteenMenuRegisterVo">
        select register_id,
               menu_number,
               cuisine_number,
               cuisine_name,
               price,
               integral,
               stock,
               is_delete,
               surplus,
               maximum,
               jgid
        from hs_canteen_menu_register
    </sql>

    <select id="selectHsCanteenMenuRegisterList" parameterType="HsCanteenMenuRegister"
            resultMap="HsCanteenMenuRegisterResult">
        <include refid="selectHsCanteenMenuRegisterVo"/>
        <where>
            is_delete = 0
            and menu_number = #{menuNumber}
            and jgid = #{jgid}
            <!--            <if test="periodId != null "> and period_id = #{periodId}</if>-->
            <!--            <if test="patternId != null "> and pattern_id = #{patternId}</if>-->
            <!--            <if test="consumerType != null "> and consumer_type = #{consumerType}</if>-->
            <if test="cuisineNumber != null  and cuisineNumber != ''">and cuisine_number = #{cuisineNumber}</if>
            <if test="cuisineName != null  and cuisineName != ''">and cuisine_name like concat('%', #{cuisineName},
                '%')
            </if>
        </where>
    </select>

    <select id="selectCuisineList" parameterType="HsCanteenMenuRegister"
            resultType="com.willalp.canteen.domain.resp.CuisineListResp">
        select a.menu_number as menuNumber, a.cuisine_number as cuisineNumber, a.cuisine_name as cuisineName,
        a.price as price, a.integral as integral, a.stock as stock, a.surplus as surplus,
        a.jgid as jgid,b.cuisine_pictrue as cuisinePicture,a.maximum as maximum
        from hs_canteen_menu_register a join hs_canteen_cuisine b on a.cuisine_number = b.cuisine_number and a.jgid =
        b.jgid
        <where>
            a.is_delete = 0 and b.is_delete = 0
            <if test="hsCanteenMenuRegister.menuNumber != null  and hsCanteenMenuRegister.menuNumber != ''">and
                a.menu_number = #{hsCanteenMenuRegister.menuNumber}
            </if>
            <if test="hsCanteenMenuRegister.cuisineNumber != null  and hsCanteenMenuRegister.cuisineNumber != ''">and
                a.cuisine_number = #{hsCanteenMenuRegister.cuisineNumber}
            </if>
            <if test="hsCanteenMenuRegister.cuisineName != null  and hsCanteenMenuRegister.cuisineName != ''">and
                a.cuisine_name like concat('%', #{hsCanteenMenuRegister.cuisineName},
                '%')
            </if>
            <if test="hsCanteenMenuRegister.jgid != null and hsCanteenMenuRegister.jgid != ''">and a.jgid =
                #{hsCanteenMenuRegister.jgid}
            </if>
        </where>
        <if test="size !=0">
            limit #{page},#{size}
        </if>
    </select>

    <select id="selectRegisterStockList" parameterType="HsCanteenMenuRegister" resultMap="HsCanteenMenuRegisterResult">
        select a.register_id, a.menu_number, a.cuisine_number, a.cuisine_name,
        a.price, a.integral, t.stock, a.is_delete,
        a.surplus, a.maximum, sd.dept_name as jgid
        from hs_canteen_menu_register a left join(
        select sum(b.amount) as stock,b.menu_number,b.cuisine_number,a.jgid
        from hs_canteen_order a join hs_canteen_order_detail b on a.order_number = b.order_number and a.zfbz != 1 and
        a.is_delete = 0 and a.jgid=b.jgid
        group by b.menu_number,b.cuisine_number,a.jgid
        ) t on a.menu_number = t.menu_number and a.cuisine_number = t.cuisine_number and a.jgid = t.jgid
        left join sys_dept sd on sd.dept_code = a.jgid
        where
        a.is_delete = 0
        <if test="menuNumber != null  and menuNumber != ''">and a.menu_number = #{menuNumber}</if>
        <if test="cuisineNumber != null  and cuisineNumber != ''">and a.cuisine_number = #{cuisineNumber}</if>
        <if test="cuisineName != null  and cuisineName != ''">and a.cuisine_name like concat('%', #{cuisineName},'%')
        </if>
        <if test="jgid != null and jgid != ''">and a.jgid = #{jgid}</if>
    </select>
    <select id="selectCuisineCount" resultType="java.lang.Integer">
        select count(0)
        from hs_canteen_menu_register a join hs_canteen_cuisine b on a.cuisine_number = b.cuisine_number and a.jgid =
        b.jgid
        <where>
            a.is_delete = 0 and b.is_delete = 0
            <if test="hsCanteenMenuRegister.menuNumber != null  and hsCanteenMenuRegister.menuNumber != ''">and
                a.menu_number = #{hsCanteenMenuRegister.menuNumber}
            </if>
            <if test="hsCanteenMenuRegister.cuisineNumber != null  and hsCanteenMenuRegister.cuisineNumber != ''">and
                a.cuisine_number = #{hsCanteenMenuRegister.cuisineNumber}
            </if>
            <if test="hsCanteenMenuRegister.cuisineName != null  and hsCanteenMenuRegister.cuisineName != ''">and
                a.cuisine_name like concat('%', #{hsCanteenMenuRegister.cuisineName},
                '%')
            </if>
            <if test="hsCanteenMenuRegister.jgid != null and hsCanteenMenuRegister.jgid != ''">and a.jgid =
                #{hsCanteenMenuRegister.jgid}
            </if>
        </where>
    </select>

</mapper>
