package thyyxxk.webserver.dao.his.inpatient;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.MzChargeDetail;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.ZyPatientInfo;
import thyyxxk.webserver.entity.transferInOfExpenses.data.CorrectInvoice;

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

/**
 * <p>
 * 描述： 门急诊费用转入
 * </p>
 *
 * @author xc
 * @date 2021-05-25 17:35
 */
@Mapper
public interface TransferInOfExpensesDao {

    /**
     * 查询患者的基本信息
     *
     * @param inpatientNo 住院号
     * @return 返回患者的住院信息
     */
    @Select("select rtrim(inpatient_no) inpatient_no,admiss_times,rtrim(zk_dept) zk_dept,ward,dept," +
            "rtrim(admiss_ward) wardCode," +
            "rtrim(small_dept) deptCode,zy_serial_no, " +
            "rtrim(isnull(operation, '')) as operation, " +
            "rtrim(refer_physician) refer_physician, " +
            "rtrim(consult_physician) consult_physician,zk_ward, " +
            "ledgerSn=(select max(ledger_sn) from zy_ledger_file  " +
            "where inpatient_no=a.inpatient_no and admiss_times=a.admiss_times), " +
            "maxDetailSn=(select max(detail_sn) from zy_detail_charge " +
            "where inpatient_no=a.inpatient_no and admiss_times=a.admiss_times), " +
            "rtrim(a.name)name,sex,rtrim(bed_no)bed_no,(b.name) ward_name, " +
            "admiss_date,dis_date,(c.name)responce_type_name,total_charge,balance " +
            "from zy_actpatient a left join zd_unit_code b on (a.ward = b.code) left join zy_zd_responce_type c on (a.responce_type = c.code) " +
            "where inpatient_no = #{inpatientNo}")
    ZyActpatient queryHuanZhe(String inpatientNo);

    /**
     * 根据机制号来查询门诊发票信息
     *
     * @param tableName 要查询的表格
     * @param serialNo  机制号
     * @return 返回多个
     */
    @Select("select patient_name   =rtrim(a.name), " +
            "       item_name      = c.name, " +
            "       a.quantity, " +
            "       self_flag=c.self_flag, " +
            "       separate_flag  = c.separate_flag, " +
            "       suppress_flag  = c.suppress_flag, " +
            "       charge_code    = case a.bill_item_code " +
            "                            when '010' then 'BILL01' " +
            "                            when '011' then 'BILL28' " +
            "                            when '012' then 'BILL02' " +
            "                            ELSE a.charge_item_code " +
            "           end, " +
            "       charge_code_mx = a.charge_item_code, " +
            "       serial         = a.serial, " +
            "       group_no       = a.group_no, " +
            "       a.unit_price, " +
            "       check_flag='0', " +
            "       warn_dept      =a.warn_dept, " +
            "       doctor_code    = a.doctor_code, " +
            "       bill_item_code = rtrim(a.bill_item_code), " +
            "       exec_dept      = a.exec_dept, " +
            "       a.drug_win " +
            "from ${mzTableName} a, " +
            "     (select distinct patient_id, times, receipt_no, serial_no from ${tableName} where receipt_sn = #{serialNo}) b, " +
            "     zd_charge_item c " +
            "where a.patient_id = b.patient_id " +
            "  and a.times = b.times " +
            "  and a.receipt_no = b.receipt_no " +
            "  and a.serial_no = b.serial_no " +
            "  and a.charge_item_code = c.code " +
            "  and isnull(a.group_no, '00') = '00' " +
            "union " +
            "    all " +
            "select patient_name   =rtrim(a.name), " +
            "       item_name      = (select name " +
            "                         from yp_zd_dict " +
            "                         where yp_zd_dict.code = c.charge_code " +
            "                           and yp_zd_dict.serial = c.serial), " +
            "       a.quantity, " +
            "       self_flag=a.self_flag, " +
            "       separate_flag  = (select separate_flag " +
            "                         from yp_zd_dict " +
            "                         where yp_zd_dict.code = c.charge_code " +
            "                           and yp_zd_dict.serial = c.serial), " +
            "       suppress_flag  = (select suprice_flag " +
            "                         from yp_zd_dict " +
            "                         where yp_zd_dict.code = c.charge_code " +
            "                           and yp_zd_dict.serial = c.serial), " +
            "       charge_code    = case a.bill_item_code " +
            "                            when '010' then 'BILL01' " +
            "                            when '011' then 'BILL28' " +
            "                            when '012' then 'BILL02' " +
            "                            ELSE a.charge_item_code " +
            "           end, " +
            "       charge_code_mx = a.charge_item_code, " +
            "       serial         = a.serial, " +
            "       group_no       = a.group_no, " +
            "       a.unit_price, " +
            "       check_flag='0', " +
            "       warn_dept      =a.warn_dept, " +
            "       doctor_code    = a.doctor_code, " +
            "       bill_item_code = rtrim(a.bill_item_code), " +
            "       exec_dept      = a.exec_dept, " +
            "       a.drug_win " +
            "from ${mzTableName} a, " +
            "     (select distinct patient_id, times, receipt_no, serial_no from ${tableName} where receipt_sn = #{serialNo}) b, " +
            "     yp_base_yf c " +
            "where a.patient_id = b.patient_id " +
            "  and a.times = b.times " +
            "  and a.receipt_no = b.receipt_no " +
            "  and a.serial_no = b.serial_no " +
            "  and a.charge_item_code = c.charge_code " +
            "  and a.serial = c.serial" +
            "  and a.group_no = c.group_no " +
            "  and isnull(a.group_no, '00') <> '00' ")
    List<MzChargeDetail> getSerialNo(@Param("mzTableName") String mzTableName,
                                     @Param("tableName") String tableName,
                                     @Param("serialNo") Integer serialNo);

