package thyyxxk.webserver.dao.his.medicine;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.inpatient.YpZdDict;
import thyyxxk.webserver.entity.medicine.YpPrintName;
import thyyxxk.webserver.entity.medicine.YpZdDictVo;

import java.util.List;
import java.util.Map;

@Mapper
public interface YpDictDao extends BaseMapper<YpZdDict> {

    @Select(" select yf.charge_code as chargeCode,  " +
            " yf.serial,  " +
            " zd.name as drug,  " +
            " yf.group_no as groupNo,  " +
            " ks.group_name as groupName,  " +
            " zd.specification,  " +
            " zd.pack_retprice as packRetprice,  " +
            " isnull(yf.visible_flag_mz, '0') as visibleFlagMz,  " +
            " isnull(yf.visible_flag_zy, '0') as visibleFlagZy, " +
            " zd.py_code as pyCode, " +
            " zd.d_code as dCode " +
            " from yp_base_yf as yf " +
            " left join yp_zd_group_name as ks " +
            " on yf.group_no = ks.group_no, " +
            " yp_zd_dict as zd " +
            " where yf.charge_code = zd.code and yf.serial = zd.serial " +
            " and (zd.name like '%${text}%' or zd.code like '%${text}%' or zd.py_code like ('%' + upper(#{text}) + '%') or zd.d_code like ('%' + upper(#{text}) + '%') ) " +
            " order by yf.charge_code ")
    List<Map<String, Object>> selectYpVisibleFlag(@Param("text") String text);

    @Update(" update yp_base_yf set visible_flag_mz = #{visibleFlagMz}, visible_flag_zy = #{visibleFlagZy}" +
            " where charge_code = #{chargeCode} and serial = #{serial} and group_no = #{groupNo} ")
    int updateYpVisibleFlag(@Param("chargeCode") String chargeCode, @Param("serial") String serial, @Param("groupNo") String groupNo,
                            @Param("visibleFlagMz") String visibleFlagMz, @Param("visibleFlagZy") String visibleFlagZy);

    @Select("<script>" +
            " select rtrim(zd.code) as code,  " +
            " zd.serial,  " +
            " rtrim(zd.name) as name,  " +
            " zd.specification,  " +
            " zd.pack_retprice as packRetprice,  " +
            " isnull(zd.temp_purchase_flag, '0') as tempPurchaseFlag,  " +
            " isnull(zd.high_warning_flag, '0') as highWarningFlag,  " +
            " isnull(zd.winning_bidder_flag, '0') as winningBidderFlag,  " +
            " isnull(zd.focus_monitor_flag, '0') as focusMonitorFlag,  " +
            " isnull(zd.return_flag, '0') as returnFlag,  " +
            " rtrim(isnull(zd.jb_flag, '0')) as jbFlag,  " +
            " isnull(zd.del_flag, '0') as delFlag,  " +
            " zd.categories_flag as categoriesFlag,  " +
            " isnull(yc.name, '') as manuFactory, " +
            "<if test=\" groupNo != null and groupNo != '' \">" +
            " isnull(base.del_flag, '0') as ykFlag,  " +
            "</if>" +
            " zd.py_code as pyCode, " +
            " zd.d_code as dCode " +
            " from yp_zd_dict as zd " +
            "<if test=\" groupNo != null and groupNo != '' \">" +
            " inner join (select distinct charge_code, serial, del_flag from yp_base where group_no = #{groupNo}) as base " +
            " on zd.code = base.charge_code and zd.serial = base.serial " +
            "</if>" +
            " left join yp_zd_manufactory as yc on zd.manu_code = yc.code " +
            " where (zd.code like '%${text}%' or zd.name like '%${text}%'  " +
            " or zd.py_code like ('%' + upper(#{text}) + '%') " +
            " or zd.d_code like ('%' + upper(#{text}) + '%')) " +
            "<if test=\" isSee == '1'.toString \">" +
            " and isnull(zd.del_flag, '0') = '0' " +
            "</if>" +
            " order by zd.code " +
            "</script>")
    List<Map<String, String>> selectYpDict(@Param("text") String text, @Param("groupNo") String groupNo, @Param("isSee") String isSee);

