<?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.MedMyMaterialMapper">

    <sql id="includeMyMaterial">
        m.id id,
        m.id my_material_id,
        m.material_id material_id,
        m.user_id user_id,
        mc.id category_id,
        mc.category_name category_name,
        m.title title,
        COALESCE(m.description, '') description,
        COALESCE(m.thumb, '') thumb,
        m.material_type material_type,
        <!-- COALESCE(m.content, '') content, -->
        <!-- COALESCE(m.file, '') file, -->
        m.create_time create_time,
        m.update_time update_time
    </sql>

    <sql id="includeMyMaterialDetail">
        m.id id,
        m.id my_material_id,
        m.material_id material_id,
        m.user_id user_id,
        mc.id category_id,
        mc.category_name category_name,
        m.title title,
        COALESCE(m.description, '') description,
        COALESCE(m.thumb, '') thumb,
        m.material_type material_type,
        COALESCE(m.content, '') content,
        COALESCE(m.file, '') file,
        m.create_time create_time,
        m.update_time update_time
    </sql>

    <resultMap id="resultMapMaterial" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="my_material_id" column="my_material_id" javaType="java.lang.Long"/>
        <result property="material_id" column="material_id" javaType="java.lang.Long"/>
        <result property="user_id" column="user_id" javaType="java.lang.Long"/>
        <result property="category_id" column="category_id" javaType="java.lang.Long"/>
        <result property="category_name" column="category_name" />
        <result property="title" column="title" />
        <result property="description" column="description" />
        <result property="thumb" column="thumb" />
        <result property="material_type" column="material_type" javaType="java.lang.Integer"/>
        <!-- <result property="content" column="content" /> -->
        <!-- <result property="file" column="file" /> -->
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <resultMap id="resultMapMaterialDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="my_material_id" column="my_material_id" javaType="java.lang.Long"/>
        <result property="material_id" column="material_id" javaType="java.lang.Long"/>
        <result property="user_id" column="user_id" javaType="java.lang.Long"/>
        <result property="category_id" column="category_id" javaType="java.lang.Long"/>
        <result property="category_name" column="category_name" />
        <result property="title" column="title" />
        <result property="description" column="description" />
        <result property="thumb" column="thumb" />
        <result property="material_type" column="material_type" javaType="java.lang.Integer"/>
        <result property="content" column="content" />
        <result property="file" column="file" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <sql id="leftJoinCategory">
        LEFT JOIN med_material_category mc ON m.category_id = mc.id
    </sql>

    <!-- 查 列表 -->
    <select id="queryMyMaterialList" resultMap="resultMapMaterial">
        SELECT
           <include refid="includeMyMaterial" />
        FROM med_my_material m
        <include refid="leftJoinCategory" />
        <where>
            <if test="my_material_ids != null and my_material_ids.size() > 0">
                AND m.id IN
                <foreach item="id" collection="my_material_ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="title != null and title != ''">
                AND m.title LIKE CONCAT('%', #{title}, '%')
            </if>
            <if test="description != null and description != ''">
                AND m.description LIKE CONCAT('%', #{description}, '%')
            </if>
            <if test="category_id != null and category_id != ''">
                AND m.category_id = #{category_id}
            </if>
            <if test="user_id != null and user_id != ''">
                AND m.user_id = #{user_id}
            </if>
            <if test="material_id != null and material_id != ''">
                AND m.material_id = #{material_id}
            </if>
            <if test="create_time != null and create_time != ''">
                AND m.create_time >= #{create_time}
            </if>
        </where>
        ORDER BY m.update_time DESC
    </select>

    <!-- 查 详情 (二次查询) -->
    <select id="queryMyMaterialDetail" resultMap="resultMapMaterialDetail">
        SELECT <include refid="includeMyMaterialDetail" />
        FROM med_my_material m
        <include refid="leftJoinCategory" />
        WHERE m.id = #{my_material_id}
    </select>


    <insert id="insertMyMaterial" parameterType="com.backendsys.entity.Med.MedMyMaterialDTO"
        useGeneratedKeys="true" keyProperty="my_material_id">
        INSERT INTO med_my_material (
            category_id, user_id, title, material_type
            <if test="material_id != null and material_id != ''">, material_id</if>
            <if test="description != null and description != ''">, description</if>
            <if test="thumb != null and thumb != ''">, thumb</if>
            <if test="content != null and content != ''">, content</if>
            <if test="file != null and file != ''">, file</if>
        )
        VALUES (
            #{category_id}, #{user_id}, #{title}, #{material_type}
            <if test="material_id != null and material_id != ''">, #{material_id}</if>
            <if test="description != null and description != ''">, #{description}</if>
            <if test="thumb != null and thumb != ''">, #{thumb}</if>
            <if test="content != null and content != ''">, #{content}</if>
            <if test="file != null and file != ''">, #{file}</if>
        );
    </insert>

    <update id="updateMyMaterial" parameterType="com.backendsys.entity.Med.MedMaterialDTO"
        useGeneratedKeys="true" keyProperty="my_material_id">
        UPDATE med_my_material
        SET
            category_id = #{category_id},
            user_id = #{user_id},
            title = #{title},
            material_type = #{material_type},
            <if test="material_id != null and material_id != ''">, material_id = #{material_id}</if>
            <if test="description != null and description != ''">, description = #{description}</if>
            <if test="thumb != null and thumb != ''">, thumb = #{thumb}</if>
            <if test="content != null and content != ''">, content = #{content}</if>
            <if test="file != null and file != ''">, file = #{file}</if>
        WHERE id = #{my_material_id};
    </update>

    <!-- 删除 -->
    <delete id="deleteMyMaterial" parameterType="java.lang.Long">
        DELETE FROM med_my_material WHERE id = #{my_material_id};
    </delete>

    <!-- 删除 (批量) -->
    <delete id="deleteMyMaterialBatch" parameterType="java.lang.Long">
        DELETE FROM med_my_material WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>