<?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.TInspectWorkInfoMapper">
    
    <resultMap type="TInspectWorkInfo" id="TInspectWorkInfoResult">
        <result property="workId"    column="work_id"    />
        <result property="workType"    column="work_type"    />
        <result property="parentId"    column="parent_id"    />
        <result property="workName"    column="work_name"    />
        <result property="workBeginTime"    column="work_begin_time"    />
        <result property="workLimitTime"    column="work_limit_time"    />
        <result property="workDescribe"    column="work_describe"    />
        <result property="pantrolId"    column="pantrol_id"    />
        <result property="deptId"      column="dept_id"/>
        <result property="deptName"      column="dept_name"/>
        <result property="inspectPointNum"    column="inspect_point_num"    />
        <result property="inspectSuccessNum"    column="inspect_success_num"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTInspectWorkInfoVo">
        select work_id, work_type, parent_id, work_name, work_begin_time, work_limit_time, work_describe, pantrol_id,dept_id, inspect_point_num, inspect_success_num, create_by, create_time, update_by, update_time, is_del, remarks from t_inspect_work_info
    </sql>

    <select id="selectTInspectWorkInfoList" parameterType="TInspectWorkInfo" resultMap="TInspectWorkInfoResult">
        select w.work_id, w.work_type, w.parent_id, w.work_name, w.work_begin_time, w.work_limit_time,
        w.work_describe, w.pantrol_id,w.dept_id, w.inspect_point_num, w.inspect_success_num, w.create_by,
        w.create_time, w.update_by, w.update_time, w.is_del, w.remarks ,d.dept_name
        from t_inspect_work_info w
        LEFT JOIN sys_dept d on d.dept_id = w.dept_id
        <where>
            <if test="parentId != null "> and w.parent_id = #{parentId}</if>
            <if test="workType != null "> and w.work_type = #{workType}</if>
            <if test="workName != null  and workName != ''"> and w.work_name like concat('%', #{workName}, '%')</if>
        </where>
        order by create_time desc
    </select>
    <select id="taskList" parameterType="TInspectWorkInfo" resultMap="TInspectWorkInfoResult">
        <include refid="selectTInspectWorkInfoVo"/>
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="workType != null "> and work_type = #{workType}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            and is_del = '0'
        </where>
        order by create_time desc
    </select>
    <select id="selectTInspectWorkInfoList2" parameterType="java.lang.Integer" resultMap="TInspectWorkInfoResult">
        <include refid="selectTInspectWorkInfoVo"/>
        WHERE is_del = 0 AND work_type = #{workType}
        and parent_id = 0
        and work_begin_time &lt; now()
        and work_limit_time &gt; now()
    </select>
    <select id="selectTInspectWorkInfoListLimit" resultMap="TInspectWorkInfoResult">
        select w.work_id, w.work_type, w.parent_id, w.work_name, w.work_begin_time, w.work_limit_time,
        w.work_describe, w.pantrol_id,w.dept_id, w.inspect_point_num, w.inspect_success_num, w.create_by,
        w.create_time, w.update_by, w.update_time, w.is_del, w.remarks ,d.dept_name
        from t_inspect_work_info w
        LEFT JOIN sys_dept d on d.dept_id = w.dept_id
        WHERE w.is_del = 0
        and w.parent_id = #{parentId}
        order by w.create_time desc
        limit 5
    </select>
    <select id="selectTInspectWorkInfoById" parameterType="Long" resultMap="TInspectWorkInfoResult">
        <include refid="selectTInspectWorkInfoVo"/>
        where work_id = #{workId}
    </select>
        
    <insert id="insertTInspectWorkInfo" parameterType="TInspectWorkInfo" useGeneratedKeys="true" keyProperty="workId">
        insert into t_inspect_work_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workType != null">work_type,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="workName != null and workName != ''">work_name,</if>
            <if test="workBeginTime != null">work_begin_time,</if>
            <if test="workLimitTime != null">work_limit_time,</if>
            <if test="workDescribe != null">work_describe,</if>
            <if test="pantrolId != null">pantrol_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="inspectPointNum != null">inspect_point_num,</if>
            <if test="inspectSuccessNum != null">inspect_success_num,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workType != null">#{workType},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="workName != null and workName != ''">#{workName},</if>
            <if test="workBeginTime != null">#{workBeginTime},</if>
            <if test="workLimitTime != null">#{workLimitTime},</if>
            <if test="workDescribe != null">#{workDescribe},</if>
            <if test="pantrolId != null">#{pantrolId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="inspectPointNum != null">#{inspectPointNum},</if>
            <if test="inspectSuccessNum != null">#{inspectSuccessNum},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTInspectWorkInfo" parameterType="TInspectWorkInfo">
        update t_inspect_work_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="workType != null">work_type = #{workType},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="workName != null and workName != ''">work_name = #{workName},</if>
            <if test="workBeginTime != null">work_begin_time = #{workBeginTime},</if>
            <if test="workLimitTime != null">work_limit_time = #{workLimitTime},</if>
            <if test="workDescribe != null">work_describe = #{workDescribe},</if>
            <if test="pantrolId != null">pantrol_id = #{pantrolId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="inspectPointNum != null">inspect_point_num = #{inspectPointNum},</if>
            <if test="inspectSuccessNum != null">inspect_success_num = #{inspectSuccessNum},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where work_id = #{workId}
    </update>

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

    <delete id="deleteTInspectWorkInfoByIds" parameterType="String">
        delete from t_inspect_work_info where work_id in 
        <foreach item="workId" collection="array" open="(" separator="," close=")">
            #{workId}
        </foreach>
    </delete>
    <insert id="inserts" parameterType="java.util.List" useGeneratedKeys = "true" keyProperty = "workId">
        INSERT INTO t_inspect_work_info (work_id,work_type,parent_id,work_name,work_begin_time,work_limit_time,work_describe,pantrol_id,dept_id,
        inspect_point_num,inspect_success_num,create_by,create_time,remarks)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.workId},#{item.workType},#{item.parentId},#{item.workName},#{item.workBeginTime},#{item.workLimitTime},#{item.workDescribe},
            #{item.pantrolId},#{item.deptId},#{item.inspectPointNum},#{item.inspectSuccessNum},#{item.createBy},#{item.createTime},#{item.remarks})
        </foreach>

    </insert>
</mapper>