package thyyxxk.webserver.dao.his.dictionary;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.dictionary.ChargeItemMatch;
import thyyxxk.webserver.entity.nationalmatch.*;

import java.util.List;

/**
 * @description: 新医保国家目录匹配mapper
 * @author: DingJie
 * @create: 2021-06-10 14:49:56
 **/
@Mapper
public interface NationalMatchDao {

    @Select("select a.code,a.serial,isnull(a.new_name,a.name) as name,type=#{type},${upldFlagColumn} as uploadedFlag, " +
            "${codeColumn} as nationalCode,${nameColumn} as nationalName,${statement} " +
            "aaz231 as injuryUniqueId,ake003 as catalogueType, bke215 as fundType, " +
            "unit=(select name from yp_zd_unit where yp_zd_unit.code=a.dosage_unit), " +
            "dosage=(select name from yp_zd_dosage where yp_zd_dosage.code=a.dosage), " +
            "ltrim(a.specification) as specification,rtrim(a.pzwh) as approvalNumber, " +
            "factory=(select name from yp_zd_manufactory where yp_zd_manufactory.code=a.manu_code) " +
            "from yp_zd_dict a, (select max(serial) as serial,code from yp_zd_dict where isnull(visible_flag,0)=#{delFlag} " +
            "and isnull(del_flag,0)=#{delFlag} group by code) b,yp_base c " +
            "where isnull(${statusColumn},0)=#{status} ${uploadStatement} and a.code=b.code and a.serial=b.serial " +
            "and c.group_no=11 and a.code=c.charge_code and a.drug_kind in (select code from yp_zd_drug_kind where yp_type in (1,2)) " +
            "and a.code like #{code} and a.name like #{name}")
    Page<StandardLocalItem> selectLocalMedicines(Page<StandardLocalItem> iPage,
                                                  @Param("code") String code,
                                                  @Param("name") String name,
                                                  @Param("type") int type,
                                                  @Param("delFlag") int delFlag,
                                                  @Param("status") int status,
                                                  @Param("statusColumn") String statusColumn,
                                                  @Param("codeColumn") String codeColumn,
                                                  @Param("nameColumn") String nameColumn,
                                                  @Param("upldFlagColumn") String upldFlagColumn,
                                                  @Param("statement") String statement,
                                                  @Param("uploadStatement") String uploadStatement);

    @Select("select a.code,a.serial,isnull(a.new_name,a.name) as name,type=#{type},${upldFlagColumn} as uploadedFlag, " +
            "${codeColumn} as nationalCode,${nameColumn} as nationalName,${statement} " +
            "aaz231 as injuryUniqueId,ake003 as catalogueType, bke215 as fundType, " +
            "unit=(select name from yp_zd_unit where yp_zd_unit.code=a.dosage_unit), " +
            "dosage=(select name from yp_zd_dosage where yp_zd_dosage.code=a.dosage), " +
            "ltrim(a.specification) as specification,rtrim(a.pzwh) as approvalNumber, " +
            "factory=(select name from yp_zd_manufactory where yp_zd_manufactory.code=a.manu_code) " +
            "from yp_zd_dict a, (select max(serial) as serial,code from yp_zd_dict where isnull(visible_flag,0)=#{delFlag} " +
            "and isnull(del_flag,0)=#{delFlag} group by code) b,yp_base c " +
            "where isnull(${statusColumn},0)=#{status} ${uploadStatement} and a.code=b.code and a.serial=b.serial " +
            "and c.group_no=22 and a.code=c.charge_code and a.drug_kind in ('0007','0003') " +
            "and a.code like #{code} and a.name like #{name} ")
    Page<StandardLocalItem> selectLocalHerbals(Page<StandardLocalItem> iPage,
                                                  @Param("code") String code,
                                                  @Param("name") String name,
                                                  @Param("type") int type,
                                                  @Param("delFlag") int delFlag,
                                                  @Param("status") int status,
                                                  @Param("statusColumn") String statusColumn,
                                                  @Param("codeColumn") String codeColumn,
                                                  @Param("nameColumn") String nameColumn,
                                                  @Param("upldFlagColumn") String upldFlagColumn,
                                                  @Param("statement") String statement,
                                                  @Param("uploadStatement") String uploadStatement);

