<?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.CmcDeviceApprovalMapper">
    
    <resultMap type="CmcDeviceApproval" id="CmcDeviceApprovalResult">
        <result property="deviceApplyId"    column="device_apply_id"    />
        <result property="applier"    column="applier"    />
        <result property="useDept"    column="use_dept"    />
        <result property="devices"    column="devices"    />
        <result property="modifyDevices"    column="modify_devices"    />
        <result property="repairDevices"    column="repair_devices"    />
        <result property="returnDevices"    column="return_devices"    />
        <result property="projectId"    column="project_id"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="returnDate"    column="return_date"    />
        <result property="beginDate"    column="begin_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="days"    column="days"    />
        <result property="managerUserId"    column="manager_user_id"    />
        <result property="managerComment"    column="manager_comment"    />
        <result property="dispatcher"    column="dispatcher"    />
        <result property="dispatchComment"    column="dispatch_comment"    />
        <result property="estimateCost"    column="estimate_cost"    />
        <result property="remark"    column="remark"    />
        <result property="managerTime"    column="manager_time"    />
        <result property="dispatchTime"    column="dispatch_time"    />
        <association property="applierUser"    javaType="SysUser"         resultMap="SysUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
        <association property="managerUser"    javaType="SysUser"         resultMap="ManagerUserResult" />
        <association property="dispatchUser"    javaType="SysUser"         resultMap="DispatchUserResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="applier_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="ManagerUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="manager_nick_name"    />
    </resultMap>

    <resultMap type="SysUser" id="DispatchUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="dispatch_nick_name"    />
    </resultMap>

    <resultMap type="SysDept" id="SysDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <resultMap type="CmcProject" id="CmcProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectNumber"    column="project_number"    />
        <result property="projectName"    column="project_name"    />
    </resultMap>

    <sql id="selectCmcDeviceApprovalVo">
        select da.device_apply_id, da.applier, u.nick_name as applier_nick_name, da.use_dept, d.dept_name, da.devices, da.modify_devices, da.return_devices, da.project_id, p.project_number, p.project_name,
               da.apply_reason, da.apply_date, da.return_date, da.begin_date, da.end_date, da.days, da.repair_devices, da.manager_user_id, u1.nick_name as manager_nick_name, da.manager_comment,
               da.dispatcher, u2.nick_name as dispatch_nick_name, da.dispatch_comment, da.estimate_cost, da.remark, da.manager_time, da.dispatch_time from cmc_device_approval as da
            left join sys_user as u on u.user_id = da.applier
            left join sys_user as u1 on u1.user_id = da.manager_user_id
            left join sys_user as u2 on u2.user_id = da.dispatcher
            left join sys_dept as d on d.dept_id = da.use_dept
            left join cmc_project as p on da.project_id = p.project_id
    </sql>

    <select id="selectCmcDeviceApprovalList" parameterType="CmcDeviceApproval" resultMap="CmcDeviceApprovalResult">
        <include refid="selectCmcDeviceApprovalVo"/>
        <where>  
            <if test="applier != null "> and da.applier = #{applier}</if>
            <if test="useDept != null "> and da.use_dept = #{useDept}</if>
            <if test="devices != null  and devices != ''"> and find_in_set(#{devices}, da.devices)</if>
            <if test="modifyDevices != null  and modifyDevices != ''"> and find_in_set(#{modifyDevices}, da.modify_devices)</if>
            <if test="repairDevices != null  and repairDevices != ''"> and find_in_set(#{repairDevices}, da.repair_devices)</if>
            <if test="projectId != null  and projectId != ''"> and da.project_id = #{projectId}</if>
            <if test="applyReason != null  and applyReason != ''"> and da.apply_reason = #{applyReason}</if>
            <if test="applyDate != null "> and YEAR(da.apply_date) = YEAR(#{applyDate})</if>
            <if test="returnDate != null "> and da.return_date = #{returnDate}</if>
            <if test="beginDate != null "> and da.begin_date = #{beginDate}</if>
            <if test="endDate != null "> and da.end_date = #{endDate}</if>
            <if test="days != null "> and da.days = #{days}</if>
            <if test="managerUserId != null "> and da.manager_user_id = #{managerUserId}</if>
            <if test="managerComment != null  and managerComment != ''"> and da.manager_comment = #{managerComment}</if>
            <if test="dispatcher != null "> and da.dispatcher = #{dispatcher}</if>
            <if test="dispatchComment != null  and dispatchComment != ''"> and da.dispatch_comment = #{dispatchComment}</if>
            <if test="estimateCost != null "> and da.estimate_cost = #{estimateCost}</if>
            <if test="managerTime != null "> and da.manager_time = #{managerTime}</if>
            <if test="dispatchTime != null "> and da.dispatch_time = #{dispatchTime}</if>
        </where>
        order by da.apply_date desc
    </select>
    
    <select id="selectCmcDeviceApprovalByDeviceApplyId" parameterType="String" resultMap="CmcDeviceApprovalResult">
        <include refid="selectCmcDeviceApprovalVo"/>
        where da.device_apply_id = #{deviceApplyId}
    </select>
        
    <insert id="insertCmcDeviceApproval" parameterType="CmcDeviceApproval">
        insert into cmc_device_approval
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceApplyId != null">device_apply_id,</if>
            <if test="applier != null">applier,</if>
            <if test="useDept != null">use_dept,</if>
            <if test="devices != null">devices,</if>
            <if test="modifyDevices != null">modify_devices,</if>
            <if test="repairDevices != null">repair_devices,</if>
            <if test="projectId != null">project_id,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="applyDate != null">apply_date,</if>
            <if test="returnDate != null">return_date,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="days != null">days,</if>
            <if test="managerUserId != null">manager_user_id,</if>
            <if test="managerComment != null">manager_comment,</if>
            <if test="dispatcher != null">dispatcher,</if>
            <if test="dispatchComment != null">dispatch_comment,</if>
            <if test="estimateCost != null">estimate_cost,</if>
            <if test="remark != null">remark,</if>
            <if test="managerTime != null">manager_time,</if>
            <if test="dispatchTime != null">dispatch_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceApplyId != null">#{deviceApplyId},</if>
            <if test="applier != null">#{applier},</if>
            <if test="useDept != null">#{useDept},</if>
            <if test="devices != null">#{devices},</if>
            <if test="modifyDevices != null">#{modifyDevices},</if>
            <if test="repairDevices != null">#{repairDevices},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="applyDate != null">#{applyDate},</if>
            <if test="returnDate != null">#{returnDate},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="days != null">#{days},</if>
            <if test="managerUserId != null">#{managerUserId},</if>
            <if test="managerComment != null">#{managerComment},</if>
            <if test="dispatcher != null">#{dispatcher},</if>
            <if test="dispatchComment != null">#{dispatchComment},</if>
            <if test="estimateCost != null">#{estimateCost},</if>
            <if test="remark != null">#{remark},</if>
            <if test="managerTime != null">#{managerTime},</if>
            <if test="dispatchTime != null">#{dispatchTime},</if>
         </trim>
    </insert>

    <update id="updateCmcDeviceApproval" parameterType="CmcDeviceApproval">
        update cmc_device_approval
        <trim prefix="SET" suffixOverrides=",">
            <if test="applier != null">applier = #{applier},</if>
            <if test="useDept != null">use_dept = #{useDept},</if>
            <if test="devices != null">devices = #{devices},</if>
            <if test="modifyDevices != null">modify_devices = #{modifyDevices},</if>
            <if test="repairDevices != null">repair_devices = #{repairDevices},</if>
            <if test="returnDevices != null">return_devices = #{returnDevices},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="applyDate != null">apply_date = #{applyDate},</if>
            <if test="returnDate != null">return_date = #{returnDate},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="days != null">days = #{days},</if>
            <if test="managerUserId != null">manager_user_id = #{managerUserId},</if>
            <if test="managerComment != null">manager_comment = #{managerComment},</if>
            <if test="dispatcher != null">dispatcher = #{dispatcher},</if>
            <if test="dispatchComment != null">dispatch_comment = #{dispatchComment},</if>
            <if test="estimateCost != null">estimate_cost = #{estimateCost},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="managerTime != null">manager_time = #{managerTime},</if>
            <if test="dispatchTime != null">dispatch_time = #{dispatchTime},</if>
        </trim>
        where device_apply_id = #{deviceApplyId}
    </update>

    <delete id="deleteCmcDeviceApprovalByDeviceApplyId" parameterType="String">
        delete from cmc_device_approval where device_apply_id = #{deviceApplyId}
    </delete>

    <delete id="deleteCmcDeviceApprovalByDeviceApplyIds" parameterType="String">
        delete from cmc_device_approval where device_apply_id in 
        <foreach item="deviceApplyId" collection="array" open="(" separator="," close=")">
            #{deviceApplyId}
        </foreach>
    </delete>
</mapper>