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.setlinfo.SiSetlinfo;

import java.util.List;

/**
 * @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}")
    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 a.*, " +
            "hospPay=(select sum(b.fund_payamt) from t_si_setldetail b where b.pat_no=a.pat_no and b.times=a.times " +
            "and b.ledger_sn=a.ledger_sn and (b.fund_pay_type='999996' or b.setl_proc_info='999996')) " +
            "from t_si_setlinfo a where a.setl_type=21 and a.revoked=0 and a.insutype like #{insutype} " +
            "and a.insuplc_admdvs like #{insuplcAdmdvs} and a.setl_time>=#{begntime} and a.setl_time<=#{endtime}")
    List<SiSetlinfo> selectMyList(@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);
}
