package thyyxxk.webserver.dao.his.inpatient.nursemodule;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.inpatient.nursemodule.request.InfusionInsert;
import thyyxxk.webserver.entity.inpatient.nursemodule.response.OccCount;
import thyyxxk.webserver.entity.inpatient.nursemodule.response.PatientBed;
import thyyxxk.webserver.entity.inpatient.nursemodule.response.PatientBriefInfo;
import thyyxxk.webserver.entity.inpatient.nursemodule.response.YzActOccInfusionNew;

import java.util.List;

@Mapper
public interface PrintInfusionCardDao {
    @Select("select sortNo=(ROW_NUMBER() over (ORDER BY cast(bed_no AS int))-1)," +
            "rtrim(bed_no) as bedNo,rtrim(inpatient_no) as patNo,admiss_times as times," +
            "rtrim(name) as patName from zy_actpatient where ward=#{wardCode} " +
            "order by cast(bed_no as int)")
    List<PatientBed> selectPatientBedList(String wardCode);

    @Select("select rtrim(a.inpatient_no) as patNo,a.admiss_times as times, " +
            "wardName=(select rtrim(d.name) from zd_unit_code d where d.code=#{wardCode}), " +
            "rtrim(a.name) as patName,rtrim(bed_no)+N'床' as bedNo, " +
            "patGender=case when a.sex='1' then '男' else '女' end, " +
            "ageDays=datediff(day,b.birth_date,getdate()) " +
            "from zy_actpatient a,a_patient_mi b where a.inpatient_no=b.inpatient_no " +
            "and a.ward=#{wardCode} and a.bed_no in (${bedNos}) order by cast(bed_no as int)")
    List<PatientBriefInfo> selectTargetPatients(String wardCode, String bedNos);

    @Delete("delete from yz_act_occ_infusion_new where ward_code=#{wardCode}")
    void deleteOldInfusion(String wardCode);

    @Insert("insert into yz_act_occ_infusion_new(inpatient_no,admiss_times,ward_code,act_order_no,charge_code," +
            "drug_flag,specification,drug_occ,frequ_code,supply_name,start_time,end_time,drug_name,instruction," +
            "parent_no,yz_dose,yz_dose_unit,entrust) " +
            "select distinct rtrim(a.inpatient_no),a.admiss_times,#{wardCode}," +
            "cast(cast(a.act_order_no as decimal) as varchar),rtrim(a.order_code),'i'," +
            "rtrim(a.drug_specification)+'('+rtrim(isnull(e.print_name,''))+')'," +
            "rtrim(a.drug_occ),rtrim(d.code),rtrim(convert(varchar,a.dose))+isnull(f.name, '')," +
            "a.start_time,a.end_time,rtrim(a.order_name),rtrim(e.supply_name),0,rtrim(a.dose)," +
            "rtrim(a.dose_unit),rtrim(a.instruction) " +
            "from yz_act_order a left join yp_zd_unit f on a.dose_unit=f.code," +
            "yz_act_occ b, zy_actpatient c, yz_order_frequency d,yz_supply_type e " +
            "where b.ward_code=#{wardCode} ${alwaysFlag} and b.occ_time>=#{startTime} and b.occ_time<=#{endTime} " +
            "and isnull(b.status_flag, '')!='d' and a.status_flag>'1' and a.supply_code=e.supply_code " +
            "and e.class='2' and isnull(a.parent_no,0)=0 and a.inpatient_no=c.inpatient_no " +
            "and a.admiss_times=c.admiss_times and a.frequ_code=d.code ${printComment} " +
            "and a.act_order_no=b.act_order_no and b.bed_no in (${bedNos}) " +
            "and(isnull(a.end_time,'')='' or a.frequ_code like '%ONCE%' or a.end_time>=getdate())")
    void insertNewInfusion(InfusionInsert data);

