<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.emr.dao.recordLock.Emr_LockMapper" >
    <resultMap id="BaseResultMap" type="com.emr.vo.FontVo.LockVo" >
        <result column="id" property="id" jdbcType="INTEGER" />
        <result column="user_id" property="userId" jdbcType="INTEGER" />
        <result column="patient_id" property="patientId" jdbcType="NVARCHAR" />
        <result column="admiss_times" property="admissTimes" jdbcType="NCHAR" />
        <result column="inpatient_no" property="inpatientNo" jdbcType="NCHAR" />
        <result column="lock_state" property="lockState" jdbcType="INTEGER" />
        <result column="locker" property="locker" jdbcType="NVARCHAR" />
        <result column="locke_time" property="lockeTime" jdbcType="NCHAR" />
        <result column="unlocker" property="unlocker" jdbcType="NVARCHAR" />
        <result column="unlocke_time" property="unlockeTime" jdbcType="NCHAR" />
        <result column="remarke" property="remarke" jdbcType="NVARCHAR" />
    </resultMap>
    <resultMap id="BaseResultMap1" type="com.emr.vo.EmrLockVo" >
        <result column="id" property="id" jdbcType="INTEGER" />
        <result column="user_id" property="userId" jdbcType="INTEGER" />
        <result column="patient_id" property="patientId" jdbcType="NVARCHAR" />
        <result column="admiss_times" property="admissTimes" jdbcType="NCHAR" />
        <result column="inpatient_no" property="inpatientNo" jdbcType="NCHAR" />
        <result column="lock_state" property="lockState" jdbcType="INTEGER" />
        <result column="locker" property="locker" jdbcType="NVARCHAR" />
        <result column="locke_time" property="lockeTime" jdbcType="NCHAR" />
        <result column="unlocker" property="unlocker" jdbcType="NVARCHAR" />
        <result column="unlocke_time" property="unlockeTime" jdbcType="NCHAR" />
        <result column="remarke" property="remarke" jdbcType="NVARCHAR" />
        <result column="name" property="name" jdbcType="NVARCHAR" />
    </resultMap>
    <insert id="insert" parameterType="com.emr.entity.recordLock.Emr_Lock" >
        insert into emr_lock (user_id, patient_id,
                              admiss_times, inpatient_no, lock_state,
                              locker, locke_time, unlocker,
                              unlocke_time, remarke)
        values (#{userId,jdbcType=INTEGER}, #{patientId,jdbcType=NVARCHAR},
                #{admissTimes,jdbcType=NCHAR}, #{inpatientNo,jdbcType=NCHAR}, #{lockState,jdbcType=INTEGER},
                #{locker,jdbcType=NVARCHAR}, #{lockeTime,jdbcType=NCHAR}, #{unlocker,jdbcType=NVARCHAR},
                #{unlockeTime,jdbcType=NCHAR}, #{remarke,jdbcType=NVARCHAR})
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.recordLock.Emr_Lock" >
        update emr_lock
        <set >
            <if test="userId != null" >
                user_id = #{userId,jdbcType=INTEGER},
            </if>
            <if test="patientId != null" >
                patient_id = #{patientId,jdbcType=NVARCHAR},
            </if>
            <if test="admissTimes != null" >
                admiss_times = #{admissTimes,jdbcType=NCHAR},
            </if>
            <if test="inpatientNo != null" >
                inpatient_no = #{inpatientNo,jdbcType=NCHAR},
            </if>
            <if test="lockState != null" >
                lock_state = #{lockState,jdbcType=INTEGER},
            </if>
            <if test="locker != null" >
                locker = #{locker,jdbcType=NVARCHAR},
            </if>
            <if test="lockeTime != null" >
                locke_time = #{lockeTime,jdbcType=NCHAR},
            </if>
            <if test="unlocker != null" >
                unlocker = #{unlocker,jdbcType=NVARCHAR},
            </if>
            <if test="unlockeTime != null" >
                unlocke_time = #{unlockeTime,jdbcType=NCHAR},
            </if>
            <if test="remarke != null" >
                remarke = #{remarke,jdbcType=NVARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <!--通过锁定状态查询patientId集合-->
    <select id="selectLockListByStatus" resultMap="BaseResultMap" parameterType="java.lang.Integer">
    SELECT
        patient_id
    FROM
        emr_lock
    WHERE
        lock_state = ${lockStatus}
  </select>
    <!--通过patientId查询未锁定对象-->
    <select id="selectLockByPatientId" resultType="com.emr.entity.recordLock.Emr_Lock" parameterType="java.lang.String">
        SELECT
        *
        FROM
        emr_lock
        <where>
            patient_id = #{patientId}
            <if test="lockStatus != null">
                AND lock_state = #{lockStatus}
            </if>
        </where>
    </select>

    <!--通过patientId查询未锁定对象-->
    <update id="udpateUnlockByPatients">
    UPDATE
        emr_lock
    SET
        lock_state = 0,
        unlocker=#{unlocker},
        unlocke_time=#{unlockeTime}
    WHERE
        patient_id IN (${patientIds})
  </update>
    <!--批量修改成上锁-->
    <update id="udpateLockByPatientIds">
        UPDATE
        emr_lock
        SET
        lock_state = 1,
        user_id = #{record.userId},
        locker=#{record.locker},
        locke_time=#{record.lockeTime}
        WHERE
        patient_id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.patientId}
        </foreach>
    </update>
    <!--通过patientId查询锁定对象-->
    <select id="seleceAll" resultMap="BaseResultMap">
        SELECT
        emr_lock.id,
        emr_lock.user_id,
        emr_lock.patient_id,
        emr_lock.admiss_times,
        commomtable.admiss_id,
        commomtable.inpatient_no,
        emr_lock.lock_state,
        emr_lock.locker,
        emr_lock.locke_time,
        emr_lock.unlocker,
        emr_lock.unlocke_time,
        emr_lock.remarke
        FROM
        emr_lock
        LEFT JOIN
        commomtable
        ON
        emr_lock.patient_id = commomtable.patient_id
        AND
        emr_lock.admiss_times = commomtable.admiss_times
        <where>
            emr_lock.lock_state = 1
            <if test="patientId != null and patientId != ''">
                AND emr_lock.patient_id = #{patientId}
            </if>
            <if test="admissId != null and admissId != ''">
                AND commomtable.admiss_id = #{admissId}
            </if>
            <if test="admissTimes != null and admissTimes != ''">
                AND emr_lock.admiss_times = #{admissTimes}
            </if>
        </where>
    </select>
    <select id="selectLockByPatientIds" resultMap="BaseResultMap">
        SELECT *
        FROM emr_lock
        <where>
            patient_id IN
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item.patientId}
            </foreach>
            <if test="lockStatus != null">
                AND lock_state = #{lockStatus}
            </if>
        </where>
    </select>
    <insert id="SimpleInsert" parameterType="java.util.List">
        INSERT INTO
        emr_lock(
        user_id, patient_id,admiss_times, inpatient_no, lock_state,locker, locke_time, unlocker,
        unlocke_time, remarke
        )
        <foreach collection="list" item="item" separator="union">
            select #{item.userId,jdbcType=INTEGER}, #{item.patientId,jdbcType=NVARCHAR},
            #{item.admissTimes,jdbcType=NCHAR}, #{item.inpatientNo,jdbcType=NCHAR}, #{item.lockState,jdbcType=INTEGER},
            #{item.locker,jdbcType=NVARCHAR}, #{item.lockeTime,jdbcType=NCHAR}, #{item.unlocker,jdbcType=NVARCHAR},
            #{item.unlockeTime,jdbcType=NCHAR}, #{item.remarke,jdbcType=NVARCHAR}
        </foreach>
    </insert>
    <!--锁定列表-->
    <select id="getLockList" resultMap="BaseResultMap1" parameterType="com.emr.vo.EmrLockVo">
        SELECT
        emr_lock.patient_id,
        emr_lock.lock_state,
        emr_lock.locker,
        emr_lock.locke_time,
        emr_lock.unlocker,
        emr_lock.unlocke_time,
        commomtable.name,
        commomtable.admiss_times,
        commomtable.inpatient_no
        FROM
        emr_lock
        LEFT JOIN commomtable ON emr_lock.patient_id = commomtable.patient_id
        <where>
            <if test="name != null and name != ''">
                AND commomtable.name LIKE '%${name}%'
            </if>
            <if test="inpatientNo != null and inpatientNo != ''">
                AND commomtable.inpatient_no LIKE '%${inpatientNo}%'
            </if>
            <if test="lockState != null">
                AND emr_lock.lock_state = #{lockState}
            </if>
            <if test="locker != null and locker != ''">
                AND emr_lock.locker LIKE '%${locker}%'
            </if>
            <if test="unlocker != null and unlocker != ''">
                AND emr_lock.unlocker LIKE '%${unlocker}%'
            </if>
            <if test="startTime1 != null and startTime1 != '' and endTime1 != null and endTime1 != ''">
                <if test="startTime1 != null and startTime1 != ''">
                    AND emr_lock.locke_time  between  '${startTime1} 00:00:00' and  '${endTime1} 23:59:59'
                </if>
            </if>
            <if test="startTime2 != null and startTime2 != '' and endTime2 != null and endTime2 != ''">
                <if test="startTime2 != null and startTime2 != ''">
                    AND emr_lock.unlocke_time  between  '${startTime2} 00:00:00' and  '${endTime2} 23:59:59'
                </if>
            </if>
        </where>
        ORDER BY emr_lock.locke_time desc
    </select>
</mapper>