<?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.task.dao.PlanManageMapper" >
  <resultMap id="BaseResultMap" type="com.zcxk.task.dto.PlanManageDto" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="plan_id" property="planId" jdbcType="VARCHAR" />
    <result column="plan_name" property="planName" jdbcType="VARCHAR" />
    <result column="plan_user_id" property="planUserId" jdbcType="VARCHAR" />
    <result column="user_id" property="userId" jdbcType="VARCHAR" />
    <result column="plan_date" property="planDate" jdbcType="TIMESTAMP" />
    <result column="plan_start_date" property="planStartDate" jdbcType="TIMESTAMP" />
    <result column="plan_end_date" property="planEndDate" jdbcType="TIMESTAMP" />
    <result column="task_content" property="taskContent" jdbcType="VARCHAR" />
    <result column="task_area_shape" property="taskAreaShape" jdbcType="VARCHAR" />
    <result column="task_area_name" property="taskAreaName" jdbcType="VARCHAR" />
    <result column="task_type" property="taskType" jdbcType="INTEGER" />
    <result column="plan_status" property="planStatus" jdbcType="INTEGER" />
    <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="company_org_id" property="companyOrgId" jdbcType="VARCHAR" />
    <result column="department_org_id" property="departmentOrgId" jdbcType="VARCHAR" />
    <result column="task_checked" property="taskChecked" jdbcType="LONGVARCHAR" />
    <result column="flow_detail" property="flowDetail" jdbcType="VARCHAR" />
    <result column="task_count" property="taskCount" jdbcType="VARCHAR" />
    <result column="finished_count" property="finishedCount" jdbcType="VARCHAR" />
    <result column="in_process_status" property="inProcessStatus" jdbcType="INTEGER" />
    <result column="interval_count" property="intervalCount" 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="TaskResultMap" type="com.zcxk.task.dto.PlanManageDto" 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>

  <sql id="Base_Column_List" >
    id, plan_id, plan_name, plan_user_id, user_id, plan_date, plan_start_date, plan_end_date, 
    task_content, task_area_shape, task_area_name, task_type, plan_status, tenant_id, 
    process_instance_id, process_def_id, current_task_id, current_users, current_task_name, 
    create_by, date_create, update_by, date_update, company_org_id, department_org_id,
    task_checked,in_process_status
  </sql>

  <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=1
  </select>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from sc_plan_manage
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="selectByPlanId" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select
    <include refid="Base_Column_List" />
    from sc_plan_manage
    where plan_id = #{planId,jdbcType=VARCHAR}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from sc_plan_manage
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <insert id="insertSelective" parameterType="com.zcxk.task.entity.PlanManage" >
    insert into sc_plan_manage
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="planId != null" >
        plan_id,
      </if>
      <if test="planName != null" >
        plan_name,
      </if>
      <if test="planUserId != null" >
        plan_user_id,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="planDate != null" >
        plan_date,
      </if>
      <if test="planStartDate != null" >
        plan_start_date,
      </if>
      <if test="planEndDate != null" >
        plan_end_date,
      </if>
      <if test="taskContent != null" >
        task_content,
      </if>
      <if test="taskAreaShape != null" >
        task_area_shape,
      </if>
      <if test="taskAreaName != null" >
        task_area_name,
      </if>
      <if test="taskType != null" >
        task_type,
      </if>
      <if test="planStatus != null" >
        plan_status,
      </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="companyOrgId != null" >
        company_org_id,
      </if>
      <if test="departmentOrgId != null" >
        department_org_id,
      </if>
      <if test="taskChecked != null" >
        task_checked,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="planId != null" >
        #{planId,jdbcType=VARCHAR},
      </if>
      <if test="planName != null" >
        #{planName,jdbcType=VARCHAR},
      </if>
      <if test="planUserId != null" >
        #{planUserId,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=VARCHAR},
      </if>
      <if test="planDate != null" >
        #{planDate,jdbcType=TIMESTAMP},
      </if>
      <if test="planStartDate != null" >
        #{planStartDate,jdbcType=DATE},
      </if>
      <if test="planEndDate != null" >
        #{planEndDate,jdbcType=DATE},
      </if>
      <if test="taskContent != null" >
        #{taskContent,jdbcType=VARCHAR},
      </if>
      <if test="taskAreaShape != null" >
        #{taskAreaShape,jdbcType=VARCHAR},
      </if>
      <if test="taskAreaName != null" >
        #{taskAreaName,jdbcType=VARCHAR},
      </if>
      <if test="taskType != null" >
        #{taskType,jdbcType=INTEGER},
      </if>
      <if test="planStatus != null" >
        #{planStatus,jdbcType=INTEGER},
      </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="companyOrgId != null" >
        #{companyOrgId,jdbcType=VARCHAR},
      </if>
      <if test="departmentOrgId != null" >
        #{departmentOrgId,jdbcType=VARCHAR},
      </if>
      <if test="taskChecked != null" >
        #{taskChecked,jdbcType=LONGVARCHAR},
      </if>
    </trim>
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.task.entity.PlanManage" >
    update sc_plan_manage
    <set >
      <if test="planId != null" >
        plan_id = #{planId,jdbcType=VARCHAR},
      </if>
      <if test="planName != null" >
        plan_name = #{planName,jdbcType=VARCHAR},
      </if>
      <if test="planUserId != null" >
        plan_user_id = #{planUserId,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        user_id = #{userId,jdbcType=VARCHAR},
      </if>
      <if test="planDate != null" >
        plan_date = #{planDate,jdbcType=TIMESTAMP},
      </if>
      <if test="planStartDate != null" >
        plan_start_date = #{planStartDate,jdbcType=TIMESTAMP},
      </if>
      <if test="planEndDate != null" >
        plan_end_date = #{planEndDate,jdbcType=TIMESTAMP},
      </if>
      <if test="taskContent != null" >
        task_content = #{taskContent,jdbcType=VARCHAR},
      </if>
      <if test="taskAreaShape != null" >
        task_area_shape = #{taskAreaShape,jdbcType=VARCHAR},
      </if>
      <if test="taskAreaName != null" >
        task_area_name = #{taskAreaName,jdbcType=VARCHAR},
      </if>
      <if test="taskType != null" >
        task_type = #{taskType,jdbcType=INTEGER},
      </if>
      <if test="planStatus != null" >
        plan_status = #{planStatus,jdbcType=INTEGER},
      </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="companyOrgId != null" >
        company_org_id = #{companyOrgId,jdbcType=VARCHAR},
      </if>
      <if test="departmentOrgId != null" >
        department_org_id = #{departmentOrgId,jdbcType=VARCHAR},
      </if>
      <if test="taskChecked != null" >
        task_checked = #{taskChecked,jdbcType=LONGVARCHAR},
      </if>
      <if test="inProcessStatus != null" >
        in_process_status = #{inProcessStatus,jdbcType=INTEGER},
      </if>
      <if test="finishDate != null" >
        finish_date = #{finishDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateByPrimaryKey" parameterType="com.zcxk.task.entity.PlanManage" >
    update sc_plan_manage
    set plan_id = #{planId,jdbcType=VARCHAR},
      plan_name = #{planName,jdbcType=VARCHAR},
      plan_user_id = #{planUserId,jdbcType=VARCHAR},
      user_id = #{userId,jdbcType=VARCHAR},
      plan_date = #{planDate,jdbcType=TIMESTAMP},
      plan_start_date = #{planStartDate,jdbcType=TIMESTAMP},
      plan_end_date = #{planEndDate,jdbcType=TIMESTAMP},
      task_content = #{taskContent,jdbcType=VARCHAR},
      task_area_shape = #{taskAreaShape,jdbcType=VARCHAR},
      task_area_name = #{taskAreaName,jdbcType=VARCHAR},
      task_type = #{taskType,jdbcType=INTEGER},
      plan_status = #{planStatus,jdbcType=INTEGER},
      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},
      company_org_id = #{companyOrgId,jdbcType=VARCHAR},
      department_org_id = #{departmentOrgId,jdbcType=VARCHAR},
      task_checked = #{taskChecked,jdbcType=LONGVARCHAR},
      in_process_status = #{inProcessStatus,jdbcType=INTEGER},
      finish_date=#{finishDate,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!--分页查询-->
  <select id="findPage" resultMap="TaskResultMap">
    select
    t1.id, t1.plan_id, plan_name, plan_user_id, user_id, plan_date, plan_start_date, plan_end_date,
    task_content, task_area_shape, task_area_name, task_type, plan_status, tenant_id,
    t1.process_instance_id, process_def_id, current_task_id, current_users, current_task_name,
    t1.create_by, t1.date_create, t1.update_by, t1.date_update, company_org_id, department_org_id,task_checked,
    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_plan_manage t1 left  join sc_work_flow_detail t2
    on t1.id = t2.flow_id and t2.flow_type=1
    <where>
      <if test="plan.tenantId != null and plan.tenantId != ''">
        and t1.tenant_id = #{plan.tenantId}
      </if>
      <if test="plan.key != null and plan.key != ''">
        and (t1.plan_id like CONCAT('%',#{plan.key},'%') or t1.plan_name like CONCAT('%',#{plan.key},'%'))
      </if>
      <if test="plan.userType!=null and plan.userType!=-999 and plan.userType!=-9999 and  plan.programItems != null and plan.programItems.size() > 0">
        <if test="plan.permissonType == 5 or plan.permissonType == 2">
          and ( t1.department_org_id in
          <foreach collection="plan.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          or
          t1.COMPANY_ORG_ID in
          <foreach collection="plan.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
          )
        </if>
        <if test="plan.permissonType == 4 or plan.permissonType == 3">
          and t1.department_org_id in
          <foreach collection="plan.programItems" item="item" open="(" close=")" separator=",">
            #{item.orgId}
          </foreach>
        </if>
        <if test="plan.permissonType == 1">
          and t1.COMPANY_ORG_ID in
          <foreach collection="plan.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="plan.planStatus != null">
        and t1.plan_status =  #{plan.planStatus,jdbcType=INTEGER}
      </if>
      <if test="plan.planStatus == null">
        and t1.plan_status != 0
      </if>
      <if test="plan.taskType != null">
        and t1.task_type = #{plan.taskType,jdbcType=INTEGER}
      </if>
      <if test="plan.currentUsers != null">
        and FIND_IN_SET(#{plan.currentUsers,jdbcType=VARCHAR},t1.current_users)
      </if>
      <if test="plan.startDate != null and plan.endDate != null">
        and t1.date_create &gt;= date_format(#{plan.startDate,jdbcType=VARCHAR},'%Y-%c-%d')
        and t1.date_create &lt;= DATE_ADD(date_format(#{plan.endDate,jdbcType=VARCHAR},'%Y-%c-%d'),INTERVAL 1 DAY)
      </if>
    </where>
    order by t1.date_create desc
  </select>

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

  <!--查询流程节点-->
  <select id="selectTaskName" resultType="map" parameterType="map">
    select distinct current_task_name 任务节点,task_type 任务类型 from sc_plan_manage
    where INSTR(current_users,#{currentUsers,jdbcType=VARCHAR})
    <if test="taskType != null">
      and task_type = #{taskType,jdbcType=INTEGER}
    </if>
    order by task_type
  </select>

  <!--删除-->
  <delete id="batchDelete">
    delete from  sc_plan_manage
    where plan_status = 0
    and id in
    <foreach collection="ids" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </delete>

  <select id="selectPlanDetail" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select t1.*,t2.flow_detail
    from sc_plan_manage t1
    left join sc_work_flow_detail t2 on t1.id=t2.flow_id and t2.flow_type=1
    where t1.id = #{id,jdbcType=INTEGER}
  </select>

  <select id="selectTaskFinished" resultMap="BaseResultMap" >
    select task_type,sum(task_count) task_count,sum(case when plan_status=2 or plan_status=3 then task_count else 0 end) finished_count
    from(
      select task_type,plan_status,count(1) task_count
      from sc_plan_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 task_type,plan_status
    )tab1
    group by task_type
  </select>

  <select id="selectTaskFinishedByDate" resultMap="BaseResultMap" >
    select task_type,sum(task_count) task_count,sum(case when plan_status=2 or plan_status=3 then task_count else 0 end) finished_count
    from(
      select task_type,plan_status,count(1) task_count
      from sc_plan_manage t1
      where
      <if test="statsType == 0">
        t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{startDate}, '-1') + INTERVAL 1 MONTH
      </if>
      <if test="statsType == 1">
        t1.date_create >= concat(#{startDate}, '-1-1') and t1.date_create &lt; concat(#{startDate}, '-1-1') + INTERVAL 1 YEAR
      </if>
      <if test="statsType == 2">
        t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{endDate}, '-1') + INTERVAL 1 MONTH
      </if>
      <if test="tenantId != null and tenantId != ''">
        and t1.tenant_id=#{tenantId}
      </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 task_type,plan_status
    )tab1
    group by task_type
  </select>

  <select id="selectTaskCondition" resultMap="BaseResultMap" >
    select count(1) task_count,ifnull(sum(case when plan_status=2 or plan_status=3 then 1 else 0 end),0) finished_count
    from sc_plan_manage t1
    where
    <if test="statsType == 0">
      t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{startDate}, '-1') + interval 1 month
    </if>
    <if test="statsType == 1">
      t1.date_create >= concat(#{startDate}, '-1-1') and t1.date_create &lt; concat(#{startDate}, '-1-1') + interval 1 year
    </if>
    <if test="statsType == 2">
      t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{endDate}, '-1') + interval 1 month
    </if>
    <if test="tenantId != null and tenantId != ''">
      and t1.tenant_id=#{tenantId}
    </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>
  </select>
  <select id="selectTaskChangeTrend" resultType="com.zcxk.task.dto.TaskChangeTendDto" >
    select
      tab1.dateCreate,
      right(tab1.dateCreate,2) dateLabel,
      ifnull(TaskCount, 0) TaskCount
      <if test="maintainerCount != 0">
        ,ifnull(TaskCount, 0) / #{maintainerCount} taskCountAvg
      </if>
    from(
        select
        <if test="statsType == 0 or statsType == 2">
          date_format(date_add(concat(#{startDate}, '-1'),interval rownum day),'%Y-%m-%d') dateCreate
        </if>
        <if test="statsType == 1">
          date_format(date_add(concat(#{startDate}, '-1-1'),interval rownum month),'%Y-%m') dateCreate
        </if>
        from(
            select @rowNum :=@rowNum + 1 rownum
            from sc_gps_data_his a,(select @rowNum :=- 1) b limit #{intervalCount}
        ) duration
    ) tab1
    left join (
      select
      <if test="statsType == 0 or statsType == 2">
        date_format(t1.date_create, '%Y-%m-%d') dateCreate
      </if>
      <if test="statsType == 1">
        date_format(t1.date_create, '%Y-%m') dateCreate
      </if>,count(1) TaskCount
      from sc_plan_manage t1
      where
      <if test="statsType == 0">
        t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{startDate}, '-1') + interval 1 month
      </if>
      <if test="statsType == 1">
        t1.date_create >= concat(#{startDate}, '-1-1') and t1.date_create &lt; concat(#{startDate}, '-1-1') + interval 1 year
      </if>
      <if test="statsType == 2">
        t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{endDate}, '-1') + interval 1 month
      </if>
      <if test="tenantId != null and tenantId != ''">
        and t1.tenant_id=#{tenantId}
      </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>
      <if test="statsType == 0 or statsType == 2">
        group by date_format(t1.date_create, '%Y-%m-%d')
      </if>
      <if test="statsType == 1">
        group by date_format(t1.date_create, '%Y-%m')
      </if>
    ) tab2 on tab1.dateCreate = tab2.dateCreate
    <if test="statsType == 0 or statsType == 2">
      where tab1.dateCreate &lt;=date_format(now(),'%Y-%m-%d')
    </if>
    <if test="statsType == 1">
      where tab1.dateCreate &lt;=date_format(now(),'%Y-%m')
    </if>
    order by tab1.dateCreate
  </select>

  <select id="selectTaskDuration" resultMap="BaseResultMap" >
    SELECT timestampdiff(day,date_create,finish_date) interval_count,count(1) finished_count
    FROM sc_plan_manage t1
    where (plan_status=2 or plan_status=3) and
    <if test="statsType == 0">
      t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{startDate}, '-1') + interval 1 month
    </if>
    <if test="statsType == 1">
      t1.date_create >= concat(#{startDate}, '-1-1') and t1.date_create &lt; concat(#{startDate}, '-1-1') + interval 1 year
    </if>
    <if test="statsType == 2">
      t1.date_create >= concat(#{startDate}, '-1') and t1.date_create &lt; concat(#{endDate}, '-1') + interval 1 month
    </if>
    <if test="tenantId != null and tenantId != ''">
      and t1.tenant_id=#{tenantId}
    </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 timestampdiff(day,date_create,finish_date)
  </select>

  <select id="selectWorkFlowById" resultMap="TaskResultMap">
    select
    t1.id, t1.plan_id, plan_name, plan_user_id, user_id, plan_date, plan_start_date, plan_end_date,
    task_content, task_area_shape, task_area_name, task_type, plan_status, tenant_id,
    t1.process_instance_id, process_def_id, current_task_id, current_users, current_task_name,
    t1.create_by, t1.date_create, t1.update_by, t1.date_update, company_org_id, department_org_id,task_checked,
    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_plan_manage t1 left  join sc_work_flow_detail t2
    on t1.id = t2.flow_id and t2.flow_type=1  where t1.id = #{id,jdbcType=INTEGER}
  </select>
</mapper>