    @Select(" select count(1) as num from yp_base where charge_code = #{chargeCode} ")
    int selectYpBaseCountByGroupNo(@Param("chargeCode") String chargeCode);

    @Select(" select * from yp_zd_dict where code = #{code} and serial = #{serial} ")
    YpZdDict selectYpDictByCode(@Param("code") String code, @Param("serial") String serial);

    @Select(" select * from yp_zd_dict where code = #{code} ")
    List<YpZdDict> selectYpDictByCodeNoSerial(@Param("code") String code);

    @Update(" update yp_zd_dict set del_flag = #{delFlag} where code = #{code} ")
    int updateYpDictDelFlagByCode(@Param("code") String code, @Param("delFlag") String delFlag);

    /**
     * @Description 获取药品大类
     * @Date 2024/1/19 11:14
     */
    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(is_del, '0') as statusFlag, " +
            " case when is_del = '1' then 'false' else 'true' end as disabled " +
            "from yp_zd_categories with(nolock) order by code ")
    List<CodeName> selectYpCategories();

    /**
     * @Description 获取门诊收费码
     * @Author hsh
     * @Date 2024/1/19 11:18
     */
    @Select("select rtrim(code) as code, rtrim(name) as name from mz_bill_item with(nolock) order by code ")
    List<CodeName> selectMzBillItem();

    /**
     * @Description 获取住院收费码
     * @Author hsh
     * @Date 2024/1/19 11:20
     */
    @Select("select rtrim(code) as code, rtrim(name) as name from zy_bill_item with(nolock) order by code ")
    List<CodeName> selectZyBillItem();

    /**
     * @Description 获取药品医保类别
     * @Author hsh
     * @Date 2024/1/19 14:09
     */
    @Select("select rtrim(code) as code, rtrim(name) as name from yp_zd_yb_type with(nolock) order by code ")
    List<CodeName> selectYpYbType();

    /**
     * @Description 获取给药方式(门诊)
     * @Author hsh
     * @Date 2024/1/24 11:40
     */
    @Select("select rtrim(supply_code) as code, rtrim(supply_name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'false' else 'true' end as disabled " +
            "from mz_zd_supply_type with(nolock) order by supply_code ")
    List<CodeName> selectMzSupply();

    /**
     * @Description 获取执行频率(门诊)
     * @Author hsh
     * @Date 2024/1/25 10:34
     */
    @Select("select rtrim(code) as code, rtrim(comm) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'false' else 'true' end as disabled " +
            "from mz_order_frequency with(nolock) order by code ")
    List<CodeName> selectMzFrequency();

    /**
     * @Description 获取核算码(住院)
     * @Author hsh
     * @Date 2024/1/25 10:35
     */
    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'true' else 'false' end as disabled " +
            "from zy_zd_audit_item with(nolock) order by code ")
    List<CodeName> selectZyAuditItem();

    /**
     * @Description 获取药品来源字典
     * @Author hsh
     * @Date 2024/1/31 14:45
     */
    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'true' else 'false' end as disabled " +
            "from yp_zd_from_type with(nolock) order by code ")
    List<CodeName> selectYpFrom();

    /**
     * @Description 获取药品抗菌药物字典
     * @Author hsh
     * @Date 2024/1/31 14:46
     */
    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'true' else 'false' end as disabled " +
            "from yp_zd_kjyw_type with(nolock) order by code ")
    List<CodeName> selectYpKjywCode();

    /**
     * @Description 查询编码范围（药品大类）内最大编码值
     * @Author hsh
     * @param minNum 最小范围， maxNum 最大范围
     * @return String
     * @Date 2024/1/30 9:00
     */
    @Select("select max ( code ) from yp_zd_dict WITH(NOLOCK) where code >= #{minNum} and code <= #{maxNum}")
    String selectMaxCode(@Param("minNum") String minNum, @Param("maxNum") String maxNum);

    /**
     * @Description 药品编码与医嘱编码冲突检查
     * @Author hsh
     * @param code 新增预置药品编码
     * @return int
     * @Date 2024/1/29 9:05
     */
    @Select(" select count(1) from yz_zd_order_item_confirm where yz_zd_order_item_confirm.order_code = #{code} ")
    int selectYzOrderItemByCode(String code);

    /**
     * @Description 保存药品通用名
     * @Author hsh
     * @param ypPrintName 药品别名信息
     * @Date 2024/1/30 16:17
     */
    @Insert("insert into yp_print_name ( charge_code, print_name, py_code, d_code, yp_flag ) " +
            "values ( #{chargeCode}, #{printName}, #{pyCode}, #{dCode}, #{ypFlag} )")
    int insertYpPrintName(YpPrintName ypPrintName);

    @Select(" select * from yp_print_name where charge_code = #{chargeCode} ")
    List<YpPrintName> selectYpPrintNameByChargeCode(String chargeCode);

    @Select(" select * from yp_print_name where charge_code = #{chargeCode} and print_name = #{printName} ")
    YpPrintName selectYpPrintNameByPrintName(String chargeCode, String printName);

    @Update("<script>" +
            "update yp_print_name " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"printName != null and printName != '' \">" +
            "print_name = #{printName}, " +
            "</if>" +
            "<if test=\"pyCode != null and pyCode != '' \">" +
            "py_code = #{pyCode}, " +
            "</if>" +
            "<if test=\"dCode != null and dCode != '' \">" +
            "d_code = #{dCode}, " +
            "</if>" +
            "<if test=\"ypFlag != null and ypFlag != '' \">" +
            "yp_flag = #{ypFlag}, " +
            "</if>" +
            "</trim>" +
            "where charge_code = #{chargeCode} and print_name = #{printName} " +
            "</script>")
    int updateYpPrintNameByPrintName(YpPrintName ypPrintName);

    @Delete(" delete from yp_print_name where charge_code = #{chargeCode} and print_name = #{printName} ")
    int delYpPrintNameByPrintName(String chargeCode, String printName);

    @Insert("insert into yp_base" +
            "  (charge_code, serial, group_no, drugname, py_code, d_code, specification, specification2, pack_retprice, " +
            "   retprice, pack_size, drug_flag, infusion, class_code, dosage, concentration, vol_unit, mini_unit, pack_unit, " +
            "   self_flag, separate_flag, suprice_flag, country_flag, drug_kind, drug_id, drug_id_link, weight, " +
            "   weigh_unit, volum, serial_new, mz_bill_item, zy_bill_item )" +
            " select code, serial, #{groupNo} as group_no, name, py_code, d_code, left(specification, 10) as specification, " +
            "       left(specification2, 10) as specification2, pack_retprice, retprice, pack_size," +
            "       drug_flag, infusion_flag, class_code, dosage, concentration, vol_unit, mini_unit, pack_unit, self_flag, " +
            "       separate_flag, suprice_flag, country_flag, right(drug_kind, 2) as drug_kind, drug_id, " +
            "       rtrim(ltrim(drug_id)) as drug_id_link, weight, weigh_unit, volum, serial as serial_new, bill_item_mz, bill_item_zy " +
            " from yp_zd_dict where code = #{code} and serial = #{serial}")
    int insertYpBaseSync(@Param("code") String code, @Param("serial") String serial, @Param("groupNo") String groupNo);

    @Update({"<script>" +
            "update yp_zd_dict set" +
            "  yp_zd_dict.name =b.name, py_code =b.py_code, d_code =b.d_code, specification =b.specification2, specification2 =b.specification2, location =b.location, " +
            "  class_code =b.class_code, dosage =b.dosage, concentration =b.concentration, weight =b.weight, weigh_unit =b.weigh_unit, fh_name = b.fh_name, " +
            "  volum =b.volum, vol_unit =b.vol_unit, mini_unit =b.mini_unit, pack_unit =b.mini_unit, self_flag =b.self_flag, js_flag = b.js_flag, order_dosage =b.order_dosage, " +
            "  separate_flag =b.separate_flag, suprice_flag =b.suprice_flag, drug_flag =b.drug_flag, infusion_flag =b.infusion_flag, pzwh =b.pzwh, dosage_unit =b.dosage_unit, " +
            "  country_flag =b.country_flag, manu_code =b.manu_code, drug_kind =b.drug_kind, gmp_flag =b.gmp_flag, cold_flag =b.cold_flag, visible_flag =b.visible_flag, " +
            "  chem_remark =b.chem_remark, bill_item_mz =b.bill_item_mz, bill_item_zy =b.bill_item_zy, percent_group_mz =b.percent_group_mz, fz_flag =b.fz_flag, " +
            "  percent_group_zy =b.percent_group_zy, del_flag =b.del_flag, yb_flag =b.yb_flag, self_flag_yb =b.self_flag_yb, n_code =b.n_code, zb_flag =b.zb_flag, " +
            "  zb_supplyer =b.zb_supplyer, ps_flag =b.ps_flag, qj_flag = b.qj_flag, ex_code =b.ex_code, supply_type =b.supply_type, frequency =rtrim(b.frequency), " +
            "  kss_flag =b.kss_flag, yp_level =b.yp_level, drug_id =b.drug_id, yb_code =b.yb_code, yb_comment =b.yb_comment, xnh_flag =b.xnh_flag, jb_flag =b.jb_flag, " +
            "  audit_code =b.audit_code, comment =b.comment, categories_flag =b.categories_flag, ym_flag =b.ym_flag, dpym_flag =b.dpym_flag, wg_type =b.wg_type, " +
            "  father_flag =b.father_flag, yb_code2 =b.yb_code2, yb_comment2 =b.yb_comment2, kjyw_flag =rtrim(b.kjyw_flag), ygyyjc_type =b.ygyyjc_type, cl_flag =b.cl_flag, " +
            "  ddd_value =b.ddd_value, yp_id =b.yp_id, xtpp_name =b.xtpp_name, sp_name =b.sp_name, yb_comment_new =b.yb_comment_new, is_central_purchase =b.is_central_purchase,  " +
            "  yb_bl_new =b.yb_bl_new, yb_flag_new =b.yb_flag_new, byj_flag =b.byj_flag, drug_control_code =b.drug_control_code, antitumor_level=b.antitumor_level " +
            " from ( select * from yp_zd_dict ) as b " +
            " where yp_zd_dict.code = #{code} and yp_zd_dict.serial != #{serial} and yp_zd_dict.code = b.code and b.serial = #{serial}" +
            "</script>"})
    int updateYpZdDictSyncElseSerial(@Param("code") String code, @Param("serial") String serial);

    @Update({"<script>" +
            "update yp_base" +
            " set drugname =b.name, py_code =b.py_code, d_code =b.d_code, specification =left(b.specification,10), specification2 =LEFT(b.specification2,10), " +
            "    class_code =b.class_code, drug_flag =b.drug_flag, infusion =b.infusion_flag, dosage =b.dosage, concentration =b.concentration, " +
            "    weight =b.weight, weigh_unit =b.weigh_unit, volum =b.volum, vol_unit =b.vol_unit, mini_unit =b.mini_unit, pack_size =b.pack_size, " +
            "    pack_unit =b.pack_unit, pack_retprice =b.pack_retprice, retprice =b.retprice, self_flag =b.self_flag, separate_flag =b.separate_flag, " +
            "    suprice_flag =b.suprice_flag, country_flag =b.country_flag, drug_kind =right(b.drug_kind,2), del_flag =b.del_flag, " +
            "    drug_id_link =b.drug_id, drug_id =rtrim(ltrim(b.drug_id)), mz_bill_item =b.bill_item_mz, zy_bill_item =b.bill_item_zy, " +
            "    zb_flag =b.zb_flag from yp_zd_dict b" +
            " where yp_base.charge_code = b.code and yp_base.serial_new = b.serial " +
            "   and yp_base.charge_code = #{code} and yp_base.serial_new = #{serial}" +
            "</script>"})
    int updateYpBaseSync(@Param("code") String code, @Param("serial") String serial);

    @Update({"<script>" +
            "update yp_base_yf " +
            " set pack_retprice =b.pack_retprice, retprice =b.retprice from yp_zd_dict b " +
            " where yp_base_yf.charge_code =b.code and yp_base_yf.charge_code =#{code} " +
            " and yp_base_yf.serial =b.serial and yp_base_yf.serial =#{serial} " +
            "</script>"})
    int updateYpBaseYfSync(@Param("code") String code, @Param("serial") String serial);

    @Update(" update yp_zd_dict set temp_purchase_flag = #{tempPurchaseFlag} where code = #{code} ")
    int updateYpTempPurchaseByCode(@Param("code") String code, @Param("tempPurchaseFlag") String tempPurchaseFlag);

    @Select(" select * from yp_base where group_no = #{groupNo} and charge_code = #{code} and serial = #{serial} ")
    Map<String, Object> selectYpBaseByCode(@Param("groupNo") String groupNo, @Param("code") String code, @Param("serial") String serial);

    @Update(" update yp_base set del_flag = #{delFlag} where group_no = #{groupNo} and charge_code = #{code} ")
    int updateYpBaseDelFlagByCode(@Param("groupNo") String groupNo, @Param("code") String code, @Param("delFlag") String delFlag);

    @Update(" update yp_zd_dict set jb_flag = #{jbFlag} where code = #{code} ")
    int updateYpJbFlagByCode(@Param("code") String code, @Param("jbFlag") String jbFlag);

    @Select("<script>" +
            " select yk.charge_code as code, yk.stock_amount as stockAmount, yk.group_no as groupNo, kf.group_name as groupName " +
            " from yp_base as yk, yp_zd_group_name as kf " +
            " where yk.charge_code = #{code} " +
            " and yk.stock_amount > 0.0001 " +
            " and yk.group_no = kf.group_no " +
            "<if test=\" groupNo != null and groupNo != '' \">" +
            " and yk.group_no = #{groupNo} " +
            "</if>" +
            " </script>")
    List<Map<String, Object>> selectYpStorageBase(@Param("code") String code, @Param("groupNo") String groupNo);

    @Select(" select yf.charge_code as code, yf.stock_amount as stockAmount, yf.group_no as groupNo, kf.group_name as groupName " +
            " from yp_base_yf as yf, yp_zd_group_name as kf " +
            " where yf.charge_code = #{code} " +
            " and yf.stock_amount > 0.0001 " +
            " and yf.group_no = kf.group_no ")
    List<Map<String, Object>> selectYpStorageBaseYf(@Param("code") String code);

    @Update({"<script>" +
            " update yp_base set del_flag = #{delFlag} " +
            " where charge_code = #{code} " +
            "<if test=\" groupNo != null and groupNo != '' \">" +
            " and group_no = #{groupNo} " +
            "</if>" +
            "</script>"})
    void updateYpBaseNoDelFlagByCode(@Param("code") String code, @Param("delFlag") String delFlag, @Param("groupNo") String groupNo);

    @Update(" update yp_base_yf set visible_flag_mz = #{delFlag}, visible_flag_zy = #{delFlag} where charge_code = #{code} ")
    void updateYpBaseYfDelFlagByCode(@Param("code") String code, @Param("delFlag") String delFlag);

    @Update(" update yp_zd_dict set high_warning_flag = #{flag} where code = #{code} ")
    int updateYpHighWarningByCode(@Param("code") String code, @Param("flag") String flag);

    @Update(" update yp_zd_dict set winning_bidder_flag = #{flag} where code = #{code} ")
    int updateYpWinningBidderByCode(@Param("code") String code, @Param("flag") String flag);

    @Update(" update yp_zd_dict set focus_monitor_flag = #{flag} where code = #{code} ")
    int updateYpFocusMonitorByCode(@Param("code") String code, @Param("flag") String flag);

    @Update(" update yp_zd_dict set return_flag = #{flag} where code = #{code} ")
    int updateYpReturnFlagByCode(@Param("code") String code, @Param("flag") String flag);

}
