<?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="cn.com.tenlion.materialstore.dao.materialtext.IMaterialTextDao">

    <resultMap id="materialTextDTO" type="cn.com.tenlion.materialstore.pojo.dtos.materialtext.MaterialTextDTO">
        <result column="material_status" property="materialStatus"/>
        <result column="creator_name" property="creatorName"/>
        <result column="material_text_id" property="materialTextId"/>
        <result column="material_text_content" property="materialTextContent"/>
        <result column="material_text_title" property="materialTextTitle"/>
        <result column="material_id" property="materialId"/>
        <result column="material_order" property="materialOrder"/>
        <result column="material_remark" property="materialRemark"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="materialTextBO" type="cn.com.tenlion.materialstore.pojo.bos.materialtext.MaterialTextBO">
        <result column="material_text_id" property="materialTextId"/>
        <result column="material_text_content" property="materialTextContent"/>
        <result column="material_text_title" property="materialTextTitle"/>
        <result column="material_id" property="materialId"/>
        <result column="material_order" property="materialOrder"/>
        <result column="material_remark" property="materialRemark"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="materialTextPO" type="cn.com.tenlion.materialstore.pojo.pos.materialtext.MaterialTextPO">
        <result column="material_text_id" property="materialTextId"/>
        <result column="material_text_content" property="materialTextContent"/>
        <result column="material_text_title" property="materialTextTitle"/>
        <result column="material_id" property="materialId"/>
        <result column="material_order" property="materialOrder"/>
        <result column="material_remark" property="materialRemark"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 新增文字素材 -->
    <insert id="save" parameterType="map">
        INSERT INTO m_material_text(
            material_status,
            material_text_id,
            material_text_content,
            material_text_title,
            material_id,
            material_order,
            material_remark,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            1,
            #{materialTextId},
            #{materialTextContent},
            #{materialTextTitle},
            #{materialId},
            #{materialOrder},
            #{materialRemark},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除文字素材（物理） -->
    <update id="removeByMaterialId" parameterType="map">
        DELETE FROM
            m_material_text
        WHERE
            material_id = #{materialId}
    </update>

    <!-- 删除文字素材 -->
    <update id="remove" parameterType="map">
        UPDATE
            m_material_text
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
            material_text_id IN
        <foreach collection="materialTextIds" index="index" open="(" separator="," close=")">
            #{materialTextIds[${index}]}
        </foreach>
    </update>

    <!-- 删除文字素材（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            m_material_text
        WHERE
            material_text_id IN
        <foreach collection="materialTextIds" index="index" open="(" separator="," close=")">
            #{materialTextIds[${index}]}
        </foreach>
    </update>

    <!-- 更改状态 -->
    <update id="updateCheck" parameterType="map">
        UPDATE
            m_material_text
        SET
            material_status = #{materialStatus}
        WHERE
            material_text_id = #{materialTextId}
    </update>

    <!-- 修改文字素材 -->
    <update id="update" parameterType="map">
        UPDATE
            m_material_text
        SET
        <if test="materialTextContent != null and materialTextContent != ''">
            material_text_content = #{materialTextContent},
        </if>
        <if test="materialTextTitle != null and materialTextTitle != ''">
            material_text_title = #{materialTextTitle},
        </if>
        <if test="materialId != null and materialId != ''">
            material_id = #{materialId},
        </if>
        <if test="materialOrder != null">
            material_order = #{materialOrder},
        </if>
        <if test="materialRemark != null and materialRemark != ''">
            material_remark = #{materialRemark},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            material_text_id = material_text_id
        WHERE
            material_text_id = #{materialTextId}
    </update>

    <!-- 文字素材详情 -->
    <select id="get" parameterType="map" resultMap="materialTextDTO">
        SELECT
            t1.material_text_content,
            t1.material_text_title,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.material_text_id,
            t1.material_status
        FROM
            m_material_text t1
        WHERE
            t1.is_delete = 0
            <if test="materialTextId != null and materialTextId != ''">
        AND
            t1.material_text_id = #{materialTextId}
            </if>
    </select>

    <!-- 文字素材详情 -->
    <select id="getBO" parameterType="map" resultMap="materialTextBO">
        SELECT
            t1.material_text_id,
            t1.material_text_content,
            t1.material_text_title,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_text t1
        WHERE
            t1.is_delete = 0
            <if test="materialTextId != null and materialTextId != ''">
        AND
            t1.material_text_id = #{materialTextId}
            </if>
    </select>

    <!-- 文字素材详情 -->
    <select id="getPO" parameterType="map" resultMap="materialTextPO">
        SELECT
            t1.material_text_id,
            t1.material_text_content,
            t1.material_text_title,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_text t1
        WHERE
            t1.is_delete = 0
            <if test="materialTextId != null and materialTextId != ''">
        AND
            t1.material_text_id = #{materialTextId}
            </if>
    </select>

    <!-- 文字素材列表 -->
    <select id="list" parameterType="map" resultMap="materialTextDTO">
        SELECT
            t1.material_text_id,
            t1.material_text_content,
            t1.material_text_title,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete,
            t1.material_status,
            t2.user_name AS creator_name
        FROM
            m_material_text t1
        LEFT JOIN
            sys_user t2
        ON
            t1.creator = t2.user_id
        WHERE
        t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND (
            <!-- 这里添加其他条件 -->
            t1.material_text_title LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.material_text_content LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.material_remark LIKE CONCAT('%', #{keywords}, '%')
            OR
            t2.user_name LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="materialStatus != null and materialStatus != ''">
            AND
            t1.material_status = #{materialStatus}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="materialTextIds != null and materialTextIds.size > 0">
        AND
            t1.material_text_id IN
            <foreach collection="materialTextIds" index="index" open="(" separator="," close=")">
                #{materialTextIds[${index}]}
            </foreach>
        </if>
        <if test="materialId != '' and materialId != null">
            AND t1.material_id = #{materialId}
        </if>
        <if test="creator != '' and creator != null">
            AND t1.creator = #{creator}
        </if>
        ORDER BY t1.gmt_create DESC, t1.material_order
    </select>

    <!-- 文字素材列表 -->
    <select id="listBO" parameterType="map" resultMap="materialTextBO">
        SELECT
            t1.material_text_id,
            t1.material_text_content,
            t1.material_text_title,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_text t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="materialTextIds != null and materialTextIds.size > 0">
        AND
            t1.material_text_id IN
            <foreach collection="materialTextIds" index="index" open="(" separator="," close=")">
                #{materialTextIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 文字素材列表 -->
    <select id="listPO" parameterType="map" resultMap="materialTextPO">
        SELECT
            t1.material_text_id,
            t1.material_text_content,
            t1.material_text_title,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_text t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="materialTextIds != null and materialTextIds.size > 0">
        AND
            t1.material_text_id IN
            <foreach collection="materialTextIds" index="index" open="(" separator="," close=")">
                #{materialTextIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 文字素材统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            m_material_text t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>