<?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.THiddenTroubleWorkMapper">
    
    <resultMap type="THiddenTroubleWork" id="THiddenTroubleWorkResult">
        <result property="workId"    column="work_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="bookId"    column="book_id"    />
        <result property="troubleName" column="trouble_name"/>
        <result property="workCycle"    column="work_cycle"    />
        <result property="workName"    column="work_name"    />
        <result property="workType"    column="work_type"    />
        <result property="workForm"    column="work_form"    />
        <result property="workBeginTime"    column="work_begin_time"    />
        <result property="workEndTime"    column="work_end_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="timeTerm"    column="time_term"    />
        <result property="workRange"    column="work_range"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="workStep"    column="work_step"    />
        <result property="content"    column="content"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectTHiddenTroubleWorkVo">
        select work_id, parent_id, book_id, work_cycle, work_name, work_type, work_form, finish_time,work_begin_time, work_end_time, time_term, work_range, dept_id, work_step, content, create_time, create_by, is_del from t_hidden_trouble_work
    </sql>

    <select id="selectTHiddenTroubleWorkList" parameterType="THiddenTroubleWork" resultMap="THiddenTroubleWorkResult">
        SELECT tw.work_id, tw.parent_id, tw.book_id, tw.work_cycle, tw.work_name, tw.work_type, tw.work_form, tw.work_begin_time, tw.work_end_time,tw.finish_time,
        tw.time_term, tw.work_range, tw.dept_id, tw.work_step, tw.content, tw.create_time, tw.create_by, tw.is_del,
        d.dept_name,b.trouble_name
        FROM t_hidden_trouble_work tw
        LEFT JOIN sys_dept d ON d.dept_id = tw.dept_id
        LEFT JOIN t_staning_book b ON b.book_id = tw.book_id
        <where>  
            <if test="parentId != null "> and tw.parent_id = #{parentId}</if>
            <if test="bookId != null "> and tw.book_id = #{bookId}</if>
            <if test="workCycle != null "> and tw.work_cycle = #{workCycle}</if>
            <if test="workName != null  and workName != ''"> and tw.work_name like concat('%', #{workName}, '%')</if>
            <if test="workType != null "> and tw.work_type = #{workType}</if>
        </where>
        order by tw.create_time desc
        <if test="limits==1">
            limit 5
        </if>
    </select>
    
    <select id="selectTHiddenTroubleWorkById" parameterType="Long" resultMap="THiddenTroubleWorkResult">
        SELECT tw.work_id, tw.parent_id, tw.book_id, tw.work_cycle, tw.work_name, tw.work_type, tw.work_form, tw.work_begin_time, tw.work_end_time,tw.finish_time,
        tw.time_term, tw.work_range, tw.dept_id, tw.work_step, tw.content, tw.create_time, tw.create_by, tw.is_del,
        d.dept_name,b.trouble_name
        FROM t_hidden_trouble_work tw
        LEFT JOIN sys_dept d ON d.dept_id = tw.dept_id
        LEFT JOIN t_staning_book b ON b.book_id = tw.book_id
        where tw.work_id = #{workId}
    </select>
        
    <insert id="insertTHiddenTroubleWork" parameterType="THiddenTroubleWork" useGeneratedKeys="true" keyProperty="workId">
        insert into t_hidden_trouble_work
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="bookId != null">book_id,</if>
            <if test="workCycle != null">work_cycle,</if>
            <if test="workName != null and workName != ''">work_name,</if>
            <if test="workType != null">work_type,</if>
            <if test="workForm != null">work_form,</if>
            <if test="workBeginTime != null">work_begin_time,</if>
            <if test="workEndTime != null">work_end_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="timeTerm != null">time_term,</if>
            <if test="workRange != null">work_range,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="workStep != null">work_step,</if>
            <if test="content != null">content,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="bookId != null">#{bookId},</if>
            <if test="workCycle != null">#{workCycle},</if>
            <if test="workName != null and workName != ''">#{workName},</if>
            <if test="workType != null">#{workType},</if>
            <if test="workForm != null">#{workForm},</if>
            <if test="workBeginTime != null">#{workBeginTime},</if>
            <if test="workEndTime != null">#{workEndTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="timeTerm != null">#{timeTerm},</if>
            <if test="workRange != null">#{workRange},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="workStep != null">#{workStep},</if>
            <if test="content != null">#{content},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateTHiddenTroubleWork" parameterType="THiddenTroubleWork">
        update t_hidden_trouble_work
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="bookId != null">book_id = #{bookId},</if>
            <if test="workCycle != null">work_cycle = #{workCycle},</if>
            <if test="workName != null and workName != ''">work_name = #{workName},</if>
            <if test="workType != null">work_type = #{workType},</if>
            <if test="workForm != null">work_form = #{workForm},</if>
            <if test="workBeginTime != null">work_begin_time = #{workBeginTime},</if>
            <if test="workEndTime != null">work_end_time = #{workEndTime},</if>
            <if test="finishTime != null">work_end_time = #{finishTime},</if>
            <if test="timeTerm != null">time_term = #{timeTerm},</if>
            <if test="workRange != null">work_range = #{workRange},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="workStep != null">work_step = #{workStep},</if>
            <if test="content != null">content = #{content},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where work_id = #{workId}
    </update>

    <delete id="deleteTHiddenTroubleWorkById" parameterType="Long">
        delete from t_hidden_trouble_work where work_id = #{workId}
    </delete>

    <delete id="deleteTHiddenTroubleWorkByIds" parameterType="String">
        delete from t_hidden_trouble_work where work_id in 
        <foreach item="workId" collection="array" open="(" separator="," close=")">
            #{workId}
        </foreach>
    </delete>
</mapper>