package thyyxxk.webserver.dao.his.medicalinsurance;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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.medicalinsurance.inpatient.ZyInactpatient;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;

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

/**
 * @description: 结算基本信息
 * @author: DingJie
 * @create: 2021/7/1317:20
 */
@Mapper
public interface SiSetlinfoDao extends BaseMapper<SiSetlinfo> {

    /**
     * 更新患者的医保基本信息（结算id）
     *
     * @param patNo        住院号/门诊号
     * @param times        住院次数/门诊次数
     * @param ledgerSn     账页号
     * @param setlId       结算id
     * @param medinsSetlId 医药机构结算ID，存放发送方报文ID
     */
    @Update("update t_si_pat_info set setl_id=#{setlId},medins_setl_id=#{medinsSetlId}," +
            "setl_msgid=#{setlMsgid} where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn}")
    void updateSiZyInfoSetlId(@Param("patNo") String patNo,
                              @Param("times") int times,
                              @Param("ledgerSn") int ledgerSn,
                              @Param("setlId") String setlId,
                              @Param("medinsSetlId") String medinsSetlId,
                              @Param("setlMsgid") String setlMsgid);

    @Update("update t_si_settle_apply set settled=#{settled} where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn} and " +
            "sort_no=(select max(sort_no) from t_si_settle_apply d where d.pat_no=#{patNo} and d.times=#{times} and d.ledger_sn=#{ledgerSn})")
    void updateApplySettled(@Param("patNo") String patNo,
                            @Param("times") int times,
                            @Param("ledgerSn") int ledgerSn,
                            @Param("settled") int settled);

    @Select("select a.*,b.insuplc_admdvs from t_si_setlinfo a, t_si_pat_info b where setl_type=#{setlType} " +
            "and revoked=0 and setl_time>=#{begndate} and setl_time<=#{enddate} and a.setl_id=b.setl_id")
    List<SiSetlinfo> selectSetlinfos(@Param("setlType") String setlType,
                                     @Param("begndate") String begndate,
                                     @Param("enddate") String enddate);

    @Select("select a.*,b.insuplc_admdvs from t_si_setlinfo a, t_si_pat_info b where setl_type=#{setlType} " +
            "and revoked=0 and a.insutype=#{insutype} and setl_time>=#{begndate} " +
            "and setl_time<=#{enddate} and a.setl_id=b.setl_id")
    List<SiSetlinfo> selectSetlinfosWithInsutype(@Param("setlType") String setlType,
                                                 @Param("insutype") String insutype,
                                                 @Param("begndate") String begndate,
                                                 @Param("enddate") String enddate);

    @Select("select *,sbjjPay=(select fund_payamt from t_si_setldetail " +
            "where setl_id=a.setl_id and fund_pay_type='650100'), " +
            "cyzd=case when #{setltype}='21' then " +
            "(select rtrim(dis_diag_comment) from zy_dis_diag_yb " +
            "where inpatient_no=a.pat_no and admiss_times=a.times and dis_diag_no=1) " +
            "else (select isnull(icd_text_new, '') from mz_visit_table " +
            "where a.pat_no=patient_id and a.times=times) end " +
            "from t_si_setlinfo a " +
            "where setl_type=#{setltype} and revoked=0 and insutype like #{insutype} " +
            "and setl_time>=#{begntime} and setl_time<=#{endtime} ${insuplcAdmdvs}")
    List<SiSetlinfo> selectMyList(@Param("setltype") String setltype,
                                  @Param("insutype") String insutype,
                                  @Param("insuplcAdmdvs") String insuplcAdmdvs,
                                  @Param("begntime") String begntime,
                                  @Param("endtime") String endtime);

    @Select("select psn_no,setl_id,mdtrt_id,insuplc_admdvs from t_si_setlinfo with(nolock) " +
            "where revoked=0 and charges_analyzed=0")
    List<SiSetlinfo> selectUnAnalyzedData();

    @Update("update t_si_setlinfo set charge_western_medicine=#{chargeWesternMedicine}, " +
            "charge_patent_medicine=#{chargePatentMedicine},charge_herbal=#{chargeHerbal}, " +
            "charge_examination=#{chargeExamination},charge_treatment=#{chargeTreatment}," +
            "charge_operation=#{chargeOperation},charge_nursing=#{chargeNursing}, " +
            "charge_sanitary_material=#{chargeSanitaryMaterial},charge_assay=#{chargeAssay}, " +
            "charge_general_diagnosis=#{chargeGeneralDiagnosis},charge_inspection=#{chargeInspection}, " +
            "charge_registration=#{chargeRegistration},charge_bed=#{chargeBed},charge_others=#{chargeOthers}, " +
            "charges_analyzed=#{chargesAnalyzed} where setl_id=#{setlId}")
    void fillAnalyzedData(SiSetlinfo setlinfo);

    @Select(" <script> " +
            " <foreach collection='list' item='item' index='index' separator=' union all '> " +
            " select rtrim(t.inpatient_no) as inpatient_no, rtrim(t.admiss_times) as admiss_times, " +
            " z.name as dept, e.name as jobDoctor, t.clinic_diag_str " +
            " from zy_inactpatient t " +
            " left join zd_unit_code z on t.zk_ward=z.code " +
            " left join a_employee_mi e on t.refer_physician=e.code " +
            " where t.inpatient_no=#{item.patNo} and t.admiss_times=#{item.times} " +
            " </foreach> " +
            " </script> ")
    List<ZyInactpatient> selectZyPatient(@Param("list") List<SiSetlinfo> list);

    @Select(" <script> " +
            " <foreach collection='list' item='item' index='index' separator=' union all '> " +
            " select rtrim(t.patient_id) as patNo, t.times, z.name as dept, " +
            " e.name as doctor, t.icd_text_new as zyzd " +
            " from mz_visit_table t " +
            " left join zd_unit_code z on t.visit_dept_code=z.code " +
            " left join a_employee_mi e on t.doctor_code=e.code " +
            " where t.patient_id=#{item.patNo} and t.times=#{item.times} " +
            " </foreach> " +
            " </script> ")
    List<Map> selectMzPatient(@Param("list") List<SiSetlinfo> list);

    @Select("select count(1) from t_si_setlinfo where setl_id=#{setlId} and revoked=0")
    int getSetlCount(String setlId);
}
