<?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.MenuCollectionDishesMapper">
    <resultMap type="com.bonus.canteen.core.menu.domain.MenuCollectionDishes" id="MenuCollectionDishesResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="shopstallId"    column="shopstall_id"    />
        <result property="dishesId"    column="dishes_id"    />
        <result property="revision"    column="revision"    />
        <result property="delFlag"    column="del_flag"    />
        <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="dishesName"    column="dishes_name"    />
        <result property="imageUrl"    column="image_url"    />
        <result property="price"    column="price"    />
        <result property="saleNum"    column="sale_num"    />
    </resultMap>

    <sql id="selectMenuCollectionDishesVo">
        select mcd.id, mcd.user_id, mcd.shopstall_id, mcd.dishes_id, mcd.revision, mcd.del_flag, mcd.create_by,
               mcd.create_time, mcd.update_by, mcd.update_time, md.dishes_name, md.image_url, md.price, mdsr.sale_num
        from menu_collection_dishes mcd
        left join menu_dishes md on md.dishes_id = mcd.dishes_id
        left join menu_dishes_sale_record mdsr on mcd.dishes_id = mdsr.dishes_id
    </sql>

    <select id="selectMenuCollectionDishesList" parameterType="com.bonus.canteen.core.menu.domain.MenuCollectionDishes" resultMap="MenuCollectionDishesResult">
        <include refid="selectMenuCollectionDishesVo"/>
        <where>  
            <if test="userId != null "> and mcd.user_id = #{userId}</if>
            <if test="shopstallId != null "> and mcd.shopstall_id = #{shopstallId}</if>
            <if test="dishesId != null "> and mcd.dishes_id = #{dishesId}</if>
            <if test="revision != null "> and mcd.revision = #{revision}</if>
        </where>
    </select>
    
    <select id="selectMenuCollectionDishesById" parameterType="Long" resultMap="MenuCollectionDishesResult">
        <include refid="selectMenuCollectionDishesVo"/>
        where mcd.dishes_id = #{dishesId}
    </select>

    <select id = "getCollectionCountByUserIdAndDishesId" resultType="Integer">
        select count(1)
        from menu_collection_dishes
        where user_id = #{userId} and dishes_id = #{dishesId}
    </select>
        
    <insert id="insertMenuCollectionDishes" parameterType="com.bonus.canteen.core.menu.domain.MenuCollectionDishes" useGeneratedKeys="true" keyProperty="id">
        insert into menu_collection_dishes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="shopstallId != null">shopstall_id,</if>
            <if test="dishesId != null">dishes_id,</if>
            <if test="revision != null">revision,</if>
            <if test="delFlag != null">del_flag,</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="userId != null">#{userId},</if>
            <if test="shopstallId != null">#{shopstallId},</if>
            <if test="dishesId != null">#{dishesId},</if>
            <if test="revision != null">#{revision},</if>
            <if test="delFlag != null">#{delFlag},</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="updateMenuCollectionDishes" parameterType="com.bonus.canteen.core.menu.domain.MenuCollectionDishes">
        update menu_collection_dishes
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="shopstallId != null">shopstall_id = #{shopstallId},</if>
            <if test="dishesId != null">dishes_id = #{dishesId},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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 dishes_id = #{dishesId}
    </update>

    <delete id="deleteMenuCollectionDishesById" parameterType="String">
        delete from menu_collection_dishes where dishes_id = #{dishesId}
    </delete>

    <delete id="deleteMenuCollectionDishesByIds">
        delete from menu_collection_dishes
        where user_id = #{userId} and dishes_id in
        <foreach item="dishesId" collection="dishesIds" open="(" separator="," close=")">
            #{dishesId}
        </foreach>
    </delete>
</mapper>