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

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

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

public interface ZyActpatientMapper {
    /**
     * 入库病人在院或者出院信息
     *
     * @param zyActpatient
     * @return
     */
    @Insert("INSERT INTO ${tableName}(inpatient_no, admiss_times, name, outpatient_no, charge_type, visit_type, admiss_date, admiss_status, " +
            "clinic_diag, admiss_diag, admiss_physician, refer_physician, consult_physician, dept_director, admiss_dept, admiss_ward," +
            " dis_date, dis_diag, dis_status, dis_dept, dis_ward, insur_code, contract_code, responce_type, times_billed, balance," +
            " total_charge, charge1, charge2, charge3, charge4, charge5, charge6, charge7, charge8, charge9, charge10, charge11," +
            " charge12, charge13, charge14, charge15, charge16, charge17, charge18, charge19, charge20, charge21, charge22, charge23, " +
            "charge24, charge25, charge26, charge27, charge28, charge29, charge30, ward, dept, bed_no, sex, blood_type, diagnosis_date, " +
            "allergy_medicine, rescue_times, success_times, followup_mark, followup_prior, autopsy_mark, quality_level, op_id_code, " +
            "archive_code, archive_mark, teaching_mark, diagnose_conform1, diagnose_conform2, diagnose_conform3, diagnose_conform4," +
            " diagnose_conform5, diagnose_conform6, diagnose_conform7, diagnose_conform8, intern_code, diagnose_conform9, diagnose_conform10," +
            " pathology_diag, dis_hour, admiss_hour, cap_bloody, max_ledger, admiss_diag_str, patient_status, move_flag, jzjlh, zk_dept, " +
            "hbsag, archive_code1, job_doctor, job_nurse, ybjy, bczf, zflj, dis_diag_str, yb_zyh, yb_jzlb, yz_in, yz_out, yb_flag, owe_flag, " +
            "zk_ward, zk_date, yb_jlh, yb_type, zy_serial_no, mz_no, clinic_diag_str, bed_status, input_date, yb_jsffbz, yb_sylb, yb_zszh," +
            " admiss_date_yb, ba_receive_flag, blzk_status, room, dis_yb, dis_yb_str, yb_bzfx, dis_room, dis_yb1, dis_yb_str1, dis_yb2, " +
            "dis_yb_str2, small_dept, xnh_familysysno, xnh_membersysno, xnh_bookno, xnh_cardno, xnh_cmisno, xnh_zyh, xnh_referralno, " +
            "xnh_redeem_type, xnh_hasreferral, zy_flag, xnh_reject_flag, own_flag, own_time, free_flag, operation, cyyy, yb_gs_no, " +
            "treat_type, hic_no, yb_register_date, pathology_diag_comment, file_status) " +
            "VALUES(#{inpatientNo,jdbcType=CHAR},#{admissTimes,jdbcType=SMALLINT},#{name,jdbcType=CHAR},#{outpatientNo,jdbcType=CHAR},#{chargeType,jdbcType=CHAR},#{visitType,jdbcType=CHAR},#{admissDate,jdbcType=TIMESTAMP},#{admissStatus,jdbcType=CHAR}," +
            "#{clinicDiag,jdbcType=CHAR},#{admissDiag,jdbcType=CHAR},#{admissPhysician,jdbcType=CHAR},#{referPhysician,jdbcType=CHAR},#{consultPhysician,jdbcType=CHAR},#{deptDirector,jdbcType=CHAR},#{admissDept,jdbcType=CHAR},#{admissWard,jdbcType=CHAR}," +
            "#{disDate,jdbcType=TIMESTAMP},#{disDiag,jdbcType=CHAR},#{disStatus,jdbcType=CHAR},#{disDept,jdbcType=CHAR},#{disWard,jdbcType=CHAR}," +
            "#{insurCode,jdbcType=CHAR},#{contractCode,jdbcType=CHAR},#{responceType,jdbcType=CHAR},#{timesBilled,jdbcType=INTEGER},#{balance,jdbcType=DECIMAL}," +
            "#{totalCharge,jdbcType=DECIMAL},#{charge1,jdbcType=DECIMAL},#{charge2,jdbcType=DECIMAL},#{charge3,jdbcType=DECIMAL},#{charge4,jdbcType=DECIMAL}," +
            "#{charge5,jdbcType=DECIMAL},#{charge6,jdbcType=DECIMAL},#{charge7,jdbcType=DECIMAL},#{charge8,jdbcType=DECIMAL},#{charge9,jdbcType=DECIMAL}," +
            "#{charge10,jdbcType=DECIMAL},#{charge11,jdbcType=DECIMAL},#{charge12,jdbcType=DECIMAL},#{charge13,jdbcType=DECIMAL},#{charge14,jdbcType=DECIMAL}," +
            "#{charge15,jdbcType=DECIMAL},#{charge16,jdbcType=DECIMAL},#{charge17,jdbcType=DECIMAL},#{charge18,jdbcType=DECIMAL},#{charge19,jdbcType=DECIMAL}," +
            "#{charge20,jdbcType=DECIMAL},#{charge21,jdbcType=DECIMAL},#{charge22,jdbcType=DECIMAL},#{charge23,jdbcType=DECIMAL},#{charge24,jdbcType=DECIMAL}," +
            "#{charge25,jdbcType=DECIMAL},#{charge26,jdbcType=DECIMAL},#{charge27,jdbcType=DECIMAL},#{charge28,jdbcType=DECIMAL},#{charge29,jdbcType=DECIMAL}," +
            "#{charge30,jdbcType=DECIMAL},#{ward,jdbcType=CHAR},#{dept,jdbcType=CHAR},#{bedNo,jdbcType=CHAR},#{sex,jdbcType=CHAR},#{bloodType,jdbcType=CHAR}," +
            "#{diagnosisDate,jdbcType=TIMESTAMP},#{allergyMedicine,jdbcType=CHAR},#{rescueTimes,jdbcType=CHAR},#{successTimes,jdbcType=CHAR},#{followupMark,jdbcType=CHAR}," +
            "#{followupPrior,jdbcType=CHAR},#{autopsyMark,jdbcType=CHAR},#{qualityLevel,jdbcType=CHAR},#{opIdCode,jdbcType=CHAR}," +
            "#{archiveCode,jdbcType=CHAR},#{archiveMark,jdbcType=CHAR},#{teachingMark,jdbcType=CHAR},#{diagnoseConform1,jdbcType=CHAR}," +
            "#{diagnoseConform2,jdbcType=CHAR},#{diagnoseConform3,jdbcType=CHAR},#{diagnoseConform4,jdbcType=CHAR},#{diagnoseConform5,jdbcType=CHAR}," +
            "#{diagnoseConform6,jdbcType=CHAR},#{diagnoseConform7,jdbcType=CHAR},#{diagnoseConform8,jdbcType=CHAR},#{internCode,jdbcType=CHAR},#{diagnoseConform9,jdbcType=CHAR}," +
            "#{diagnoseConform10,jdbcType=CHAR},#{pathologyDiag,jdbcType=CHAR}," +
            "#{disHour,jdbcType=TIMESTAMP},#{admissHour,jdbcType=TIMESTAMP},#{capBloody,jdbcType=INTEGER},#{maxLedger,jdbcType=INTEGER},#{admissDiagStr,jdbcType=CHAR}," +
            "#{patientStatus,jdbcType=CHAR}," +
            "#{moveFlag,jdbcType=CHAR},#{jzjlh,jdbcType=CHAR},#{zkDept,jdbcType=CHAR},#{hbsag,jdbcType=CHAR},#{archiveCode1,jdbcType=CHAR},#{jobDoctor,jdbcType=CHAR}," +
            "#{jobNurse,jdbcType=CHAR},#{ybjy,jdbcType=FLOAT},#{bczf,jdbcType=FLOAT},#{zflj,jdbcType=FLOAT},#{disDiagStr,jdbcType=CHAR},#{ybZyh,jdbcType=VARCHAR}," +
            "#{ybJzlb,jdbcType=CHAR},#{yzIn,jdbcType=TIMESTAMP},#{yzOut,jdbcType=TIMESTAMP},#{ybFlag,jdbcType=CHAR},#{oweFlag,jdbcType=CHAR},#{zkWard,jdbcType=VARCHAR}," +
            "#{zkDate,jdbcType=TIMESTAMP},#{ybJlh,jdbcType=VARCHAR},#{ybType,jdbcType=VARCHAR},#{zySerialNo,jdbcType=VARCHAR},#{mzNo,jdbcType=VARCHAR}," +
            "#{clinicDiagStr,jdbcType=CHAR},#{bedStatus,jdbcType=CHAR},#{inputDate,jdbcType=TIMESTAMP},#{ybJsffbz,jdbcType=VARCHAR},#{ybSylb,jdbcType=VARCHAR},#{ybZszh,jdbcType=VARCHAR}," +
            "#{admissDateYb,jdbcType=TIMESTAMP},#{baReceiveFlag,jdbcType=CHAR},#{blzkStatus,jdbcType=CHAR},#{room,jdbcType=CHAR},#{disYb,jdbcType=VARCHAR},#{disYbStr,jdbcType=VARCHAR}," +
            "#{ybBzfx,jdbcType=VARCHAR},#{disRoom,jdbcType=VARCHAR},#{disYb1,jdbcType=VARCHAR},#{disYbStr1,jdbcType=VARCHAR},#{disYb2,jdbcType=VARCHAR},#{disYbStr2,jdbcType=VARCHAR}," +
            "#{smallDept,jdbcType=CHAR},#{xnhFamilysysno,jdbcType=VARCHAR},#{xnhMembersysno,jdbcType=VARCHAR},#{xnhBookno,jdbcType=VARCHAR},#{xnhCardno,jdbcType=VARCHAR},#{xnhCmisno,jdbcType=VARCHAR}," +
            "#{xnhZyh,jdbcType=VARCHAR},#{xnhReferralno,jdbcType=VARCHAR},#{xnhRedeemType,jdbcType=CHAR},#{xnhHasreferral,jdbcType=CHAR},#{zyFlag,jdbcType=CHAR},#{xnhRejectFlag,jdbcType=CHAR}," +
            " #{ownFlag,jdbcType=CHAR},#{ownTime,jdbcType=TIMESTAMP},#{freeFlag,jdbcType=CHAR},#{operation,jdbcType=CHAR},#{cyyy,jdbcType=CHAR},#{ybGsNo,jdbcType=VARCHAR}," +
            "#{treatType,jdbcType=VARCHAR},#{hicNo,jdbcType=CHAR},#{ybRegisterDate,jdbcType=TIMESTAMP},#{pathologyDiagComment,jdbcType=VARCHAR},#{fileStatus,jdbcType=CHAR}" +
            ") ")
    int insertZyActpatient(ZyActpatient zyActpatient);


