<?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.TWorkTaskInspectMapper">
    
    <resultMap type="TWorkTaskInspect" id="TWorkTaskInspectResult">
        <result property="inspectId"    column="inspect_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="inspectContent"    column="inspect_content"    />
        <result property="memberId"    column="member_id"    />
        <result property="receiveId"    column="receive_id"    />
        <result property="status"    column="status"    />
        <result property="isReport"    column="is_report"    />
        <result property="createTime"    column="create_time"    />
        <result property="memberName"    column="member_name"    />
        <result property="nickName"    column="nick_name"    />
        <result property="address"    column="address"    />
        <result property="username" column="username"    />
    </resultMap>

    <sql id="selectTWorkTaskInspectVo">
        SELECT
            inspect.inspect_id,
            inspect.task_id,
            inspect.longitude,
            inspect.latitude,
            inspect.inspect_content,
            inspect.member_id,
            inspect.receive_id,
            inspect.STATUS,
            inspect.is_report,
            inspect.create_time,
            person.person_name as member_name,
            us.nick_name,
            us.address,
            us.username
        FROM
            t_work_task_inspect inspect
        LEFT JOIN t_line_patrol_person person ON person.person_id = inspect.member_id
        LEFT JOIN t_detector_user us ON us.user_id = inspect.receive_id
    </sql>

    <select id="selectTWorkTaskInspectList" parameterType="TWorkTaskInspect" resultMap="TWorkTaskInspectResult">
        <include refid="selectTWorkTaskInspectVo"/>
        <where>
            <if test="taskId != null "> and inspect.task_id = #{taskId}</if>
            <if test="longitude != null  and longitude != ''"> and inspect.longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and inspect.latitude = #{latitude}</if>
            <if test="inspectContent != null  and inspectContent != ''"> and inspect.inspect_content = #{inspectContent}</if>
            <if test="memberId != null "> and inspect.member_id = #{memberId}</if>
            <if test="receiveId != null "> and inspect.receive_id = #{receiveId}</if>
            <if test="status != null "> and inspect.status = #{status}</if>
            <if test="isReport != null "> and inspect.is_report = #{isReport}</if>
        </where>
        order by inspect.create_time desc
    </select>
    <select id="selectTWorkTaskInspectwxList" parameterType="TWorkTaskInspect" resultMap="TWorkTaskInspectResult">
        SELECT
        inspect.inspect_id,
        inspect.task_id,
        inspect.longitude,
        inspect.latitude,
        inspect.inspect_content,
        inspect.member_id,
        inspect.receive_id,
        inspect.STATUS,
        inspect.is_report,
        inspect.create_time,
        person.person_name AS member_name,
        us.nick_name,
        us.address,
        us.username,
        h.`hidden_trouble_id`

        FROM
        (SELECT MAX(inspect_id) AS inspect_id  FROM t_work_task_inspect GROUP BY receive_id) t1
        LEFT JOIN t_work_task_inspect inspect ON t1.inspect_id = inspect.inspect_id
        LEFT JOIN t_line_patrol_person person ON person.person_id = inspect.member_id
        LEFT JOIN t_detector_user us ON us.user_id = inspect.receive_id
        LEFT JOIN t_hidden_trouble_info h ON h.`work_id` = inspect.inspect_id

        <where>
            <if test="taskId != null "> and inspect.task_id = #{taskId}</if>
            <if test="longitude != null  and longitude != ''"> and inspect.longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and inspect.latitude = #{latitude}</if>
            <if test="inspectContent != null  and inspectContent != ''"> and inspect.inspect_content = #{inspectContent}</if>
            <if test="memberId != null "> and inspect.member_id = #{memberId}</if>
            <if test="receiveId != null "> and inspect.receive_id = #{receiveId}</if>
            <if test="status != null and status==1">
              and  (inspect.`status` = 1 and h.`hidden_trouble_id` is null)
            </if>
            <if test="status != null and status==2">
                and  ( h.`hidden_trouble_id` is not null and inspect.`status` = 1 )
            </if>
            <if test="status != null and status==3">
                and inspect.`status` = 2
            </if>
            <if test="isReport != null "> and inspect.is_report = #{isReport}</if>
        </where>
        GROUP BY receive_id
        ORDER BY inspect.create_time DESC
    </select>
    <select id="selectTWorkTaskInspectwxnotList" parameterType="TWorkTaskInspect" resultMap="com.zehong.system.mapper.TDetectorUserMapper.TDetectorUserResult">
        SELECT
        us.*
        FROM t_detector_user us
        LEFT JOIN (SELECT * FROM t_work_task_inspect  WHERE task_id =#{taskId}) inspect ON us.user_id = inspect.receive_id
        WHERE inspect.inspect_id  is NULL
        and us.beyond_enterprise_id = #{receiveId}
    </select>
    <select id="selectTWorkTaskInspectById" parameterType="Long" resultMap="TWorkTaskInspectResult">
        <include refid="selectTWorkTaskInspectVo"/>
        where inspect.inspect_id = #{inspectId}
    </select>
        
    <insert id="insertTWorkTaskInspect" parameterType="TWorkTaskInspect" useGeneratedKeys="true" keyProperty="inspectId">
        insert into t_work_task_inspect
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="inspectContent != null">inspect_content,</if>
            <if test="memberId != null">member_id,</if>
            <if test="receiveId != null">receive_id,</if>
            <if test="status != null">status,</if>
            <if test="isReport != null">is_report,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="inspectContent != null">#{inspectContent},</if>
            <if test="memberId != null">#{memberId},</if>
            <if test="receiveId != null">#{receiveId},</if>
            <if test="status != null">#{status},</if>
            <if test="isReport != null">#{isReport},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateTWorkTaskInspect" parameterType="TWorkTaskInspect">
        update t_work_task_inspect
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="inspectContent != null">inspect_content = #{inspectContent},</if>
            <if test="memberId != null">member_id = #{memberId},</if>
            <if test="receiveId != null">receive_id = #{receiveId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isReport != null">is_report = #{isReport},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where inspect_id = #{inspectId}
    </update>
    <update id="updateOld">
        UPDATE t_work_task set status = 2 WHERE end_time &lt; now()
    </update>
    <delete id="deleteTWorkTaskInspectById" parameterType="Long">
        delete from t_work_task_inspect where inspect_id = #{inspectId}
    </delete>

    <delete id="deleteTWorkTaskInspectByIds" parameterType="String">
        delete from t_work_task_inspect where inspect_id in 
        <foreach item="inspectId" collection="array" open="(" separator="," close=")">
            #{inspectId}
        </foreach>
    </delete>

    <select id="inspectStatistic" parameterType="TWorkTaskInspect" resultType="java.util.HashMap">
        SELECT
          DATE_FORMAT(inspect.create_time, "%Y-%m-%d") AS timeType,
          COUNT(1) AS countNum
        FROM
            t_work_task_inspect inspect
        LEFT JOIN t_work_task task ON task.task_id = inspect.task_id
        <where>
            inspect.create_time BETWEEN concat(YEAR(now()),'-01-01 00:00:00') AND concat(YEAR(now()),'-12-31 23:59:59')
            <if test="taskTitle != null">
                and task.task_title like concat('%', #{taskTitle}, '%')
            </if>
            <if test="createEnterpriseId != null "> and task.create_enterprise_id = #{createEnterpriseId}</if>
            <if test="taskStatus != null "> and task.status = #{taskStatus}</if>
        </where>
        GROUP BY timeType
    </select>

    <select id="getHasInspectByTaskId" parameterType="java.lang.Long" resultType="java.lang.Long">
        SELECT
            receive_id
        FROM
            t_work_task_inspect
        where task_id = #{taskId}
        group by receive_id
    </select>
</mapper>