<?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.microservices.zone.detail.mapper.ZoneDetailMapper">

    <resultMap type="com.microservices.zone.detail.domain.ZoneDetail" id="ZoneDetailResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="key" column="key"/>
        <result property="deptId" column="dept_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="mainTitle" column="main_title"/>
        <result property="subTitle" column="sub_title"/>
        <result property="firstTitle" column="first_title"/>
        <result property="bannerList" column="banner_list"/>
        <result property="introductionTitle" column="introduction_title"/>
        <result property="introductionContent" column="introduction_content"/>
        <result property="introductionImage" column="introduction_image"/>
        <result property="homepageMembershipSize" column="homepage_membership_size"/>
        <result property="homepageCmsSize" column="homepage_cms_size"/>
        <result property="homepageProjectSize" column="homepage_project_size"/>
        <result property="cmsProjectId" column="cms_project_id"/>
        <result property="gitlinkProjectKeys" column="gitlink_project_keys"/>
        <result property="isPublish" column="is_publish"/>
        <result property="cmsShow" column="cms_show"/>
        <result property="projectShow" column="project_show"/>
        <result property="memberShow" column="member_show"/>
        <result property="partnersShow" column="partners_show"/>
        <result property="specialProjectShow" column="special_project_show"/>
        <result property="homepageSpecialProjectTitle" column="homepage_special_project_title"/>
        <result property="resourceShow" column="resource_show"/>
        <result property="homepageResourceTitle" column="homepage_resource_title"/>
        <result property="homepageCmsTitle" column="homepage_cms_title"/>
        <result property="homepageProjectTitle" column="homepage_project_title"/>
        <result property="homepageMemberTitle" column="homepage_member_title"/>
        <result property="homepagePartnersTitle" column="homepage_partners_title"/>
        <result property="template" column="template"/>
        <result property="helperShow" column="helper_show"/>
        <result property="helperTitle" column="helper_title"/>
        <result property="docNeedAudit" column="doc_need_audit"/>
        <result property="resourceNeedAudit" column="resource_need_audit"/>
        <result property="projectNeedAudit" column="project_need_audit"/>
        <result property="isLockCms" column="is_lock_cms"/>
        <result property="sectionCmsShow" column="section_cms_show"/>
        <result property="sectionProjectShow" column="section_project_show"/>
        <result property="sectionMemberShow" column="section_member_show"/>
        <result property="sectionHelperShow" column="section_helper_show"/>
        <result property="sectionHomepageShow" column="section_homepage_show"/>
        <result property="sectionResourceShow" column="section_resource_show"/>
        <result property="sectionDataSetShow" column="section_dataset_show"/>
        <result property="sectionCmsTitle" column="section_cms_title"/>
        <result property="sectionProjectTitle" column="section_project_title"/>
        <result property="sectionMemberTitle" column="section_member_title"/>
        <result property="sectionHelperTitle" column="section_helper_title"/>
        <result property="sectionHomepageTitle" column="section_homepage_title"/>
        <result property="sectionResourceTitle" column="section_resource_title"/>
        <result property="sectionDataSetTitle" column="section_dataset_title"/>
    </resultMap>

    <sql id="selectZoneDetailVo">
        select id,
               name,
               `key`,
               dept_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               main_title,
               sub_title,
               first_title,
               banner_list,
               introduction_title,
               introduction_content,
               introduction_image,
               homepage_membership_size,
               homepage_cms_size,
               homepage_project_size,
               gitlink_project_keys,
               cms_project_id,
               is_publish,
               helper_title,
               helper_show,
               cms_show,
               project_show,
               member_show,
               partners_show,
               special_project_show,
               homepage_special_project_title,
               resource_show,
               homepage_resource_title,
               homepage_cms_title,
               homepage_project_title,
               homepage_member_title,
               homepage_partners_title,
               template,
               is_lock_cms,
               resource_need_audit,
               project_need_audit,
               doc_need_audit,
               section_cms_show,
               section_project_show,
               section_member_show,
               section_helper_show,
               section_homepage_show,
               section_resource_show,
               section_dataset_show,
               section_cms_title,
               section_project_title,
               section_member_title,
               section_homepage_title,
               section_resource_title,
               section_dataset_title,
               section_helper_title
        from zone_detail
    </sql>

    <sql id="selectZoneDetailAliasVo">
        select z.id,
               z.name,
               z.`key`,
               z.dept_id,
               z.del_flag,
               z.create_by,
               z.create_time,
               z.update_by,
               z.update_time,
               z.main_title,
               z.sub_title,
               z.first_title,
               z.banner_list,
               z.introduction_title,
               z.introduction_content,
               z.introduction_image,
               z.homepage_membership_size,
               z.homepage_cms_size,
               z.homepage_project_size,
               z.gitlink_project_keys,
               z.cms_project_id,
               z.is_publish,
               z.helper_title,
               z.helper_show,
               z.cms_show,
               z.project_show,
               z.member_show,
               z.partners_show,
               z.special_project_show,
               z.homepage_special_project_title,
               z.resource_show,
               z.homepage_resource_title,
               z.homepage_cms_title,
               z.homepage_project_title,
               z.homepage_member_title,
               z.homepage_partners_title,
               z.template,
               z.is_lock_cms,
               z.resource_need_audit,
               z.project_need_audit,
               z.doc_need_audit,
               z.section_cms_show,
               z.section_project_show,
               z.section_member_show,
               z.section_helper_show,
               z.section_homepage_show,
               z.section_resource_show,
               z.section_dataset_show,
               z.section_cms_title,
               z.section_project_title,
               z.section_member_title,
               z.section_homepage_title,
               z.section_resource_title,
               z.section_dataset_title,
               z.section_helper_title
        from zone_detail as z
    </sql>
    <sql id="selectWhere">
        <where>
            <if test="name != null  and name != ''">and z.name like concat('%', #{name}, '%')</if>
            <if test="deptId != null ">and z.dept_id = #{deptId}</if>
            <if test="id != null ">and z.id = #{id}</if>
            <if test="isPublish != null ">and z.is_publish = #{isPublish}</if>
            <if test="mainTitle != null  and mainTitle != ''">and z.main_title like concat('%', #{mainTitle}, '%')</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </sql>
    <select id="selectZoneDetailList" parameterType="ZoneDetail" resultMap="ZoneDetailResult">
        <include refid="selectZoneDetailAliasVo"/>
        <include refid="selectWhere"/>
    </select>

      <select id="selectZoneDetailById" parameterType="Long" resultMap="ZoneDetailResult">
          <include refid="selectZoneDetailVo"/>
          where id = #{id}
      </select>
      <select id="selectZoneDetailByDeptId" parameterType="Long" resultMap="ZoneDetailResult">
          <include refid="selectZoneDetailVo"/>
          where dept_id = #{deptId}
      </select>
      <select id="selectZoneDetailCount" resultType="java.lang.Integer">
          select count(0) from zone_detail as z
          <include refid="selectWhere"/>
      </select>
      <select id="selectZoneDetailByKey" resultMap="ZoneDetailResult">
          <include refid="selectZoneDetailVo"/>
          where `key` = #{key}
      </select>
      <select id="selectZoneDetailByName" resultMap="ZoneDetailResult">
          <include refid="selectZoneDetailVo"/>
          where `name` = #{name}
      </select>

      <select id="countArticlesGroupByKey" resultType="com.microservices.zone.detail.domain.vo.ZoneDetailDataVo">
          SELECT  zd.`key`, COUNT(*) AS weekArticles
          FROM  cms_doc cd
          left join zone_detail zd on zd.dept_id = cd.dept_id
          where zd.`key` = #{key} and cd.create_time &gt;= #{createTimeS} and cd.create_time &lt;= #{createTimeE}
          GROUP BY zd.`key`
      </select>

      <select id="countPUVsGroupByKey" resultType="com.microservices.zone.detail.domain.vo.ZoneDetailDataVo">
          SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(zv.url, '/', 3), '/', -1) AS `key`,
          COUNT(*)                                                  AS weekPV,
          COUNT(DISTINCT uuid)                                      AS weekUV
          FROM zone_visits as zv
          left JOIN
          zone_detail d ON SUBSTRING_INDEX(SUBSTRING_INDEX(zv.url, '/', 3), '/', -1) = d.key
          WHERE d.key = #{key} and zv.create_time &gt;= #{createTimeS}
          and zv.create_time &lt;= #{createTimeE}
          GROUP BY SUBSTRING_INDEX(SUBSTRING_INDEX(zv.url, '/', 3), '/', -1)
      </select>

      <insert id="insertZoneDetail" parameterType="ZoneDetail" useGeneratedKeys="true" keyProperty="id">
          insert into zone_detail
          <trim prefix="(" suffix=")" suffixOverrides=",">
              <if test="name != null and name != ''">name,</if>
              <if test="key != null and key != ''">`key`,</if>
              <if test="deptId != null">dept_id,</if>
              <if test="delFlag != null">del_flag,</if>
              <if test="createBy != null">create_by,</if>
              <if test="createTime != null">create_time,</if>
              <if test="updateBy != null">update_by,</if>
              <if test="updateTime != null">update_time,</if>
              <if test="mainTitle != null">main_title,</if>
              <if test="subTitle != null">sub_title,</if>
              <if test="firstTitle != null">first_title,</if>
              <if test="bannerList != null">banner_list,</if>
              <if test="introductionTitle != null">introduction_title,</if>
              <if test="introductionContent != null">introduction_content,</if>
              <if test="introductionImage != null">introduction_image,</if>
              <if test="homepageMembershipSize != null">homepage_membership_size,</if>
              <if test="homepageCmsSize != null">homepage_cms_size,</if>
              <if test="homepageProjectSize != null">homepage_project_size,</if>
              <if test="cmsProjectId != null">cms_project_id,</if>
              <if test="gitlinkProjectKeys != null">gitlink_project_keys,</if>
              <if test="cmsShow != null">cms_show,</if>
              <if test="projectShow != null">project_show,</if>
              <if test="memberShow != null">member_show,</if>
              <if test="partnersShow != null">partners_show,</if>
              <if test="specialProjectShow != null">special_project_show,</if>
              <if test="homepageSpecialProjectTitle != null">homepage_special_project_title,</if>
              <if test="resourceShow != null">resource_show,</if>
              <if test="homepageResourceTitle != null">homepage_resource_title,</if>
              <if test="homepageCmsTitle != null">homepage_cms_title,</if>
              <if test="homepageProjectTitle != null">homepage_project_title,</if>
              <if test="homepageMemberTitle != null">homepage_member_title,</if>
              <if test="homepagePartnersTitle != null">homepage_partners_title,</if>
              <if test="template != null">template,</if>
              <if test="helperTitle != null">helper_title,</if>
              <if test="helperShow != null">helper_show,</if>
              <if test="docNeedAudit != null">doc_need_audit,</if>
              <if test="resourceNeedAudit != null">resource_need_audit,</if>
              <if test="projectNeedAudit != null">project_need_audit,</if>
              <if test="isLockCms != null">is_lock_cms,</if>
              <if test="sectionCmsShow != null">section_cms_show,</if>
              <if test="sectionProjectShow != null">section_project_show,</if>
              <if test="sectionMemberShow != null">section_member_show,</if>
              <if test="sectionHelperShow != null">section_helper_show,</if>
              <if test="sectionHomepageShow != null">section_homepage_show,</if>
              <if test="sectionResourceShow != null">section_resource_show,</if>
              <if test="sectionDataSetShow != null">section_dataset_show,</if>
              <if test="sectionCmsTitle != null">section_cms_title,</if>
              <if test="sectionProjectTitle != null">section_project_title,</if>
              <if test="sectionMemberTitle != null">section_member_title,</if>
              <if test="sectionHelperTitle != null">section_helper_title,</if>
              <if test="sectionHomepageTitle != null">section_homepage_title,</if>
              <if test="sectionResourceTitle != null">section_resource_title,</if>
              <if test="sectionDataSetTitle != null">section_dataset_title,</if>
          </trim>
          <trim prefix="values (" suffix=")" suffixOverrides=",">
              <if test="name != null and name != ''">#{name},</if>
              <if test="key != null and key != ''">#{key},</if>
              <if test="deptId != null">#{deptId},</if>
              <if test="delFlag != null">#{delFlag},</if>
              <if test="createBy != null">#{createBy},</if>
              <if test="createTime != null">#{createTime},</if>
              <if test="updateBy != null">#{updateBy},</if>
              <if test="updateTime != null">#{updateTime},</if>
              <if test="mainTitle != null">#{mainTitle},</if>
              <if test="subTitle != null">#{subTitle},</if>
              <if test="firstTitle != null">#{firstTitle},</if>
              <if test="bannerList != null">#{bannerList},</if>
              <if test="introductionTitle != null">#{introductionTitle},</if>
              <if test="introductionContent != null">#{introductionContent},</if>
              <if test="introductionImage != null">#{introductionImage},</if>
              <if test="homepageMembershipSize != null">#{homepageMembershipSize},</if>
              <if test="homepageCmsSize != null">#{homepageCmsSize},</if>
              <if test="homepageProjectSize != null">#{homepageProjectSize},</if>
              <if test="cmsProjectId != null">#{cmsProjectId},</if>
              <if test="gitlinkProjectKeys != null">#{gitlinkProjectKeys},</if>
              <if test="cmsShow != null">#{cmsShow},</if>
              <if test="projectShow != null">#{projectShow},</if>
              <if test="memberShow != null">#{memberShow},</if>
              <if test="partnersShow != null">#{partnersShow},</if>
              <if test="specialProjectShow != null">#{specialProjectShow},</if>
              <if test="homepageSpecialProjectTitle != null">#{homepageSpecialProjectTitle},</if>
              <if test="resourceShow != null">#{resourceShow},</if>
              <if test="homepageResourceTitle != null">#{homepageResourceTitle},</if>
              <if test="homepageCmsTitle != null">#{homepageCmsTitle},</if>
              <if test="homepageProjectTitle != null">#{homepageProjectTitle},</if>
              <if test="homepageMemberTitle != null">#{homepageMemberTitle},</if>
              <if test="homepagePartnersTitle != null">#{homepagePartnersTitle},</if>
              <if test="template != null">#{template},</if>
              <if test="helperTitle != null">#{helperTitle},</if>
              <if test="helperShow != null">#{helperShow},</if>
              <if test="docNeedAudit != null">#{docNeedAudit},</if>
              <if test="resourceNeedAudit != null">#{resourceNeedAudit},</if>
              <if test="projectNeedAudit != null">#{projectNeedAudit},</if>
              <if test="isLockCms != null">#{isLockCms},</if>
              <if test="sectionCmsShow != null">#{sectionCmsShow},</if>
              <if test="sectionProjectShow != null">#{sectionProjectShow},</if>
              <if test="sectionMemberShow != null">#{sectionMemberShow},</if>
              <if test="sectionHelperShow != null">#{sectionHelperShow},</if>
              <if test="sectionHomepageShow != null">#{sectionHomepageShow},</if>
              <if test="sectionResourceShow != null">#{sectionResourceShow},</if>
              <if test="sectionDataSetShow != null">#{sectionDataSetShow},</if>
              <if test="sectionCmsTitle != null">#{sectionCmsTitle},</if>
              <if test="sectionProjectTitle != null">#{sectionProjectTitle},</if>
              <if test="sectionMemberTitle != null">#{sectionMemberTitle},</if>
              <if test="sectionHelperTitle != null">#{sectionHelperTitle},</if>
              <if test="sectionHomepageTitle != null">#{sectionHomepageTitle},</if>
              <if test="sectionResourceTitle != null">#{sectionResourceTitle},</if>
              <if test="sectionDataSetTitle != null">#{sectionDataSetTitle},</if>
          </trim>
      </insert>

      <update id="updateZoneDetail" parameterType="ZoneDetail">
          update zone_detail
          <trim prefix="SET" suffixOverrides=",">
              <if test="name != null and name != ''">name = #{name},</if>
              <if test="key != null and key != ''">`key` = #{key},</if>
              <if test="deptId != null">dept_id = #{deptId},</if>
              <if test="delFlag != null">del_flag = #{delFlag},</if>
              <if test="createBy != null">create_by = #{createBy},</if>
              <if test="createTime != null">create_time = #{createTime},</if>
              <if test="updateBy != null">update_by = #{updateBy},</if>
              <if test="updateTime != null">update_time = #{updateTime},</if>
              <if test="mainTitle != null">main_title = #{mainTitle},</if>
              <if test="subTitle != null">sub_title = #{subTitle},</if>
              <if test="firstTitle != null">first_title = #{firstTitle},</if>
              <if test="bannerList != null">banner_list = #{bannerList},</if>
              <if test="introductionTitle != null">introduction_title = #{introductionTitle},</if>
              <if test="introductionContent != null">introduction_content = #{introductionContent},</if>
              <if test="introductionImage != null">introduction_image = #{introductionImage},</if>
              <if test="homepageMembershipSize != null">homepage_membership_size = #{homepageMembershipSize},</if>
              <if test="homepageCmsSize != null">homepage_cms_size = #{homepageCmsSize},</if>
              <if test="homepageProjectSize != null">homepage_project_size = #{homepageProjectSize},</if>
              <if test="cmsProjectId != null">cms_project_id = #{cmsProjectId},</if>
              <if test="gitlinkProjectKeys != null">gitlink_project_keys = #{gitlinkProjectKeys},</if>
              <if test="isPublish != null">is_publish = #{isPublish},</if>
              <if test="cmsShow != null">cms_show = #{cmsShow},</if>
              <if test="projectShow != null">project_show = #{projectShow},</if>
              <if test="memberShow != null">member_show = #{memberShow},</if>
              <if test="partnersShow != null">partners_show = #{partnersShow},</if>
              <if test="specialProjectShow != null">special_project_show = #{specialProjectShow},</if>
              <if test="homepageSpecialProjectTitle != null">homepage_special_project_title =
                  #{homepageSpecialProjectTitle},
              </if>
              <if test="resourceShow != null">resource_show = #{resourceShow},</if>
              <if test="homepageResourceTitle != null">homepage_resource_title =
                  #{homepageResourceTitle},
              </if>
              <if test="homepageCmsTitle != null">homepage_cms_title = #{homepageCmsTitle},</if>
              <if test="homepageProjectTitle != null">homepage_project_title = #{homepageProjectTitle},</if>
              <if test="homepageMemberTitle != null">homepage_member_title = #{homepageMemberTitle},</if>
              <if test="homepagePartnersTitle != null">homepage_partners_title = #{homepagePartnersTitle},</if>
              <if test="template != null">template = #{template},</if>
              <if test="helperShow != null">helper_show = #{helperShow},</if>
              <if test="helperTitle != null">helper_title = #{helperTitle},</if>
              <if test="docNeedAudit != null">doc_need_audit = #{docNeedAudit},</if>
              <if test="resourceNeedAudit != null">resource_need_audit = #{resourceNeedAudit},</if>
              <if test="projectNeedAudit != null">project_need_audit = #{projectNeedAudit},</if>
              <if test="isLockCms != null">is_lock_cms = #{isLockCms},</if>
              <if test="sectionCmsShow != null">section_cms_show = #{sectionCmsShow},</if>
              <if test="sectionProjectShow != null">section_project_show = #{sectionProjectShow},</if>
              <if test="sectionMemberShow != null">section_member_show = #{sectionMemberShow},</if>
              <if test="sectionHelperShow != null">section_helper_show = #{sectionHelperShow},</if>
              <if test="sectionHomepageShow != null">section_homepage_show = #{sectionHomepageShow},</if>
              <if test="sectionResourceShow != null">section_resource_show = #{sectionResourceShow},</if>
              <if test="sectionDataSetShow != null">section_dataset_show = #{sectionDataSetShow},</if>
              <if test="sectionCmsTitle != null">section_cms_title = #{sectionCmsTitle},</if>
              <if test="sectionProjectTitle != null">section_project_title = #{sectionProjectTitle},</if>
              <if test="sectionMemberTitle != null">section_member_title = #{sectionMemberTitle},</if>
              <if test="sectionHelperTitle != null">section_helper_title = #{sectionHelperTitle},</if>
              <if test="sectionHomepageTitle != null">section_homepage_title = #{sectionHomepageTitle},</if>
              <if test="sectionResourceTitle != null">section_resource_title = #{sectionResourceTitle},</if>
              <if test="sectionDataSetTitle != null">section_dataset_title = #{sectionDataSetTitle},</if>
          </trim>


          where id = #{id}
      </update>

      <delete id="deleteZoneDetailById" parameterType="Long">
          delete
          from zone_detail
          where id = #{id}
      </delete>

      <delete id="deleteZoneDetailByIds" parameterType="String">
          delete from zone_detail where id in
          <foreach item="id" collection="array" open="(" separator="," close=")">
              #{id}
          </foreach>
      </delete>
  </mapper>