    @Select("select code,rtrim(name) as name,charge_unit as unit,type=#{type}," +
            "discription,descriptions as specification," +
            "aaz231 as injuryUniqueId,ake003 as catalogueType, bke215 as fundType,${statement} " +
            "national_code as nationalCode,${nameColumn} as nationalName,${upldFlagColumn} as uploadedFlag, " +
            "yb_comment as factory from zd_charge_item a " +
            "where isnull(${statusColumn},0)=#{status} ${uploadStatement} " +
            "and isnull(del_flag,0)=#{delFlag} and " +
            "class_code='J' and code like #{code} and name like #{name}")
    Page<StandardLocalItem> selectLocalSupplies(Page<StandardLocalItem> iPage,
                                                 @Param("code") String code,
                                                 @Param("name") String name,
                                                 @Param("type") int type,
                                                 @Param("delFlag") int delFlag,
                                                 @Param("status") int status,
                                                 @Param("statusColumn") String statusColumn,
                                                 @Param("codeColumn") String codeColumn,
                                                 @Param("nameColumn") String nameColumn,
                                                 @Param("upldFlagColumn") String upldFlagColumn,
                                                 @Param("statement") String statement,
                                                 @Param("uploadStatement") String uploadStatement);

    @Select("select code,rtrim(name) as name,charge_unit as unit,type=#{type}, discription,specification,${upldFlagColumn} as uploadedFlag," +
            "${codeColumn} as nationalCode,${nameColumn} as nationalName,s_code as standardCode, charge_amount as price, " +
            "aaz231 as injuryUniqueId,ake003 as catalogueType, bke215 as fundType,${statement} " +
            "localName=(select b.local_medical_service_name from si_central_services b where b.national_code=a.national_code) " +
            "from zd_charge_item a " +
            "where isnull(${statusColumn},0)=#{status} ${uploadStatement} and isnull(del_flag,0)=#{delFlag} and " +
            "class_code!='J' and code like #{code} and name like #{name}")
    Page<StandardLocalItem> selectLocalServices(Page<StandardLocalItem> iPage,
                                                 @Param("code") String code,
                                                 @Param("name") String name,
                                                 @Param("type") int type,
                                                 @Param("delFlag") int delFlag,
                                                 @Param("status") int status,
                                                 @Param("statusColumn") String statusColumn,
                                                 @Param("codeColumn") String codeColumn,
                                                 @Param("nameColumn") String nameColumn,
                                                 @Param("upldFlagColumn") String upldFlagColumn,
                                                 @Param("statement") String statement,
                                                 @Param("uploadStatement") String uploadStatement);

    @Update("update zd_charge_item set new_name=name where new_name is null")
    void asyncNewNameForServices();

    @Select("select code as nationalCode,registration_name as nationalName,drug_dosage_forms as actualDosage, " +
            "specifications=(drug_specifications+' x'+minimum_packing_quantity+minimum_preparation_unit+'/'+minimum_packing_unit), " +
            "packaging_material as packingMaterial,isnull(production_name,'') as factory,license_number as approvalNumber, " +
            "drug_supervision_local_code as drugStandardCode,start_time,end_time, " +
            "matchedCount=(select count(1) from yp_zd_dict t where t.national_code=a.code) " +
            "from t_si_dl_ptnt_wstn_mdcn a where registration_name like #{name} and license_number like #{approve}")
    List<SiCentralMedicine> selectNationalMedicines(@Param("name") String name, @Param("approve") String approve);

