<?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.fastbee.ggroup.mapper.GGroupsMapper">

    <resultMap type="GGroups" id="GGroupsResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="icon"    column="icon"    />
        <result property="tag"    column="tag"    />
        <result property="parentId"    column="parent_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="space"    column="space"    />
    </resultMap>

    <resultMap id="GGroupsSiteResult" type="GGroupSiteDto" >
        <result property="parentId" column="group_id" />
        <result property="parentName" column="group_name" />
        <result property="siteId" column="id" />
        <result property="name" column="name" />
        <result property="icon" column="icon" />
        <result property="type" column="type" />
        <result property="space" column="space" />

    </resultMap>


    <sql id="selectGGroupsVo">
        select id, name, icon, tag, parent_id, project_id, project_name, order_num, create_by, create_time, update_time, update_by, space from g_groups
    </sql>


    <!-- 查询组结构列表 -->
    <select id="selectGGroupsList" parameterType="GGroups" resultMap="GGroupsResult">
        <include refid="selectGGroupsVo"/>
        <where>
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="tag != null "> and tag = #{tag}</if>
        </where>
    </select>



<!--    <select id="selectGGroupsSiteList" parameterType="GGroups" resultMap="" >-->

<!--    </select>-->


    <select id="selectGGroupsById" parameterType="Long" resultMap="GGroupsResult">
        <include refid="selectGGroupsVo"/>
        where id = #{id}
    </select>

    <insert id="insertGGroups" parameterType="GGroups" useGeneratedKeys="true" keyProperty="id">
        insert into g_groups
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="icon != null">icon,</if>
            <if test="tag != null">tag,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="projectName != null">project_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="space != null">space,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="icon != null">#{icon},</if>
            <if test="tag != null">#{tag},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="space != null">#{space},</if>
        </trim>
    </insert>

    <update id="updateGGroups" parameterType="GGroups">
        update g_groups
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="icon != null">icon = #{icon},</if>
            <if test="tag != null">tag = #{tag},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>

            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="space != null">space = #{space},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGGroupsById" parameterType="Long">
        delete from g_groups where id = #{id}
    </delete>

    <delete id="deleteGGroupsByIds" parameterType="String">
        delete from g_groups where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>