package thyyxxk.webserver.dao.his.zhuyuanyiji;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.datamodify.YzActOrder;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.entity.zhuyuanyisheng.jianyanjiancha.YshYjReq;

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

/**
 * <p>
 * 描述： 确认医嘱收费
 * </p>
 *
 * @author xc
 * @date 2021-09-27 11:51
 */
@Mapper
public interface QueRenYiZhuShouFeiDao {

    @Select("<script>" +
            "select  " +
            "rtrim(a.inpatient_no) inpatient_no, a.admiss_times, bed_no =b.bed_no," +
            "case when b.name IS NULL  then (select top(1) rtrim(name) name from zy_inactpatient where zy_inactpatient.inpatient_no = a.inpatient_no) else  rtrim(b.name) end name, " +
            "a.ward_code ,b.sex,b.admiss_date,  " +
            "ledger_sn = (select max(ledger_sn) from zy_ledger_file where zy_ledger_file.inpatient_no = b.inpatient_no  " +
            "and zy_ledger_file.admiss_times = b.admiss_times),b.total_charge,  " +
            "responce_type_name = (select rtrim(name) from zy_zd_responce_type where zy_zd_responce_type.code = b.responce_type),  " +
            "ward_name = (select name from zd_unit_code where code = ward_code),  " +
            "shi_fou_you_chu_yuan_yi_zhu =  " +
            "case when (select count(1) from yz_act_order where inpatient_no= a.inpatient_no and admiss_times= a.admiss_times  " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in (select order_code\n" +
            "from yz_zd_order_item_confirm\n" +
            "where item_name = N'出院')) > 0  " +
            "then 1  " +
            "when (select count(1) from yz_inact_order where inpatient_no= a.inpatient_no and admiss_times= a.admiss_times  " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in (select order_code\n" +
            "from yz_zd_order_item_confirm\n" +
            "where item_name = N'出院')) > 0  " +
            "then 1 else 0 end  " +
            "FROM yz_zy_patient_fee a  left join  zy_actpatient b on (a.inpatient_no = b.inpatient_no and a.admiss_times = b.admiss_times)  " +
            "where a.charge_status ='3' " +
            "<if test=\"list.size > 0\">" +
            "and a.exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item}  " +
            "</foreach>" +
            "</if>" +
            "<if test=\"inpatientNo !=null and inpatientNo != '' \">" +
            " and a.inpatient_no = #{inpatientNo}  " +
            "</if>" +
            "<if test=\"wardCode !=null and wardCode != '' \">" +
            " and a.ward_code = #{wardCode}  " +
            "</if>" +
            "<choose>" +
            "<when  test=\"startTime != null and startTime != '' \">" +
            " and a.occ_time &gt;= #{startTime} and a.occ_time &lt;= #{endTime} " +
            "</when >" +
            "<otherwise>" +
            " and convert(varchar(20),a.occ_time,23)&lt;=convert(varchar(20),getdate(),23) " +
            "</otherwise>" +
            "</choose>" +
            "group by a.inpatient_no, a.admiss_times, b.bed_no, b.name, a.ward_code, b.total_charge, b.sex, b.admiss_date,b.inpatient_no,b.admiss_times,b.responce_type " +
            "order by ${fenLei}" +
            "</script>")
    IPage<YzActOrder> getXuQueFeiYiZhu(IPage<YzActOrder> page,
                                       @Param("inpatientNo") String inpatientNo,
                                       @Param("wardCode") String wardCode,
                                       @Param("list") List<String> list,
                                       @Param("startTime") String startTime,
                                       @Param("endTime") String endTime,
                                       @Param("fenLei") String fenLei);


    /**
     * 获取需要确认医嘱收费的总数
     *
     * @param list 科室集合
     * @return 返回信息
     */
    @Select("<script>" +
            "select count(*) from (" +
            "select  " +
            "a.inpatient_no, a.admiss_times, bed_no =b.bed_no, b.name, a.ward_code ,b.sex,b.admiss_date,  " +
            "ledger_sn = (select max(ledger_sn) from zy_ledger_file where zy_ledger_file.inpatient_no = b.inpatient_no  " +
            "and zy_ledger_file.admiss_times = b.admiss_times),b.total_charge,  " +
            "responce_type_name = (select rtrim(name) from zy_zd_responce_type where zy_zd_responce_type.code = b.responce_type),  " +
            "ward_name = (select name from zd_unit_code where code = ward_code),  " +
            "shi_fou_you_chu_yuan_yi_zhu =  " +
            "case when (select count(1) from yz_act_order where inpatient_no= a.inpatient_no and admiss_times= a.admiss_times  " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in ('06026','06053','05973')) > 0  " +
            "then 1  " +
            "when (select count(1) from yz_inact_order where inpatient_no= a.inpatient_no and admiss_times= a.admiss_times  " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in ('06026','06053','05973')) > 0  " +
            "then 1 else 0 end  " +
            "FROM yz_zy_patient_fee a  left join  zy_actpatient b on (a.inpatient_no = b.inpatient_no and a.admiss_times = b.admiss_times)  " +
            "where a.charge_status = '3' " +
            "<if test=\"list.size > 0\">" +
            "and a.exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item}  " +
            "</foreach>" +
            "</if>" +
            "<if test=\"inpatientNo !=null and inpatientNo != '' \">" +
            " and a.inpatient_no = #{inpatientNo}  " +
            "</if>" +
            "<if test=\"wardCode !=null and wardCode != '' \">" +
            " and a.ward_code = #{wardCode}  " +
            "</if>" +
            "<choose>" +
            "<when  test=\"startTime != null and startTime != '' \">" +
            " and a.occ_time &gt;= #{startTime} and a.occ_time &lt;= #{endTime} " +
            "</when >" +
            "<otherwise>" +
            " and convert(varchar(20),a.occ_time,23)&lt;=convert(varchar(20),getdate(),23) " +
            "</otherwise>" +
            "</choose>" +
            "group by a.inpatient_no, a.admiss_times, b.bed_no, b.name, a.ward_code, b.total_charge, b.sex, b.admiss_date,b.inpatient_no," +
            "b.admiss_times,b.responce_type" +
            ") temp " +
            "</script>")
    long getXuQueFeiYiZhuTotal(@Param("inpatientNo") String inpatientNo,
                               @Param("wardCode") String wardCode,
                               @Param("list") List<String> list,
                               @Param("startTime") String startTime,
                               @Param("endTime") String endTime);