    @Select("select aaz231 as injuryUniqueId,ake001 as nationalCode,bke601 as drugStandardCode,isnull(bke212,'') as factory, " +
            "aka070 as actualDosage,bke608 as approvalNumber,ake002 as nationalName,aka074 as specifications, " +
            "ake003 as catalogueType,bke215 as fundType,aae030 as start_time, aae031 as end_time, " +
            "matchedCount=(select count(1) from yp_zd_dict t where t.aaz231=a.aaz231) " +
            "from t_industrial_injury_catalogue a where a.ake003 in ('11','12') and a.ake002 like #{name} ")
    List<SiCentralMedicine> selectInjuryMedicines(String name);

    @Select("select code as nationalCode,monoo_name as nationalName,start_time,end_time, " +
            "matchedCount=(select count(1) from yp_zd_dict t where t.national_code=a.code) " +
            "from t_si_dl_chns_hbl a where monoo_name like #{name}")
    List<SiCentralHerbal> selectNationalHerbals(@Param("name") String name);

    @Select("select aaz231 as injuryUniqueId,ake001 as nationalCode,ake002 as nationalName, " +
            "ake003 as catalogueType,bke215 as fundType,aae030 as start_time, aae031 as end_time, " +
            "matchedCount=(select count(1) from yp_zd_dict t where t.aaz231=a.aaz231) " +
            "from t_industrial_injury_catalogue a where a.ake003 in ('12','13') and a.ake002 like #{name} ")
    List<SiCentralHerbal> selectInjuryHerbals(String name);

    @Select("select code as nationalCode,service_name as nationalName,isnull(connotation,'') as projectConnotation, " +
            "exclude_content as exclusions,charge_unit as valuationUnit,entry_specification as projectDescription, " +
            "start_date as start_time,end_date as end_time from t_si_dl_mdcn_srvc where service_name like #{name}")
    List<SiCentralServices> selectNationalServices(@Param("name") String name);

    @Select("select aaz231 as injuryUniqueId,ake001 as nationalCode,projectConnotation='',exclusions='',ake002 as nationalName, " +
            "aka052 as valuationUnit,ake003 as catalogueType,bke215 as fundType,aae030 as start_time, aae031 as end_time, " +
            "matchedCount=(select count(1) from yp_zd_dict t where t.aaz231=a.aaz231) " +
            "from t_industrial_injury_catalogue a where a.ake003 in ('21','23') and a.ake002 like #{name} ")
    List<SiCentralServices> selectInjuryServices(String name);

    @Select("select code as nationalCode,supplies_name as nationalName,si_universal_name as siName, " +
            "consumable_material as material,isnull(production_name,'') as factory,start_time,end_time " +
            "from t_si_dl_mdcn_sply where supplies_name like #{name} or si_universal_name like #{name}")
    List<SiCentralSuppliesMini> selectNationalSupplies(@Param("name") String name);

    @Select("select code as nationalCode,supplies_name as nationalName,si_universal_name as siName, " +
            "consumable_material as material,isnull(production_name,'') as factory,start_time,end_time " +
            "from t_si_dl_mdcn_sply where ${column}=#{value}")
    List<SiCentralSuppliesMini> selectNationalSuppliesByCondition(@Param("column") String column, @Param("value") String value);

    @Select("select aaz231 as injuryUniqueId,ake001 as nationalCode,isnull(bke212,'') as factory, " +
            "bke608 as approvalNumber,ake002 as nationalName,aka074 as specifications, " +
            "ake003 as catalogueType,bke215 as fundType,aae030 as start_time, aae031 as end_time, " +
            "matchedCount=(select count(1) from yp_zd_dict t where t.aaz231=a.aaz231) " +
            "from t_industrial_injury_catalogue a where a.ake003 in ('21','22','23','24') and a.ake002 like #{name} ")
    List<SiCentralSuppliesMini> selectInjurySupplies(String name);

    @Update("update yp_zd_dict set national_status=1,national_code=#{nationalCode}," +
            "national_name=#{nationalName},match_oper=#{staffId}, match_date=getdate()," +
            "selfpay_prop=#{selfpayProp} where code=#{code}")
    void matchMedicineAndHerbal(@Param("code") String code,
                                @Param("staffId") String staffId,
                                @Param("nationalCode") String nationalCode,
                                @Param("nationalName") String nationalName,
                                @Param("selfpayProp") String selfpayProp);

