package thyyxxk.webserver.dao.his.medicalinsurance;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.markmtfees.*;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.*;
import thyyxxk.webserver.entity.medicalinsurance.query.PsnBaseInfo;
import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo;

import java.util.List;

/**
 * @description: 新医保门诊mapper
 * @author: DingJie
 * @create: 2021-06-28 14:35:25
 **/
@Mapper
public interface SiMzDao {

    @Select("select times from mz_patient_mi where patient_id=#{patientId} ")
    Integer selectMaxTimes(@Param("patientId") String patientId);

    @Select("select insuplc_admdvs from t_si_pat_info where " +
            "pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn}")
    String selectAdmdvs(@Param("patNo") String patNo,
                        @Param("times") int times,
                        @Param("ledgerSn") int ledgerSn);

    @Select("select * from t_si_pat_info where pat_no=#{patNo} and times=#{times}")
    PsnBaseInfo selectPsnBaseinfo(@Param("patNo") String patNo,
                                @Param("times") int times);

    @Select("SELECT a.patient_id,a.times,a.visit_dept_code,b.adress,b.phone_no, " +
            "b.name,b.age,sex=case when b.sex='1' then '男' when b.sex='2' then '女' else '未知' end, " +
            "visitDeptName=(select rtrim(name) from zd_unit_code where code=a.visit_dept_code), " +
            "a.doctor_code,a.visit_date,a.receipt_no,a.order_no,a.op_day, " +
            "doctorName=(select rtrim(name) from a_employee_mi where code=a.doctor_code), " +
            "doctorYbCode=(select rtrim(yb_code) from a_employee_mi where code=a.doctor_code), " +
            "icdText=case when nullif(a.icd_code_new,'') is null then rtrim(a.icd_text) else rtrim(a.icd_text_new) end, " +
            "settledFlag=(select count(1) from t_si_setlinfo where pat_no=#{patientId} and times=#{times} and revoked=0) " +
            "FROM mz_visit_table a, mz_patient_mi b " +
            "WHERE a.patient_id=#{patientId} AND a.times=#{times} and a.patient_id=b.patient_id")
    MzVisit selectMzVisit(@Param("patientId") String patientId,
                          @Param("times") Integer times);

    @Select("select count(1) from t_mt_receipt where patient_id=#{patientId} and times=#{times} and " +
            "receipt_no=#{receiptNo} and order_no=#{orderNo}")
    int selectFeeCount(@Param("patientId") String patientId,
                       @Param("times") Integer times,
                       @Param("receiptNo") Integer receiptNo,
                       @Param("orderNo") Integer orderNo);

    @Select("select rtrim(name) from a_employee_mi where code=#{code}")
    String selectDoctorName(@Param("code") String code);

    @Select("select rtrim(specification) from yp_zd_dict where code=#{code} and serial=#{serial}")
    String selectSpecification(@Param("code") String code, @Param("serial") String serial);

    @Select("select rtrim(supply_name) from mz_zd_supply_type where supply_code=#{code}")
    String selectSupplyName(@Param("code") String code);

    @Select("select rtrim(charge_unit) from zd_charge_item where code=#{code}")
    String selectXmChargeUnit(@Param("code") String code);

    @Select("select rtrim(name) from yp_zd_unit where code=#{code}")
    String selectDrugUnit(@Param("code") String code);

    @Select("select receipt_no from t_mt_receipt where patient_id=#{patNo} and times=#{times} group by receipt_no")
    List<Integer> selectGeneratedReceiptNos(@Param("patNo") String patNo, @Param("times") int times);

    @Select("select count(1) from mz_charge_detail where patient_id=#{patNo} and times=#{times} " +
            "and receipt_no=#{receiptNo} and pay_mark=0 and confirm_flag!=4")
    int isReceiptPayed(@Param("patNo") String patNo, @Param("times") int times, @Param("receiptNo") int receiptNo);

    @Delete("delete from t_mt_receipt where patient_id=#{patNo} and times=#{times} and receipt_no=#{receiptNo}")
    void deleteCertainReceipt(@Param("patNo") String patNo, @Param("times") int times, @Param("receiptNo") int receiptNo);

    @Insert("insert into t_mt_receipt (patient_id, times, receipt_no, order_no, item_no, his_item_name, " +
            "his_item_code, fee_date, input_date, medi_item_type, charge_fee, price, quantity, drug_win, " +
            "input_staff, input_man, serial_no, yb_trans_flag, charge_type, bill_item_code) values (" +
            "#{patientId},#{times},#{receiptNo},#{orderNo},#{itemNo},#{drugName},#{chargeItemCode}," +
            "#{priceTime},#{chargeDate},#{mediItemType},#{chargeFee},#{price},#{quantity},#{drugWin}," +
            "#{doctorCode},#{doctorName},#{serialNo},0, #{groupNo},#{billItemCode})")
    void insertBatchedMtFeeInfo(MzReceipt param);