    @Select("<script>" +
            "select a.charge_status,e.name as doctorName,a.act_order_no,c.name charge_name,a.charge_code,a.charge_fee," +
            "a.exec_unit,exec_unit_name = (select rtrim(name) from zd_unit_code where code = a.exec_unit)," +
            "a.order_count,occ_time,rtrim(a.inpatient_no) inpatient_no,op_id,a.op_date, " +
            "a.admiss_times" +
            "<if test=\"startTime != null and startTime != '' \"> " +
            ",op_id_name = (select rtrim(name) name from a_employee_mi where a_employee_mi.code = a.op_id)," +
            "a.op_date" +
            "</if>" +
            "from yz_zy_patient_fee a  left join  zd_charge_item c on (a.charge_code=c.code) " +
            "left join yz_act_order d on (a.inpatient_no=d.inpatient_no and a.admiss_times=d.admiss_times and a.act_order_no=d.act_order_no ) " +
            "left join a_employee_mi e on (d.physician=e.code)" +
            "where  " +
            "a.charge_status = '3' " +
            "<choose>" +
            "<when  test=\"startTime != null and startTime != '' \">" +
            " and a.occ_time &gt;= #{startTime} and a.occ_time &lt;= #{endTime} " +
            "</when >" +
            "<otherwise>" +
            " and convert(varchar(20),a.occ_time,23)&lt;=convert(varchar(20),getdate(),23) " +
            "</otherwise>" +
            "</choose>" +
            "<if test=\"list.size > 0\">" +
            "and a.exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item}  " +
            "</foreach>" +
            "</if>" +
            "and a.inpatient_no in " +
            "<foreach collection='inpatientNoList' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item.inpatientNo}  " +
            "</foreach>" +
            "</script>")
    List<YzActOrder> getXuQueFeiXiangXiXinXi(@Param("list") List<String> list,
                                             @Param("inpatientNoList") List<YzActOrder> inpatientNoList,
                                             @Param("startTime") String startTime,
                                             @Param("endTime") String endTime);

    @Select("select * from yz_zy_patient_fee with (NOLOCK) ${ew.customSqlSegment}")
    List<YzActOrder> getXuQueFeiXiangXiByOrderList(@Param("ew") QueryWrapper<YzActOrder> actOrderNo);


