<?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.xinelu.manage.mapper.projectgroup.ProjectGroupMapper">

    <resultMap type="ProjectGroup" id="ProjectGroupResult">
            <result property="id" column="id"/>
            <result property="parentId" column="parent_id"/>
            <result property="groupName" column="group_name"/>
            <result property="groupCode" column="group_code"/>
            <result property="enableStatus" column="enable_status"/>
            <result property="groupSort" column="group_sort"/>
            <result property="groupRemark" column="group_remark"/>
            <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"/>
    </resultMap>

    <sql id="selectProjectGroupVo">
        select id, parent_id, group_name, group_code, enable_status, group_sort, group_remark, del_flag, create_by, create_time, update_by, update_time from project_group
    </sql>

    <select id="selectProjectGroupList" parameterType="ProjectGroup" resultMap="ProjectGroupResult">
        <include refid="selectProjectGroupVo"/>
        <where>
			del_flag = 0
            <if test="parentId != null ">
                and parent_id = #{parentId}
            </if>
            <if test="groupName != null  and groupName != ''">
                and group_name like concat('%', #{groupName}, '%')
            </if>
            <if test="groupCode != null  and groupCode != ''">
                and group_code = #{groupCode}
            </if>
            <if test="enableStatus != null ">
                and enable_status = #{enableStatus}
            </if>
        </where>
    </select>

    <select id="selectProjectGroupById" parameterType="Long"
            resultMap="ProjectGroupResult">
            <include refid="selectProjectGroupVo"/>
            where id = #{id}
    </select>

    <insert id="insertProjectGroup" parameterType="ProjectGroup" useGeneratedKeys="true"
            keyProperty="id">
        insert into project_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,
            </if>
            <if test="groupName != null">group_name,
            </if>
            <if test="groupCode != null">group_code,
            </if>
            <if test="enableStatus != null">enable_status,
            </if>
            <if test="groupSort != null">group_sort,
            </if>
            <if test="groupRemark != null">group_remark,
            </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="parentId != null">#{parentId},
            </if>
            <if test="groupName != null">#{groupName},
            </if>
            <if test="groupCode != null">#{groupCode},
            </if>
            <if test="enableStatus != null">#{enableStatus},
            </if>
            <if test="groupSort != null">#{groupSort},
            </if>
            <if test="groupRemark != null">#{groupRemark},
            </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="updateProjectGroup" parameterType="ProjectGroup">
        update project_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id =
                #{parentId},
            </if>
            <if test="groupName != null">group_name =
                #{groupName},
            </if>
            <if test="groupCode != null">group_code =
                #{groupCode},
            </if>
            <if test="enableStatus != null">enable_status =
                #{enableStatus},
            </if>
            <if test="groupSort != null">group_sort =
                #{groupSort},
            </if>
            <if test="groupRemark != null">group_remark =
                #{groupRemark},
            </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 id = #{id}
    </update>

    <update id="deleteProjectGroupById" parameterType="Long">
	    update project_group set del_flag = 1 where id = #{id}
    </update>

    <update id="deleteProjectGroupByIds" parameterType="String">
        update project_group set del_flag = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
	<insert id="insertBatch">
		insert into project_group(parent_id,group_name,
		group_code,enable_status,group_sort,
		group_remark,del_flag,
		create_by,create_time,update_by,
		update_time)
		values
		<foreach collection="projectGroupList" item="item" separator=",">
			(#{item.parentId,jdbcType=NUMERIC},#{item.groupName,jdbcType=VARCHAR},
			#{item.groupCode,jdbcType=VARCHAR},#{item.enableStatus,jdbcType=NUMERIC},#{item.groupSort,jdbcType=NUMERIC},
			#{item.groupRemark,jdbcType=VARCHAR},#{item.delFlag,jdbcType=NUMERIC},
			#{item.createBy,jdbcType=VARCHAR},#{item.createTime,jdbcType=TIMESTAMP},#{item.updateBy,jdbcType=VARCHAR},
			#{item.updateTime,jdbcType=TIMESTAMP})
		</foreach>
	</insert>
	<select id="hasSameCodeGroup" parameterType="Long" resultMap="ProjectGroupResult">
		<include refid="selectProjectGroupVo"/>
		<where>
			del_flag = 0
			<if test="id != null">
				and id != #{id}
			</if>
			<if test="groupCode != null and groupCode != ''">
				and group_code = #{groupCode}
			</if>
		</where>
	</select>
</mapper>