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

import cn.hnthyy.thmz.entity.his.mz.MzfzPatientOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface MzfzPatientOrderMapper {
    /**
     * 插入分诊信息
     * @param mzfzPatientOrder
     * @return
     */
    @Insert("insert into mzfz_patient_order (patient_id,times,name,dept_code,room_code,doctor_code,gh_no,visit_date,slow_flag,serial_no,req_type,req_order,ampm,fz_no)" +
            " values(#{patientId},#{times},#{name},#{deptCode},#{roomCode},#{doctorCode},#{ghNo},#{visitDate,jdbcType=TIMESTAMP},#{slowFlag},#{serialNo},#{reqType},#{reqOrder},#{ampm},9999) ")
    int insertMzfzPatientOrder(MzfzPatientOrder mzfzPatientOrder);


    /**
     * 查询符合条件的待就诊分诊数据总数
     *
     * @param mzfzPatientOrder
     * @return
     */
    @Select({"<script>",
            "select count(*) count from ${tableName}  where 1=1 ",
            "<when test='statusFlagList!=null'>",
            " and status_flag in ",
            "<foreach item='item' index='index' collection='statusFlagList' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            " and (doctor_code =#{doctorCode} ",
            "<when test='deptCodes!=null'>",
            " or dept_code in " +
            "<foreach item='item' index='index' collection='deptCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            ") ",
            "<when test='patientId!=null'>",
            " and patient_id =#{patientId}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and visit_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and visit_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='name!=null'>",
            " and name like #{name}  ",
            "</when>",
            "</script>"})
    Integer countUnClinic(MzfzPatientOrder mzfzPatientOrder);

    /**
     * 分页查询待就诊分诊信息
     *
     * @param mzfzPatientOrder
     * @return
     */
    @Select({"<script>",
            "select top ${pageSize} rtrim(patient_id) patient_id,times,name,dept_code,room_code,doctor_code,gh_no,visit_date,slow_flag,serial_no,req_type,req_order,ampm,fz_no,status_flag,call_time FROM (SELECT ROW_NUMBER() OVER (ORDER BY mzfz_patient_order_page.fz_no asc) AS RowNumber,",
            "* from ${tableName} mzfz_patient_order_page where 1=1 ",
            "<when test='statusFlagList!=null'>",
            " and status_flag in ",
            "<foreach item='item' index='index' collection='statusFlagList' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            " and (doctor_code =#{doctorCode} ",
            "<when test='deptCodes!=null'>",
            " or dept_code in " +
                    "<foreach item='item' index='index' collection='deptCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            ") ",
            "<when test='patientId!=null'>",
            " and patient_id =#{patientId}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and visit_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and visit_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='name!=null'>",
            " and name like #{name}  ",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<MzfzPatientOrder> selectUnClinicWithPage(MzfzPatientOrder mzfzPatientOrder);


    /**
     * 查询符合条件的分诊数据总数
     *
     * @param mzfzPatientOrder
     * @return
     */
    @Select({"<script>",
            "select count(*) count from ${tableName}  where 1=1 ",
            "<when test='statusFlagList!=null'>",
            " and status_flag in ",
            "<foreach item='item' index='index' collection='statusFlagList' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "<when test='doctorCode!=null'>",
            " and doctor_code =#{doctorCode}  ",
            "</when>",
            "<when test='beginTime!=null'>",
            " and visit_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and visit_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "</script>"})
    Integer countMzfzPatientOrder(MzfzPatientOrder mzfzPatientOrder);

    /**
     * 分页查询分诊信息
     *
     * @param mzfzPatientOrder
     * @return
     */
    @Select({"<script>",
            "select top ${pageSize} rtrim(patient_id) patient_id,times,name,dept_code,room_code,doctor_code,gh_no,visit_date,slow_flag,serial_no,req_type,req_order,ampm,fz_no,status_flag,call_time FROM (SELECT ROW_NUMBER() OVER (ORDER BY mzfz_patient_order_page.fz_no asc) AS RowNumber,",
            "* from ${tableName} mzfz_patient_order_page where 1=1 ",
            "<when test='statusFlagList!=null'>",
            " and status_flag in ",
            "<foreach item='item' index='index' collection='statusFlagList' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "<when test='doctorCode!=null'>",
            " and doctor_code =#{doctorCode} ",
            "</when>",
            "<when test='beginTime!=null'>",
            " and visit_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and visit_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<MzfzPatientOrder> selectMzfzPatientOrderWithPage(MzfzPatientOrder mzfzPatientOrder);

    /**
     * 根据门诊分诊流水号查询分诊信息
     * @param serialNo
     * @return
     */
    @Select("select rtrim(patient_id) patient_id,times,name,dept_code,room_code,doctor_code,gh_no,visit_date,slow_flag,serial_no,req_type,req_order,ampm,fz_no,status_flag,call_time FROM mzfz_patient_order where serial_no=#{serialNo}")
    MzfzPatientOrder selectBySerialNo(@Param("serialNo") String serialNo);

    /**
     * 修改分诊信息
     *
     * @param mzfzPatientOrder
     * @return
     */
    @Update({"<script>",
            "update mzfz_patient_order ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where serial_no = #{serialNo}' >",
            "<when test='statusFlag!=null'>",
            "status_flag =#{statusFlag}",
            "</when>",
            "<when test='callTime!=null'>",
            "call_time =#{callTime}",
            "</when>",
            "</trim>"
            , "</script>"})
    int updateMzfzPatientOrder(MzfzPatientOrder mzfzPatientOrder);
}
