<?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.zehong.system.mapper.TWorkPermitMapper">
    
    <resultMap type="TWorkPermit" id="TWorkPermitResult">
        <result property="workPermitId"    column="work_permit_id"    />
        <result property="applyDept"    column="apply_dept"    />
        <result property="linkMan"    column="link_man"    />
        <result property="linkPhone"    column="link_phone"    />
        <result property="workBound"    column="work_bound"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceNum"    column="device_num"    />
        <result property="applyWorkStartTime"    column="apply_work_start_time"    />
        <result property="applyWorkEndTime"    column="apply_work_end_time"    />
        <result property="workContent"    column="work_content"    />
        <result property="contractor"    column="contractor"    />
        <result property="ratifier"    column="ratifier"    />
        <result property="mainDangerousMark"    column="main_dangerous_mark"    />
        <result property="dangerousMarkDetial"    column="dangerous_mark_detial"    />
        <result property="licenceInfo"    column="licence_info"    />
        <result property="relationSign"    column="relation_sign"    />
        <result property="produceComfirm"    column="produce_comfirm"    />
        <result property="workDeal"    column="work_deal"    />
        <result property="applyStatus"    column="apply_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTWorkPermitVo">
        select work_permit_id, apply_dept, link_man, link_phone, work_bound, device_name, device_num, apply_work_start_time, apply_work_end_time, work_content, contractor, ratifier, main_dangerous_mark, dangerous_mark_detial, licence_info, relation_sign, produce_comfirm, work_deal, apply_status, create_time, update_time, remarks from t_work_permit
    </sql>

    <select id="selectTWorkPermitList" parameterType="TWorkPermit" resultMap="TWorkPermitResult">
        <include refid="selectTWorkPermitVo"/>
        <where>  
            <if test="applyDept != null  and applyDept != ''"> and apply_dept = #{applyDept}</if>
            <if test="linkMan != null  and linkMan != ''"> and link_man = #{linkMan}</if>
            <if test="linkPhone != null  and linkPhone != ''"> and link_phone = #{linkPhone}</if>
            <if test="workBound != null  and workBound != ''"> and work_bound = #{workBound}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceNum != null  and deviceNum != ''"> and device_num like concat('%', #{deviceNum},'%')</if>
            <if test="applyWorkStartTime != null "> and apply_work_start_time = #{applyWorkStartTime}</if>
            <if test="applyWorkEndTime != null "> and apply_work_end_time = #{applyWorkEndTime}</if>
            <if test="workContent != null  and workContent != ''"> and work_content = #{workContent}</if>
            <if test="contractor != null "> and contractor = #{contractor}</if>
            <if test="ratifier != null "> and ratifier = #{ratifier}</if>
            <if test="mainDangerousMark != null  and mainDangerousMark != ''"> and main_dangerous_mark = #{mainDangerousMark}</if>
            <if test="dangerousMarkDetial != null  and dangerousMarkDetial != ''"> and dangerous_mark_detial = #{dangerousMarkDetial}</if>
            <if test="licenceInfo != null  and licenceInfo != ''"> and licence_info = #{licenceInfo}</if>
            <if test="relationSign != null  and relationSign != ''"> and relation_sign = #{relationSign}</if>
            <if test="produceComfirm != null  and produceComfirm != ''"> and produce_comfirm = #{produceComfirm}</if>
            <if test="workDeal != null  and workDeal != ''"> and work_deal = #{workDeal}</if>
            <if test="applyStatus != null  and applyStatus != ''"> and apply_status = #{applyStatus}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
        </where>
        ORDER BY create_time DESC,apply_status
    </select>
    
    <select id="selectTWorkPermitById" parameterType="Long" resultMap="TWorkPermitResult">
        <include refid="selectTWorkPermitVo"/>
        where work_permit_id = #{workPermitId}
    </select>
        
    <insert id="insertTWorkPermit" parameterType="TWorkPermit" useGeneratedKeys="true" keyProperty="workPermitId">
        insert into t_work_permit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyDept != null">apply_dept,</if>
            <if test="linkMan != null">link_man,</if>
            <if test="linkPhone != null">link_phone,</if>
            <if test="workBound != null">work_bound,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceNum != null">device_num,</if>
            <if test="applyWorkStartTime != null">apply_work_start_time,</if>
            <if test="applyWorkEndTime != null">apply_work_end_time,</if>
            <if test="workContent != null">work_content,</if>
            <if test="contractor != null">contractor,</if>
            <if test="ratifier != null">ratifier,</if>
            <if test="mainDangerousMark != null">main_dangerous_mark,</if>
            <if test="dangerousMarkDetial != null">dangerous_mark_detial,</if>
            <if test="licenceInfo != null">licence_info,</if>
            <if test="relationSign != null">relation_sign,</if>
            <if test="produceComfirm != null">produce_comfirm,</if>
            <if test="workDeal != null">work_deal,</if>
            <if test="applyStatus != null">apply_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyDept != null">#{applyDept},</if>
            <if test="linkMan != null">#{linkMan},</if>
            <if test="linkPhone != null">#{linkPhone},</if>
            <if test="workBound != null">#{workBound},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceNum != null">#{deviceNum},</if>
            <if test="applyWorkStartTime != null">#{applyWorkStartTime},</if>
            <if test="applyWorkEndTime != null">#{applyWorkEndTime},</if>
            <if test="workContent != null">#{workContent},</if>
            <if test="contractor != null">#{contractor},</if>
            <if test="ratifier != null">#{ratifier},</if>
            <if test="mainDangerousMark != null">#{mainDangerousMark},</if>
            <if test="dangerousMarkDetial != null">#{dangerousMarkDetial},</if>
            <if test="licenceInfo != null">#{licenceInfo},</if>
            <if test="relationSign != null">#{relationSign},</if>
            <if test="produceComfirm != null">#{produceComfirm},</if>
            <if test="workDeal != null">#{workDeal},</if>
            <if test="applyStatus != null">#{applyStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTWorkPermit" parameterType="TWorkPermit">
        update t_work_permit
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyDept != null">apply_dept = #{applyDept},</if>
            <if test="linkMan != null">link_man = #{linkMan},</if>
            <if test="linkPhone != null">link_phone = #{linkPhone},</if>
            <if test="workBound != null">work_bound = #{workBound},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceNum != null">device_num = #{deviceNum},</if>
            <if test="applyWorkStartTime != null">apply_work_start_time = #{applyWorkStartTime},</if>
            <if test="applyWorkEndTime != null">apply_work_end_time = #{applyWorkEndTime},</if>
            <if test="workContent != null">work_content = #{workContent},</if>
            <if test="contractor != null">contractor = #{contractor},</if>
            <if test="ratifier != null">ratifier = #{ratifier},</if>
            <if test="mainDangerousMark != null">main_dangerous_mark = #{mainDangerousMark},</if>
            <if test="dangerousMarkDetial != null">dangerous_mark_detial = #{dangerousMarkDetial},</if>
            <if test="licenceInfo != null">licence_info = #{licenceInfo},</if>
            <if test="relationSign != null">relation_sign = #{relationSign},</if>
            <if test="produceComfirm != null">produce_comfirm = #{produceComfirm},</if>
            <if test="workDeal != null">work_deal = #{workDeal},</if>
            <if test="applyStatus != null">apply_status = #{applyStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where work_permit_id = #{workPermitId}
    </update>

    <delete id="deleteTWorkPermitById" parameterType="Long">
        delete from t_work_permit where work_permit_id = #{workPermitId}
    </delete>

    <delete id="deleteTWorkPermitByIds" parameterType="String">
        delete from t_work_permit where work_permit_id in 
        <foreach item="workPermitId" collection="array" open="(" separator="," close=")">
            #{workPermitId}
        </foreach>
    </delete>
</mapper>