    @Select("select rtrim(patient_id) as patient_id, times, receipt_no, serial_no,operator_id  as opId from mz_receipt_serial " +
            " where receipt_sn = #{serialNo} " +
            "group by patient_id, times, receipt_no, serial_no,operator_id")
    CorrectInvoice getSerialNoByInvoiceNo(@Param("serialNo") Integer serialNo);

    /**
     * 分页获取这个病人是通过急门诊转入的费用   如果确实插入了数据但是没有看到 那就是他们没有维护好两个字典表
     *
     * @param page        分页对象
     * @param inpatientNo 住院号
     * @return 返回
     */
    @Select("<script>" +
            "select * from (" +
            "select rtrim(inpatient_no)inpatient_no,admiss_times,detail_sn,ledger_sn, " +
            "charge_date,charge_code,charge_fee,a.charge_amount,charge_code_mx,(b.name)charge_code_name,gen_time,  " +
            "zy_serial_no " +
            "from zy_detail_charge a,zd_charge_item b  where (code =charge_code) and order_no=6 and inpatient_no = #{inpatientNo} and ledger_sn = #{ledgerSn} " +
            "and admiss_times = #{admissTimes} AND (a.charge_amount !&lt; 0 or charge_code_mx = 'BILL99') " +
            "<if test=\"chargeCodeMX!=null and chargeCodeMX!=''\"> " +
            "and (charge_code_mx = #{chargeCodeMX} or charge_code = #{chargeCodeMX}) " +
            "</if> " +
            "union " +
            "select rtrim(inpatient_no)inpatient_no,admiss_times,detail_sn,ledger_sn, " +
            "charge_date,charge_code,charge_fee,a.charge_amount,charge_code_mx,(b.name)charge_code_name,gen_time, " +
            "zy_serial_no " +
            "from zy_detail_charge a,yp_zd_dict b where (code=charge_code) and order_no=6 and inpatient_no = #{inpatientNo} and ledger_sn = #{ledgerSn} " +
            "and admiss_times = #{admissTimes} AND (a.charge_amount !&lt; 0 or charge_code_mx = 'BILL99')  " +
            "<if test=\"chargeCodeMX!=null and chargeCodeMX!=''\"> " +
            "and (charge_code_mx = #{chargeCodeMX} or charge_code = #{chargeCodeMX}) " +
            "</if>" +
            ") temp" +
            "</script> ")
    List<ZyDetailCharge> getZyJiMenZhenZhuangRuFeiYong(IPage<ZyDetailCharge> page,
                                                       @Param("inpatientNo") String inpatientNo,
                                                       @Param("chargeCodeMX") String chargeCodeMX,
                                                       @Param("admissTimes") String admissTimes,
                                                       @Param("ledgerSn") Integer ledgerSn);

