<?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.boman.system.mapper.BomanGroupMapper">

    <resultMap type="com.boman.domain.BomanGroup" id="BomanGroupResult">
        <id property="id" column="id"/>
        <result property="groupName" column="group_name"/>
        <result property="status" column="status"/>
        <result property="number" column="number"/>
        <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="isDel" column="is_del"/>
        <collection  property="sysUserList"   javaType="java.util.List"        resultMap="SysUserResult" />
    </resultMap>

    <resultMap type="com.boman.domain.SysUser" id="SysUserResult">
        <id     property="id"       column="id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="userName"     column="user_name"    />
        <result property="nickName"     column="nick_name"    />
        <result property="email"        column="email"        />
        <result property="phonenumber"  column="phonenumber"  />
        <result property="sex"          column="sex"          />
        <result property="avatar"       column="avatar"       />
        <result property="password"     column="password"     />
        <result property="status"       column="status"       />
        <result property="delFlag"      column="del_flag"     />
        <result property="loginIp"      column="login_ip"     />
        <result property="loginDate"    column="login_date"   />
        <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="remark"       column="remark"       />
    </resultMap>


    <sql id="selectGroupVo">
        select g.id, g.group_name, g.status, g.number, g.create_by, g.create_time, g.is_del
        from boman_group g
    </sql>

    <select id="selectGroupList" parameterType="com.boman.domain.BomanGroup" resultMap="BomanGroupResult">
        select g.id, g.group_name, g.status, g.number,  g.create_by, g.create_time, g.is_del, u.user_name , u.id
        from boman_group g
        left join boman_group_user gu on gu.group_id = g.id
        where g.is_del = 'N'
        <if test="groupName != null and groupName != ''">
            AND g.groupName like concat('%', #{groupName}, '%')
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by g.number
    </select>

    <select id="selectGroupById" parameterType="Long" resultMap="BomanGroupResult">
        select g.id, g.group_name, g.status, g.number,  g.create_by, g.create_time, g.is_del, gu.user_name , gu.id
        from boman_group g
        left join boman_group_user gu on gu.group_id = g.id
        where g.is_del = 'N'
        and g.id = #{id}
    </select>

    <select id="checkGroupNameUnique" resultMap="BomanGroupResult">
        <include refid="selectGroupVo"/>
        where group_name=#{GroupName} limit 1
    </select>

    <insert id="insertGroup" parameterType="com.boman.domain.BomanGroup">
        insert into boman_group(
        <if test="id != null and id != 0">id,</if>
        <if test="groupName != null and groupName != ''">group_name,</if>
        <if test="number != null and number != ''">number,</if>
        <if test="status != null">status,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="isDel != null and isDel != ''">is_del,</if>
        create_time
        )values(
        <if test="id != null and id != 0">#{id},</if>
        <if test="groupName != null and groupName != ''">#{groupName},</if>
        <if test="number != null and number != ''">#{number},</if>
        <if test="status != null">#{status},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="isDel != null and isDel != ''">#{isDel},</if>
        sysdate()
        )
    </insert>

    <update id="updateGroup" parameterType="com.boman.domain.BomanGroup">
        update boman_group
        <set>
            <if test="groupName != null and groupName != ''">group_name = #{groupName},</if>
            <if test="number != null and number != ''">number = #{number},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="isDel != null and isDel != ''">is_del = #{isDel},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>


    <select id="checkGroupExistUser" parameterType="Long" resultType="int">
        select count(1) from boman_group_user where group_id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and is_del = 'N'
    </select>


	<update id="deleteGroupById" parameterType="Long">
		update  boman_group_user set is_del = 'Y' where id in
		<foreach collection="array" item="id" open="(" separator="," close=")">
			#{id}
		</foreach>
	</update>
</mapper> 