<?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.architecture.mapper.BizDrugIncompatibilityStandardMapper">

    <resultMap type="com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard" id="BizDrugIncompatibilityStandardResult">
        <id     property="id"                column="id"/>
        <result property="mainDrugName"     column="main_drug_name"/>
        <result property="compatResult"     column="compat_result"/>
        <result property="kbSyncStatus"     column="kb_sync_status"/>
        <result property="kbDocId"         column="kb_doc_id"/>
        <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"/>
        <result property="companionCount"  column="companion_count"/>
    </resultMap>

    <select id="selectBizDrugIncompatibilityStandardById" parameterType="long" resultMap="BizDrugIncompatibilityStandardResult">
        select s.id, s.main_drug_name, s.compat_result, s.kb_sync_status, s.kb_doc_id,
               s.create_by, s.create_time, s.update_by, s.update_time, s.remark,
               (select count(1) from biz_drug_incompatibility_companion c where c.standard_id = s.id) as companion_count
        from biz_drug_incompatibility_standard s
        where s.id = #{id}
    </select>

    <select id="countByMainDrugNameExcludeId" resultType="int">
        select count(1)
        from biz_drug_incompatibility_standard
        where main_drug_name = #{mainDrugName}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

    <select id="selectBizDrugIncompatibilityStandardList" parameterType="com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard" resultMap="BizDrugIncompatibilityStandardResult">
        select s.id, s.main_drug_name, s.compat_result, s.kb_sync_status, s.kb_doc_id,
               s.create_by, s.create_time, s.update_by, s.update_time, s.remark,
               (select count(1) from biz_drug_incompatibility_companion c where c.standard_id = s.id) as companion_count
        from biz_drug_incompatibility_standard s
        <where>
            <if test="keyword != null and keyword != ''">
                and s.main_drug_name like concat('%', #{keyword}, '%')
            </if>
        </where>
        order by s.create_time desc, s.id desc
    </select>

    <insert id="insertBizDrugIncompatibilityStandard" parameterType="com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard" useGeneratedKeys="true" keyProperty="id">
        insert into biz_drug_incompatibility_standard (
            main_drug_name, compat_result, kb_sync_status, kb_doc_id,
            create_by, create_time, remark
        ) values (
            #{mainDrugName}, #{compatResult}, #{kbSyncStatus}, #{kbDocId},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizDrugIncompatibilityStandard" parameterType="com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard">
        update biz_drug_incompatibility_standard set
            main_drug_name = #{mainDrugName},
            compat_result = #{compatResult},
            kb_sync_status = #{kbSyncStatus},
            kb_doc_id = #{kbDocId},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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