package thyyxxk.webserver.dao.his.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.view.patient360.MedicalTrack;
import thyyxxk.webserver.entity.view.patient360.PatientInfo360;
import thyyxxk.webserver.entity.view.patient360.ZyFeeDetail;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrPatientData;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.YiZhuFeiYongChaXunTiaoJian;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface Patient360Dao {

    @Select("select rtrim(inpatient_no) as inpatient_no," +
            "       rtrim(mz_no) as mz_no, " +
            "       sex, " +
            "       social_no, " +
            "       rtrim(name) as name," +
            "       CONVERT(varchar(100), birth_date, 23) as birth_date " +
            "from a_patient_mi " +
            "where ${field} = #{patNo};")
    PatientInfo360 getPatientInfo(String patNo, String field);


    @Select("select          CONVERT(varchar(100), admiss_date, 23) as date, " +
            "    deptName  = (select name from zd_unit_code where code = small_dept), " +
            "    outOrHosp = 2 ," +
            "    uid       = (rtrim(inpatient_no) + '_' + cast(admiss_times as varchar(5)))," +
            "    rtrim(inpatient_no) as patNo," +
            "    admiss_times as  times," +
            "    inTheHospital," +
            "    admiss_date , dis_date " +
            "from view_zy_patient_all " +
            "where inpatient_no = #{patNo} " +
            "order by admiss_times;")
    List<MedicalTrack> getHospMedicalTrack(String patNo);


    @Select("select emr_document_id, emr_name " +
            "from emr_patient_data " +
            "where pat_no =  #{patNo} " +
            "  and times = #{times} " +
            "  and del_flag = 0")
    List<EmrPatientData> getEmrData(String patNo, Integer times);


    @Select("select ledger_sn, " +
            "       detail_sn, " +
            "       charge_date, " +
            "       op_id_code, " +
            "       op_id_code_name = (select rtrim(name) from a_employee_mi where code = op_id_code), " +
            "       charge_code_mx," +
            "case\n" +
            "                          when serial != '00' then\n" +
            "                              (select rtrim(name)\n" +
            "                               from yp_zd_dict\n" +
            "                               where code = charge_code_mx\n" +
            "                                 and zy_detail_charge.serial = yp_zd_dict.serial)\n" +
            "                          else (select rtrim(name) from zd_charge_item where code = charge_code_mx)\n" +
            "                          end as charge_code_name, " +
            "       charge_amount, " +
            "       charge_fee, " +
            "       ward_code, " +
            "       ward_code_name  = (select rtrim(name) from zd_unit_code where code = ward_code), " +
            "       exec_unit, " +
            "       exec_unit_name  = (select rtrim(name) from zd_unit_code where code = exec_unit), " +
            "       order_no," +
            "       actOrderNoStr = cast(cast(order_no as decimal(10)) as varchar(50)), " +
            "       serial, " +
            "       ori_detail_sn " +
            "from zy_detail_charge " +
            " ${ew.customSqlSegment} ")
    List<ZyFeeDetail> getZyFeeDetail(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);


    @Select("select rtrim(inpatient_no) as patNo , admiss_times as times," +
            "       cast(cast(act_order_no as decimal) as varchar) actOrderNoStr," +
            "       cast(cast(parent_no as decimal) as varchar(max)) as parent_no_str " +
            " from view_xc_all_yz where act_order_no = #{orderNo}")
    YiZhuFeiYongChaXunTiaoJian getOrderNoInfo(BigDecimal orderNo);


    @Select("select cast(act_order_no as decimal)  " +
            " from view_xc_all_yz " +
            "where inpatient_no= #{patNo}" +
            " and admiss_times = #{times} " +
            " and parent_no = #{orderNo}")
    List<BigDecimal> getParentNoByOrderNo(String patNo, Integer times, BigDecimal orderNo);


    @Select("select rtrim(patient_id)                      as patNo," +
            "       rtrim(patient_id) + '_' +  CONVERT(varchar(100), charge_date, 23) as uid, " +
            "       CONVERT(varchar(100), charge_date, 23) as date," +
            "       outOrHosp  = 1" +
            "from mz_charge_detail a\n" +
            "where a.patient_id = #{mzNo}\n" +
            "  and pay_mark = 0\n" +
            "  and confirm_flag != 4\n" +
            "  and isnull(cash_id, '') != '99998'" +
            "  and charge_date is not null " +
            "group by patient_id,\n" +
            "         CONVERT(varchar(100), charge_date, 23)\n" +
            "order by date;")
    List<MedicalTrack> getMzMedicalTrack(String mzNo);

}
