<?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.ruoyi.mall.mapper.MallCombineOrderLinkMapper">

    <resultMap type="MallCombineOrderLink" id="MallCombineOrderLinkResult">
        <result property="linkId"    column="link_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="teamId"    column="team_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="combineId"    column="combine_id"    />
        <result property="peopleNum"    column="people_num"    />
        <result property="endMinute"    column="end_minute"    />
        <result property="timeId"    column="time_id"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMallCombineOrderLinkVo">
        select link_id, parent_id, team_id, order_id, order_no, user_id, user_name, avatar, goods_id, combine_id, people_num, end_minute, time_id, start_time, end_time, status, create_at, update_at, del_flag from mall_combine_order_link
    </sql>

    <select id="selectMallCombineOrderLinkList" parameterType="MallCombineOrderLink" resultMap="MallCombineOrderLinkResult">
        select mcol.link_id, mcol.parent_id, mcol.team_id, mcol.order_id, mcol.order_no, mcol.user_id, mmu.name user_name, mmu.avatar, mcol.goods_id
             , mcol.combine_id, mcol.people_num, mcol.end_minute, mcol.time_id, mcol.start_time, mcol.end_time,
               mcol.status, mcol.create_at, mcol.update_at, mcol.del_flag
        from mall_combine_order_link mcol
        left join mall_member_user mmu on mcol.user_id = mmu.id
        <where>
            <if test="parentId != null "> and mcol.parent_id = #{parentId}</if>
            <if test="teamId != null "> and mcol.team_id = #{teamId}</if>
            <if test="orderId != null "> and mcol.order_id = #{orderId}</if>
            <if test="orderNo != null  and orderNo != ''"> and mcol.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="userId != null "> and mcol.user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="goodsId != null "> and mcol.goods_id = #{goodsId}</if>
            <if test="combineId != null "> and mcol.combine_id = #{combineId}</if>
            <if test="peopleNum != null "> and mcol.people_num = #{peopleNum}</if>
            <if test="endMinute != null "> and mcol.end_minute = #{endMinute}</if>
            <if test="timeId != null "> and mcol.time_id = #{timeId}</if>
            <if test="startTime != null  and startTime != ''"> and mcol.start_time = #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and mcol.end_time = #{endTime}</if>
            <if test="status != null "> and mcol.status = #{status}</if>
            <if test="createAt != null  and createAt != ''"> and mcol.create_at = #{createAt}</if>
            <if test="updateAt != null  and updateAt != ''"> and mcol.update_at = #{updateAt}</if>
        </where>
    </select>

    <select id="selectMallCombineOrderLinkByLinkId" parameterType="Long" resultMap="MallCombineOrderLinkResult">
        <include refid="selectMallCombineOrderLinkVo"/>
        where link_id = #{linkId}
    </select>

    <insert id="insertMallCombineOrderLink" parameterType="MallCombineOrderLink" useGeneratedKeys="true" keyProperty="linkId">
        insert into mall_combine_order_link
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="teamId != null">team_id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="combineId != null">combine_id,</if>
            <if test="peopleNum != null">people_num,</if>
            <if test="endMinute != null">end_minute,</if>
            <if test="timeId != null">time_id,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="combineId != null">#{combineId},</if>
            <if test="peopleNum != null">#{peopleNum},</if>
            <if test="endMinute != null">#{endMinute},</if>
            <if test="timeId != null">#{timeId},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateMallCombineOrderLink" parameterType="MallCombineOrderLink">
        update mall_combine_order_link
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="combineId != null">combine_id = #{combineId},</if>
            <if test="peopleNum != null">people_num = #{peopleNum},</if>
            <if test="endMinute != null">end_minute = #{endMinute},</if>
            <if test="timeId != null">time_id = #{timeId},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where link_id = #{linkId}
    </update>

    <delete id="deleteMallCombineOrderLinkByLinkId" parameterType="Long">
        delete from mall_combine_order_link where link_id = #{linkId}
    </delete>

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

    <select id="getWaitCombineList" resultType="map">
        select mcol.link_id linkId, mcol.team_id teamId, mcol.user_id userId,
               mmu.name userName, mmu.avatar,
               mcol.goods_id goodsId, mcol.combine_id combineId, mcol.people_num peopleNum,
               ifnull(mcol.people_num - count(mcol.team_id),mcol.people_num - 1) needNum,
               mcol.start_time startTime, mcol.end_time endTime, mcol.status
        from mall_combine_order_link mcol
        left join mall_combine_goods mcg on mcol.combine_id = mcg.combine_id
        left join mall_member_user mmu on mcol.user_id = mmu.id
        <where>
            <if test="parentId != null "> and mcol.parent_id = #{parentId}</if>
            <if test="teamId != null "> and mcol.team_id = #{teamId}</if>
            <if test="orderId != null "> and mcol.order_id = #{orderId}</if>
            <if test="orderNo != null  and orderNo != ''"> and mcol.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="userId != null "> and mcol.user_id = #{userId}</if>
            <if test="goodsId != null "> and mcol.goods_id = #{goodsId}</if>
            <if test="combineId != null "> and mcol.combine_id = #{combineId}</if>
            <if test="peopleNum != null "> and mcol.people_num = #{peopleNum}</if>
            <if test="startTime != null  and startTime != ''"> and mcol.start_time = #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and mcol.end_time = #{endTime}</if>
            <if test="status != null "> and mcol.status = #{status}</if>
        </where>
        group BY mcol.team_id
        order by mcol.end_time asc, needNum asc
    </select>

    <select id="getWaitCombineInfo" resultType="map">
        select mcol.link_id linkId, mcol.team_id teamId,
               mcol.user_id userId, mmu.name userName, mmu.avatar,
               mcol.goods_id goodsId, mcol.combine_id combineId, mcol.people_num peopleNum,
               mcol.people_num - (SELECT count(mcolX.team_id) FROM mall_combine_order_link mcolX WHERE mcol.link_id = mcolX.team_id and mcolX.status = 0) needNum,
               mcol.start_time startTime, mcol.end_time endTime, mcol.status
        from mall_combine_order_link mcol
        left join mall_combine_goods mcg on mcol.combine_id = mcg.combine_id
        left join mall_member_user mmu on mcol.user_id = mmu.id
        <where>
            <if test="linkId != null "> and mcol.link_id = #{linkId}</if>
            <if test="parentId != null "> and mcol.parent_id = #{parentId}</if>
            <if test="teamId != null "> and mcol.team_id = #{teamId}</if>
            <if test="orderId != null "> and mcol.order_id = #{orderId}</if>
            <if test="orderNo != null  and orderNo != ''"> and mcol.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="userId != null "> and mcol.user_id = #{userId}</if>
            <if test="goodsId != null "> and mcol.goods_id = #{goodsId}</if>
            <if test="combineId != null "> and mcol.combine_id = #{combineId}</if>
            <if test="peopleNum != null "> and mcol.people_num = #{peopleNum}</if>
            <if test="startTime != null  and startTime != ''"> and mcol.start_time = #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and mcol.end_time = #{endTime}</if>
            <if test="status != null "> and mcol.status = #{status}</if>
        </where>
    </select>
</mapper>
