<?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.ruoyi.system.mapper.grid.GridRegionExtMapper">
  <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.grid.GridRegion">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="wg_type" jdbcType="VARCHAR" property="wgType" />
    <result column="wg_code" jdbcType="VARCHAR" property="wgCode" />
    <result column="wg_name" jdbcType="VARCHAR" property="wgName" />
    <result column="wg_parent_id" jdbcType="VARCHAR" property="wgParentId" />
    <result column="wg_area" jdbcType="VARCHAR" property="wgArea" />
    <result column="wg_centre_coordinate" jdbcType="VARCHAR" property="wgCentreCoordinate" />
    <result column="wg_principal" jdbcType="VARCHAR" property="wgPrincipal" />
    <result column="create_time" jdbcType="VARCHAR" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="VARCHAR" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="level" jdbcType="VARCHAR" property="level" />
    <result column="is_valid" jdbcType="VARCHAR" property="isValid" />
    <result column="ylzd1" jdbcType="VARCHAR" property="ylzd1" />
    <result column="ylzd2" jdbcType="VARCHAR" property="ylzd2" />
    <result column="ylzd3" jdbcType="VARCHAR" property="ylzd3" />
    <result column="ylzd4" jdbcType="VARCHAR" property="ylzd4" />
    <result column="ylzd5" jdbcType="VARCHAR" property="ylzd5" />
    <result column="wg_purpose" jdbcType="VARCHAR" property="wgPurpose" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.ruoyi.system.domain.grid.GridRegion">
    <result column="wg_coordinate" jdbcType="LONGVARCHAR" property="wgCoordinate" />
  </resultMap>

  <resultMap extends="ResultMapWithBLOBs" id="BaseResultMap2" type="com.ruoyi.system.domain.grid.ext.GridRegionExt">

  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, wg_type, wg_code, wg_name, wg_parent_id, wg_area, wg_centre_coordinate,
    wg_principal, create_time, create_by, update_time, update_by, level, is_valid, ylzd1,
    ylzd2, ylzd3, ylzd4, ylzd5, wg_purpose
  </sql>
  <sql id="Blob_Column_List">
    wg_coordinate
  </sql>
  <select id="getThirdWgList"  parameterType="java.util.Map" resultMap="BaseResultMap">
    SELECT
      wg_code,
      wg_name,
      level,
      id
    FROM
      grid_region
    WHERE
        wg_code IN (
        SELECT
          wg_parent_id
        FROM
          `grid_region`
        WHERE
            wg_code IN
        <foreach collection="wgcodeList" open="(" separator="," close=")" item="wgCode" index="i">
          #{wgCode,jdbcType=VARCHAR}
        </foreach>
      );

  </select>

  <select id="getEconomyList"  resultMap="BaseResultMap2">
    SELECT
    bw.*,
    wcei.year,
    wcei.per_mu as perMu
    FROM
    grid_region bw
    left join (select * from grid_company_land  where is_valid = 1) as wcl
    on wcl.land_id = bw.id
    left join (select * from grid_company_economy_info where is_valid = 1) as wcei
    on wcl.company_id = wcei.company_id
    <where>
      and bw.is_valid = 1
      and bw.wg_type = '4'
      <if test="wgName != null and wgName != ''">
        and bw.wg_name like concat('%',#{wgName},'%')
      </if>
      <if test="level != null and level != ''">
        and bw.level = #{level}
      </if>
      <if test="purpose != null and purpose != ''">
        and bw.wg_purpose = #{purpose}
      </if>
      <if test="perMuRangeStart != null and perMuRangeStart != '' and perMuRangeEnd != null and perMuRangeEnd != ''">
        and wcei.per_mu &gt;= #{perMuRangeStart}
        and wcei.per_mu &lt;= #{perMuRangeEnd}
      </if>
      <if test="(perMuRangeStart == null or perMuRangeStart == '') and perMuRangeEnd != null and perMuRangeEnd != ''">
        and wcei.per_mu &lt; #{perMuRangeEnd}
      </if>
      <if test="perMuRangeStart != null and perMuRangeStart != '' and (perMuRangeEnd == null or perMuRangeEnd == '') ">
        and wcei.per_mu &gt; #{perMuRangeStart}
      </if>
      <if test="year != null and year != ''">
        and wcei.year = #{year}
      </if>
    </where>
    group by bw.id
    order by bw.wg_code
  </select>

  <select id="getEconomyPointList" resultType="com.ruoyi.system.domain.grid.vo.SimpleWgItem">
    SELECT
    bw.id as id, bw.wg_code as wgCode, bw.wg_name as wgName, bw.wg_coordinate as wgCoordinate, bw.wg_centre_coordinate as wgCenterCoordinate
    FROM
    grid_region bw
    left join (select * from grid_company_land  where is_valid = 1) as wcl
    on wcl.land_id = bw.id
    left join (select * from grid_company_economy_info where is_valid = 1) as wcei
    on wcl.company_id = wcei.company_id
    <where>
      and bw.is_valid = 1
      and bw.wg_type = '4'
      <if test="perMuRangeStart != null and perMuRangeStart != '' and perMuRangeEnd != null and perMuRangeEnd != ''">
        and wcei.per_mu &gt;= #{perMuRangeStart}
        and wcei.per_mu &lt;= #{perMuRangeEnd}
      </if>
      <if test="(perMuRangeStart == null or perMuRangeStart == '') and perMuRangeEnd != null and perMuRangeEnd != ''">
        and wcei.per_mu &lt; #{perMuRangeEnd}
      </if>
      <if test="perMuRangeStart != null and perMuRangeStart != '' and (perMuRangeEnd == null or perMuRangeEnd == '') ">
        and wcei.per_mu &gt; #{perMuRangeStart}
      </if>
      <if test="year != null and year != ''">
        and wcei.year = #{year}
      </if>
    </where>
    group by bw.id
    order by bw.wg_code
  </select>

  <select id="getEconomyCount"  resultType="java.lang.Integer">
    SELECT
    count(DISTINCT bw.id)
    FROM
    grid_region bw
    left join (select * from grid_company_land  where is_valid = 1) as wcl
    on wcl.land_id = bw.id
    left join (select * from grid_company_economy_info where is_valid = 1) as wcei
    on wcl.company_id = wcei.company_id
    <where>
      and bw.is_valid = 1
      and bw.wg_type = '4'
      <if test="wgName != null and wgName != ''">
        and bw.wg_name like concat('%',#{wgName},'%')
      </if>
      <if test="level != null and level != ''">
        and bw.level = #{level}
      </if>
      <if test="purpose != null and purpose != ''">
        and bw.wg_purpose = #{purpose}
      </if>
      <if test="perMuRangeStart != null and perMuRangeStart != '' and perMuRangeEnd != null and perMuRangeEnd != ''">
        and wcei.per_mu &gt;= #{perMuRangeStart}
        and wcei.per_mu &lt;= #{perMuRangeEnd}
      </if>
      <if test="(perMuRangeStart == null or perMuRangeStart == '') and perMuRangeEnd != null and perMuRangeEnd != ''">
        and wcei.per_mu &lt; #{perMuRangeEnd}
      </if>
      <if test="perMuRangeStart != null and perMuRangeStart != '' and (perMuRangeEnd == null or perMuRangeEnd == '') ">
        and wcei.per_mu &gt; #{perMuRangeStart}
      </if>
      <if test="year != null and year != ''">
        and wcei.year = #{year}
      </if>
    </where>
  </select>

  <select id="selectThirdWgByBaseId"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultMap="BaseResultMap">

    select wg.wg_name,wg.wg_code,wg.wg_parent_id,u.ylzd1 from
    grid_region_user u
    left join grid_region wg on wg.wg_code=u.wg_id
    where wg.wg_type=1 and wg.is_valid='1'
    and wg.`level`='3'
    <if test="TkExample.wgCode != null and TkExample.wgCode !='' ">
      and wg.wg_parent_id=#{TkExample.wgCode}
    </if>
    <if test="TkExample.userIds != null and TkExample.userIds.size()>0 ">
      and u.user_id in
      <foreach close=")" collection="TkExample.userIds" item="listItem" open="(" separator=",">
        #{listItem}
      </foreach>
    </if>
    and u.is_valid='1' order by wg.wg_code asc
    <if test="TkExample.startLimit != null
        and TkExample.endLimit != null">
      limit #{TkExample.startLimit,jdbcType=INTEGER},#{TkExample.endLimit,jdbcType=INTEGER}
    </if>
  </select>

  <select id ="selectSecWgTaskStat" parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="com.ruoyi.system.domain.task.dto.TKGridRegion">
    select count(1) sumCount
         ,sum(case when tk.tk_status=50 then 1 or 0 end) doneCount
         ,tk.wg_parent_id wgCode
         ,wg.wg_name wgName
    from grid_region wg
           inner join tk_common_task tk on tk.wg_parent_id=wg.wg_code
    where 1=1
      and DATE_FORMAT(tk.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
      and wg.wg_type=1 and wg.is_valid='1'
      and wg.`level`='2'
    group by tk.wg_parent_id
    union
    select count(1) sumCount
         ,sum(case when tk.tk_status=50 then 1 or 0 end) doneCount
         ,tk.wg_code wgCode
         ,wg.wg_name wgName
    from grid_region wg
           inner join tk_common_task tk on tk.wg_code=wg.wg_code
    where 1=1
      and DATE_FORMAT(tk.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
      and wg.wg_type=1 and wg.is_valid='1'
      and wg.`level`='2'
    group by tk.wg_code
  </select>

  <select id="countThirdWgByBaseId"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="java.lang.Long">
    select count(1) from
    grid_region_user u
    left join grid_region wg on wg.wg_code=u.wg_id
    where wg.wg_type=1 and wg.is_valid='1'
    and wg.`level`='3'
    <if test="TkExample.wgCode != null and TkExample.wgCode !='' ">
      and wg.wg_parent_id=#{TkExample.wgCode}
    </if>
    <if test="TkExample.userIds != null and TkExample.userIds.size()>0 ">
      and u.user_id in
      <foreach close=")" collection="TkExample.userIds" item="listItem" open="(" separator=",">
        #{listItem}
      </foreach>
    </if>
    and u.is_valid='1'
  </select>

  <select id="countMatterId" parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="java.lang.Long">
    select count(distinct(t.matter_id))
    from tk_common_task  t
    where 1=1
      and  DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
  </select>

  <select id="countAllMatterStat"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="java.lang.Long">
    select count(1) from
      tk_common_task  t
    where 1=1 and
      DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
  </select>

  <select id="selectMatterStat"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="com.ruoyi.system.domain.task.dto.TKGridRegion">
    select t.matter_name as matterName,t.matter_id as matterId,
           count(1) sumCount,sum(case when t.tk_status = 50 then 1 else 0 end) doneCount,
           max(case when t.tk_status = 50 then
                      (case when date_format(t.assigned_time,'%H:%i:%s')='00:00:00' then  UNIX_TIMESTAMP(date_add(t.assigned_time,interval 23 hour))
                            else UNIX_TIMESTAMP(t.assigned_time) end )-
                      (select UNIX_TIMESTAMP(max(a.create_time)) from tk_common_assigned a where a.common_tk_id=t.id ) end) maxTime,
           min(case when t.tk_status = 50 then
                      (case when date_format(t.assigned_time,'%H:%i:%s')='00:00:00' then  UNIX_TIMESTAMP(date_add(t.assigned_time,interval 23 hour))
                            else UNIX_TIMESTAMP(t.assigned_time) end )-
                      (select UNIX_TIMESTAMP(max(a.create_time)) from tk_common_assigned a where a.common_tk_id=t.id ) end) minTime,
           AVG(case when t.tk_status = 50 then
                      (case when date_format(t.assigned_time,'%H:%i:%s')='00:00:00' then  UNIX_TIMESTAMP(date_add(t.assigned_time,interval 23 hour))
                            else UNIX_TIMESTAMP(t.assigned_time) end )-
                      (select UNIX_TIMESTAMP(max(a.create_time)) from tk_common_assigned a where a.common_tk_id=t.id ) end) avgTime
    from tk_common_task t
    where 1=1
      and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{TkExample.startDate,jdbcType=DATE},'%Y-%m')
    group by t.matter_id order by count(1) desc
    limit #{TkExample.startLimit,jdbcType=INTEGER},#{TkExample.endLimit,jdbcType=INTEGER};
  </select>

  <select id="selectMatterDetailStat"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="com.ruoyi.system.domain.task.dto.TKGridRegion">
    select t.wg_parent_name wgMaster,count(1) sumCount,sum(case when t.tk_status = 50 then 1 else 0 end) doneCount
    from tk_common_task  t
    where 1=1
      and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
      and t.matter_id=#{matterId,jdbcType=VARCHAR}  group by t.wg_parent_name order by count(1) desc
    limit #{startLimit,jdbcType=INTEGER},#{endLimit,jdbcType=INTEGER}
  </select>

  <select id="countAllMatterDetail"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="java.lang.Long">
    select count(1)
    from tk_common_task  t
    where 1=1
      and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
      and t.matter_id=#{matterId,jdbcType=VARCHAR}
  </select>

  <select id ="selectComeFrom3" parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="com.ruoyi.system.domain.task.TkCommonTask">
    select t.tk_name tkName
         ,t.tk_detail tkDetail
         ,t.perform_user_id performUserId
         ,t.guishu_code guishuCode
         ,t.perform_user_name performUserName
         ,r.user_name standby4
         ,t.assigned_end_time assignedEndTime
         ,t.tk_status tkStatus,
      (case when t.tk_status = 50 then
              (case when date_format(t.assigned_time,'%H:%i:%s')='00:00:00' then  UNIX_TIMESTAMP(date_add(t.assigned_time,interval 23 hour))
                    else UNIX_TIMESTAMP(t.assigned_time) end )-
              (select UNIX_TIMESTAMP(max(a.create_time)) from tk_common_assigned a where a.common_tk_id=t.id ) end) as standby5
    from tk_common_task  t
           left join tk_common_receive r on t.id= r.common_tk_id
    where t.come_from='3'
      and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
    limit #{startLimit,jdbcType=INTEGER},#{endLimit,jdbcType=INTEGER};
  </select>

  <select id ="countComeFrom3" parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="java.lang.Long">
    select count(1) sumCount
    from tk_common_task  t
    where t.come_from='3'
      and DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
  </select>

  <select id ="selectDeptStat" parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="com.ruoyi.system.domain.task.dto.TKGridRegion">
    select
    t.perform_user_id performUserId
    ,t.perform_user_name performUserName
    ,count(1) reportNum
    ,sum(case when t.tk_status = 50 then 1 else 0 end) doneNum
    from tk_common_task  t
    where 1=1
    <if test="TkExample.type == 2 ">
      and t.come_from='3'
    </if>
    <if test="TkExample.type == 1 ">

    </if>
    <if test="TkExample.type == 3 ">
      and (t.come_from  is null  or t.come_from !='3')
    </if>

    <if test="TkExample.yearMonthDay != null and TkExample.yearMonthDay != '' ">
      and DATE_FORMAT(t.release_time ,'%Y-%m-%d') =#{TkExample.yearMonthDay}
    </if>
    <if test="TkExample.yearMonth != null and TkExample.yearMonth != '' ">
      and DATE_FORMAT(t.release_time ,'%Y-%m') =#{TkExample.yearMonth}
    </if>
    and t.perform_user_id is not null
    group by t.perform_user_id order by t.perform_user_id desc
  </select>

  <select id="selectMatterId" parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="com.ruoyi.system.domain.task.dto.TKGridRegion">

    select t.matter_id as matterId
    from tk_common_task  t
    where 1=1
      and  DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
    group by t.matter_id order by t.matter_id desc
    limit #{startLimit,jdbcType=INTEGER},#{endLimit,jdbcType=INTEGER}

  </select>

  <select id="countMatterDetailStat"
          parameterType="com.ruoyi.system.domain.task.dto.TKGridRegionExample"
          resultType="java.lang.Long">
    select count(DISTINCT(t.wg_parent_name)) from
      tk_common_task  t
    where 1=1 and
      DATE_FORMAT(t.release_time ,'%Y-%m')=DATE_FORMAT(#{startDate,jdbcType=DATE},'%Y-%m')
      and t.matter_id=#{matterId,jdbcType=VARCHAR}
  </select>
</mapper>
