package thyyxxk.webserver.dao.his.medicaladvice.patientinfo;

import org.apache.ibatis.annotations.Insert;
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.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.patientinfo.ZkBedVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.ZyOrderZk;

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

/**
 * @Description: 转科处理
 * @Author:lihong
 * @Date: 2024/1/3
 */
@Mapper
public interface ZkManageDao {

    @Select(" <script>" +
            "  SELECT inpatient_no,  " +
            "       admiss_times,  " +
            "       name,  " +
            "       admiss_date,  " +
            "       bed_status,  " +
            "       ward,  " +
            "       ward_name=(select name from zd_unit_code where ward=code),  " +
            "       dept,  " +
            "       bed_no,  " +
            "       zk_dept,  " +
            "       zk_ward,  " +
            "       zk_ward_name =(select name from zd_unit_code where zk_ward=code),  " +
            "       times_billed,  " +
            "       refer_physician,  " +
            "       consult_physician, " +
            "   '安排' as file_status, " +
            "       dept_director  " +
            " FROM zy_actpatient  " +
            " where  " +
            "     ward = #{wardCode}  " +
            "<if test=\" keyWard !='' and keyWard !=null  \">"+
            " <bind name=\"pattenName\" value=\"keyWard+'%'\" />"+
            "     and (inpatient_no like #{pattenName}  or name like #{pattenName} )  " +
            "</if>"+
            " order by cast(bed_no as float), inpatient_no" +
            " </script>")
    List<ZyActpatient> selectZkPatient(@Param("keyWard") String keyWard, @Param("wardCode")String wardCode);

