<?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.delivery.mapper.CtArticleMapper">
    
    <resultMap type="CtArticle" id="CtArticleResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="companyName"/>
        <result property="title"    column="title"    />
        <result property="tagIds"    column="tag_ids"    />
        <result property="taskType"    column="task_type"    />
        <result property="authorType"    column="author_type"    />
        <result property="authorId"    column="author_id"    />
        <result property="authorName"    column="author_name"    />
        <result property="isOriginal"    column="is_original"    />
        <result property="coverImage"    column="cover_image"    />
        <result property="groupId"    column="group_id"    />
        <result property="summary"    column="summary"    />
        <result property="content"    column="content"    />
        <result property="attachmentUrl"    column="attachment_url"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="deleted"    column="deleted"    />
    </resultMap>

    <sql id="selectCtArticleVo">
        select
        t.id,
        t.company_id,
        c.company_name as companyName,
        t.title,
        t.tag_ids,
        t.task_type,
        t.author_type,
        t.author_id,
        t.author_name,
        t.is_original,
        t.cover_image,
        t.group_id,
        t.summary,
        t.content,
        t.attachment_url,
        t.status,
        t.create_time,
        t.update_time,
        t.create_by,
        t.update_by,
        t.deleted
        from ct_article t
        left join company c
        on c.company_id = t.company_id
        and c.is_del = 0
    </sql>


    <select id="selectCtArticleList" parameterType="CtArticle" resultMap="CtArticleResult">
        <include refid="selectCtArticleVo"/>
        <where>
            <if test="companyId != null and companyId != ''"> and t.company_id = #{companyId}</if>
            <if test="title != null and title != ''"> and t.title = #{title}</if>
            <if test="tagIds != null and tagIds != ''"> and t.tag_ids = #{tagIds}</if>
            <if test="taskType != null"> and t.task_type = #{taskType}</if>
            <if test="authorType != null"> and t.author_type = #{authorType}</if>
            <if test="authorId != null"> and t.author_id = #{authorId}</if>
            <if test="authorName != null and authorName != ''"> and t.author_name like concat('%', #{authorName}, '%')</if>
            <if test="isOriginal != null"> and t.is_original = #{isOriginal}</if>
            <if test="coverImage != null and coverImage != ''"> and t.cover_image = #{coverImage}</if>
            <if test="groupId != null"> and t.group_id = #{groupId}</if>
            <if test="summary != null and summary != ''"> and t.summary = #{summary}</if>
            <if test="content != null and content != ''"> and t.content = #{content}</if>
            <if test="attachmentUrl != null and attachmentUrl != ''"> and t.attachment_url = #{attachmentUrl}</if>
            <if test="status != null"> and t.status = #{status}</if>
            <if test="deleted != null"> and t.deleted = #{deleted}</if>
        </where>
        order by t.create_time desc
    </select>
    
    <select id="selectCtArticleById" parameterType="Long" resultMap="CtArticleResult">
        <include refid="selectCtArticleVo"/>
        where t.id = #{id}
    </select>
        
    <insert id="insertCtArticle" parameterType="CtArticle" useGeneratedKeys="true" keyProperty="id">
        insert into ct_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="tagIds != null">tag_ids,</if>
            <if test="taskType != null">task_type,</if>
            <if test="authorType != null">author_type,</if>
            <if test="authorId != null">author_id,</if>
            <if test="authorName != null">author_name,</if>
            <if test="isOriginal != null">is_original,</if>
            <if test="coverImage != null">cover_image,</if>
            <if test="groupId != null">group_id,</if>
            <if test="summary != null">summary,</if>
            <if test="content != null">content,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="tagIds != null">#{tagIds},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="authorType != null">#{authorType},</if>
            <if test="authorId != null">#{authorId},</if>
            <if test="authorName != null">#{authorName},</if>
            <if test="isOriginal != null">#{isOriginal},</if>
            <if test="coverImage != null">#{coverImage},</if>
            <if test="groupId != null">#{groupId},</if>
            <if test="summary != null">#{summary},</if>
            <if test="content != null">#{content},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateCtArticle" parameterType="CtArticle">
        update ct_article
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="tagIds != null">tag_ids = #{tagIds},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="authorType != null">author_type = #{authorType},</if>
            <if test="authorId != null">author_id = #{authorId},</if>
            <if test="authorName != null">author_name = #{authorName},</if>
            <if test="isOriginal != null">is_original = #{isOriginal},</if>
            <if test="coverImage != null">cover_image = #{coverImage},</if>
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="summary != null">summary = #{summary},</if>
            <if test="content != null">content = #{content},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCtArticleById" parameterType="Long">
        delete from ct_article where id = #{id}
    </delete>

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