<?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.backendsys.mapper.Med.MedMaterialCategoryMapper">

    <sql id="includeMaterialCategory">
        id,
        id material_category_id,
        COALESCE(parent_id, '') parent_id,
        category_name,
        COALESCE(category_description, '') category_description,
        sort,
        status
    </sql>

    <resultMap id="resultMapMaterialCategory" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="parent_id" column="parent_id" javaType="java.lang.Long"/>
        <result property="category_name" column="category_name" />
        <result property="category_description" column="category_description" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
    </resultMap>

    <resultMap id="resultMapMaterialCategoryDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="parent_id" column="parent_id" javaType="java.lang.Long"/>
        <result property="category_name" column="category_name" />
        <result property="category_description" column="category_description" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
    </resultMap>

    <select id="queryMaterialCategoryList" resultMap="resultMapMaterialCategory">
        SELECT <include refid="includeMaterialCategory" />
        FROM med_material_category
        <where>
            <if test="category_name != null and category_name != ''">
                AND category_name LIKE CONCAT('%', #{category_name}, '%')
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
        </where>
        ORDER BY sort DESC
    </select>

    <select id="queryMaterialCategoryDetail" resultMap="resultMapMaterialCategoryDetail">
        SELECT <include refid="includeMaterialCategory" />
        FROM med_material_category
        WHERE id = #{material_category_id}
    </select>

    <insert id="insertMaterialCategory" parameterType="com.backendsys.entity.Med.MedMaterialCategoryDTO"
        useGeneratedKeys="true" keyProperty="material_category_id">
        INSERT INTO med_material_category (category_name
            <if test="category_description != null and category_description != ''">, category_description</if>
            <if test="parent_id != null and parent_id != ''">, parent_id</if>
            <if test="sort != null and sort != ''">, sort</if>
            <if test="status != null and status != ''">, status</if>
        )
        VALUES (#{category_name}
            <if test="category_description != null and category_description != ''">, #{category_description}</if>
            <if test="parent_id != null and parent_id != ''">, #{parent_id}</if>
            <if test="sort != null and sort != ''">, #{sort}</if>
            <if test="status != null and status != ''">, #{status}</if>
        )
    </insert>

    <update id="updateMaterialCategory" parameterType="com.backendsys.entity.Med.MedMaterialCategoryDTO"
        useGeneratedKeys="true" keyProperty="material_category_id">
        UPDATE med_material_category
        SET category_name = #{category_name}
            <if test="category_description != null and category_description != ''">, category_description = #{category_description}</if>
            <if test="parent_id != null and parent_id != ''">, parent_id = #{parent_id}</if>
            <if test="sort != null and sort != ''">, sort = #{sort}</if>
            <if test="status != null and status != ''">, status = #{status}</if>
        WHERE id = #{material_category_id}
    </update>

    <delete id="deleteMaterialCategory">
        DELETE FROM med_material_category WHERE id = #{material_category_id}
    </delete>

</mapper>