<?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.agencycategory.AgencyCategoryMapper">

    <resultMap type="AgencyCategory" id="AgencyCategoryResult">
        <result property="id" column="id"/>
        <result property="parentCategoryId" column="parent_category_id"/>
        <result property="categoryName" column="category_name"/>
        <result property="categoryCode" column="category_code"/>
        <result property="categoryLevel" column="category_level"/>
        <result property="categorySort" column="category_sort"/>
        <result property="categoryRemark" column="category_remark"/>
        <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="selectAgencyCategoryVo">
        select id,
               parent_category_id,
               category_name,
               category_code,
               category_level,
               category_sort,
               category_remark,
               create_by,
               create_time,
               update_by,
               update_time
        from agency_category
    </sql>

    <select id="selectAgencyCategoryList" parameterType="AgencyCategory" resultMap="AgencyCategoryResult">
        <include refid="selectAgencyCategoryVo"/>
        <where>
            <if test="parentCategoryId != null ">
                and parent_category_id = #{parentCategoryId}
            </if>
            <if test="categoryName != null  and categoryName != ''">
                and category_name like concat('%', #{categoryName}, '%')
            </if>
            <if test="categoryCode != null  and categoryCode != ''">
                and category_code = #{categoryCode}
            </if>
            <if test="categoryLevel != null ">
                and category_level = #{categoryLevel}
            </if>
            <if test="categorySort != null ">
                and category_sort = #{categorySort}
            </if>
            <if test="categoryRemark != null  and categoryRemark != ''">
                and category_remark = #{categoryRemark}
            </if>
        </where>
    </select>

    <select id="selectAgencyCategoryVOList" resultType="com.xinelu.manage.vo.agencycategory.AgencyCategoryVO">
        <include refid="selectAgencyCategoryVo"/>
    </select>

    <select id="selectAgencyCategoryById" parameterType="Long"
            resultMap="AgencyCategoryResult">
        <include refid="selectAgencyCategoryVo"/>
        where id = #{id}
    </select>

    <insert id="insertAgencyCategory" parameterType="AgencyCategory" useGeneratedKeys="true"
            keyProperty="id">
        insert into agency_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentCategoryId != null">parent_category_id,
            </if>
            <if test="categoryName != null">category_name,
            </if>
            <if test="categoryCode != null">category_code,
            </if>
            <if test="categoryLevel != null">category_level,
            </if>
            <if test="categorySort != null">category_sort,
            </if>
            <if test="categoryRemark != null">category_remark,
            </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="parentCategoryId != null">#{parentCategoryId},
            </if>
            <if test="categoryName != null">#{categoryName},
            </if>
            <if test="categoryCode != null">#{categoryCode},
            </if>
            <if test="categoryLevel != null">#{categoryLevel},
            </if>
            <if test="categorySort != null">#{categorySort},
            </if>
            <if test="categoryRemark != null">#{categoryRemark},
            </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="updateAgencyCategory" parameterType="AgencyCategory">
        update agency_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentCategoryId != null">parent_category_id =
                #{parentCategoryId},
            </if>
            <if test="categoryName != null">category_name =
                #{categoryName},
            </if>
            <if test="categoryCode != null">category_code =
                #{categoryCode},
            </if>
            <if test="categoryLevel != null">category_level =
                #{categoryLevel},
            </if>
            <if test="categorySort != null">category_sort =
                #{categorySort},
            </if>
            <if test="categoryRemark != null">category_remark =
                #{categoryRemark},
            </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>

    <delete id="deleteAgencyCategoryById" parameterType="Long">
        delete
        from agency_category
        where id = #{id}
    </delete>

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