<?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.AppMedicationDrugMapper">

    <resultMap type="com.ruoyi.web.modules.architecture.domain.BizWithdrawalPeriodStandard" id="WithdrawalBriefResult">
        <result property="drugName"       column="drug_name"/>
        <result property="drugType"       column="drug_type"/>
        <result property="withdrawalDays" column="withdrawal_days"/>
        <result property="usageGuide"     column="usage_guide"/>
        <result property="drugEffect"     column="drug_effect"/>
    </resultMap>

    <select id="selectDistinctDrugNames" resultType="string">
        select distinct drug_name
        from biz_withdrawal_period_standard
        <where>
            <if test="keyword != null and keyword != ''">
                and drug_name like concat('%', #{keyword}, '%')
            </if>
        </where>
        order by drug_name asc
        limit #{limit}
    </select>

    <select id="selectWithdrawalByDrugName" resultMap="WithdrawalBriefResult">
        select drug_name, drug_type, withdrawal_days, usage_guide, drug_effect
        from biz_withdrawal_period_standard
        where drug_name = #{drugName}
        limit 1
    </select>

    <select id="countIncompatibilityPair" resultType="int">
        select count(1)
        from biz_drug_incompatibility_standard s
        inner join biz_drug_incompatibility_companion c on c.standard_id = s.id
        where s.main_drug_name = #{mainDrugName}
          and c.companion_drug_name = #{companionDrugName}
    </select>

    <select id="selectCompatResultByMainDrugName" resultType="string">
        select compat_result
        from biz_drug_incompatibility_standard
        where main_drug_name = #{mainDrugName}
        limit 1
    </select>

    <select id="selectIncompatibilityCompanionsCsv" resultType="string">
        select group_concat(c.companion_drug_name order by c.sort_order asc separator ',')
        from biz_withdrawal_period_standard w
        inner join biz_drug_incompatibility_standard s on s.main_drug_name = w.drug_name
        inner join biz_drug_incompatibility_companion c on c.standard_id = s.id
        where w.drug_name = #{drugName}
    </select>
</mapper>
