<?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.zcxk.order.dao.WorkOrderManageMapper" >
  <resultMap id="BaseResultMap" type="com.zcxk.order.dto.WorkOrderManageDto" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="task_no" property="taskNo" jdbcType="VARCHAR" />
    <result column="task_desc" property="taskDesc" jdbcType="VARCHAR" />
    <result column="order_user_id" property="orderUserId" jdbcType="VARCHAR" />
    <result column="send_time" property="sendTime" jdbcType="TIMESTAMP" />
    <result column="send_by" property="sendBy" jdbcType="VARCHAR" />
    <result column="plan_finish_date" property="planFinishDate" jdbcType="TIMESTAMP" />
    <result column="finish_date" property="finishDate" jdbcType="TIMESTAMP" />
    <result column="date_limit" property="dateLimit" jdbcType="VARCHAR" />
    <result column="urgency" property="urgency" jdbcType="VARCHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="contact_user" property="contactUser" jdbcType="VARCHAR" />
    <result column="contact_phone" property="contactPhone" jdbcType="VARCHAR" />
    <result column="order_time" property="orderTime" jdbcType="VARCHAR" />
    <result column="company_org_id" property="companyOrgId" jdbcType="VARCHAR" />
    <result column="department_org_id" property="departmentOrgId" jdbcType="VARCHAR" />
    <result column="order_status" property="orderStatus" jdbcType="INTEGER" />
    <result column="event_type" property="eventType" jdbcType="INTEGER" />
    <result column="event_type_name" property="eventTypeName" jdbcType="VARCHAR" />
    <result column="order_type_id" property="orderTypeId" jdbcType="INTEGER" />
    <result column="order_pid" property="orderPid" jdbcType="VARCHAR" />
    <result column="tenant_id" property="tenantId" jdbcType="VARCHAR" />
    <result column="process_instance_id" property="processInstanceId" jdbcType="VARCHAR" />
    <result column="process_def_id" property="processDefId" jdbcType="VARCHAR" />
    <result column="current_task_id" property="currentTaskId" jdbcType="VARCHAR" />
    <result column="current_users" property="currentUsers" jdbcType="VARCHAR" />
    <result column="current_task_name" property="currentTaskName" jdbcType="VARCHAR" />
    <result column="create_by" property="createBy" jdbcType="VARCHAR" />
    <result column="date_create" property="dateCreate" jdbcType="TIMESTAMP" />
    <result column="update_by" property="updateBy" jdbcType="VARCHAR" />
    <result column="date_update" property="dateUpdate" jdbcType="TIMESTAMP" />
    <result column="geo" property="geo" jdbcType="VARCHAR" />
    <result column="videos" property="videos" jdbcType="VARCHAR" />
    <result column="pictures" property="pictures" jdbcType="VARCHAR" />
    <result column="flow_detail" property="flowDetail" jdbcType="VARCHAR" />
    <result column="device_id" property="deviceId" jdbcType="INTEGER" />
    <result column="scenes_id" property="scenesId" jdbcType="INTEGER" />
    <result column="device_name" property="deviceName" jdbcType="VARCHAR" />
    <result column="scenes_name" property="scenesName" jdbcType="VARCHAR" />
    <result column="order_status_name" property="orderStatusName" jdbcType="VARCHAR" />
    <result column="order_count" property="orderCount" jdbcType="VARCHAR" />
    <result column="in_process_status" property="inProcessStatus" jdbcType="INTEGER" />
  </resultMap>

  <resultMap id="workFlowLogResultMap" type="com.zcxk.order.entity.WorkFlowLog">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="flow_id" property="flowId" jdbcType="INTEGER" />
    <result column="log_process_instance_id" property="processInstanceId" jdbcType="VARCHAR" />
    <result column="task_id" property="taskId" jdbcType="VARCHAR" />
    <result column="handle_user_id" property="handleUserId" jdbcType="VARCHAR" />
    <result column="handle_user_name" property="handleUserName" jdbcType="VARCHAR" />
    <result column="flow_type" property="flowType" jdbcType="INTEGER" />
    <result column="flow_result" property="flowResult" jdbcType="VARCHAR" />
    <result column="log_create_by" property="createBy" jdbcType="VARCHAR" />
    <result column="log_date_create" property="dateCreate" jdbcType="TIMESTAMP" />
    <result column="log_update_by" property="updateBy" jdbcType="VARCHAR" />
    <result column="log_date_update" property="dateUpdate" jdbcType="TIMESTAMP" />
  </resultMap>

  <resultMap id="OrderResultMap" type="com.zcxk.order.dto.WorkOrderManageDto" extends="BaseResultMap">
    <association property="workFlowDetail" javaType="com.zcxk.order.entity.WorkFlowDetail">
      <id column="id" property="id" jdbcType="INTEGER" />
      <result column="flow_id" property="flowId" jdbcType="INTEGER" />
      <result column="flow_type" property="flowType" jdbcType="INTEGER" />
      <result column="flow_detail" property="flowDetail" jdbcType="VARCHAR" />
      <result column="detail_create_by" property="createBy" jdbcType="VARCHAR" />
      <result column="detail_date_create" property="dateCreate" jdbcType="TIMESTAMP" />
      <result column="detail_update_by" property="updateBy" jdbcType="VARCHAR" />
      <result column="detail_date_update" property="dateUpdate" jdbcType="TIMESTAMP" />
    </association>
    <collection property="workFlowLogList" ofType="com.zcxk.order.entity.WorkFlowLog" javaType="java.util.List" select="getWorkFlowLog" column="{flowId=id}">
      <id column="id" property="id" jdbcType="INTEGER" />
      <result column="flow_id" property="flowId" jdbcType="INTEGER" />
      <result column="log_process_instance_id" property="processInstanceId" jdbcType="VARCHAR" />
      <result column="task_id" property="taskId" jdbcType="VARCHAR" />
      <result column="handle_user_id" property="handleUserId" jdbcType="VARCHAR" />
      <result column="handle_user_name" property="handleUserName" jdbcType="VARCHAR" />
      <result column="flow_type" property="flowType" jdbcType="INTEGER" />
      <result column="flow_result" property="flowResult" jdbcType="VARCHAR" />
      <result column="log_create_by" property="createBy" jdbcType="VARCHAR" />
      <result column="log_date_create" property="dateCreate" jdbcType="TIMESTAMP" />
      <result column="log_update_by" property="updateBy" jdbcType="VARCHAR" />
      <result column="log_date_update" property="dateUpdate" jdbcType="TIMESTAMP" />
    </collection>
  </resultMap>


  <resultMap id="StatisticsMap" type="com.zcxk.order.dto.StatisticsDto" >
    <result column="statistics_name" property="statisticsName" jdbcType="VARCHAR" />
    <result column="statistics_value" property="statisticsValue" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, task_no, task_desc, order_user_id, send_time, send_by, plan_finish_date, finish_date, 
    date_limit, urgency, address, contact_user, contact_phone, order_time, company_org_id, 
    department_org_id, order_status, event_type, order_type_id, order_pid, tenant_id, 
    process_instance_id, process_def_id, current_task_id, current_users, current_task_name, 
    create_by, date_create, update_by, date_update, geo,videos, pictures, device_id, scenes_id,
    device_name, scenes_name,in_process_status
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from sc_work_order_manage
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="getWorkFlowLog" resultMap="workFlowLogResultMap" parameterType="map">
        select t3.process_instance_id log_process_instance_id,t3.task_id,t3.handle_user_id,t3.handle_user_name,t3.flow_result,t3.create_by log_create_by,
        t3.date_create log_date_create,t3.update_by log_update_by,t3.date_update log_date_update
        from sc_work_flow_log t3
        where t3.flow_id= #{flowId,jdbcType=INTEGER} and t3.flow_type=2
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from sc_work_order_manage
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zcxk.order.entity.WorkOrderManage" >
    insert into sc_work_order_manage (id, task_no, task_desc, 
      order_user_id, send_time, send_by, 
      plan_finish_date, finish_date, date_limit, 
      urgency, address, contact_user, 
      contact_phone, order_time, company_org_id, 
      department_org_id, order_status, event_type, 
      order_type_id, order_pid, tenant_id, 
      process_instance_id, process_def_id, current_task_id, 
      current_users, current_task_name, create_by, 
      date_create, update_by, date_update, 
      geo,videos,pictures)
    values (#{id,jdbcType=INTEGER}, #{taskNo,jdbcType=VARCHAR}, #{taskDesc,jdbcType=VARCHAR}, 
      #{orderUserId,jdbcType=VARCHAR}, #{sendTime,jdbcType=TIMESTAMP}, #{sendBy,jdbcType=VARCHAR}, 
      #{planFinishDate,jdbcType=TIMESTAMP}, #{finishDate,jdbcType=TIMESTAMP}, #{dateLimit,jdbcType=VARCHAR}, 
      #{urgency,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{contactUser,jdbcType=VARCHAR}, 
      #{contactPhone,jdbcType=VARCHAR}, #{orderTime,jdbcType=VARCHAR}, #{companyOrgId,jdbcType=VARCHAR}, 
      #{departmentOrgId,jdbcType=VARCHAR}, #{orderStatus,jdbcType=INTEGER}, #{eventType,jdbcType=INTEGER}, 
      #{orderTypeId,jdbcType=INTEGER}, #{orderPid,jdbcType=VARCHAR}, #{tenantId,jdbcType=VARCHAR}, 
      #{processInstanceId,jdbcType=VARCHAR}, #{processDefId,jdbcType=VARCHAR}, #{currentTaskId,jdbcType=VARCHAR}, 
      #{currentUsers,jdbcType=VARCHAR}, #{currentTaskName,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR}, 
      #{dateCreate,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}, #{dateUpdate,jdbcType=TIMESTAMP}, 
      #{geo,jdbcType=VARCHAR},#{videos,jdbcType=VARCHAR},#{pictures,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zcxk.order.entity.WorkOrderManage" keyProperty="id" useGeneratedKeys="true">
    insert into sc_work_order_manage
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="taskNo != null" >
        task_no,
      </if>
      <if test="taskDesc != null" >
        task_desc,
      </if>
      <if test="orderUserId != null" >
        order_user_id,
      </if>
      <if test="sendTime != null" >
        send_time,
      </if>
      <if test="sendBy != null" >
        send_by,
      </if>
      <if test="planFinishDate != null" >
        plan_finish_date,
      </if>
      <if test="finishDate != null" >
        finish_date,
      </if>
      <if test="dateLimit != null" >
        date_limit,
      </if>
      <if test="urgency != null" >
        urgency,
      </if>
      <if test="address != null" >
        address,
      </if>
      <if test="contactUser != null" >
        contact_user,
      </if>
      <if test="contactPhone != null" >
        contact_phone,
      </if>
      <if test="orderTime != null" >
        order_time,
      </if>
      <if test="companyOrgId != null" >
        company_org_id,
      </if>
      <if test="departmentOrgId != null" >
        department_org_id,
      </if>
      <if test="orderStatus != null" >
        order_status,
      </if>
      <if test="eventType != null" >
        event_type,
      </if>
      <if test="orderTypeId != null" >
        order_type_id,
      </if>
      <if test="orderPid != null" >
        order_pid,
      </if>
      <if test="tenantId != null" >
        tenant_id,
      </if>
      <if test="processInstanceId != null" >
        process_instance_id,
      </if>
      <if test="processDefId != null" >
        process_def_id,
      </if>
      <if test="currentTaskId != null" >
        current_task_id,
      </if>
      <if test="currentUsers != null" >
        current_users,
      </if>
      <if test="currentTaskName != null" >
        current_task_name,
      </if>
      <if test="createBy != null" >
        create_by,
      </if>
      <if test="dateCreate != null" >
        date_create,
      </if>
      <if test="updateBy != null" >
        update_by,
      </if>
      <if test="dateUpdate != null" >
        date_update,
      </if>
      <if test="geo != null" >
        geo,
      </if>
      <if test="videos != null" >
        videos,
      </if>
      <if test="pictures != null" >
        pictures,
      </if>
      <if test="deviceId != null" >
        device_id,
      </if>
      <if test="scenesId != null" >
        scenes_id,
      </if>
      <if test="deviceName != null" >
        device_name,
      </if>
      <if test="scenesName != null" >
        scenes_name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="taskNo != null" >
        #{taskNo,jdbcType=VARCHAR},
      </if>
      <if test="taskDesc != null" >
        #{taskDesc,jdbcType=VARCHAR},
      </if>
      <if test="orderUserId != null" >
        #{orderUserId,jdbcType=VARCHAR},
      </if>
      <if test="sendTime != null" >
        #{sendTime,jdbcType=TIMESTAMP},
      </if>
      <if test="sendBy != null" >
        #{sendBy,jdbcType=VARCHAR},
      </if>
      <if test="planFinishDate != null" >
        #{planFinishDate,jdbcType=TIMESTAMP},
      </if>
      <if test="finishDate != null" >
        #{finishDate,jdbcType=TIMESTAMP},
      </if>
      <if test="dateLimit != null" >
        #{dateLimit,jdbcType=VARCHAR},
      </if>
      <if test="urgency != null" >
        #{urgency,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="contactUser != null" >
        #{contactUser,jdbcType=VARCHAR},
      </if>
      <if test="contactPhone != null" >
        #{contactPhone,jdbcType=VARCHAR},
      </if>
      <if test="orderTime != null" >
        #{orderTime,jdbcType=VARCHAR},
      </if>
      <if test="companyOrgId != null" >
        #{companyOrgId,jdbcType=VARCHAR},
      </if>
      <if test="departmentOrgId != null" >
        #{departmentOrgId,jdbcType=VARCHAR},
      </if>
      <if test="orderStatus != null" >
        #{orderStatus,jdbcType=INTEGER},
      </if>
      <if test="eventType != null" >
        #{eventType,jdbcType=INTEGER},
      </if>
      <if test="orderTypeId != null" >
        #{orderTypeId,jdbcType=INTEGER},
      </if>
      <if test="orderPid != null" >
        #{orderPid,jdbcType=VARCHAR},
      </if>
      <if test="tenantId != null" >
        #{tenantId,jdbcType=VARCHAR},
      </if>
      <if test="processInstanceId != null" >
        #{processInstanceId,jdbcType=VARCHAR},
      </if>
      <if test="processDefId != null" >
        #{processDefId,jdbcType=VARCHAR},
      </if>
      <if test="currentTaskId != null" >
        #{currentTaskId,jdbcType=VARCHAR},
      </if>
      <if test="currentUsers != null" >
        #{currentUsers,jdbcType=VARCHAR},
      </if>
      <if test="currentTaskName != null" >
        #{currentTaskName,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null" >
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="dateCreate != null" >
        #{dateCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="dateUpdate != null" >
        #{dateUpdate,jdbcType=TIMESTAMP},
      </if>
      <if test="geo != null" >
        #{geo,jdbcType=VARCHAR},
      </if>
      <if test="videos != null" >
        #{videos,jdbcType=VARCHAR},
      </if>
      <if test="pictures != null" >
        #{pictures,jdbcType=VARCHAR},
      </if>
      <if test="deviceId != null" >
        #{deviceId,jdbcType=INTEGER},
      </if>
      <if test="scenesId != null" >
        #{scenesId,jdbcType=INTEGER},
      </if>
      <if test="deviceName != null" >
        #{deviceName,jdbcType=VARCHAR},
      </if>
      <if test="scenesName != null" >
        #{scenesName,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.order.entity.WorkOrderManage" >
    update sc_work_order_manage
    <set >
      <if test="taskNo != null" >
        task_no = #{taskNo,jdbcType=VARCHAR},
      </if>
      <if test="taskDesc != null" >
        task_desc = #{taskDesc,jdbcType=VARCHAR},
      </if>
      <if test="orderUserId != null" >
        order_user_id = #{orderUserId,jdbcType=VARCHAR},
      </if>
      <if test="sendTime != null" >
        send_time = #{sendTime,jdbcType=TIMESTAMP},
      </if>
      <if test="sendBy != null" >
        send_by = #{sendBy,jdbcType=VARCHAR},
      </if>
      <if test="planFinishDate != null" >
        plan_finish_date = #{planFinishDate,jdbcType=TIMESTAMP},
      </if>
      <if test="finishDate != null" >
        finish_date = #{finishDate,jdbcType=TIMESTAMP},
      </if>
      <if test="dateLimit != null" >
        date_limit = #{dateLimit,jdbcType=VARCHAR},
      </if>
      <if test="urgency != null" >
        urgency = #{urgency,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="contactUser != null" >
        contact_user = #{contactUser,jdbcType=VARCHAR},
      </if>
      <if test="contactPhone != null" >
        contact_phone = #{contactPhone,jdbcType=VARCHAR},
      </if>
      <if test="orderTime != null" >
        order_time = #{orderTime,jdbcType=VARCHAR},
      </if>
      <if test="companyOrgId != null" >
        company_org_id = #{companyOrgId,jdbcType=VARCHAR},
      </if>
      <if test="departmentOrgId != null" >
        department_org_id = #{departmentOrgId,jdbcType=VARCHAR},
      </if>
      <if test="orderStatus != null" >
        order_status = #{orderStatus,jdbcType=INTEGER},
      </if>
      <if test="eventType != null" >
        event_type = #{eventType,jdbcType=INTEGER},
      </if>
      <if test="orderTypeId != null" >
        order_type_id = #{orderTypeId,jdbcType=INTEGER},
      </if>
      <if test="orderPid != null" >
        order_pid = #{orderPid,jdbcType=VARCHAR},
      </if>
      <if test="tenantId != null" >
        tenant_id = #{tenantId,jdbcType=VARCHAR},
      </if>
      <if test="processInstanceId != null" >
        process_instance_id = #{processInstanceId,jdbcType=VARCHAR},
      </if>
      <if test="processDefId != null" >
        process_def_id = #{processDefId,jdbcType=VARCHAR},
      </if>
      <if test="currentTaskId != null" >
        current_task_id = #{currentTaskId,jdbcType=VARCHAR},
      </if>
      <if test="currentUsers != null" >
        current_users = #{currentUsers,jdbcType=VARCHAR},
      </if>
      <if test="currentTaskName != null" >
        current_task_name = #{currentTaskName,jdbcType=VARCHAR},
      </if>
      <if test="createBy != null" >
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="dateCreate != null" >
        date_create = #{dateCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="dateUpdate != null" >
        date_update = #{dateUpdate,jdbcType=TIMESTAMP},
      </if>
      <if test="geo != null" >
        geo = #{geo,jdbcType=VARCHAR},
      </if>
      <if test="videos != null" >
        videos=#{videos,jdbcType=VARCHAR},
      </if>
      <if test="pictures != null" >
        pictures=#{pictures,jdbcType=VARCHAR},
      </if>
      <if test="inProcessStatus != null" >
        in_process_status=#{inProcessStatus,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zcxk.order.entity.WorkOrderManage" >
    update sc_work_order_manage
    set task_no = #{taskNo,jdbcType=VARCHAR},
      task_desc = #{taskDesc,jdbcType=VARCHAR},
      order_user_id = #{orderUserId,jdbcType=VARCHAR},
      send_time = #{sendTime,jdbcType=TIMESTAMP},
      send_by = #{sendBy,jdbcType=VARCHAR},
      plan_finish_date = #{planFinishDate,jdbcType=TIMESTAMP},
      finish_date = #{finishDate,jdbcType=TIMESTAMP},
      date_limit = #{dateLimit,jdbcType=VARCHAR},
      urgency = #{urgency,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      contact_user = #{contactUser,jdbcType=VARCHAR},
      contact_phone = #{contactPhone,jdbcType=VARCHAR},
      order_time = #{orderTime,jdbcType=VARCHAR},
      company_org_id = #{companyOrgId,jdbcType=VARCHAR},
      department_org_id = #{departmentOrgId,jdbcType=VARCHAR},
      order_status = #{orderStatus,jdbcType=INTEGER},
      event_type = #{eventType,jdbcType=INTEGER},
      order_type_id = #{orderTypeId,jdbcType=INTEGER},
      order_pid = #{orderPid,jdbcType=VARCHAR},
      tenant_id = #{tenantId,jdbcType=VARCHAR},
      process_instance_id = #{processInstanceId,jdbcType=VARCHAR},
      process_def_id = #{processDefId,jdbcType=VARCHAR},
      current_task_id = #{currentTaskId,jdbcType=VARCHAR},
      current_users = #{currentUsers,jdbcType=VARCHAR},
      current_task_name = #{currentTaskName,jdbcType=VARCHAR},
      create_by = #{createBy,jdbcType=VARCHAR},
      date_create = #{dateCreate,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      date_update = #{dateUpdate,jdbcType=TIMESTAMP},
      geo = #{geo,jdbcType=VARCHAR},
      videos = #{videos,jdbcType=VARCHAR},
      pictures = #{pictures,jdbcType=VARCHAR},
      in_process_status=#{inProcessStatus,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="selectAppTodo" resultMap="BaseResultMap">
    select id,task_no,send_time,address,order_type_id,create_by,update_by,task_desc,order_status,current_task_name,in_process_status
    from sc_work_order_manage
    where find_in_set(#{userId},current_users) and tenant_id = #{tenantId}
    union
    select id,plan_id,plan_date,task_area_name,task_type,create_by,update_by,task_content,plan_status,current_task_name,in_process_status
    from sc_plan_manage
    where find_in_set(#{userId},current_users) and tenant_id = #{tenantId}
    order by send_time desc
  </select>
  <select id="selectAppAll" resultMap="BaseResultMap">
    select distinct t1.id,task_no,send_time,address,order_type_id,current_task_name,t1.create_by,t1.update_by,task_desc,order_status,in_process_status
    from sc_work_order_manage t1
    left join sc_work_flow_log t2 ON t1.id=t2.flow_id and flow_type=2 and find_in_set(#{userId},handle_user_id) and left(flow_result,2)='拒单'
    where (find_in_set(#{userId},current_users)
     or t1.id in(select flow_id from sc_work_flow_log where flow_type=2 and find_in_set(#{userId},handle_user_id)))
     and tenant_id = #{tenantId}
     and (t2.id is null or t1.order_user_id=t2.handle_user_id)
    union
    select id,plan_id,plan_date,task_area_name,task_type,current_task_name,create_by,update_by,task_content,plan_status,in_process_status
    from sc_plan_manage
    where (find_in_set(#{userId},current_users)
     or id in(select flow_id from sc_work_flow_log where flow_type=1 and find_in_set(#{userId},handle_user_id)))
     and tenant_id = #{tenantId}
    order by send_time desc
  </select>
  <select id="selectOrderDetail" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select t1.*,t2.flow_detail
    from sc_work_order_manage t1
    left join sc_work_flow_detail t2 on t1.id=t2.flow_id and t2.flow_type=2
    where t1.id = #{id,jdbcType=INTEGER} or t1.order_pid = #{orderPid,jdbcType=INTEGER}
    order by t1.order_pid
  </select>
  <insert id="batchInsertByAlarms" >
    insert into sc_work_order_manage( task_no, task_desc, address, order_time, company_org_id, department_org_id, order_status, event_type, order_pid, tenant_id, current_task_name,  date_create, date_update, geo,scenes_id,scenes_name,device_id,device_name)
    values
    <foreach collection="workOrderManages" item="item" index="index" separator=",">
      (#{item.taskNo} , #{item.taskDesc}, #{item.address}, #{item.orderTime}, #{item.companyOrgId}, #{item.departmentOrgId}, 0, 3, 0, #{item.tenantId}, '设备报警工单',  now(),  now(), #{item.geo}, #{item.parentSceneId}, #{item.parentSceneName}, #{item.deviceId}, #{item.deviceName})
    </foreach>
  </insert>
  <delete id="batchDeleteByAlarms" >
    delete from sc_work_order_manage
    where  order_status = 0 and  device_id in
    <foreach collection="deviceIds" item="item" open="(" close=")" index="index" separator=",">
      #{item}
    </foreach>
  </delete>
  <select id="findWorkOrderByDeviceIds" resultType="com.zcxk.order.dto.WorkOrderManageByAlarmDto">
    select
    id
    ,device_id as "deviceId"
    , task_desc as "taskDesc"
    , order_status as "orderStatus"
    from sc_work_order_manage
    where (order_status = 0 or order_status = 1)
    and device_id in
    <foreach collection="deviceIds" item="item" open="(" close=")" index="index" separator=",">
      #{item}
    </foreach>
  </select>
  <update id="updateByAlarms">
        update sc_work_order_manage set task_desc =#{workOrderManage.taskDesc} ,date_update = now() where id=#{workOrderManage.id}
  </update>

  <select id="findByNo" resultMap="OrderResultMap">
    select
    t1.id, t1.task_no, t1.task_desc, t1.order_user_id, t1.send_time, t1.send_by, t1.plan_finish_date, t1.finish_date,
    t1.date_limit, t1.urgency, t1.address, t1.contact_user, t1.contact_phone, t1.order_time, t1.company_org_id,
    t1.department_org_id, t1.order_status, t1.event_type,case when t1.event_type=1 then '运维上报' when t1.event_type=2 then '用户上报' when t1.event_type=3 then '设备告警' end  event_type_name,
    t1.order_type_id, t1.order_pid, t1.tenant_id,
    t1.process_instance_id, t1.process_def_id, t1.current_task_id, t1.current_users, t1.current_task_name,
    t1.create_by, t1.date_create, t1.update_by, t1.date_update, t1.geo,t1.videos,t1.pictures,
    t1.device_id, t1.scenes_id, t1.device_name, t1.scenes_name,
    t2.flow_detail,t2.create_by detail_create_by,t2.date_create detail_date_create,t2.update_by detail_update_by,t2.date_update detail_date_update
    from sc_work_order_manage t1 left  join sc_work_flow_detail t2
    on t1.id = t2.flow_id and t2.flow_type=2
    <where>
      <if test="order.taskNo != null and order.taskNo != ''">
        and t1.task_no = #{order.taskNo}
      </if>
    </where>
  </select>

  <!--分页查询-->
  <select id="findPage" resultMap="OrderResultMap">
    select
    t1.id, t1.task_no, t1.task_desc, t1.order_user_id, t1.send_time, t1.send_by, t1.plan_finish_date, t1.finish_date,
    t1.date_limit, t1.urgency, t1.address, t1.contact_user, t1.contact_phone, t1.order_time, t1.company_org_id,
    t1.department_org_id, t1.order_status, t1.event_type,case when t1.event_type=1 then '运维上报' when t1.event_type=2 then '用户上报' when t1.event_type=3 then '设备告警' end  event_type_name,
    t1.order_type_id, t1.order_pid, t1.tenant_id,
    t1.process_instance_id, t1.process_def_id, t1.current_task_id, t1.current_users, t1.current_task_name,
    t1.create_by, t1.date_create, t1.update_by, t1.date_update, t1.geo,t1.videos,t1.pictures,
    t1.device_id, t1.scenes_id, t1.device_name, t1.scenes_name,
    t2.flow_detail,t2.create_by detail_create_by,t2.date_create detail_date_create,t2.update_by detail_update_by,t2.date_update detail_date_update
    from sc_work_order_manage t1 left  join sc_work_flow_detail t2
    on t1.id = t2.flow_id and t2.flow_type=2
    <where>
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.taskNo != null and order.taskNo != ''">
        and t1.task_no like CONCAT('%',#{order.taskNo},'%')
      </if>
      <if test="order.orderPid != null">
        and t1.order_pid = #{order.orderPid,jdbcType=INTEGER}
      </if>
      <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
        <if test="order.permissonType == 5 or order.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="order.permissonType == 4 or order.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="order.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
      <if test="order.orderStatus != null">
        and t1.order_status =  #{order.orderStatus,jdbcType=INTEGER}
      </if>
      <if test="order.orderStatus == null">
        and t1.order_status != 0
      </if>
      <if test="order.orderTypeId != null">
        and t1.order_type_id = #{order.orderTypeId,jdbcType=INTEGER}
      </if>
      <if test="order.eventType != null">
        and t1.event_type = #{order.eventType,jdbcType=INTEGER}
      </if>
      <if test="order.currentUsers != null">
        and FIND_IN_SET(#{order.currentUsers,jdbcType=VARCHAR},t1.current_users)
      </if>

      <if test="order.startDate != null and order.endDate != null">
        <if test="order.orderStatus == 0">
          and t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
          and t1.date_create &lt;= DATE_ADD(date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d'),INTERVAL 1 DAY)
          order by t1.date_create desc
        </if>
        <if test="order.orderStatus != 0">
          and t1.send_time &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
          and t1.send_time &lt;= DATE_ADD(date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d'),INTERVAL 1 DAY)
          order by t1.send_time desc
        </if>
      </if>
      <if test="order.startDate == null and order.endDate == null">
         order by t1.send_time desc
      </if>
    </where>
  </select>
  <!--删除-->
  <delete id="batchDelete">
    delete from  sc_work_order_manage
    where order_status = 0
    and id in
    <foreach collection="ids" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </delete>
  <!--根据orderPid 查询工单-->
  <select id="selectByPId" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"></include>
    from sc_work_order_manage
    where order_pid =  #{orderPid,jdbcType=VARCHAR}
  </select>

  <!--查询工单派单时的用户状态-->
   <select id="selectOrderUserStatus" parameterType="java.lang.String" resultType="map">
      SELECT DISTINCT X, Y,
       CASE WHEN S IS NULL THEN '空闲' WHEN S='0' THEN '空闲' ELSE '执行中' END S ,
       CASE WHEN offlinetime IS NULL THEN '离线' WHEN offlinetime>10 THEN '离线' WHEN offlinetime &lt;= 10 THEN '在线' END L
       FROM
        (
       select order_user_id,longtitude X, latitude Y, TIMESTAMPDIFF(second,upload_date, now()) offlinetime from sc_work_order_manage t1 left join sc_gps_data t2
       on t1.order_user_id = t2.user_id
       group by order_user_id,longtitude, latitude,upload_date
       ) a
      left join
      (
       select count(*) S,order_user_id from sc_work_order_manage
       where order_status = 1
       group by order_user_id
       ) b
       on a.order_user_id=b.order_user_id
       where a.order_user_id= #{userId,jdbcType=INTEGER}
  </select>
  <!--查询任务单派单时的用户状态-->
  <select id="selectPlanUserStatus" parameterType="java.lang.String" resultType="map">
     SELECT
     CASE WHEN S IS NULL THEN '空闲' WHEN S='0' THEN '空闲' ELSE '执行中' END S
     FROM (
       select count(*) S,user_id from sc_plan_manage
       where plan_status = 1
       group by user_id
     ) a
     where a.user_id= #{userId,jdbcType=INTEGER}
  </select>


  <!--查询维修列表的用户状态-->
  <select id="selectMaintainUserStatus" parameterType="java.lang.String" resultType="map">
    select distinct X, Y,
    case when offlinetime is null then '离线' when offlinetime>10 then '离线' when offlinetime &lt;= 10 THEN '在线' END L
    from (
        select longtitude X, latitude Y, TIMESTAMPDIFF(second,upload_date, now()) offlinetime from sc_gps_data
        where  user_id= #{userId,jdbcType=INTEGER}
    ) a
  </select>

  <!--待处理权限-->
  <select id="selectPendingPower" resultType="java.lang.Integer" parameterType="map">
    select count(1) from sc_work_order_manage
    where id = #{id,jdbcType=INTEGER} and FIND_IN_SET(#{currentUsers,jdbcType=VARCHAR},current_users)
  </select>

  <!--综合展示-查询工单执行情况-->
  <select id="selectOrderStatus" resultMap="BaseResultMap" >
    select order_status_name,sum(order_count) order_count
    from(
      select order_status,count(1) order_count ,
       case when order_status=0 then '未派单' when order_status=1 or order_status=4 then '处理中' else '已完结' end order_status_name
      from sc_work_order_manage t1
      where
      <if test="statsType == null or statsType == 0">
        t1.date_create>=date_format(curdate(), '%Y-%m' )
      </if>
      <if test="statsType == 1">
        t1.date_create>=date_format(curdate(), '%Y-1-1' )
      </if>
      <if test="statsType == 2">
        t1.date_create>=date_sub(date_format(curdate(), '%Y-%m-1' ), interval 11 month)
      </if>
      <if test="tenantId != null and tenantId != ''">
        and t1.tenant_id=#{tenantId}
      </if>
      <if test="companyOrgId != null and companyOrgId !=''">
        and t1.company_org_id=#{companyOrgId}
      </if>
      <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
        <if test="permissonType == 5 or permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="permissonType == 4 or permissonType == 3">
          and t1.department_org_id in
          <foreach collection="programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
      group by order_status
    )tab1
    group by order_status_name
  </select>
  <!--维修列表-工单情况（未派单，处理中）-->
  <select id="selectMaintainOrder" resultMap="BaseResultMap" >
    select t1.id ,t1.task_no,  t1.address, t1.task_desc, t1.plan_finish_date, t1.date_create, t1.order_user_id, t2.flow_detail,
    case when t1.order_status=0 then '未派单' when t1.order_status =1 or t1.order_status=4 then '处理中' end order_status_name,
    case when t1.event_type=1 then '运维上报' when t1.event_type=2 then '用户上报' when t1.event_type=3 then '设备告警' end  event_type_name,
    geo
    from sc_work_order_manage t1 left join sc_work_flow_detail t2 on t1.id=t2.flow_id and t2.flow_type=2
    <where>
      t1.order_status !=2 and t1.order_status !=3
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
        <if test="order.permissonType == 5 or order.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="order.permissonType == 4 or order.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="order.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
    </where>
    order by t1.date_create desc
  </select>

  <!--App事件列表-->
  <select id="findEventListPage" resultMap="OrderResultMap">
    select
    t1.id, t1.task_no, t1.task_desc, t1.order_user_id, t1.send_time, t1.send_by, t1.plan_finish_date, t1.finish_date,
    t1.date_limit, t1.urgency, t1.address, t1.contact_user, t1.contact_phone, t1.order_time, t1.company_org_id,
    t1.department_org_id, t1.order_status, t1.event_type,case when t1.event_type=1 then '运维上报' when t1.event_type=2 then '用户上报' when t1.event_type=3 then '设备告警' end  event_type_name,
    t1.order_type_id, t1.order_pid, t1.tenant_id,
    t1.process_instance_id, t1.process_def_id, t1.current_task_id, t1.current_users, t1.current_task_name,
    t1.create_by, t1.date_create, t1.update_by, t1.date_update, t1.geo,t1.videos,t1.pictures,
    t1.device_id, t1.scenes_id, t1.device_name, t1.scenes_name,
    t2.flow_detail,t2.create_by detail_create_by,t2.date_create detail_date_create,t2.update_by detail_update_by,t2.date_update detail_date_update
    from sc_work_order_manage t1 left  join sc_work_flow_detail t2
    on t1.id = t2.flow_id and t2.flow_type=2
    <where>
      t1.event_type = 1 and t1.create_by = #{order.createBy}
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.departmentOrgId != null and order.departmentOrgId != ''">
        and t1.department_org_id = #{order.departmentOrgId}
      </if>
      order by t1.date_create desc
    </where>
  </select>

  <!--工单完成情况统计-->
  <select id="workOrderStatistics" resultType="map">
    select  total 工单总数, finished 工单完成数, ROUND(finished*100/total,2) 工单完成率 from
    (
      select count(1) total,sum(case when order_status=2 then 1 when order_status =3 then 1 else 0 end) finished
      from sc_work_order_manage t1
      <where>
        t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
        and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
        <if test="order.tenantId != null and order.tenantId != ''">
          and t1.tenant_id = #{order.tenantId}
        </if>
        <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
          <if test="order.permissonType == 5 or order.permissonType == 2">
            and ( t1.department_org_id in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
            or
            t1.COMPANY_ORG_ID in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
            )
          </if>
          <if test="order.permissonType == 4 or order.permissonType == 3">
            and t1.department_org_id in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
          </if>
          <if test="order.permissonType == 1">
            and t1.COMPANY_ORG_ID in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
            and (t1.department_org_id is null or t1.department_org_id =0)
          </if>
        </if>
      </where>
    ) a
  </select>

  <!--事件数据月统计-->
  <select id="eventMonthStatistics" resultType="map">
    select count(1) 数量,date_format(date_create,'%Y-%m-%d') 日期
    from sc_work_order_manage t1
    <where>
      t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
      and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
        <if test="order.permissonType == 5 or order.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="order.permissonType == 4 or order.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="order.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
    </where>
    group by date_format(date_create,'%Y-%m-%d')
    order by date_format(date_create,'%Y-%m-%d')
  </select>

  <!--事件数据年统计-->
  <select id="eventYearStatistics" resultType="map">
    select count(1) 数量,date_format(date_create,'%Y-%m') 日期
    from sc_work_order_manage t1
    <where>
      t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
      and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
        <if test="order.permissonType == 5 or order.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="order.permissonType == 4 or order.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="order.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
    </where>
    group by date_format(date_create,'%Y-%m')
    order by date_format(date_create,'%Y-%m')
  </select>

  <!--上报类型统计-->
  <select id="reportStatistics" resultMap="StatisticsMap">
    select  case when t1.event_type=1 then '运维上报' when t1.event_type=2 then '用户上报' when t1.event_type=3 then '设备告警' end statistics_name,
    count(*) statistics_value
    from sc_work_order_manage t1
    <where>
      t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
      and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
        <if test="order.permissonType == 5 or order.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="order.permissonType == 4 or order.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="order.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
    </where>
    group by  t1.event_type
  </select>

  <!--工单状态统计-->
  <select id="orderStatusStatistics" resultMap="StatisticsMap">
    select 工单状态 statistics_name,sum(数量) statistics_value from
    (
      select case when t1.order_status=0 then '未派单' when t1.order_status=1 or t1.order_status=4 then '处理中'
      when t1.order_status=2 then '已完成' when t1.order_status=3 then '终止' end 工单状态,count(*) 数量
      from sc_work_order_manage t1
      <where>
        t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
        and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
        <if test="order.tenantId != null and order.tenantId != ''">
          and t1.tenant_id = #{order.tenantId}
        </if>
        <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
          <if test="order.permissonType == 5 or order.permissonType == 2">
            and ( t1.department_org_id in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
            or
            t1.COMPANY_ORG_ID in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
            )
          </if>
          <if test="order.permissonType == 4 or order.permissonType == 3">
            and t1.department_org_id in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
          </if>
          <if test="order.permissonType == 1">
            and t1.COMPANY_ORG_ID in
            <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
              #{item.orgId}
            </foreach>
            and (t1.department_org_id is null or t1.department_org_id =0)
          </if>
        </if>
      </where>
      group by t1.order_status
    ) a
    group by 工单状态
  </select>

  <!--工单类型统计-->
  <select id="orderTypeStatistics" resultMap="StatisticsMap">
    select  case when t1.order_type_id=1 then '表务工单' when t1.order_type_id=2 then '停通水工单'
    when t1.order_type_id=3 then '抢修工单' when t1.order_type_id=4 then '漏点定位工单'
    when t1.order_type_id=5 then '测漏工单' when t1.order_type_id=6 then '违章跑水工单'
    when t1.order_type_id=10 then '压力调度工单' end statistics_name,
    count(*) statistics_value
    from sc_work_order_manage t1
    <where>
      t1.order_type_id>=1
      and t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
      and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
      <if test="order.tenantId != null and order.tenantId != ''">
        and t1.tenant_id = #{order.tenantId}
      </if>
      <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
        <if test="order.permissonType == 5 or order.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="order.permissonType == 4 or order.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="order.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          and (t1.department_org_id is null or t1.department_org_id =0)
        </if>
      </if>
    </where>
    group by t1.order_type_id
  </select>

  <!--工单时长统计-->
  <select id="orderDurationStatistics" resultMap="StatisticsMap">
    select count(*) statistics_value,duration statistics_name from(
      select case when duration&lt;=1 then 1 when duration>1 and duration&lt;=2 then 2
      when duration>2 and duration&lt;=3 then 3 when duration>3 and duration&lt;=4 then 4
      when duration>4 and duration&lt;=5 then 5 else 6 end duration
      from
      (
      select TIMESTAMPDIFF(DAY,date_create,finish_date) duration
      from sc_work_order_manage t1
      <where>
        (t1.order_status=2 or t1.order_status=3)
        and t1.date_create &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
        and t1.date_create &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
        <if test="order.tenantId != null and order.tenantId != ''">
            and t1.tenant_id = #{order.tenantId}
          </if>
          <if test="order.userType!=null and order.userType!=-999 and order.userType!=-9999 and  order.programItems != null and order.programItems.size() > 0">
            <if test="order.permissonType == 5 or order.permissonType == 2">
              and ( t1.department_org_id in
              <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
              </foreach>
              or
              t1.COMPANY_ORG_ID in
              <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
              </foreach>
              )
            </if>
            <if test="order.permissonType == 4 or order.permissonType == 3">
              and t1.department_org_id in
              <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
              </foreach>
            </if>
            <if test="order.permissonType == 1">
              and t1.COMPANY_ORG_ID in
              <foreach collection="order.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
              </foreach>
              and (t1.department_org_id is null or t1.department_org_id =0)
            </if>
          </if>
        </where>
        )a ) b
      group by duration
  </select>

  <select id="selectByTime" resultMap="BaseResultMap">
    select distinct t1.id,task_no,send_time,address,order_type_id,current_task_name,t1.create_by,t1.update_by,task_desc,order_status,in_process_status,t1.plan_finish_date,t1.finish_date,t1.date_create,t1.date_limit
    from sc_work_order_manage t1
           left join sc_work_flow_log t2 ON t1.id=t2.flow_id and flow_type=2 and find_in_set(#{userId},handle_user_id) and left(flow_result,2)='拒单'
    where (find_in_set(#{userId},current_users)
       or t1.id in(select flow_id from sc_work_flow_log where flow_type=2 and find_in_set(#{userId},handle_user_id)))
      and tenant_id = #{tenantId}
      and (t2.id is null or t1.order_user_id=t2.handle_user_id)
      and t1.date_create between #{startTime} and #{endTime}
    union
    select id,plan_id,plan_date,task_area_name,task_type,current_task_name,create_by,update_by,plan_name,plan_status,in_process_status,plan_end_date,finish_date,date_create,"0"
    from sc_plan_manage
    where (find_in_set(#{userId},current_users)
      or id in(select flow_id from sc_work_flow_log where flow_type=1 and find_in_set(#{userId},handle_user_id)))
      and tenant_id = #{tenantId}
      and date_create between #{startTime} and #{endTime}
    order by send_time desc
  </select>
  <!--派单次数月统计-->
  <select id="dispatchTimesMonthStatistics" resultType="map">
      select count(1) 数量, DATE_FORMAT(send_time,'%Y-%m-%d') 日期
      from sc_work_order_manage t1
      where t1.send_time &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
      and t1.send_time &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
      <if test="list == null">
        and t1.scenes_id= #{order.scenesId,jdbcType=INTEGER}
      </if>
      <if test="list != null">
        and t1.scenes_id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
          #{item,jdbcType=INTEGER}
        </foreach>
      </if>
      <if test="order.deviceId != null">
        and device_id = #{order.deviceId,jdbcType=INTEGER}
      </if>
      group by DATE_FORMAT(send_time,'%Y-%m-%d')
  </select>
  <!--派单次数年统计-->
  <select id="dispatchTimesYearStatistics" resultType="map">
    select count(1) 数量, DATE_FORMAT(send_time,'%Y-%m') 日期
    from sc_work_order_manage t1
    where t1.send_time &gt;= date_format(#{order.startDate,jdbcType=VARCHAR},'%Y-%m-%d')
    and t1.send_time &lt; date_format(#{order.endDate,jdbcType=VARCHAR},'%Y-%m-%d')
    <if test="list == null">
      and t1.scenes_id= #{order.scenesId,jdbcType=INTEGER}
    </if>
    <if test="list != null">
      and t1.scenes_id in
      <foreach collection="list" item="item" open="(" close=")" separator=",">
        #{item,jdbcType=INTEGER}
      </foreach>
    </if>
    <if test="order.deviceId != null">
      and device_id = #{order.deviceId,jdbcType=INTEGER}
    </if>
    group by DATE_FORMAT(send_time,'%Y-%m')
  </select>

  <select id="findWorkFlowById" resultMap="OrderResultMap">
    select
    t1.id, t1.task_no, t1.task_desc, t1.order_user_id, t1.send_time, t1.send_by, t1.plan_finish_date, t1.finish_date,
    t1.date_limit, t1.urgency, t1.address, t1.contact_user, t1.contact_phone, t1.order_time, t1.company_org_id,
    t1.department_org_id, t1.order_status, t1.event_type,case when t1.event_type=1 then '运维上报' when t1.event_type=2 then '用户上报' when t1.event_type=3 then '设备告警' end  event_type_name,
    t1.order_type_id, t1.order_pid, t1.tenant_id,
    t1.process_instance_id, t1.process_def_id, t1.current_task_id, t1.current_users, t1.current_task_name,
    t1.create_by, t1.date_create, t1.update_by, t1.date_update, t1.geo,t1.videos,t1.pictures,
    t1.device_id, t1.scenes_id, t1.device_name, t1.scenes_name,
    t2.flow_detail,t2.create_by detail_create_by,t2.date_create detail_date_create,t2.update_by detail_update_by,t2.date_update detail_date_update
    from sc_work_order_manage t1 left  join sc_work_flow_detail t2
    on t1.id = t2.flow_id and t2.flow_type=2
    where t1.id = #{id,jdbcType=INTEGER}
  </select>
</mapper>