<?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>

    <resultMap type="com.ruoyi.web.modules.architecture.domain.AppMedicationIncompatibilityGroupRow"
               id="IncompatibilityGroupResult">
        <result property="standardId"    column="standard_id"/>
        <result property="compatResult"  column="compat_result"/>
        <result property="companionsCsv" column="companions_csv"/>
    </resultMap>

    <select id="selectIncompatibilityGroupsByMainDrugName" resultMap="IncompatibilityGroupResult">
        select s.id as standard_id,
               s.compat_result,
               group_concat(c.companion_drug_name order by c.sort_order asc separator ',') as companions_csv
        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}
        group by s.id, s.compat_result
        order by s.id asc
    </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="selectCompatResultByPair" resultType="string">
        select s.compat_result
        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}
        order by s.id asc
        limit 1
    </select>
</mapper>