    @Insert("insert into yz_act_occ_infusion_new(inpatient_no,admiss_times,ward_code,act_order_no," +
            "charge_code,drug_flag,specification,drug_occ,frequ_code,supply_name,start_time,end_time," +
            "drug_name,instruction,parent_no,yz_dose,yz_dose_unit,entrust) " +
            "select distinct rtrim(a.inpatient_no),a.admiss_times,#{wardCode}," +
            "cast(cast(a.act_order_no as decimal) as varchar),rtrim(a.order_code),'i'," +
            "rtrim(a.drug_specification)+'('+rtrim(isnull(c.print_name,''))+')'," +
            "rtrim(a.drug_occ),rtrim(b.frequ_code),rtrim(convert(varchar,a.dose))+isnull(d.name,'')," +
            "a.start_time,a.end_time,rtrim(a.order_name),rtrim(c.supply_name)," +
            "cast(cast(a.parent_no as decimal) as varchar)," +
            "rtrim(a.dose),rtrim(a.dose_unit),rtrim(a.instruction) from yz_act_order a left join yp_zd_unit d " +
            "on a.dose_unit=d.code,yz_act_occ_infusion_new b,yz_supply_type c where " +
            "b.ward_code=#{wardCode} and a.inpatient_no=b.inpatient_no " +
            "and a.admiss_times=b.admiss_times and a.parent_no=b.act_order_no and a.supply_code=c.supply_code " +
            "and(isnull(a.end_time,'')='' or a.frequ_code like '%ONCE%' or a.end_time>=getdate())")
    void insertInfusionAgain(String wardCode);

    @Select("select * from (" +
            "select a.inpatient_no,a.act_order_no,a.admiss_times,a.bed_no,a.ward_code,a.signer," +
            "drug_name=a.drug_name+'('+cast(d.name as varchar(8))+')',a.specification,a.drug_occ," +
            "a.frequ_code,a.supply_name,a.start_time,a.end_time,a.charge_code,occ_time=a.start_time," +
            "a.drug_flag,a.instruction,a.parent_no,a.entrust,a.yz_dose,a.yz_dose_unit,b.order_code " +
            "from yz_act_occ_infusion_new a,yz_act_order b, " +
            "(select code,max(manu_code)manu_code from yp_zd_dict group by code) c " +
            "left join yp_zd_manufactory d on c.manu_code=d.code " +
            "where a.ward_code=#{wardcode} and a.parent_no=0 and a.act_order_no=b.act_order_no " +
            "and a.inpatient_no=b.inpatient_no and a.charge_code=c.code " +
            "union " +
            "select a.inpatient_no,a.act_order_no,a.admiss_times,a.bed_no,a.ward_code,a.signer, " +
            "drug_name=a.drug_name,a.specification,a.drug_occ,a.frequ_code,a.supply_name," +
            "a.start_time,a.end_time,a.charge_code,occ_time=a.start_time,a.drug_flag," +
            "a.instruction,a.parent_no,a.entrust,a.yz_dose,yz_dose_unit,b.order_code " +
            "from yz_act_occ_infusion_new a,yz_act_order b " +
            "where a.ward_code=#{wardcode} and a.parent_no=0 and a.act_order_no=b.act_order_no " +
            "and a.inpatient_no=b.inpatient_no and a.charge_code not in (select code from yp_zd_dict group by code)" +
            ") t order by t.act_order_no")
    List<YzActOccInfusionNew> selectMainDrugs(String wardCode);

    @Select("select * from (" +
            "select a.inpatient_no,a.act_order_no,a.admiss_times,a.bed_no,a.ward_code,a.parent_no, " +
            "drug_name=a.drug_name+'('+cast(d.name as varchar(8))+')',a.specification,a.drug_occ,a.frequ_code, " +
            "a.supply_name,a.start_time,a.end_time,a.signer,a.charge_code,a.drug_flag,a.instruction, " +
            "yz_dose=b.dose,yz_dose_unit=b.dose_unit,b.order_code,a.entrust " +
            "from yz_act_occ_infusion_new a,yz_act_order b, " +
            "(select code,max(manu_code)manu_code from yp_zd_dict group by code)c " +
            "left join yp_zd_manufactory d on c.manu_code=d.code " +
            "where a.ward_code=#{wardcode} and a.parent_no!=0 and a.act_order_no=b.act_order_no " +
            "and a.inpatient_no=b.inpatient_no and a.charge_code=c.code " +
            "union " +
            "select a.inpatient_no,a.act_order_no,a.admiss_times,a.bed_no,a.ward_code,a.parent_no, " +
            "drug_name=a.drug_name,a.specification,a.drug_occ,a.frequ_code,a.supply_name,a.start_time, " +
            "a.end_time,a.signer,a.charge_code,a.drug_flag,a.instruction, " +
            "yz_dose=b.dose,yz_dose_unit=b.dose_unit,b.order_code,a.entrust " +
            "from yz_act_occ_infusion_new a,yz_act_order b " +
            "where a.ward_code=#{wardcode} and a.parent_no!=0 and a.act_order_no=b.act_order_no " +
            "and a.inpatient_no=b.inpatient_no and a.charge_code not in (select code from yp_zd_dict group by code)" +
            ") t order by t.act_order_no")
    List<YzActOccInfusionNew> selectSideDrugs(String wardCode);

