<?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.ruoyi.web.modules.farming.mapper.BizLivestockKnowledgeMapper">

    <resultMap type="com.ruoyi.web.modules.farming.domain.BizLivestockKnowledge" id="BizLivestockKnowledgeResult">
        <id     property="id"               column="id"/>
        <result property="knowledgeTitle"   column="knowledge_title"/>
        <result property="knowledgeTopic"   column="knowledge_topic"/>
        <result property="introduction"     column="introduction"/>
        <result property="contentFileUrl"   column="content_file_url"/>
        <result property="contentFilePath"  column="content_file_path"/>
        <result property="contentFileName"  column="content_file_name"/>
        <result property="kbSyncStatus"     column="kb_sync_status"/>
        <result property="kbDocId"          column="kb_doc_id"/>
        <result property="delFlag"          column="del_flag"/>
        <result property="createBy"         column="create_by"/>
        <result property="createTime"       column="create_time"/>
        <result property="updateBy"         column="update_by"/>
        <result property="updateTime"       column="update_time"/>
        <result property="remark"           column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select id, knowledge_title, knowledge_topic, introduction,
               content_file_url, content_file_path, content_file_name,
               kb_sync_status, kb_doc_id, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_livestock_knowledge
    </sql>

    <select id="selectBizLivestockKnowledgeById" parameterType="long" resultMap="BizLivestockKnowledgeResult">
        <include refid="selectVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectBizLivestockKnowledgeList" parameterType="com.ruoyi.web.modules.farming.domain.BizLivestockKnowledge" resultMap="BizLivestockKnowledgeResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="keyword != null and keyword != ''">
                and (knowledge_title like concat('%', #{keyword}, '%')
                     or introduction like concat('%', #{keyword}, '%'))
            </if>
            <if test="knowledgeTopic != null">
                and knowledge_topic = #{knowledgeTopic}
            </if>
            <if test="kbSyncStatus != null">
                and kb_sync_status = #{kbSyncStatus}
            </if>
        </where>
        order by create_time desc, id desc
    </select>

    <insert id="insertBizLivestockKnowledge" parameterType="com.ruoyi.web.modules.farming.domain.BizLivestockKnowledge" useGeneratedKeys="true" keyProperty="id">
        insert into biz_livestock_knowledge (
            knowledge_title, knowledge_topic, introduction,
            content_file_url, content_file_path, content_file_name,
            kb_sync_status, kb_doc_id, del_flag,
            create_by, create_time, remark
        ) values (
            #{knowledgeTitle}, #{knowledgeTopic}, #{introduction},
            #{contentFileUrl}, #{contentFilePath}, #{contentFileName},
            #{kbSyncStatus}, #{kbDocId}, #{delFlag},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizLivestockKnowledge" parameterType="com.ruoyi.web.modules.farming.domain.BizLivestockKnowledge">
        update biz_livestock_knowledge set
            knowledge_title = #{knowledgeTitle},
            knowledge_topic = #{knowledgeTopic},
            introduction = #{introduction},
            content_file_url = #{contentFileUrl},
            content_file_path = #{contentFilePath},
            content_file_name = #{contentFileName},
            kb_sync_status = #{kbSyncStatus},
            kb_doc_id = #{kbDocId},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizLivestockKnowledgeByIds">
        update biz_livestock_knowledge set
            del_flag = '2',
            update_by = #{updateBy},
            update_time = sysdate()
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and del_flag = '0'
    </update>
</mapper>
