<?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="drugConfigMapper">
    <select id="selectOne" parameterType="pd" resultType="pd">
        select * from s_gxhpz_drugconfig where  1=1 and mdmCode = #{mdmCode}
    </select>
    <select id="selectDrugConfigByd_value_code" parameterType="pd" resultType="pd">
        select * from s_gxhpz_drugconfig where  d_value_code = #{d_value_code} and dvalueStatus is not null
    </select>
    <select id="selectproductByCodeAndName" parameterType="pd" resultType="pd">
        SELECT
            p.*,
            d.registered_item,
            d.follow_up_item,
            d.cold_chain_item,
            d.charity_aid_item,
            d.drug_delivery_way,
            d.dosage_max,
            d.dosage_min,
            d.dosage_normal,
            d.unit_normal,
            d.unit_max,
            d.unit_min,
            d.packing_unit,
            d.packing_value
        FROM
            s_gxhpz_product_info p
                LEFT JOIN
            s_gxhpz_product_dtpinfo d
            ON
                p.product_code = d.product_code
        WHERE
            p.product_name LIKE CONCAT('%', #{query}, '%')
           OR p.product_code LIKE CONCAT('%', #{query}, '%')
    </select>
    <select id="selectOneproductByCodeAndName" parameterType="pd" resultType="pd">
        SELECT
            p.*,
            d.registered_item,
            d.follow_up_item,
            d.cold_chain_item,
            d.charity_aid_item,
            d.drug_delivery_way,
            d.dosage_max,
            d.dosage_min,
            d.dosage_normal,
            d.unit_normal,
            d.unit_max,
            d.unit_min,
            d.packing_unit,
            d.packing_value
        FROM
            s_gxhpz_product_info p
                LEFT JOIN
            s_gxhpz_product_dtpinfo d
            ON
                p.product_code = d.product_code
        WHERE p.product_code = #{query}
    </select>

    <select id="selectDrugConfig" parameterType="pd" resultType="pd">
        select * from s_gxhpz_drugconfig where 1=1
        <if test="mdmCode != null and mdmCode != ''">
            and mdmCode = #{mdmCode}
        </if>
        <if test="d_value_code != null and d_value_code != ''">
            and d_value_code = #{d_value_code}
        </if>
        <if test="dValueName != null and dValueName != ''">
            and dValueName = #{dValueName}
        </if>
        <if test="specification != null and specification != ''">
            and specification = #{specification}
        </if>
        <if test="genericName != null and genericName != ''">
            and genericName = #{genericName}
        </if>
        <if test="productName != null and productName != ''">
            and productName = #{productName}
        </if>
        <if test="productImagePath != null and productImagePath != ''">
            and productImagePath = #{productImagePath}
        </if>
        <if test="manufacturer != null and manufacturer != ''">
            and manufacturer = #{manufacturer}
        </if>
        <if test="manufacturerShortName != null and manufacturerShortName != ''">
            and manufacturerShortName = #{manufacturerShortName}
        </if>
        <if test="isFollowUpManaged != null and isFollowUpManaged != ''">
            and isFollowUpManaged = #{isFollowUpManaged}
        </if>
        <if test="isColdChainManaged != null and isColdChainManaged != ''">
            and isColdChainManaged = #{isColdChainManaged}
        </if>
        <if test="isRegisteredManaged != null and isRegisteredManaged != ''">
            and isRegisteredManaged = #{isRegisteredManaged}
        </if>
        <if test="isCharityAidManaged != null and isCharityAidManaged != ''">
            and isCharityAidManaged = #{isCharityAidManaged}
        </if>
        <if test="administrationMethod != null and administrationMethod != ''">
            and administrationMethod = #{administrationMethod}
        </if>
        <if test="enterpriseFlowManagement != null and enterpriseFlowManagement != ''">
            and enterpriseFlowManagement = #{enterpriseFlowManagement}
        </if>
        <if test="indicationDescription != null and indicationDescription != ''">
            and indicationDescription = #{indicationDescription}
        </if>
        <if test="dosageAndAdministration != null and dosageAndAdministration != ''">
            and dosageAndAdministration = #{dosageAndAdministration}
        </if>
        <if test="saleStatus != null and saleStatus != ''">
            and saleStatus = #{saleStatus}
        </if>
        <if test="packaging != null and packaging != ''">
            and packaging = #{packaging}
        </if>
        <if test="dvalueStatus != null and dvalueStatus != ''">
            and  dvalueStatus= #{dvalueStatus},
        </if>
        <if test="status != null and status != ''">
            and status = #{status}
        </if>
        <if test="createdBy != null and createdBy != ''">
            and createdBy = #{createdBy}
        </if>
        <if test="createdTime != null">
            and createdTime = #{createdTime}
        </if>
        <if test="updatedBy != null and updatedBy != ''">
            and updatedBy = #{updatedBy}
        </if>
        <if test="updatedTime != null">
            and updatedTime = #{updatedTime}
        </if>
        <if test="remarks != null and remarks != ''">
            and remarks = #{remarks}
        </if>
        <if test="id != null and id != ''">
            and id = #{id}
        </if>
         ORDER BY d_value_code ASC
    </select>

    <update id="updateDrugConfig2" parameterType="map">
        update s_gxhpz_drugconfig
        <trim prefix="SET" suffixOverrides=",">
            <if test="d_value_code != null">
                d_value_code = #{d_value_code},
            </if>
            <if test="dValueName != null and dValueName != ''">
                dValueName = #{dValueName},
            </if>
            <if test="specification != null and specification != ''">
                specification = #{specification},
            </if>
            <if test="genericName != null and genericName != ''">
                genericName = #{genericName},
            </if>
            <if test="productName != null and productName != ''">
                productName = #{productName},
            </if>
            <if test="packaging != null and packaging != ''">
                packaging = #{packaging},
            </if>
            <if test="productImagePath != null and productImagePath != ''">
                productImagePath = #{productImagePath},
            </if>
            <if test="manufacturer != null and manufacturer != ''">
                manufacturer = #{manufacturer},
            </if>
            <if test="manufacturerShortName != null and manufacturerShortName != ''">
                manufacturerShortName = #{manufacturerShortName},
            </if>
            <if test="isFollowUpManaged != null and isFollowUpManaged != ''">
                isFollowUpManaged = #{isFollowUpManaged},
            </if>
            <if test="isColdChainManaged != null and isColdChainManaged != ''">
                isColdChainManaged = #{isColdChainManaged},
            </if>
            <if test="isRegisteredManaged != null and isRegisteredManaged != ''">
                isRegisteredManaged = #{isRegisteredManaged},
            </if>
            <if test="isCharityAidManaged != null and isCharityAidManaged != ''">
                isCharityAidManaged = #{isCharityAidManaged},
            </if>
            <if test="administrationMethod != null and administrationMethod != ''">
                administrationMethod = #{administrationMethod},
            </if>
            <if test="enterpriseFlowManagement != null and enterpriseFlowManagement != ''">
                enterpriseFlowManagement = #{enterpriseFlowManagement},
            </if>
            <if test="indicationDescription != null and indicationDescription != ''">
                indicationDescription = #{indicationDescription},
            </if>
            <if test="dosageAndAdministration != null and dosageAndAdministration != ''">
                dosageAndAdministration = #{dosageAndAdministration},
            </if>
            <if test="saleStatus != null">
                saleStatus = #{saleStatus},
            </if>
            <if test="dvalueStatus != null">
                dvalueStatus= #{dvalueStatus},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="createdBy != null and createdBy != ''">
                createdBy = #{createdBy},
            </if>
            <if test="createdTime != null">
                createdTime = #{createdTime},
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                updatedBy = #{updatedBy},
            </if>
            <if test="updatedTime != null">
                updatedTime = #{updatedTime},
            </if>

            <if test="dvupdatedby != null and dvupdatedby != ''">
                dvupdatedby = #{dvupdatedby},
            </if>
            <if test="dvupdatedTime != null">
                dvupdatedTime = #{dvupdatedTime},
            </if>
            <if test="dvcreatedTime != null">
                dvcreatedTime = #{dvcreatedTime},
            </if>


            <if test="remarks != null and remarks != ''">
                remarks = #{remarks},
            </if>
            <if test="dosage_max != null and dosage_max != ''">
                dosage_max=#{dosage_max},
            </if>
            <if test="dosage_min != null and dosage_min != ''">
                dosage_min=#{dosage_min},
            </if>
            <if test="dosage_normal != null and dosage_normal != ''">
                dosage_normal=#{dosage_normal},
            </if>
            <if test="unit_normal != null and unit_normal != ''">
                unit_normal=#{unit_normal},
            </if>
            <if test="unit_max != null and unit_max != ''">
                unit_max=#{unit_max},
            </if>
            <if test="unit_min != null and unit_min != ''">
                unit_min=#{unit_min},
            </if>
            <if test="packing_unit != null and packing_unit != ''">
                packing_unit=#{packing_unit},
            </if>
            <if test="packing_value != null and packing_value != ''">
                packing_value=#{packing_value},
            </if>
        </trim>
        <where>
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode = #{mdmCode}
            </if>
        </where>
    </update>
    <update id="updateDrugConfig" parameterType="map">
        update s_gxhpz_drugconfig
        <trim prefix="SET" suffixOverrides=",">
            <if test="mdmCode != null and mdmCode !='' ">
                mdmCode = #{mdmCode},
            </if>
            <if test="d_value_code != null">
                d_value_code = #{d_value_code},
            </if>
            <if test="dValueName != null and dValueName != ''">
                dValueName = #{dValueName},
            </if>
            <if test="specification != null and specification != ''">
                specification = #{specification},
            </if>
            <if test="genericName != null and genericName != ''">
                genericName = #{genericName},
            </if>
            <if test="productName != null and productName != ''">
                productName = #{productName},
            </if>
            <if test="packaging != null and packaging != ''">
                packaging = #{packaging},
            </if>
            <if test="productImagePath != null and productImagePath != ''">
                productImagePath = #{productImagePath},
            </if>
            <if test="manufacturer != null and manufacturer != ''">
                manufacturer = #{manufacturer},
            </if>
            <if test="manufacturerShortName != null and manufacturerShortName != ''">
                manufacturerShortName = #{manufacturerShortName},
            </if>
            <if test="isFollowUpManaged != null and isFollowUpManaged != ''">
                isFollowUpManaged = #{isFollowUpManaged},
            </if>
            <if test="isColdChainManaged != null and isColdChainManaged != ''">
                isColdChainManaged = #{isColdChainManaged},
            </if>
            <if test="isRegisteredManaged != null and isRegisteredManaged != ''">
                isRegisteredManaged = #{isRegisteredManaged},
            </if>
            <if test="isCharityAidManaged != null and isCharityAidManaged != ''">
                isCharityAidManaged = #{isCharityAidManaged},
            </if>
            <if test="administrationMethod != null and administrationMethod != ''">
                administrationMethod = #{administrationMethod},
            </if>
            <if test="enterpriseFlowManagement != null and enterpriseFlowManagement != ''">
                enterpriseFlowManagement = #{enterpriseFlowManagement},
            </if>
            <if test="indicationDescription != null and indicationDescription != ''">
                indicationDescription = #{indicationDescription},
            </if>
            <if test="dosageAndAdministration != null and dosageAndAdministration != ''">
                dosageAndAdministration = #{dosageAndAdministration},
            </if>
            <if test="saleStatus != null  and saleStatus != ''">
                saleStatus = #{saleStatus},
            </if>
            <if test="dvalueStatus != null and dvalueStatus != ''">
                dvalueStatus= #{dvalueStatus},
            </if>
            <if test="status != null  and status != ''">
                status = #{status},
            </if>
            <if test="createdBy != null and createdBy != ''">
                createdBy = #{createdBy},
            </if>
            <if test="createdTime != null">
                createdTime = #{createdTime},
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                updatedBy = #{updatedBy},
            </if>
            <if test="updatedTime != null">
                updatedTime = #{updatedTime},
            </if>
            <if test="dvupdatedby != null and dvupdatedby != ''">
                dvupdatedby = #{dvupdatedby},
            </if>
            <if test="dvupdatedTime != null">
                dvupdatedTime = #{dvupdatedTime},
            </if>
            <if test="dvcreatedTime != null">
                dvcreatedTime = #{dvcreatedTime},
            </if>
            <if test="remarks != null and remarks != ''">
                remarks = #{remarks},
            </if>
        </trim>
        <where>
            <if test="id != null">
                id = #{id}
            </if>
        </where>
    </update>
    <update id="updateDrugConfigByDrugCode" parameterType="map">
        update s_gxhpz_drugconfig
        <trim prefix="SET" suffixOverrides=",">

            <if test="d_value_code != null">
                d_value_code = #{d_value_code},
            </if>
            <if test="dValueName != null and dValueName != ''">
                dValueName = #{dValueName},
            </if>
            <if test="dvalueStatus != null">
                dvalueStatus= #{dvalueStatus},
            </if>
            <if test="status != null  and status != ''">
                status = #{status},
            </if>

            <if test="createdTime != null">
                createdTime = #{createdTime},
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                updatedBy = #{updatedBy},
            </if>
            <if test="updatedTime != null">
                updatedTime = #{updatedTime},
            </if>
            <if test="dvupdatedby != null and dvupdatedby != ''">
                dvupdatedby = #{dvupdatedby},
            </if>
            <if test="dvupdatedTime != null">
                dvupdatedTime = #{dvupdatedTime},
            </if>
            <if test="dvcreatedTime != null">
                dvcreatedTime = #{dvcreatedTime},
            </if>
            <if test="dosage_max != null and dosage_max != ''">
                dosage_max=#{dosage_max},
            </if>
            <if test="dosage_min != null and dosage_min != ''">
                dosage_min=#{dosage_min},
            </if>
            <if test="dosage_normal != null and dosage_normal != ''">
                dosage_normal=#{dosage_normal},
            </if>
            <if test="unit_normal != null and unit_normal != ''">
                unit_normal=#{unit_normal},
            </if>
            <if test="unit_max != null and unit_max != ''">
                unit_max=#{unit_max},
            </if>
            <if test="unit_min != null and unit_min != ''">
                unit_min=#{unit_min},
            </if>
            <if test="packing_unit != null and packing_unit != ''">
                packing_unit=#{packing_unit},
            </if>
            <if test="packing_value != null and packing_value != ''">
                packing_value=#{packing_value},
            </if>
        </trim>
        <where>
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode = #{mdmCode}
            </if>
        </where>
    </update>

    <update id="changeStatus" parameterType="pd">
        update s_gxhpz_drugconfig set  status = #{status}, updatedBy = #{updatedBy},updatedTime = #{updatedTime} where   id = #{id}
    </update>
    <update id="changedvalueStatus" parameterType="pd">
        update s_gxhpz_drugconfig set  dvalueStatus = #{dvalueStatus}, dvupdatedby = #{updatedBy},dvupdatedTime = #{updatedTime} where   id = #{id}
    </update>

    <insert id="addDrugConfig" parameterType="map">
        insert into s_gxhpz_drugconfig
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode,
            </if>
            <if test="d_value_code != null">
                d_value_code,
            </if>
            <if test="dValueName != null and dValueName != ''">
                dValueName,
            </if>
            <if test="specification != null and specification != ''">
                specification,
            </if>
            <if test="packaging != null and packaging != ''">
                packaging,
            </if>
            <if test="genericName != null and genericName != ''">
                genericName,
            </if>
            <if test="productName != null and productName != ''">
                productName,
            </if>
            <if test="productImagePath != null and productImagePath != ''">
                productImagePath,
            </if>
            <if test="manufacturer != null and manufacturer != ''">
                manufacturer,
            </if>
            <if test="manufacturerShortName != null and manufacturerShortName != ''">
                manufacturerShortName,
            </if>
            <if test="isFollowUpManaged != null and isFollowUpManaged != ''">
                isFollowUpManaged,
            </if>
            <if test="isColdChainManaged != null and isColdChainManaged != ''">
                isColdChainManaged,
            </if>
            <if test="isRegisteredManaged != null and isRegisteredManaged != ''">
                isRegisteredManaged,
            </if>
            <if test="isCharityAidManaged != null and isCharityAidManaged != ''">
                isCharityAidManaged,
            </if>
            <if test="administrationMethod != null and administrationMethod != ''">
                administrationMethod,
            </if>
            <if test="enterpriseFlowManagement != null and enterpriseFlowManagement != ''">
                enterpriseFlowManagement,
            </if>
            <if test="indicationDescription != null and indicationDescription != ''">
                indicationDescription,
            </if>
            <if test="dosageAndAdministration != null and dosageAndAdministration != ''">
                dosageAndAdministration,
            </if>
            <if test="saleStatus != null and saleStatus != ''">
                saleStatus,
            </if>
            <if test="dvalueStatus != null and dvalueStatus != ''">
                dvalueStatus,
            </if>
            <if test="status != null and status != ''">
                status,
            </if>
            <if test="createdBy != null and createdBy != ''">
                createdBy,
            </if>
            <if test="createdTime != null and createdTime != ''">
                createdTime,
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                updatedBy,
            </if>
            <if test="updatedTime != null and updatedTime != ''">
                updatedTime,
            </if>
            <if test="remarks != null and remarks != ''">
                remarks,
            </if>
            <if test="dosage_max != null and dosage_max != ''">
                dosage_max,
            </if>
            <if test="dosage_min != null and dosage_min != ''">
                dosage_min,
            </if>
            <if test="dosage_normal != null and dosage_normal != ''">
                dosage_normal,
            </if>
            <if test="unit_normal != null and unit_normal != ''">
                unit_normal,
            </if>
            <if test="unit_max != null and unit_max != ''">
                unit_max,
            </if>
            <if test="unit_min != null and unit_min != ''">
                unit_min,
            </if>
            <if test="packing_unit != null and packing_unit != ''">
                packing_unit,
            </if>
            <if test="packing_value != null and packing_value != ''">
                packing_value,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="mdmCode != null and mdmCode != ''">
                #{mdmCode},
            </if>
            <if test="d_value_code != null and d_value_code != ''">
                #{d_value_code},
            </if>
            <if test="dValueName != null and dValueName != ''">
                #{dValueName},
            </if>
            <if test="specification != null and specification != ''">
                #{specification},
            </if>
            <if test="packaging != null and packaging != ''">
                #{packaging},
            </if>
            <if test="genericName != null and genericName != ''">
                #{genericName},
            </if>
            <if test="productName != null and productName != ''">
                #{productName},
            </if>
            <if test="productImagePath != null and productImagePath != ''">
                #{productImagePath},
            </if>
            <if test="manufacturer != null and manufacturer != ''">
                #{manufacturer},
            </if>
            <if test="manufacturerShortName != null and manufacturerShortName != ''">
                #{manufacturerShortName},
            </if>
            <if test="isFollowUpManaged != null and isFollowUpManaged != ''">
                #{isFollowUpManaged},
            </if>
            <if test="isColdChainManaged != null and isColdChainManaged != ''">
                #{isColdChainManaged},
            </if>
            <if test="isRegisteredManaged != null and isRegisteredManaged != ''">
                #{isRegisteredManaged},
            </if>
            <if test="isCharityAidManaged != null and isCharityAidManaged != ''">
                #{isCharityAidManaged},
            </if>
            <if test="administrationMethod != null and administrationMethod != ''">
                #{administrationMethod},
            </if>
            <if test="enterpriseFlowManagement != null and enterpriseFlowManagement != ''">
                #{enterpriseFlowManagement},
            </if>
            <if test="indicationDescription != null and indicationDescription != ''">
                #{indicationDescription},
            </if>
            <if test="dosageAndAdministration != null and dosageAndAdministration != ''">
                #{dosageAndAdministration},
            </if>
            <if test="saleStatus != null and saleStatus != ''">
                #{saleStatus},
            </if>
            <if test="dvalueStatus != null and dvalueStatus != ''">
                #{dvalueStatus},
            </if>

            <if test="status != null and status != ''">
                #{status},
            </if>
            <if test="createdBy != null and createdBy != ''">
                #{createdBy},
            </if>
            <if test="createdTime != null and createdTime != ''">
                #{createdTime},
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                #{updatedBy},
            </if>
            <if test="updatedTime != null and updatedTime != ''">
                #{updatedTime},
            </if>
            <if test="remarks != null and remarks != ''">
                #{remarks},
            </if>
            <if test="dosage_max != null and dosage_max != ''">
                #{dosage_max},
            </if>
            <if test="dosage_min != null and dosage_min != ''">
                #{dosage_min},
            </if>
            <if test="dosage_normal != null and dosage_normal != ''">
                #{dosage_normal},
            </if>
            <if test="unit_normal != null and unit_normal != ''">
                #{unit_normal},
            </if>
            <if test="unit_max != null and unit_max != ''">
                #{unit_max},
            </if>
            <if test="unit_min != null and unit_min != ''">
                #{unit_min},
            </if>
            <if test="packing_unit != null and packing_unit != ''">
                #{packing_unit},
            </if>
            <if test="packing_value != null and packing_value != ''">
                #{packing_value},
            </if>
        </trim>
    </insert>
    <delete id="deleteDrugConfigsByIds" parameterType="java.util.List">
        <if test="list != null and list.size() > 0">
            delete from s_gxhpz_drugconfig
            where id in
            <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </delete>


    <!--配置过的药品查询-->
    <select id="getAllDrugConfig" parameterType="pd" resultType="pd">
        select *
        from s_gxhpz_drugconfig
        <where>
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="query != null and query != ''">
                AND (genericName LIKE CONCAT('%', #{query}, '%') OR productName LIKE CONCAT('%', #{query}, '%') OR mdmCode LIKE CONCAT('%', #{query}, '%'))
            </if>
            <if test="manufacturerShortName != null and manufacturerShortName != ''">
                AND manufacturerShortName = #{manufacturerShortName}
            </if>
            and delFlag =1
        </where>

    </select>
    <!--配置过的药品查询-->
    <select id="getAllProductInfo" parameterType="pd" resultType="pd">

        select
        p.*,d.is_insurance_drug,
        d.insurance_category,
        d.purchase_limit_quantity,
        d.prescription_required,
        d.prohibit_ordering,
        d.mid_pack_order_logic,
        d.max_order_limit,
        d.drug_delivery_way,
        d.registered_item,
        d.follow_up_item,
        d.cold_chain_item,
        d.flow_item,
        d.charity_aid_item,
        d.dosage_max,
        d.dosage_min,
        d.dosage_normal,
        d.unit_normal,
        d.unit_max,
        d.unit_min,
        d.packing_unit,
        d.packing_value
        from s_gxhpz_product_info p left join s_gxhpz_product_dtpinfo d on p.product_code = d.product_code
        <where>
            <if test="id != null">
                AND p.id = #{id}
            </if>
            <if test="query != null and query != ''">
                AND (p.generic_name LIKE CONCAT('%', #{query}, '%') OR p.product_name LIKE CONCAT('%', #{query}, '%') OR p.product_code LIKE CONCAT('%', #{query}, '%'))
            </if>
            <if test="manufacturerShortName != null and manufacturerShortName != ''">
                AND p.manufacturer_abbreviation = #{manufacturerShortName}
            </if>

        </where>

    </select>
</mapper>
