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

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

/**
 * 门诊就诊信息表Mapper
 */
public interface MzVisitTableMapper {
    /**
     * 按照病人编号查询最后一次就诊信息
     *
     * @param patientId 病人编号
     * @return
     */
    @Select("select top 1rtrim(patient_id) patient_id,times,rtrim(visit_dept_code) visit_dept_code,rtrim(doctor_code) doctor_code,visit_date,rtrim(icd_code) icd_code," +
            "receipt_no,order_no,req_fee,cancel_mark,error_mark,rtrim(op_id) op_id,op_day,rtrim(req_dept_code) req_dept_code,rtrim(req_doctor) req_doctor,windows_no," +
            "first_or_not,receive_flag,sick_date,rtrim(icd_text) icd_text,rtrim(symptom) symptom,jz_flag,responce_type,dwmc,gh_serial_no from ${tableName} where " +
            "patient_id= #{patientId,jdbcType=VARCHAR} order by times desc")
    MzVisitTable selectLastMzVisitTableByPatientId(@Param("patientId") String patientId,@Param("tableName") String tableName);

    /**
     * 保存门诊就诊信息记录
     * @param mzVisitTable
     * @return
     */
    @Insert("INSERT INTO mz_visit_table(patient_id,times,visit_dept_code,doctor_code,visit_date,icd_code,receipt_no,order_no,req_fee,cancel_mark,error_mark,op_id,op_day,req_dept_code,req_doctor," +
            "windows_no,first_or_not,receive_flag,sick_date,icd_text,symptom,jz_flag,responce_type,dwmc,gh_serial_no)" +
            " VALUES (#{patientId,jdbcType=CHAR},#{times,jdbcType=INTEGER},#{visitDeptCode,jdbcType=CHAR},#{doctorCode,jdbcType=CHAR},#{visitDate,jdbcType=TIMESTAMP},#{icdCode,jdbcType=CHAR}," +
            "#{receiptNo,jdbcType=TINYINT},#{orderNo,jdbcType=TINYINT},#{reqFee,jdbcType=DOUBLE},#{cancelMark,jdbcType=INTEGER},#{errorMark,jdbcType=INTEGER},#{opId,jdbcType=VARCHAR}," +
            "#{opDay,jdbcType=TIMESTAMP},#{reqDeptCode,jdbcType=INTEGER},#{reqDoctor,jdbcType=INTEGER},#{windowsNo,jdbcType=TINYINT},#{firstOrNot,jdbcType=CHAR}," +
            "#{receiveFlag,jdbcType=CHAR},#{sickDate,jdbcType=TIMESTAMP},#{icdText,jdbcType=CHAR},#{symptom,jdbcType=VARCHAR},#{jzFlag,jdbcType=CHAR},#{responceType,jdbcType=VARCHAR},#{dwmc,jdbcType=VARCHAR},#{ghSerialNo,jdbcType=INTEGER})")
    int insertMzVisitTable(MzVisitTable mzVisitTable);


    /**
     * 修改门诊就诊信息
     *
     * @param mzVisitTable
     * @return
     */
    @Update({"<script>",
            "update mz_visit_table ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times}' >",
            "<when test='cancelMark!=null'>",
            "cancel_mark =#{cancelMark,jdbcType=CHAR}",
            "</when>",
            "<when test='orderNo!=null'>",
            ",order_no =#{orderNo,jdbcType=TINYINT}",
            "</when>",
            "<when test='receiptNo!=null'>",
            ",receipt_no =#{receiptNo,jdbcType=TINYINT}",
            "</when>",
            "<when test='receiveFlag!=null'>",
            ",receive_flag =#{receiveFlag,jdbcType=CHAR}",
            "</when>",
            "</trim>"
            , "</script>"})
    int updateMzVisitTable(MzVisitTable mzVisitTable);


    /**
     * 按照病人编号和就诊次数查询就诊信息
     *
     * @param patientId 病人编号
     *
     * @return
     */
    @Select("select rtrim(patient_id) patient_id,times,rtrim(visit_dept_code) visit_dept_code,rtrim(doctor_code) doctor_code,visit_date,rtrim(icd_code) icd_code," +
            "receipt_no,order_no,req_fee,cancel_mark,error_mark,rtrim(op_id) op_id,op_day,rtrim(req_dept_code) req_dept_code,rtrim(req_doctor) req_doctor,windows_no," +
            "first_or_not,receive_flag,sick_date,rtrim(icd_text) icd_text,rtrim(symptom) symptom,jz_flag,responce_type,dwmc,gh_serial_no from ${tableName} where " +
            "patient_id= #{patientId,jdbcType=VARCHAR} and times = #{times}")
    MzVisitTable selectByPatientIdAndTimes(@Param("patientId") String patientId,@Param("times") Integer times,@Param("tableName") String tableName);


    /**
     * 按照病人编号和就诊次数查询就诊信息 历史数据
     *
     * @param patientId 病人编号
     *
     * @return
     */
    @Select("select rtrim(patient_id) patient_id,times,rtrim(visit_dept_code) visit_dept_code,rtrim(doctor_code) doctor_code,visit_date,rtrim(icd_code) icd_code," +
            "receipt_no,order_no,req_fee,cancel_mark,error_mark,rtrim(op_id) op_id,op_day,rtrim(req_dept_code) req_dept_code,rtrim(req_doctor) req_doctor,windows_no," +
            "first_or_not,receive_flag,sick_date,rtrim(icd_text) icd_text,rtrim(symptom) symptom,jz_flag,responce_type,dwmc,gh_serial_no from mz_visit_table_b where " +
            "patient_id= #{patientId,jdbcType=VARCHAR} and times = #{times}")
    MzVisitTable selectHisDataByPatientIdAndTimes(@Param("patientId") String patientId,@Param("times") Integer times);
}
