<?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.leromro.core.mapper.BusinessManagementMapper">
    
    <resultMap type="BusinessManagement" id="BusinessManagementResult">
        <result property="businessManagementId"    column="business_management_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="businessName"    column="business_name"    />
        <result property="businessTierName"    column="business_tier_name"    />
        <result property="businessIcon"    column="business-_con"    />
        <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"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <sql id="selectBusinessManagementVo">
        select business_management_id, parent_id, ancestors, business_name, business_tier_name,business_icon, create_by, create_time, update_by, update_time, remark, sort from l_business_management
    </sql>

    <select id="selectBusinessManagementList" parameterType="BusinessManagement" resultMap="BusinessManagementResult">
        <include refid="selectBusinessManagementVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="businessName != null  and businessName != ''"> and business_name like concat('%', #{businessName}, '%')</if>
            <if test="businessTierName != null  and businessTierName != ''"> and business_tier_name like concat('%', #{businessTierName}, '%')</if>
            <if test="sort != null "> and sort = #{sort}</if>
        </where>
    </select>
    
    <select id="selectBusinessManagementByBusinessManagementId" parameterType="Long" resultMap="BusinessManagementResult">
        <include refid="selectBusinessManagementVo"/>
        where business_management_id = #{businessManagementId}
    </select>
    <select id="getTreeList" resultType="com.leromro.core.domain.vo.BusinessManagementVO">
        select business_management_id as id,parent_id,business_name,business_tier_name,business_icon
        from l_business_management
        <where>
            find_in_set(#{parentId},ancestors)
        </where>
        order by sort
    </select>

    <insert id="insertBusinessManagement" parameterType="BusinessManagement" useGeneratedKeys="true" keyProperty="businessManagementId">
        insert into l_business_management
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="businessName != null">business_name,</if>
            <if test="businessTierName != null">business_tier_name,</if>
            <if test="businessIcon != null">business_icon,</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>
            <if test="remark != null">remark,</if>
            <if test="sort != null">sort,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="businessName != null">#{businessName},</if>
            <if test="businessTierName != null">#{businessTierName},</if>
            <if test="businessIcon != null">#{businessIcon},</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>
            <if test="remark != null">#{remark},</if>
            <if test="sort != null">#{sort},</if>
         </trim>
    </insert>

    <update id="updateBusinessManagement" parameterType="BusinessManagement">
        update l_business_management
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="businessName != null">business_name = #{businessName},</if>
            <if test="businessTierName != null">business_tier_name = #{businessTierName},</if>
            <if test="businessIcon != null">business_icon = #{businessIcon},</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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where business_management_id = #{businessManagementId}
    </update>

    <delete id="deleteBusinessManagementByBusinessManagementId" parameterType="Long">
        delete from l_business_management where business_management_id = #{businessManagementId}
    </delete>

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