<?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.huashe.park.core.mapper.ConsUnitInfoMapper">

    <resultMap type="ConsUnitInfo" id="ConsUnitInfoResult">
        <result property="id" column="id"/>
        <result property="penningType" column="penning_type"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="createType" column="create_type"/>
        <result property="classifyType" column="classify_type"/>
        <result property="name" column="name"/>
        <result property="ancestors" column="ancestors"/>
        <result property="projectId" column="project_id"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <sql id="selectConsUnitInfoVo">
        select id,
               penning_type,
               type,
               parent_id,
               create_type,
               classify_type,
               name,
               ancestors,
               project_id,
               update_time,
               create_time,
               create_by,
               update_by
        from cons_cons_unit_info
    </sql>

    <select id="selectConsUnitInfoList" parameterType="ConsUnitInfo" resultMap="ConsUnitInfoResult">
        <include refid="selectConsUnitInfoVo"/>
        <where>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
        </where>
    </select>

    <select id="selectConsUnitInfoById" resultMap="ConsUnitInfoResult">
        <include refid="selectConsUnitInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertConsUnitInfo" parameterType="ConsUnitInfo" useGeneratedKeys="true" keyProperty="id">
        insert into cons_cons_unit_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="penningType != null">penning_type,</if>
            <if test="type != null">type,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="createType != null">create_type,</if>
            <if test="classifyType != null">classify_type,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="ancestors != null and ancestors!=''">ancestors,</if>
            <if test="projectId != null">update_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="penningType != null">#{penningType},</if>
            <if test="type != null">#{type},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="createType != null">#{createType},</if>
            <if test="classifyType != null">#{classifyType},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="ancestors != null and ancestors!=''">#{ancestors},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>
    <update id="updateConsUnitInfo" parameterType="ConsUnitInfo">
        update cons_cons_unit_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="penningType != null">penning_type = #{penningType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="createType != null">create_type = #{createType},</if>
            <if test="classifyType != null">classify_type = #{classifyType},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="ancestors != null and ancestors!=''">ancestors = #{ancestors},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteConsUnitInfoById">
        delete
        from cons_cons_unit_info
        where id = #{id}
    </delete>

    <delete id="deleteConsUnitInfoByIds" parameterType="Long">
        delete from cons_cons_unit_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="hasChildById" parameterType="Long" resultType="int">
        select count(1)
        from cons_cons_unit_info
        where parent_id = #{id} limit 1
    </select>

    <select id="selectChildrenById" parameterType="Long" resultMap="ConsUnitInfoResult">
        select *
        from cons_cons_unit_info
        where find_in_set(#{id}, ancestors)
    </select>

    <update id="updateConstUnitChildren" parameterType="java.util.List">
        update cons_cons_unit_info set ancestors =
        <foreach collection="consUnits" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.ancestors}
        </foreach>
        where id in
        <foreach collection="consUnits" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>
</mapper>