    @Insert("insert into yz_act_occ_infusion_new(inpatient_no,admiss_times,ward_code,act_order_no," +
            "charge_code,drug_flag,specification,drug_occ,frequ_code,supply_name,start_time," +
            "end_time,drug_name,instruction,yz_dose,yz_dose_unit) " +
            "select distinct rtrim(a.inpatient_no),a.admiss_times,#{wardCode}," +
            "cast(cast(a.act_order_no as decimal) as varchar),rtrim(a.order_code),'i'," +
            "rtrim(convert(varchar,a.dose))+isnull(f.name,''),a.drug_occ,rtrim(c.print_name)," +
            "rtrim(a.supply_code),a.start_time,a.end_time, " +
            "rtrim(a.order_name)+'('+rtrim(a.drug_specification)+')',rtrim(e.print_name),rtrim(a.dose)," +
            "rtrim(a.dose_unit) from yz_act_order a left join yp_zd_unit f on a.dose_unit=f.code, " +
            "yz_act_occ b,yz_order_frequency c,zy_actpatient d,yz_supply_type e " +
            "where b.ward_code=#{wardCode} ${alwaysFlag} " +
            "and b.occ_time>=#{startTime} and b.occ_time<=#{endTime} ${printComment} " +
            "and isnull(b.status_flag,'')!='d' and d.dept=#{wardCode} and d.ward=#{wardCode} " +
            "and a.status_flag>'1' and a.supply_code=e.supply_code and e.print_name!='iv' and e.class='2' " +
            "and e.pring_flag='1' and isnull(a.parent_no,0)=0 and a.inpatient_no=d.inpatient_no " +
            "and a.admiss_times=d.admiss_times and a.frequ_code=c.code and a.act_order_no=b.act_order_no " +
            "and b.bed_no in (${bedNos})")
    void insertNewBottleCard(InfusionInsert insert);

    @Select("SELECT distinct a.inpatient_no,a.drug_occ,a.start_time,a.drug_name,a.frequ_code, " +
            "a.act_order_no,a.instruction,a.specification FROM yz_act_occ_infusion_new a,yz_act_occ b " +
            "WHERE isnull(a.visible_flag,'0')!='1' and a.ward_code=#{wardCode} " +
            "and isnull(a.instruction,'') not like N'冲管%' " +
            "and b.act_order_no=a.act_order_no and b.occ_time>=#{startTime} AND b.occ_time<=#{endTime} " +
            "AND isnull(b.status_flag,'')!='d' order by a.inpatient_no,a.act_order_no")
    List<YzActOccInfusionNew> selectMainDrugsForBottleCard(InfusionInsert insert);

    @Select("SELECT DISTINCT rtrim(c.print_name) as frequCode,rtrim(a.order_name) as orderName, " +
            "specification=cast(a.dose as varchar)+isnull(e.name,''),a.drug_occ, " +
            "parentNo=cast(cast(a.parent_no as decimal) as varchar),a.start_time, " +
            "actOrderNo=cast(cast(a.act_order_no as decimal) as varchar),a.act_order_no as ordeNo " +
            "FROM yz_act_order a left join yp_zd_unit e on a.dose_unit=e.code, " +
            "yz_supply_type b,yz_order_frequency c,yz_act_occ d " +
            "WHERE a.supply_code=b.supply_code and a.frequ_code=c.code " +
            "and a.act_order_no=d.act_order_no and a.parent_no is not null AND a.status_flag>'1' " +
            "AND a.status_flag<='5' AND d.occ_time>=#{startTime} AND d.occ_time<=#{endTime} " +
            "AND isnull(d.status_flag,'')!='d' AND d.ward_code=#{wardCode} order by a.act_order_no")
    List<YzActOccInfusionNew> selectSideDrugsForBottleCard(InfusionInsert insert);

    @Update("update yz_act_occ set print_comment=#{status} where act_order_no in (${orderNos})")
    void updateYzActOccPrintStatus(String status, String orderNos);
}