    @Select("<script>" +
            "select a.charge_status,e.name doctorName,a.act_order_no,c.name charge_name,a.charge_code,a.charge_fee, " +
            "a.exec_unit,exec_unit_name = (select rtrim(name) from zd_unit_code where code = a.exec_unit)," +
            "       a.order_count,occ_time,rtrim(a.inpatient_no) inpatient_no,op_id,a.op_date, " +
            "       a.admiss_times, " +
            "       op_id_name = (select rtrim(name) name from a_employee_mi where a_employee_mi.code = a.op_id)," +
            "       name = (select rtrim(name) from zy_patient where zy_patient.inpatient_no = a.inpatient_no " +
            "and zy_patient.admiss_times = a.admiss_times), " +
            "       a.op_date " +
            "from yz_zy_patient_fee a  left join  zd_charge_item c on (a.charge_code=c.code) " +
            "                          left join yz_act_order d on (a.inpatient_no=d.inpatient_no and a.admiss_times=d.admiss_times and a.act_order_no=d.act_order_no ) " +
            "                          left join a_employee_mi e on (d.physician=e.code) " +
            "where occ_time &gt;= #{startTime} and occ_time &lt;= #{endTime} " +
            "and a.charge_status &lt;&gt; '3' " +
            "<if test=\"inpatientNo != null and inpatientNo != '' \">" +
            "and a.inpatient_no = #{inpatientNo} " +
            "</if>" +
            "<if test=\"execUnitList.size > 0\">" +
            "and a.exec_unit in " +
            "<foreach collection='execUnitList' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item}  " +
            "</foreach>" +
            "</if>" +
            "<if test=\"wardCode != null and wardCode != '' \">" +
            "and a.ward_code = #{wardCode} " +
            "</if>" +
            "<if test=\"code != null and code != '' \">" +
            "and a.op_id = #{code}" +
            "</if>" +
            "order by a.inpatient_no" +
            "</script>")
    List<YzActOrder> huoQuLiShiXinXi(@Param("startTime") String startTime,
                                     @Param("endTime") String endTime,
                                     @Param("inpatientNo") String inpatientNo,
                                     @Param("execUnitList") List<String> execUnitList,
                                     @Param("wardCode") String wardCode,
                                     @Param("code") String code);


    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "UPDATE yz_zy_patient_fee " +
            "<set>" +
            " charge_status = #{item.chargeStatus}, op_id = #{code}, op_date = GETDATE() " +
            "</set>" +
            "WHERE act_order_no = #{item.actOrderNo} " +
            "AND inpatient_no = #{item.inpatientNo} AND admiss_times = #{item.admissTimes} " +
            "and CONVERT(varchar(100), occ_time, 20) = #{item.occTime} " +
            "and act_order_no = #{item.actOrderNo} " +
            "and charge_code = #{item.chargeCode}" +
            "</foreach>" +
            "</script>")
    void baoCunYiZhuQueFei(@Param("list") List<YzActOrder> list,
                           @Param("code") String code);


    @Select("<script>" +
            "select rtrim(a.inpatient_no) inpatient_no,a.admiss_times,rtrim(e.name) patient_name,  " +
            "a.charge_status,doctorName = (select rtrim(name) name from a_employee_mi where a_employee_mi.code = d.physician),  " +
            "a.act_order_no,c.name order_name,a.charge_code,a.charge_fee,a.order_count,occ_time, " +
            "op_id_name = (select rtrim(name) name from a_employee_mi where a_employee_mi.code = a.op_id),  " +
            "a.op_date,rtrim(f.name) ward_name  " +
            "from yz_zy_patient_fee a  " +
            "left join  zd_charge_item c on (a.charge_code=c.code)  " +
            "left join yz_act_order d on (a.inpatient_no=d.inpatient_no and a.admiss_times=d.admiss_times and a.act_order_no=d.act_order_no)  " +
            "left join zy_actpatient e on (a.inpatient_no = e.inpatient_no)  " +
            "left join zd_unit_code f on (a.ward_code = f.code)  " +
            "where a.charge_status &lt;&gt; '3' " +
            " and convert(varchar(20),a.occ_time,23)&lt;=convert(varchar(20),getdate(),23) " +
            "<if test=\"list.size > 0\">" +
            " and a.exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item}  " +
            "</foreach>" +
            "</if>" +
            "<if test=\"yiQueFeiShuJu != null and yiQueFeiShuJu.size > 0 \">" +
            " and a.inpatient_no in " +
            "<foreach collection='yiQueFeiShuJu' item='item' index='index' open='(' close=')' separator=','>" +
            "    #{item.inpatientNo}  " +
            "</foreach>" +
            "</if>" +
            "order by a.ward_code" +
            "</script>")
    List<YzActOrder> huoQuLiShiQueFeiXinXi(
            @Param("list") List<String> execUnitList,
            @Param("yiQueFeiShuJu") List<Patient> yiQueFeiShuJu);


}
