<?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.ruoyi.oa.mapper.CmcDeviceHandoverMapper">
    
    <resultMap type="CmcDeviceHandover" id="CmcDeviceHandoverResult">
        <result property="deviceHandoverId"    column="device_handover_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="handoverDeptId"    column="handover_dept_id"    />
        <result property="handoverUserId"    column="handover_user_id"    />
        <result property="handoverDate"    column="handover_date"    />
        <result property="receiveDeptId"    column="receive_dept_id"    />
        <result property="receiveUserId"    column="receive_user_id"    />
        <result property="description"    column="description"    />
        <result property="reason"    column="reason"    />
        <result property="remark"    column="remark"    />
        <association property="handoverUser"    javaType="SysUser"         resultMap="HandoverUserResult" />
        <association property="receiveUser"    javaType="SysDept"         resultMap="ReceiveUserResult" />
        <association property="handoverDept"    javaType="SysDept"         resultMap="HandoverDeptResult" />
        <association property="receiveDept"    javaType="SysDept"         resultMap="ReceiveDeptResult" />
        <association property="device"    javaType="CmcDevice"         resultMap="CmcDeviceResult" />
    </resultMap>

    <resultMap type="SysUser" id="HandoverUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="handover_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="ReceiveUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="receive_nick_name"    />
    </resultMap>

    <resultMap type="SysDept" id="HandoverDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="handover_dept_name"    />
    </resultMap>

    <resultMap type="SysDept" id="ReceiveDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="receive_dept_name"    />
    </resultMap>

    <resultMap type="CmcDevice" id="CmcDeviceResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceNumber"    column="device_number"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="series"    column="series"    />
        <result property="brand"    column="brand"    />
    </resultMap>

    <sql id="selectCmcDeviceHandoverVo">
        select dh.device_handover_id, dh.device_id, dv.device_number, dv.code, dv.name, dv.brand, dv.series, dh.handover_dept_id, d.dept_name as handover_dept_name, u.nick_name as handover_nick_name, dh.handover_user_id, dh.handover_date,
               dh.receive_dept_id, d1.dept_name as receive_dept_name, dh.receive_user_id, u1.nick_name as receive_nick_name, dh.description, dh.reason, dh.remark from cmc_device_handover as dh
        left join sys_user as u on u.user_id = dh.handover_user_id
        left join sys_user as u1 on u1.user_id = dh.receive_user_id
        left join sys_dept as d on d.dept_id = dh.handover_dept_id
        left join sys_dept as d1 on d1.dept_id = dh.receive_dept_id
        left join cmc_device as dv on dv.device_id = dh.device_id
    </sql>

    <select id="selectCmcDeviceHandoverList" parameterType="CmcDeviceHandover" resultMap="CmcDeviceHandoverResult">
        <include refid="selectCmcDeviceHandoverVo"/>
        <where>  
            <if test="deviceId != null "> and dh.device_id = #{deviceId}</if>
            <if test="handoverDeptId != null "> and dh.handover_dept_id = #{handoverDeptId}</if>
            <if test="handoverUserId != null "> and dh.handover_user_id = #{handoverUserId}</if>
            <if test="handoverDate != null "> and dh.handover_date = #{handoverDate}</if>
            <if test="receiveDeptId != null "> and dh.receive_dept_id = #{receiveDeptId}</if>
            <if test="receiveUserId != null "> and dh.receive_user_id = #{receiveUserId}</if>
            <if test="description != null  and description != ''"> and dh.description = #{description}</if>
            <if test="reason != null  and reason != ''"> and dh.reason = #{reason}</if>
        </where>
        order by dh.handover_date desc
    </select>
    
    <select id="selectCmcDeviceHandoverByDeviceHandoverId" parameterType="Integer" resultMap="CmcDeviceHandoverResult">
        <include refid="selectCmcDeviceHandoverVo"/>
        where dh.device_handover_id = #{deviceHandoverId}
    </select>
        
    <insert id="insertCmcDeviceHandover" parameterType="CmcDeviceHandover">
        insert into cmc_device_handover
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="handoverDeptId != null">handover_dept_id,</if>
            <if test="handoverUserId != null">handover_user_id,</if>
            <if test="handoverDate != null">handover_date,</if>
            <if test="receiveDeptId != null">receive_dept_id,</if>
            <if test="receiveUserId != null">receive_user_id,</if>
            <if test="description != null">description,</if>
            <if test="reason != null">reason,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceHandoverId != null">#{deviceHandoverId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="handoverDeptId != null">#{handoverDeptId},</if>
            <if test="handoverUserId != null">#{handoverUserId},</if>
            <if test="handoverDate != null">#{handoverDate},</if>
            <if test="receiveDeptId != null">#{receiveDeptId},</if>
            <if test="receiveUserId != null">#{receiveUserId},</if>
            <if test="description != null">#{description},</if>
            <if test="reason != null">#{reason},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCmcDeviceHandover" parameterType="CmcDeviceHandover">
        update cmc_device_handover
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="handoverDeptId != null">handover_dept_id = #{handoverDeptId},</if>
            <if test="handoverUserId != null">handover_user_id = #{handoverUserId},</if>
            <if test="handoverDate != null">handover_date = #{handoverDate},</if>
            <if test="receiveDeptId != null">receive_dept_id = #{receiveDeptId},</if>
            <if test="receiveUserId != null">receive_user_id = #{receiveUserId},</if>
            <if test="description != null">description = #{description},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where device_handover_id = #{deviceHandoverId}
    </update>

    <delete id="deleteCmcDeviceHandoverByDeviceHandoverId" parameterType="Integer">
        delete from cmc_device_handover where device_handover_id = #{deviceHandoverId}
    </delete>

    <delete id="deleteCmcDeviceHandoverByDeviceHandoverIds" parameterType="Integer">
        delete from cmc_device_handover where device_handover_id in 
        <foreach item="deviceHandoverId" collection="array" open="(" separator="," close=")">
            #{deviceHandoverId}
        </foreach>
    </delete>
</mapper>