    /**
     * 根据住院号查询在院记录数
     *
     * @return
     */
    @Select("select count (*) from zy_actpatient where inpatient_no =#{inpatientNo} ")
    int selectCountByInpatientNo(@Param("inpatientNo") String inpatientNo);

    /**
     * 转移住院/出院病人信息
     * @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 transferZyActpatient(@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 transferZyActpatientBaby(@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 住院次数
     * @param disDate 出院时间
     * @param bedNo 床号
     * @param ward 病区名称
     * @param dept 当前科室 病房
     * @param opId 操作人id
     * @param bedStatus 病床状态
     * @return
     */
    @Update("update ${tableName} SET dis_date =#{disDate} , bed_no =#{bedNo} ,dis_ward =#{ward} , dis_dept =#{dept} , job_nurse =#{opId} , bed_status =#{bedStatus} WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} ")
    int updateZyActpatientForTransfer(@Param("tableName") String tableName, @Param("disDate") Date disDate,@Param("bedNo") String bedNo,@Param("ward") String ward,@Param("dept") String dept,@Param("opId") String opId,@Param("bedStatus") String bedStatus,@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 修改病人的出院时间和操作人  婴儿
     * @param tableName 表名
     * @param inpatientNoBegin 病人住院号
     * @param inpatientNoEnd   病人住院号
     * @param admissTimes 住院次数
     * @param disDate 出院时间
     * @param bedNo 床号
     * @param ward 病区名称
     * @param dept 当前科室 病房
     * @param opId 操作人id
     * @param bedStatus 病床状态
     * @return
     */
    @Update("update ${tableName} SET dis_date =#{disDate} , bed_no =#{bedNo}, dis_ward =#{ward} , dis_dept =#{dept} , job_nurse =#{opId} , bed_status =#{bedStatus} WHERE inpatient_no >= #{inpatientNoBegin} and inpatient_no <= #{inpatientNoEnd} AND admiss_times =#{admissTimes} ")
    int updateZyActpatientForTransferBaby(@Param("tableName") String tableName, @Param("disDate") Date disDate,@Param("bedNo") String bedNo,@Param("ward") String ward,@Param("dept") String dept,@Param("opId") String opId,@Param("bedStatus") String bedStatus,@Param("inpatientNoBegin") String inpatientNoBegin, @Param("inpatientNoEnd") String inpatientNoEnd, @Param("admissTimes") Integer admissTimes);

    /**
     * 根据住院号查询在/出院记录
     *
     * @return
     */
    @Select({"<script>",
            "select * from ${tableName} where inpatient_no =#{inpatientNo}",
            "<when test='admissTimes!=null'>",
            " and admiss_times=#{admissTimes}",
            "</when>",
            "</script>"})
    ZyActpatient selectByInpatientNo(@Param("tableName") String tableName, @Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);


    /**
     * 修改住院或者出院记录的住院号和次数
     *
     * @param zyActpatient
     * @return
     */
    @Update({"<script>",
            "update ${tableName} ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where inpatient_no =#{oldInpatientNo} and admiss_times =#{oldAdmissTimes}' >",
            "<when test='inpatientNo!=null'>",
            "inpatient_no =#{inpatientNo}",
            "</when>",
            "<when test='admissTimes!=null'>",
            ",admiss_times =#{admissTimes}",
            "</when>",
            "<when test='name!=null'>",
            ",name =#{name}",
            "</when>",
            "</trim>"
            , "</script>"})
    int updateZyActpatient(ZyActpatient zyActpatient);

    /**
     * 修改住院记录名称
     *
     * @param name
     * @param mzNo
     * @param tableName
     * @return
     */
    @Update("update ${tableName}  set name =#{name} WHERE mz_no =#{mzNo} ")
    int updateNameByMzNo(@Param("name") String name, @Param("mzNo") String mzNo, @Param("tableName") String tableName);

    /**
     * 修改医技检查表
     *
     * @param inpatientNo
     * @param oldInpatientNo
     * @return
     */
    @Update("update yj_jc_patient set zy_id =#{inpatientNo} , ba_no =#{inpatientNo} WHERE zy_id =#{oldInpatientNo} ")
    int updateYjJcPatient(@Param("inpatientNo") String inpatientNo, @Param("oldInpatientNo") String oldInpatientNo);


    /**
     * 根据病人门诊id或者住院号查询病人信息
     *
     * @param zyActpatient
     * @return
     */
    @Select({"<script>",
            "  select top ${pageSize} inpatient_no," +
                    "         admiss_times," +
                    "         name," +
                    "         bed_no," +
                    "         ward," +
                    "         responce_type," +
                    "         balance," +
                    "         dept," +
                    "         total_charge," +
                    "         charge_type," +
                    "         times_billed," +
                    "         admiss_diag_str," +
                    "         admiss_diag," +
                    "         admiss_date," +
                    "         isnull(zk_dept,small_dept) zk_dept," +
                    "         sex," +
                    "         zy_serial_no," +
                    "         mz_no," +
                    "         code=inpatient_no," +
                    "         intern_code," +
                    "         rtrim(bed_status) bed_status, " +
                    "         dis_date " +
                    " from (SELECT ROW_NUMBER() OVER (ORDER BY " +
                    "<when test='tableName eq \"zy_actpatient\"'>",
                           " admiss_date ",
                     "</when>",
                     "<when test='tableName eq \"zy_inactpatient\"'>",
                           " dis_date ",
                    "</when>",
                    "desc) AS RowNumber,* " +
                    "    FROM ${tableName} " +
                    "   WHERE 1=1 " +
                    "<when test='inpatientNo!=null'>",
            " and inpatient_no like #{inpatientNo}",
            "</when>",
            "<when test='mzNo!=null'>",
            " and mz_no like #{mzNo}",
            "</when>",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='ward!=null'>",
            " and ward = #{ward}",
            "</when>",
            "<when test='zkDept!=null'>",
            " and zk_dept = #{zkDept}",
            "</when>",
            "<when test='beginTime!=null and tableName eq \"zy_actpatient\"'>",
            " and admiss_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and tableName eq \"zy_actpatient\"'>",
            " and admiss_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='beginTime!=null and tableName eq \"zy_inactpatient\"'>",
            " and dis_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and tableName eq \"zy_inactpatient\"'>",
            " and dis_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<ZyActpatient> selectZyActpatientByInpatientNoOrMzNO(ZyActpatient zyActpatient);


    /**
     * 根据病人门诊id或者住院号查询病人列表总数
     *
     * @param zyActpatient
     * @return
     */
    @Select({"<script>",
            "  select count(*) " +
                    "    FROM ${tableName} " +
                    "   WHERE 1=1 " +
                    "<when test='inpatientNo!=null'>",
            " and inpatient_no like #{inpatientNo}",
            "</when>",
            "<when test='mzNo!=null'>",
            " and mz_no like #{mzNo}",
            "</when>",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='ward!=null'>",
            " and ward = #{ward}",
            "</when>",
            "<when test='zkDept!=null'>",
            " and zk_dept = #{zkDept}",
            "</when>",
            "<when test='beginTime!=null and tableName eq \"zy_actpatient\"'>",
            " and admiss_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and tableName eq \"zy_actpatient\"'>",
            " and admiss_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='beginTime!=null and tableName eq \"zy_inactpatient\"'>",
            " and dis_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and tableName eq \"zy_inactpatient\"'>",
            " and dis_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "</script>"})
    Integer selectCountByInpatientNoOrMzNO(ZyActpatient zyActpatient);

    /**
     * 更新在/出院病人余额信息
     *
     * @param zyActpatient
     * @return
     */
    @Update("update ${tableName} SET balance =#{balance} WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} ")
    int updateBalance(ZyActpatient zyActpatient);

    /**
     * 删除在院/出院病人信息
     * @param tableName 表名
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @return
     */
    @Delete("delete from ${tableName} WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} ")
    int deleteZyActpatient(@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 deleteZyActpatientBaby(@Param("tableName") String tableName,@Param("inpatientNoBegin") String inpatientNoBegin, @Param("inpatientNoEnd") String inpatientNoEnd, @Param("admissTimes") Integer admissTimes);


}