    @Delete("delete from t_mt_receipt where patient_id=#{patientId} and times=#{times} and " +
            "receipt_no=#{receiptNo} and order_no=#{orderNo}")
    void deleteMzReceipt(OrderNo param);

    @Delete("delete from t_mt_receipt where patient_id=#{patNo} and times=#{times}")
    void deleteAllReceipts(@Param("patNo") String patNo,
                           @Param("times") Integer times);

    @Select("select patient_id as patNo,times,charge_date,receipt_no, " +
            "chargeFee=(select cast(sum(isnull(unit_price,0.01)*isnull(drug_win,1)*isnull(quantity,1)) as decimal(16,2))), " +
            "status=(select count(1) from t_mt_receipt d where d.patient_id=a.patient_id and d.times=a.times and d.receipt_no=a.receipt_no) " +
            "from mz_charge_detail a where a.patient_id=#{patNo} and a.charge_date>=#{start} and a.charge_date<=#{end} " +
            "and pay_mark=0 and confirm_flag!=4 and isnull(cash_id,'')!='99998' group by patient_id, times, charge_date,receipt_no order by times")
    List<MzDepositFile> selectMzDepositFiles(@Param("patNo") String patNo, @Param("start") String start, @Param("end") String end);

    @Select("select patient_id,times,receipt_no,order_no,item_no,charge_item_code, " +
            "charge_bill_code,quantity,unit_price,serial,serial_no, " +
            "frequency,drug_quan,drug_unit,supply_code,charge_date, " +
            "doctorName=(select rtrim(name) from a_employee_mi where code=doctor_code)," +
            "group_no,confirm_flag,instruction_text,drug_win,price_time,tc_no, " +
            "bill_item_code,doctor_code,tc_name,drugName=(case when group_no='00' then " +
            "(select name from zd_charge_item a where a.code=charge_item_code) else " +
            "(select top 1 name from yp_zd_dict a where a.code=charge_item_code) end)," +
            "nationalCode=(case when group_no='00' then " +
            "(select national_code from zd_charge_item a where a.code=charge_item_code) else " +
            "(select max(national_code) from yp_zd_dict a where a.code=charge_item_code) end) " +
            "from mz_charge_detail where patient_id=#{patNo} and times=#{times} and " +
            "receipt_no=#{receipt} and pay_mark=0 and confirm_flag!=4 and bill_item_code!='TC' " +
            "and charge_item_code!='BILL99' and isnull(cash_id,'')!='99998'")
    List<MzReceipt> selectMzCharge(@Param("patNo") String patNo, @Param("times") int times, @Param("receipt") int receipt);

    @Select("select max(national_code) from ${table} where code=#{code}")
    String selectNationalCode(@Param("code") String code, @Param("table") String table);

    @Delete("delete from t_si_mz_diag where pat_no=#{patNo} and times=#{times}")
    void deleteMzDiags(@Param("patNo") String patNo, @Param("times") int times);

    @Insert("insert into t_si_mz_diag (pat_no, times, diag_srt_no, diag_type, diag_code, diag_name, diag_dept, " +
            "dise_dor_no, dise_dor_name, diag_time, vali_flag, real_opter) " +
            "values (#{patNo}, #{times}, #{diagSrtNo}, #{diagType}, #{diagCode}, #{diagName}, #{diagDept}, " +
            "#{diseDorNo}, #{diseDorName}, #{diagTime}, #{valiFlag}, #{realOpter})")
    void insertNewMzDiag(SiMzDiag diag);

    @Select("select count(1) from t_si_pat_info where pat_no=#{patNo} and times=#{times} ")
    int selectSiMzInfoCount(@Param("patNo") String patNo,
                            @Param("times") int times);

    @Update("update t_si_pat_info set insutype=#{insutype},balc=#{balc} where pat_no=#{patNo} and times=#{times}")
    void updateSiZyInfoPsnNo(@Param("patNo") String patNo,
                             @Param("times") int times,
                             @Param("insutype") String insutype,
                             @Param("balc") String balc);

    @Insert("insert into t_si_pat_info (pat_no, times, ledger_sn, psn_no, psn_cert_type, insutype, insuplc_admdvs, " +
            "certno, psn_name, gend, naty, brdy, age, med_type, psn_idet_type,psn_type,emp_name, create_datetime, balc) " +
            "values (#{patNo},#{times},#{ledgerSn},#{psnNo},#{psnCertType},#{insutype},#{insuplc},#{certno}," +
            "#{psnName},#{gend},#{naty},#{brdy},#{age},#{medType},#{psnIdetType},#{psnType},#{empName},getdate(),#{balc})")
    void insertSiMzInfoPsnNo(PsnBaseInfo psnBaseinfo);

    @Update("update t_si_setlinfo set fund_pay_sumamt=#{fundPay},acct_pay=#{acctPay} " +
            "where pat_no=#{patientId} and times=#{times} and revoked=0")
    Integer modifyFundAmt(ModifyFundAmt amt);
}
