<?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.lightinit.hsdataplatform.mapper.SectorMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdataplatform.entity.Sector">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="parent_id" jdbcType="BIGINT" property="parentId" />
    <result column="district_id" jdbcType="BIGINT" property="districtId" />
    <result column="sector_level" jdbcType="INTEGER" property="sectorLevel" />
    <result column="sector_name" jdbcType="VARCHAR" property="sectorName" />
    <result column="short_name" jdbcType="VARCHAR" property="shortName" />
    <result column="code_type" jdbcType="VARCHAR" property="codeType" />
    <result column="sector_code" jdbcType="VARCHAR" property="sectorCode" />
    <result column="contact" jdbcType="VARCHAR" property="contact" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="fax" jdbcType="VARCHAR" property="fax" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="BIGINT" property="createdBy" />
    <result column="deleted_at" jdbcType="TIMESTAMP" property="deletedAt" />
    <result column="deleted_by" jdbcType="BIGINT" property="deletedBy" />
  </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, parent_id, district_id, sector_level, sector_name, short_name, code_type, sector_code, 
    contact, phone, email, fax, created_at, created_by, deleted_at, deleted_by
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdataplatform.entity.SectorExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from sector
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sector
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from sector
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdataplatform.entity.SectorExample">
    delete from sector
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdataplatform.entity.Sector">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into sector (parent_id, district_id, sector_level, 
      sector_name, short_name, code_type, 
      sector_code, contact, phone, 
      email, fax, created_at, 
      created_by, deleted_at, deleted_by
      )
    values (#{parentId,jdbcType=BIGINT}, #{districtId,jdbcType=BIGINT}, #{sectorLevel,jdbcType=INTEGER}, 
      #{sectorName,jdbcType=VARCHAR}, #{shortName,jdbcType=VARCHAR}, #{codeType,jdbcType=VARCHAR}, 
      #{sectorCode,jdbcType=VARCHAR}, #{contact,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{fax,jdbcType=VARCHAR}, #{createdAt,jdbcType=TIMESTAMP}, 
      #{createdBy,jdbcType=BIGINT}, #{deletedAt,jdbcType=TIMESTAMP}, #{deletedBy,jdbcType=BIGINT}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdataplatform.entity.Sector">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into sector
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="parentId != null">
        parent_id,
      </if>
      <if test="districtId != null">
        district_id,
      </if>
      <if test="sectorLevel != null">
        sector_level,
      </if>
      <if test="sectorName != null">
        sector_name,
      </if>
      <if test="shortName != null">
        short_name,
      </if>
      <if test="codeType != null">
        code_type,
      </if>
      <if test="sectorCode != null">
        sector_code,
      </if>
      <if test="contact != null">
        contact,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="fax != null">
        fax,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="deletedBy != null">
        deleted_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="parentId != null">
        #{parentId,jdbcType=BIGINT},
      </if>
      <if test="districtId != null">
        #{districtId,jdbcType=BIGINT},
      </if>
      <if test="sectorLevel != null">
        #{sectorLevel,jdbcType=INTEGER},
      </if>
      <if test="sectorName != null">
        #{sectorName,jdbcType=VARCHAR},
      </if>
      <if test="shortName != null">
        #{shortName,jdbcType=VARCHAR},
      </if>
      <if test="codeType != null">
        #{codeType,jdbcType=VARCHAR},
      </if>
      <if test="sectorCode != null">
        #{sectorCode,jdbcType=VARCHAR},
      </if>
      <if test="contact != null">
        #{contact,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="fax != null">
        #{fax,jdbcType=VARCHAR},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        #{deletedBy,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdataplatform.entity.SectorExample" resultType="java.lang.Long">
    select count(*) from sector
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update sector
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.parentId != null">
        parent_id = #{record.parentId,jdbcType=BIGINT},
      </if>
      <if test="record.districtId != null">
        district_id = #{record.districtId,jdbcType=BIGINT},
      </if>
      <if test="record.sectorLevel != null">
        sector_level = #{record.sectorLevel,jdbcType=INTEGER},
      </if>
      <if test="record.sectorName != null">
        sector_name = #{record.sectorName,jdbcType=VARCHAR},
      </if>
      <if test="record.shortName != null">
        short_name = #{record.shortName,jdbcType=VARCHAR},
      </if>
      <if test="record.codeType != null">
        code_type = #{record.codeType,jdbcType=VARCHAR},
      </if>
      <if test="record.sectorCode != null">
        sector_code = #{record.sectorCode,jdbcType=VARCHAR},
      </if>
      <if test="record.contact != null">
        contact = #{record.contact,jdbcType=VARCHAR},
      </if>
      <if test="record.phone != null">
        phone = #{record.phone,jdbcType=VARCHAR},
      </if>
      <if test="record.email != null">
        email = #{record.email,jdbcType=VARCHAR},
      </if>
      <if test="record.fax != null">
        fax = #{record.fax,jdbcType=VARCHAR},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createdBy != null">
        created_by = #{record.createdBy,jdbcType=BIGINT},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.deletedBy != null">
        deleted_by = #{record.deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update sector
    set id = #{record.id,jdbcType=BIGINT},
      parent_id = #{record.parentId,jdbcType=BIGINT},
      district_id = #{record.districtId,jdbcType=BIGINT},
      sector_level = #{record.sectorLevel,jdbcType=INTEGER},
      sector_name = #{record.sectorName,jdbcType=VARCHAR},
      short_name = #{record.shortName,jdbcType=VARCHAR},
      code_type = #{record.codeType,jdbcType=VARCHAR},
      sector_code = #{record.sectorCode,jdbcType=VARCHAR},
      contact = #{record.contact,jdbcType=VARCHAR},
      phone = #{record.phone,jdbcType=VARCHAR},
      email = #{record.email,jdbcType=VARCHAR},
      fax = #{record.fax,jdbcType=VARCHAR},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=BIGINT}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdataplatform.entity.Sector">
    update sector
    <set>
      <if test="parentId != null">
        parent_id = #{parentId,jdbcType=BIGINT},
      </if>
      <if test="districtId != null">
        district_id = #{districtId,jdbcType=BIGINT},
      </if>
      <if test="sectorLevel != null">
        sector_level = #{sectorLevel,jdbcType=INTEGER},
      </if>
      <if test="sectorName != null">
        sector_name = #{sectorName,jdbcType=VARCHAR},
      </if>
      <if test="shortName != null">
        short_name = #{shortName,jdbcType=VARCHAR},
      </if>
      <if test="codeType != null">
        code_type = #{codeType,jdbcType=VARCHAR},
      </if>
      <if test="sectorCode != null">
        sector_code = #{sectorCode,jdbcType=VARCHAR},
      </if>
      <if test="contact != null">
        contact = #{contact,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="fax != null">
        fax = #{fax,jdbcType=VARCHAR},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        deleted_by = #{deletedBy,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdataplatform.entity.Sector">
    update sector
    set parent_id = #{parentId,jdbcType=BIGINT},
      district_id = #{districtId,jdbcType=BIGINT},
      sector_level = #{sectorLevel,jdbcType=INTEGER},
      sector_name = #{sectorName,jdbcType=VARCHAR},
      short_name = #{shortName,jdbcType=VARCHAR},
      code_type = #{codeType,jdbcType=VARCHAR},
      sector_code = #{sectorCode,jdbcType=VARCHAR},
      contact = #{contact,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      fax = #{fax,jdbcType=VARCHAR},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="queryList" parameterType="com.lightinit.hsdataplatform.model.admin.SectorCountSearchModel" resultType="com.lightinit.hsdataplatform.model.admin.SectorCountListModel">
   SELECT rdata.id
, rdata.`sector_name`
, rdata.`viewTimesData`
, rdata.`applyCountdata`
, rdata.`followData`

, rapi.`viewTimesApi`
, rapi.`applyCountApi`
, rapi.`followApi`

, rmicroapp.`viewTimesMicroapp`
, rmicroapp.`applyCountMicroapp`
, rmicroapp.`followMicroapp`

FROM (
	SELECT s.id
	, s.`sector_name`
	, SUM(rvt1.`view_times`) viewTimesData
	, SUM(apply1.`apply_times`) applyCountdata
	, SUM(flow1.`follow_times`) followData
	FROM sector s
	LEFT JOIN sector s2 ON s2.`sector_code` LIKE CONCAT(s.`sector_code`, '%')
	LEFT JOIN resource_data rd ON s2.`id` = rd.`belong_to`
	LEFT JOIN resource r ON rd.`id` = r.`id`
	LEFT JOIN resource_view_times rvt1 ON r.`id` = rvt1.`id`
	LEFT JOIN (
		SELECT p.`resource_id` resource_id, COUNT(ps.`id`) apply_times
		FROM publish_info p
		JOIN publish_setting ps ON p.`id` = ps.`publish_info_id`
		JOIN resource_auth ra ON ps.`id` = ra.`publish_setting_id`
		GROUP BY p.`resource_id`
	) apply1 ON r.`id` = apply1.`resource_id`
	LEFT JOIN (
		SELECT p.`resource_id` resource_id, COUNT(rf.`id`) follow_times
		FROM publish_info p
		JOIN publish_setting ps ON p.`id` = ps.`publish_info_id`
		JOIN resource_follow rf ON ps.`id` = rf.`publish_setting_id`
		GROUP BY p.`resource_id`
	) flow1 ON r.`id` = flow1.`resource_id`
	WHERE s.`parent_id` IS NULL
	GROUP BY s.id, s.`sector_name`
) rdata
JOIN (
	SELECT s.id
	, s.`sector_name`
	, SUM(rvt1.`view_times`) viewTimesApi
	, SUM(apply1.`apply_times`) applyCountApi
	, SUM(flow1.`follow_times`) followApi
	FROM sector s
	LEFT JOIN sector s2 ON s2.`sector_code` LIKE CONCAT(s.`sector_code`, '%')
	LEFT JOIN resource_api rd ON s2.`id` = rd.`provider`
	LEFT JOIN resource r ON rd.`id` = r.`id`
	LEFT JOIN resource_view_times rvt1 ON r.`id` = rvt1.`id`
	LEFT JOIN (
		SELECT p.`resource_id` resource_id, COUNT(ps.`id`) apply_times
		FROM publish_info p
		JOIN publish_setting ps ON p.`id` = ps.`publish_info_id`
		JOIN resource_auth ra ON ps.`id` = ra.`publish_setting_id`
		GROUP BY p.`resource_id`
	) apply1 ON r.`id` = apply1.`resource_id`
	LEFT JOIN (
		SELECT p.`resource_id` resource_id, COUNT(rf.`id`) follow_times
		FROM publish_info p
		JOIN publish_setting ps ON p.`id` = ps.`publish_info_id`
		JOIN resource_follow rf ON ps.`id` = rf.`publish_setting_id`
		GROUP BY p.`resource_id`
	) flow1 ON r.`id` = flow1.`resource_id`
	WHERE s.`parent_id` IS NULL
	GROUP BY s.id, s.`sector_name`
) rapi ON rdata.id = rapi.id
JOIN (
	SELECT s.id
	, s.`sector_name`
	, SUM(rvt1.`view_times`) viewTimesMicroapp
	, SUM(apply1.`apply_times`) applyCountMicroapp
	, SUM(flow1.`follow_times`) followMicroapp
	FROM sector s
	LEFT JOIN sector s2 ON s2.`sector_code` LIKE CONCAT(s.`sector_code`, '%')
	LEFT JOIN resource_microapp rd ON s2.`id` = rd.`provider`
	LEFT JOIN resource r ON rd.`id` = r.`id`
	LEFT JOIN resource_view_times rvt1 ON r.`id` = rvt1.`id`
	LEFT JOIN (
		SELECT p.`resource_id` resource_id, COUNT(ps.`id`) apply_times
		FROM publish_info p
		JOIN publish_setting ps ON p.`id` = ps.`publish_info_id`
		JOIN resource_auth ra ON ps.`id` = ra.`publish_setting_id`
		GROUP BY p.`resource_id`
	) apply1 ON r.`id` = apply1.`resource_id`
	LEFT JOIN (
		SELECT p.`resource_id` resource_id, COUNT(rf.`id`) follow_times
		FROM publish_info p
		JOIN publish_setting ps ON p.`id` = ps.`publish_info_id`
		JOIN resource_follow rf ON ps.`id` = rf.`publish_setting_id`
		GROUP BY p.`resource_id`
	) flow1 ON r.`id` = flow1.`resource_id`
	WHERE s.`parent_id` IS NULL
	GROUP BY s.id, s.`sector_name`
) rmicroapp ON rdata.id = rmicroapp.id
  </select>
</mapper>