<?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.kxs.system.biz.mapper.KxsCampMapper">

    <resultMap type="com.kxs.system.api.model.KxsCamp" id="KxsCampMap">
        <result column="id" property="id" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="version" property="version" />
        <result column="status" property="status" />
        <result column="camp_num" property="campNum" />
        <result column="camp_type" property="campType" />
        <result column="create_user_id" property="createUserId" />
        <result column="create_username" property="createUsername" />
        <result column="create_code" property="createCode" />
        <result column="user_num" property="userNum" />
        <result column="bonus_pool" property="bonusPool" />
        <result column="open_num" property="openNum" />
        <result column="order_num" property="orderNum" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="create_by" property="createBy" />
        <result column="update_by" property="updateBy" />
        <result column="remark" property="remark" />
        <result column="pic_url" property="picUrl" />
        <result column="title" property="title" />

    </resultMap>
    <select id="getByPage" resultType="com.kxs.system.api.vo.kxsapp.camp.CampPageVO">
        SELECT a.id, a.status, a.camp_num, a.camp_type, a.pic_url, a.title, a.start_time, a.end_time
        from kxs_camp a
        left join kxs_camp_user b on a.id = b.camp_id
        <where>
            a.del_flag = 0
            and (b.user_id =  #{userId} or a.create_user_id = #{userId})
        </where>
        group by a.id
        order by id desc
    </select>


    <select id="selectUserCamp" resultType="com.kxs.system.api.model.KxsCamp">
        SELECT a.id, a.status, a.camp_num, a.camp_type, a.pic_url, a.title, a.bonus_pool, a.open_num, a.order_num, a.start_time, a.end_time
        from kxs_camp a
        left join kxs_camp_user b on a.id = b.camp_id
        <where>
            and a.del_flag = 0
            and b.user_id =  #{userId}
            and a.status =  1
        </where>
        limit 1
    </select>
    
    <select id="selectParentUsersCamp" resultType="com.kxs.system.api.model.KxsCampUser">
        SELECT b.id, b.camp_id, b.user_id, b.team_order_pool, b.team_lkb_num, b.team_gd_num, b.team_pos_num, b.team_leader_pool, b.team_leader_num, b.team_order_num, b.team_open_num
        from kxs_camp a
        left join kxs_camp_user b on a.id = b.camp_id
        <where>
            and a.del_flag = 0
            and b.user_id in
            <foreach collection="userIds" item="userId" index="index" open="(" close=")" separator=",">
                #{userId}
            </foreach>
            and a.status =  1
        </where>
        ORDER BY FIELD(b.user_id, <foreach collection="userIds" item="userId" separator=","> #{userId} </foreach>)
        limit 1
    </select>
    <select id="getBySysPage" resultType="com.kxs.system.api.vo.admin.camp.SysCampPageVO">
        select a.*, count(b.group_id) as group_count from kxs_camp a
        left join kxs_camp_group b on a.id = b.camp_id
        <where>
            and a.del_flag = 0
            <if test="param.getCreateCode != null and param.getCreateCode != ''">
                and  create_code = #{param.getCreateCode}
            </if>
        </where>
        group by a.id
        order by
        a.create_time desc
    </select>

</mapper>