    @Select("select top 1 code as nationalCode,descriptions,exclude_content,connotation from t_si_dl_mdcn_srvc where code=#{code}")
    ChargeItemMatch selectServiceAbout(@Param("code") String code);

    @Update("update zd_charge_item set national_status=1,national_code=#{nationalCode}," +
            "national_name=#{nationalName},match_oper=#{staffId},match_date=getdate(), " +
            "discription=#{descriptions},exclude_content=#{excludeContent},connotation=#{connotation}, " +
            "selfpay_prop=#{selfpayProp} where code=#{hisCode}")
    void matchServcieAndSupply(ChargeItemMatch chargeItemMatch);

    @Update("update yp_zd_dict set national_status=0,national_code='',national_name='',cancel_match_oper=#{staffId}," +
            "cancel_match_date=getdate() where code=#{code} and serial=#{serial}")
    void cancelMatchYp(@Param("code") String code,
                       @Param("serial") String serial,
                       @Param("staffId") String staffId);

    @Update("update zd_charge_item set national_status=0,national_code='',national_name='',cancel_match_oper=#{staffId}," +
            "cancel_match_date=getdate() where code=#{code}")
    void cancelMatchXm(@Param("code") String code,
                       @Param("staffId") String staffId);

    @Update("update yp_zd_dict set new_name=#{name},oper_id=#{staffId},oper_date=getdate() where code=#{code}")
    void updateMedicine(@Param("name") String name,
                        @Param("staffId") String staffId,
                        @Param("code") String code);

    @Update("update zd_charge_item set new_name=#{name},charge_unit=#{unit},charge_amount=#{price}," +
            "oper_id=#{staffId},s_code=#{standardCode},discription=#{discription}," +
            "oper_date=getdate() where code=#{code}")
    void updateServiceItem(@Param("name") String name,
                           @Param("unit") String unit,
                           @Param("price") String price,
                           @Param("staffId") String staffId,
                           @Param("standardCode") String standardCode,
                           @Param("discription") String discription,
                           @Param("code") String code);

    @Update("update zd_charge_item set new_name=#{name},oper_id=#{staffId},yb_comment=#{factory}," +
            "specification=#{specification},oper_date=getdate() where code=#{code}")
    void updateSupply(@Param("name") String name,
                      @Param("staffId") String staffId,
                      @Param("factory") String factory,
                      @Param("specification") String specification,
                      @Param("code") String code);

    @Update("update yp_zd_dict set selfpay_prop=#{selfpayProp} where national_code=#{hilistCode}")
    void matchMedicineSelfPayProp(@Param("hilistCode") String hilistCode,
                                  @Param("selfpayProp") String selfpayProp);

    @Update("update ${table} set injury_match_status=1,aaz231=#{injuryUniqueId},ake001=#{nationalCode}, " +
            "ake002=#{nationalName},ake003=#{catalogueType},bke215=#{fundType},injury_match_oper=#{staffId}, " +
            "injury_match_datetime=getdate() where code=#{hisCode}")
    void matchInjuryCatalogue(String injuryUniqueId,String nationalCode,String nationalName,String catalogueType,
                              String fundType,String staffId,String hisCode,String table);

    @Update("update ${table} set injury_match_status=0,aaz231='',ake001='',ake002='',ake003='',bke215='', " +
            "cancel_injury_match_oper=#{staffId} where code=#{hisCode}")
    void cancelMatchInjuryCatalogue(String staffId,String hisCode,String table);

    @Insert("insert into t_si_match_log " +
            "(id,op_staff,yb_type,op_type,item_type,code,name,national_code,national_name) values " +
            "(#{id},#{opStaff},#{ybType},#{opType},#{type},#{code},#{name},#{nationalCode},#{nationalName})")
    void insertMatchLog(StandardLocalItem item);
}
