package cn.hnthyy.thmz.mapper.his.yz;

import cn.hnthyy.thmz.entity.his.YzActOrder;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 医嘱数据接口
 */
public interface YzActOrderMapper {

    @Update({"<script>",
            " UPDATE yz_act_order SET " +
                    "order_time=#{orderTime}," +
                    "enter_time=#{enterTime}," +
                    "start_time=#{startTime}," +
                    "confirm_time=#{confirmTime}," +
                    "status_time=#{statusTime}," +
                    "end_time=#{endTime}," +
                    "performance_time=#{performanceTime} where inpatient_no=#{inpatientNo} ",
            "<when test='actOrderNoList!=null'>",
            " AND act_order_no IN  ",
            "<foreach item='item' index='index' collection='actOrderNoList' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "</script>"})
    int updateYzActOrderTime(YzActOrder yzActOrder);




    /**
     * 查询在院病人医嘱表中的住院大类
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @return
     */
    @Select({"<script>",
            "select  act_order_no actOrderNo," +
                    "inpatient_no inpatientNo ," +
                    "admiss_times admissTimes ," +
                    "        order_code   orderCode     ," +
                    "order_time    orderTime    ," +
                    "frequ_code     frequCode   ," +
                    "enter_time    enterTime    ," +
                    "enter_oper   enterOper     ," +
                    "start_time    startTime     ," +
                    "end_time       endTime   ," +
                    "infant_flag   infantFlag    ," +
                    "drug_flag  drugFlag  ," +
                    "supply_code  supplyCode ," +
                    "performance_time  performanceTime," +
                    "physician   ," +
                    "status_flag  statusFlag ," +
                    "drug_quan  drugQuan ," +
                    "instruction   ," +
                    "drug_specification drugSpecification," +
                    "dose   ," +
                    "dose_unit  doseUnit," +
                    "exclu_act_order_no excluActOrderNo," +
                    "order_name  orderName ," +
                    "group_no  groupNo," +
                    "serial  ," +
                    "pay_self paySelf ," +
                    "        self_buy selfBuy," +
                    "      cxFlag=0,  suppplyClass='' " +
                    "from    yz_act_order (nolock)" +
                    "where   inpatient_no = #{inpatientNo}  and" +
                    "     admiss_times=#{admissTimes}"
            , "</script>"})
    List<Map<String, Object>> selectZyActOrdersFromYzActOrder(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);

    /**
     * 查询出院医嘱的医嘱时间
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param tableName 表名 yz_act_order -- 在院表    yz_inact_order --出院表
     * @return
     */
    @Select("select max ( convert ( varchar ( 10 ) , dateadd ( day , 0 , start_time ) , 120 ) ) from ${tableName} where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and status_flag > '1' and isnull ( group_no , '00' ) ='00' and order_code in ('06025','06027','06026','06053') ")
    String selectLeaveOrderDate(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes,@Param("tableName") String tableName);




    /**
     * 转移住院/出院病人信息
     * @param sourceTableName 源数据表
     * @param targetTableName 目标数据表
     * @param inpatientNo 病人住院号
     * @param admissTimes 病人住院次数
     * @return
     */
    @Insert("insert into ${targetTableName} select * from ${sourceTableName} where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    int transferYzActOrder(@Param("sourceTableName") String sourceTableName,@Param("targetTableName") String targetTableName, @Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 转移住院/出院病人信息  婴儿
     * @param sourceTableName 源数据表
     * @param targetTableName 目标数据表
     * @param inpatientNoBegin 病人住院号
     * @param inpatientNoEnd   病人住院号
     * @param admissTimes 病人住院次数
     * @return
     */
    @Insert("insert into ${targetTableName} select * from ${sourceTableName} where inpatient_no >= #{inpatientNoBegin} and inpatient_no <= #{inpatientNoEnd} and admiss_times =#{admissTimes} ")
    int transferYzActOrderBaby(@Param("sourceTableName") String sourceTableName,@Param("targetTableName") String targetTableName, @Param("inpatientNoBegin") String inpatientNoBegin, @Param("inpatientNoEnd") String inpatientNoEnd, @Param("admissTimes") Integer admissTimes);

    /**
     * 删除在院/出院病人信息
     * @param tableName 表名
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @return
     */
    @Delete("delete from ${tableName} WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} ")
    int deleteYzActOrder(@Param("tableName") String tableName, @Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);

    /**
     * 删除在院/出院病人信息 婴儿
     * @param tableName 表名
     * @param inpatientNoBegin 住院号
     * @param inpatientNoEnd 住院号
     * @param admissTimes 住院次数
     * @return
     */
    @Delete("delete from ${tableName} WHERE inpatient_no >= #{inpatientNoBegin} and inpatient_no <= #{inpatientNoEnd} AND admiss_times =#{admissTimes} ")
    int deleteYzActOrderBaby(@Param("tableName") String tableName,@Param("inpatientNoBegin") String inpatientNoBegin, @Param("inpatientNoEnd") String inpatientNoEnd, @Param("admissTimes") Integer admissTimes);

}