    @Select(" SELECT ward_code,  " +
            " ward_name=(select name from zd_unit_code where ward_code=code), " +
            "       dept_code,  " +
            "       bed_no,  " +
            "       '' as inpatient_no,  " +
            "      null as  admiss_times,  " +
            "       '' as orig_dept,  " +
            "       '' as orig_ward,  " +
            "       '' as orig_bed,  " +
            "       '' as name,  " +
            "       '' as zk_dept,  " +
            "       '' as refer_physician,  " +
            "       '' as consult_physician,  " +
            "       '' as dept_director  " +
            " FROM zy_bed_mi  " +
            " where ward_code like #{wardCode}  " +
            "  and bed_status = '1'  " +
            "  and isnull(inpatient_no, '') = ''  " +
            " order by cast(bed_no as float)")
    List<ZkBedVo> selectZkBedVo(String wardCode);
   /**
    * @description: 查询是否有转科医嘱
    * @author: lihong
    * @date: 2024/1/4 10:45
    * @param: param
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    **/
   @Select(" select count(*) zk_count , max(act_order_no)  act_order_no  " +
           " from yz_act_order  " +
           " where inpatient_no = #{inpatientNo}  " +
           "  and admiss_times = #{admissTimes}  " +
           "  and status_flag > '2'  " +
           "  and isnull(group_no, '00') = '00'  " +
           "  and dept_code = #{wardCode}  " +
           "  and order_code in (select order_code from yz_zd_order_item_confirm where item_name = '转科')")
    Map<String, Object> selectZkYz(ZkBedVo param);
    @Select(" select count ( *) from zy_zk_list where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and act_order_no =#{actOrderNo} ")
    Integer selectZyZkList(@Param("inpatientNo") String inpatientNo, @Param("admissTimes")Integer admissTimes, @Param("actOrderNo")BigDecimal actOrderNo);
    /**
     * @description: 查询是否有未提交的药单
     * @author: lihong
     * @date: 2024/1/4 11:02
     * @param: param
     * @return: java.lang.Integer
     **/
    @Select(" select count(case when a.amount > 0 and a.page_no = 0 then 1 else null end)  " +
            " from yz_yp_zy_order a,  " +
            "     yz_act_order b  " +
            " where a.inpatient_no = #{inpatientNo}  " +
            "  and a.admiss_times = #{admissTimes}  " +
            "  and a.status_flag = '1'  " +
            "  and a.inpatient_no = b.inpatient_no  " +
            "  and a.admiss_times = b.admiss_times  " +
            "  and a.act_order_no = b.act_order_no")
    Integer countYd(ZkBedVo param);
    @Select( " select sum(case" +
            "               when status_flag = '1' and (case when amount < 0 then isnull(page_no_ty, 0) else page_no end) > 0 and" +
            "                    (amount > 0 or (amount < 0 and drug_class <> 'd')) then 1 end)" +
            " from yz_yp_zy_order" +
            " where admiss_times = #{admissTimes}" +
            "  and inpatient_no = #{inpatientNo}")
    Integer sumTy(ZkBedVo param);
    @Select(" select count(*) from zy_actpatient where inpatient_no like #{inpatientNo} ")
    Integer countBaby(String inpatientNo);
    @Update(" update zy_actpatient  " +
            "SET ward    =#{wardCode},  " +
            "    dept    =#{wardCode},  " +
            "    bed_no  =#{bedNo},  " +
            "    zk_ward =#{zkDept}  " +
            "WHERE inpatient_no = #{inpatientNo}  " +
            "  AND admiss_times = #{admissTimes} ")
    int updateZkZyActpatien(ZkBedVo item);
    @Insert(" INSERT INTO zy_adt (inpatient_no, admiss_times, trans_date, mark, trans_times, orig_ward, orig_dept, orig_room,  " +
            "                    orig_bed, curr_ward, curr_dept, curr_room, curr_bed, refer_physician, consult_physician,  " +
            "                    dept_director)  " +
            " select  " +
            "       #{inpatientNo},  " +
            "       #{admissTimes},  " +
            "       getdate(),  " +
            "       '5',  " +
            "       case when max(trans_times) is null then 1 else max(trans_times)+1 end trans_times,  " +
            "       #{origWard},  " +
            "       #{origDept},  " +
            "       '',  " +
            "       #{origBed},  " +
            "        #{zkDept},  " +
            "       #{wardCode},  " +
            "       '',  " +
            "       #{bedNo},  " +
            "        #{referPhysician},  " +
            "        #{consultPhysician},  " +
            "        #{deptDirector}  " +
            " from zy_adt  " +
            " where inpatient_no = #{inpatientNo}  " +
            "  and admiss_times = #{admissTimes}")
    int insertZyAdt(ZkBedVo param);
    @Insert(" insert into zy_zk_list (inpatient_no, admiss_times, f_ward_code, f_dept_code, ward_code, dept_code, zk_times, op_id, " +
            "                        op_time, act_order_no, small_dept) " +
            " select #{inpatientNo}, #{admissTimes}, #{origDept}, #{origDept}, #{wardCode}, #{wardCode},(select  case when max(trans_times) is null then 1 else max(trans_times)+1 end trans_times from zy_adt  " +
            "    WHERE inpatient_no = #{inpatientNo}  " +
            "    and admiss_times = #{admissTimes}) ,#{opId},getdate(),max(act_order_no),#{zkDept} " +
            " from yz_act_order  " +
            " where inpatient_no = #{inpatientNo}  " +
            "  and admiss_times = #{admissTimes}  " +
            "  and order_code in (select order_code from yz_zd_order_item_confirm where item_name = '转科') ")
    int insertZyZkList(ZkBedVo zkBedVo);
    @Select(" select a.act_order_no  " +
            " from yz_yp_zy_order a,  " +
            "     yz_act_order b  " +
            " where a.inpatient_no = #{inpatientNo}  " +
            "  and a.admiss_times = #{admissTimes}  " +
            "  and a.status_flag = '1'  " +
            "  and a.inpatient_no = b.inpatient_no  " +
            "  and a.admiss_times = b.admiss_times  " +
            "  and a.act_order_no = b.act_order_no  " +
            "  and  a.amount > 0 and a.page_no = 0")
    List<BigDecimal> detailYd(ZkBedVo param);
    @Select(" select act_order_no  " +
            " from yz_yp_zy_order  " +
            " where admiss_times = #{admissTimes}  " +
            "  and inpatient_no = #{inpatientNo}   " +
            "  and  status_flag = '1'  " +
            "  and (case when amount < 0 then isnull(page_no_ty, 0) else page_no end) > 0  " +
            "  and (amount > 0 or (amount < 0 and drug_class <> 'd'))")
    List<BigDecimal> detailTy(ZkBedVo param);
    @Select(" select top 1 * from zy_order_zk  where act_order_no=#{actOrderNo} ")
    ZyOrderZk selectZkOrder(BigDecimal actOrderNo);
    @Insert(" insert into zy_zk_list (inpatient_no, admiss_times, f_ward_code, f_dept_code, ward_code, dept_code, zk_times, op_id, " +
            "                        op_time, small_dept) " +
            " select top 1 #{inpatientNo}, #{admissTimes}, #{origDept}, #{origDept}, #{wardCode}, #{wardCode},  case when max(trans_times) is null then 1 else max(trans_times)+1 end trans_times, #{opId},getdate(),#{zkDept}"
            +"  from  zy_adt where  inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} "
    )
    int insertZyZkListNoActOrderNo(ZkBedVo item);
}