    /**
     * 获取费用总和
     *
     * @param inpatientNo  住院号
     * @param chargeCodeMX 项目编码
     * @return 返回费用
     */
    @Select("<script>" +
            "select charge_fee,charge_amount,charge_code " +
            "from zy_detail_charge a  where  order_no=6 and inpatient_no = #{inpatientNo}  " +
            "and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} " +
            "<if test=\"chargeCodeMX!=null and chargeCodeMX!=''\"> " +
            "and (charge_code_mx = #{chargeCodeMX} or charge_code = #{chargeCodeMX}) " +
            "</if> " +
            "</script>")
    List<ZyDetailCharge> sumZyFee(@Param("inpatientNo") String inpatientNo,
                                  @Param("chargeCodeMX") String chargeCodeMX,
                                  @Param("admissTimes") String admissTimes,
                                  @Param("ledgerSn") Integer ledgerSn);

    /**
     * 删除病人通过急门诊转入的费用 数据
     *
     * @param param 删除的条件
     */
    @Delete("delete from zy_detail_charge where inpatient_no=#{inpatientNo} and " +
            "admiss_times =#{admissTimes} and ledger_sn=#{ledgerSn} and detail_sn=#{detailSn}")
    void shanChuFeiYong(ZyDetailCharge param);

    /**
     * 插入数据 急门诊数据转入住院费用
     * 这上面的数据都是在前面查询找到的
     *
     * @param info   住院患者的信息
     * @param zyFees 费用 list
     */
    @Insert("<script>" +
            "insert into zy_detail_charge (inpatient_no,admiss_times,ledger_sn,detail_sn,charge_date,op_id_code, " +
            "charge_code,infant_flag,charge_status,charge_amount,charge_fee,ward_code,dept_code,order_no,ope_flag, " +
            "gen_time,charge_code_mx,serial,doctor_code,trans_flag_yb,refer_physician,consult_physician,exec_unit,zy_serial_no) VALUES " +
            "<foreach collection='list' item='fee' separator=',' open='' close=''>" +
            "(#{info.inpatientNo}, #{info.admissTimes}, #{info.ledgerSn}, #{fee.detailSn}, #{info.chargeDate}, #{info.staffId}, " +
            "#{fee.chargeCode}, 0, 2, #{fee.chargeQuantity}, cast(#{fee.chargeFee,jdbcType=DECIMAL} as decimal(14,5)), #{info.zkWard}, #{info.dept}, 6, " +
            "'',#{info.chargeDate}, #{fee.chargeCodeMx},#{fee.serial},#{fee.doctorCode},0,#{info.referPhysician}," +
            "#{info.consultPhysician},#{info.execUnit},#{info.zySerialNo})" +
            "</foreach>" +
            "</script>")
    void insertInfoZyDetailCharge(@Param("info") ZyActpatient info,
                                  @Param("list") List<MzChargeDetail> zyFees);

    @Select("select admiss_date,dis_date " +
            "from zy_actpatient where inpatient_no = #{patNo}")
    ZyPatientInfo getPatientHospitalStay(String patNo);


    /**
     * 查询出这个患者的最大流水号，在新增费用的时候要加一
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @return 返回最大的流水号
     */
    @Select("select isnull(max(detail_sn),0) from zy_detail_charge where " +
            "inpatient_no=#{inpatientNo} and admiss_times=#{admissTimes}")
    Integer getMaxDetailSn(@Param("inpatientNo") String inpatientNo,
                           @Param("admissTimes") Integer admissTimes);

    /**
     * 远程搜索 项目名称
     *
     * @param pyCode 拼音码 大写的
     * @return 返回多个项目的名称
     */
    @Select("select * from (" +
            "select code,rtrim(name)name from zd_charge_item where py_code like #{pyCode} or code like #{pyCode} " +
            "union " +
            "select code,rtrim(name)name from yp_zd_dict where py_code like #{pyCode} or code like #{pyCode} " +
            ") temp")
    List<GetDropdownBox> queryEntryName(@Param("pyCode") String pyCode);


    @Select("select qrcode,charge_code,name = (select rtrim(name) from mz_patient_mi where mz_patient_mi.patient_id = mz_dzfp_upload.patient_id) " +
            "from mz_dzfp_upload\n" +
            "where patient_id = '${patientId}'\n" +
            "  and times = ${times}\n" +
            "  and receipt_no = ${receiptNo} and type_flag = 5 and del_flag = 0")
    CorrectInvoice.DzcfVo getMzDzcfUpload(CorrectInvoice params);


    @Select("select sum(cast((quantity * drug_win * unit_price) as decimal(14, 2))) as money " +
            "from mz_charge_detail " +
            "${ew.customSqlSegment}")
    BigDecimal getDzfpChangeTotal(@Param("ew") QueryWrapper<?> qw);
}
