<?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.xinyilu.system.mapper.itemCategory.TestItemCategoryMapper">

    <resultMap type="TestItemCategory" id="TestItemCategoryResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="categoryName" column="category_name"/>
        <result property="categoryNameAbbreviate" column="category_name_abbreviate"/>
        <result property="sorting" column="sorting"/>
        <result property="remark" column="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"/>
        <result property="categoryCode" column="category_code"/>
    </resultMap>

    <sql id="selectTestItemCategoryVo">
        select id, parent_id, category_name, category_name_abbreviate, sorting, remark, del_flag, create_by, create_time, update_by, update_time,category_code from test_item_category
    </sql>

    <select id="selectTestItemCategoryList" parameterType="TestItemCategory" resultMap="TestItemCategoryResult">
        <include refid="selectTestItemCategoryVo"/>
        <where>
            <if test="parentId != null ">
                and parent_id = #{parentId}
            </if>
            <if test="categoryName != null  and categoryName != ''">
                and category_name like concat('%', #{categoryName}, '%')
            </if>
            <if test="categoryNameAbbreviate != null  and categoryNameAbbreviate != ''">
                and category_name_abbreviate like concat('%', #{categoryNameAbbreviate}, '%')
            </if>
            <if test="sorting != null ">
                and sorting = #{sorting}
            </if>
            and del_flag = 0
        </where>
        order by create_time desc
    </select>

    <select id="selectTestItemCategoryById" parameterType="Long"
            resultType="TestItemCategoryVO">
        <include refid="selectTestItemCategoryVo"/>
        where id = #{id} and del_flag = 0
    </select>

    <insert id="insertTestItemCategory" parameterType="TestItemCategory" useGeneratedKeys="true"
            keyProperty="id">
        insert into test_item_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,
            </if>
            <if test="categoryName != null">category_name,
            </if>
            <if test="categoryNameAbbreviate != null">category_name_abbreviate,
            </if>
            <if test="sorting != null">sorting,
            </if>
            <if test="remark != null">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>
            <if test="categoryCode != null">category_code,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},
            </if>
            <if test="categoryName != null">#{categoryName},
            </if>
            <if test="categoryNameAbbreviate != null">#{categoryNameAbbreviate},
            </if>
            <if test="sorting != null">#{sorting},
            </if>
            <if test="remark != null">#{remark},
            </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>
            <if test="categoryCode != null">#{categoryCode},
            </if>
        </trim>
    </insert>

    <update id="updateTestItemCategory" parameterType="TestItemCategory">
        update test_item_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id =
                #{parentId},
            </if>
            <if test="categoryName != null">category_name =
                #{categoryName},
            </if>
            <if test="categoryNameAbbreviate != null">category_name_abbreviate =
                #{categoryNameAbbreviate},
            </if>
            <if test="sorting != null">sorting =
                #{sorting},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </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>

    <delete id="deleteTestItemCategoryById" parameterType="Long">
        delete from test_item_category where id = #{id}
    </delete>

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

    <update id="updateTestItemCategoryByIds" parameterType="java.lang.Long">
        update test_item_category set del_flag = 1, update_time = now() where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getTestItemCategoryIds" resultType="int">
        SELECT
        COUNT(1) as categoryCount
        FROM
        test_item_category tic
        <where>
            <if test="parentId != null  and parentId != ''">
                tic.parent_id = #{parentId}
            </if>
            <if test="categoryName != null  and categoryName != ''">
                and tic.category_name = #{categoryName}
            </if>
            and tic.del_flag = 0
        </where>
    </select>

    <select id="getRootCategoryInfo" resultType="int">
        select count(1) from test_item_category where parent_id = 0
    </select>
</mapper>
