<?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.fs.qw.mapper.QwMaterialMapper">

    <resultMap type="QwMaterial" id="QwMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="materialType"    column="material_type"    />
        <result property="materialMediaId"    column="material_media_id"    />
        <result property="createdAt"    column="created_at"    />
        <result property="materialUrl"    column="material_url"    />
        <result property="materialGroupId"    column="material_group_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="companyId"    column="company_id"    />
        <result property="textContent"    column="text_content"    />
        <result property="createTime"    column="create_time"    />
        <result property="title"    column="title"    />
        <result property="titleUrl"    column="title_url"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="sendCount"    column="send_count"    />
        <result property="groupType"    column="group_type"    />
        <result property="corpId"    column="corp_id"    />
    </resultMap>

    <sql id="selectQwMaterialVo">
        select material_id, material_type, material_media_id, created_at, material_name, material_url, material_group_id, company_id, text_content, create_time, title, title_url, update_time, create_user_id, send_count, group_type , corp_id from qw_material
    </sql>

    <select id="selectQwMaterialList" parameterType="QwMaterial" resultMap="QwMaterialResult">
        <include refid="selectQwMaterialVo"/>
        <where>
            <if test="materialType != null  and materialType != ''"> and material_type = #{materialType}</if>
            <if test="materialMediaId != null  and materialMediaId != ''"> and material_media_id = #{materialMediaId}</if>
            <if test="createdAt != null  and createdAt != ''"> and created_at = #{createdAt}</if>
            <if test="materialUrl != null  and materialUrl != ''"> and material_url = #{materialUrl}</if>
            <if test="materialName != null  and materialName != ''"> and material_name = #{materialName}</if>
            <if test="materialGroupId != null "> and material_group_id = #{materialGroupId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="textContent != null  and textContent != ''"> and text_content = #{textContent}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="titleUrl != null  and titleUrl != ''"> and title_url = #{titleUrl}</if>
            <if test="createUserId != null  and createUserId != ''"> and create_user_id = #{createUserId},</if>
            <if test="sendCount != null  and sendCount != ''"> and send_count = #{sendCount}</if>
            <if test="groupType != null  and groupType != ''"> and group_type = #{groupType}</if>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
        </where>
        order by create_name desc
    </select>

    <select id="selectQwMaterialByMaterialId" parameterType="Long" resultMap="QwMaterialResult">
        <include refid="selectQwMaterialVo"/>
        where material_id = #{materialId}
    </select>

    <insert id="insertQwMaterial" parameterType="QwMaterial">
        insert into qw_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null">material_id,</if>
            <if test="materialType != null">material_type,</if>
            <if test="materialMediaId != null">material_media_id,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="materialUrl != null">material_url,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialGroupId != null">material_group_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="textContent != null">text_content,</if>
            <if test="createTime != null">create_time,</if>
            <if test="title != null">title,</if>
            <if test="titleUrl != null">title_url,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="sendCount != null">send_count,</if>
            <if test="groupType != null">group_type,</if>
            <if test="corpId != null">corp_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null">#{materialId},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="materialMediaId != null">#{materialMediaId},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="materialUrl != null">#{materialUrl},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="materialGroupId != null">#{materialGroupId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="textContent != null">#{textContent},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="title != null">#{title},</if>
            <if test="titleUrl != null">#{titleUrl},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="sendCount != null">#{sendCount},</if>
            <if test="groupType != null">#{groupType},</if>
            <if test="corpId != null">#{corpId},</if>
         </trim>
    </insert>

    <update id="updateQwMaterial" parameterType="QwMaterial">
        update qw_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="materialMediaId != null">material_media_id = #{materialMediaId},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="materialUrl != null">material_url = #{materialUrl},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialGroupId != null">material_group_id = #{materialGroupId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="textContent != null">text_content = #{textContent},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="title != null">title = #{title},</if>
            <if test="titleUrl != null">title_url = #{titleUrl},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="sendCount != null">send_count = #{sendCount},</if>
            <if test="groupType != null">group_type = #{groupType},</if>
            <if test="corpId != null">corp_id = #{corpId},</if>
        </trim>
        where material_id = #{materialId}
    </update>

    <delete id="deleteQwMaterialByMaterialId" parameterType="Long">
        delete from qw_material where material_id = #{materialId}
    </delete>

    <delete id="deleteQwMaterialByGroupMaterialId" parameterType="Long">
        delete from qw_material where material_group_id = #{materialGroupId}
    </delete>


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



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