<?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.cool.store.mapper.HyOpenAreaInfoMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.HyOpenAreaInfoDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="parent_id" jdbcType="BIGINT" property="parentId" />
    <result column="area_name" jdbcType="VARCHAR" property="areaName" />
    <result column="area_path" jdbcType="VARCHAR" property="areaPath" />
    <result column="background_banner" jdbcType="VARCHAR" property="backgroundBanner" />
    <result column="detail_banner" jdbcType="VARCHAR" property="detailBanner" />
    <result column="area_status" jdbcType="VARCHAR" property="areaStatus" />
    <result column="deleted" jdbcType="TINYINT" property="deleted" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_user_id" jdbcType="VARCHAR" property="updateUserId" />
  </resultMap>
  <sql id="Base_Column_List">
    id, parent_id, area_name, area_path, background_banner, detail_banner, area_status,
    deleted, create_time, update_time, update_user_id
  </sql>
  <select id="selectById"  resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"></include>
    from hy_open_area_info where id = #{id}
  </select>
  <insert id="insertSelective"  parameterType="com.cool.store.entity.HyOpenAreaInfoDO" keyProperty="record.id" useGeneratedKeys="true">
    insert into hy_open_area_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="record.parentId != null">
        parent_id,
      </if>
      <if test="record.areaName != null">
        area_name,
      </if>
      <if test="record.areaPath != null">
        area_path,
      </if>
      <if test="record.backgroundBanner != null">
        background_banner,
      </if>
      <if test="record.detailBanner != null">
        detail_banner,
      </if>
      <if test="record.areaStatus != null">
        area_status,
      </if>
      <if test="record.deleted != null">
        deleted,
      </if>
      <if test="record.createTime != null">
        create_time,
      </if>
      <if test="record.updateTime != null">
        update_time,
      </if>
      <if test="record.updateUserId != null">
        update_user_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="record.parentId != null">
        #{record.parentId},
      </if>
      <if test="record.areaName != null">
        #{record.areaName},
      </if>
      <if test="record.areaPath != null">
        #{record.areaPath},
      </if>
      <if test="record.backgroundBanner != null">
        #{record.backgroundBanner},
      </if>
      <if test="record.detailBanner != null">
        #{record.detailBanner},
      </if>
      <if test="record.areaStatus != null">
        #{record.areaStatus},
      </if>
      <if test="record.deleted != null">
        #{record.deleted},
      </if>
      <if test="record.createTime != null">
        #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        #{record.updateTime},
      </if>
      <if test="record.updateUserId != null">
        #{record.updateUserId},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective">
    update hy_open_area_info
    <set>
      <if test="record.parentId != null">
        parent_id = #{record.parentId},
      </if>
      <if test="record.areaName != null">
        area_name = #{record.areaName},
      </if>
      <if test="record.areaPath != null">
        area_path = #{record.areaPath},
      </if>
      <if test="record.backgroundBanner != null">
        background_banner = #{record.backgroundBanner},
      </if>
      <if test="record.detailBanner != null">
        detail_banner = #{record.detailBanner},
      </if>
      <if test="record.areaStatus != null">
        area_status = #{record.areaStatus},
      </if>
      <if test="record.deleted != null">
        deleted = #{record.deleted},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime},
      </if>
      <if test="record.updateUserId != null">
        update_user_id = #{record.updateUserId},
      </if>
    </set>
    where id = #{record.id}
  </update>



  <update id="batchUpdateById">
    update hy_open_area_info
    <set>
      <if test="backgroundBanner != null">
        background_banner = #{backgroundBanner},
      </if>
      <if test="detailBanner != null">
        detail_banner = #{detailBanner},
      </if>
      <if test="areaStatus != null">
        area_status = #{areaStatus},
      </if>
      <if test="updateUserId != null">
        update_user_id = #{updateUserId},
      </if>
    </set>
    <where>
      <if test="ids !=null and ids.size>0">
        <foreach collection="ids" item="id" open="and id in (" close=")" separator=",">
          #{id}
        </foreach>
      </if>
    </where>
  </update>



  <update id="batchUpdateByParentId">
    update hy_open_area_info
    <set>
      <if test="backgroundBanner != null">
        background_banner = #{backgroundBanner},
      </if>
      <if test="detailBanner != null">
        detail_banner = #{detailBanner},
      </if>
      <if test="areaStatus != null">
        area_status = #{areaStatus},
      </if>
      <if test="updateUserId != null">
        update_user_id = #{updateUserId},
      </if>
    </set>
    <where>
      <if test="parentIdList !=null and parentIdList.size>0">
        <foreach collection="parentIdList" item="parentId" open="and parent_id in (" close=")" separator=",">
          #{parentId}
        </foreach>
      </if>
    </where>
  </update>

  <select id="queryKeyOpenArea" resultMap="BaseResultMap">
    select * from
    hy_open_area_info
    where area_status = 'keyOpen'
    and province_city_flag = 1
    and parent_id is not null
  </select>

  <select id="queryByKeyword" resultMap="BaseResultMap">
    select * from
    hy_open_area_info
    <where>
      <if test="keyword!=null and keyword!=''">
        and area_path like  concat('%',#{keyword},'%')
      </if>
      <if test="applyFlag!=null and applyFlag==true">
        and (area_status = 'open' or area_status = 'keyOpen')
      </if>
      <if test="areaStatus!=null and areaStatus!=''">
        and area_status = #{areaStatus}
      </if>
      <if test="filterData!=null and filterData==true">
        and province_city_flag = 1
      </if>
    </where>
  </select>


  <select id="queryFirstLevel" resultMap="BaseResultMap">
    select * from
    hy_open_area_info where parent_id is null
  </select>


  <select id="queryByIdsExcludeFirstLevel" resultMap="BaseResultMap">
    select * from
    hy_open_area_info where parent_id is not null
    <if test="ids !=null and ids.size>0">
      <foreach collection="ids" item="id" open="and id in (" close=")" separator=",">
        #{id}
      </foreach>
    </if>
  </select>


  <select id="getApplyReservationProvinceCount" resultType="com.cool.store.dto.partner.ApplyReservationProvinceDTO">
    SELECT
    substring(SUBSTRING_INDEX(area_path,'/',2),2) as areaName,
    sum(if((area_status='open' or area_status = 'keyOpen') ,1,0)) as applyCount,
    sum(if((area_status='notOpen' or area_status = 'saturated') ,1,0)) as reservationCount
    FROM `hy_open_area_info` where parent_id is not null
    group by substring(SUBSTRING_INDEX(area_path,'/',2),2)
  </select>


  <select id="getChildrenList" resultMap="BaseResultMap">
    select * from
    hy_open_area_info
    <where>
      <if test="parentId!=null">
        and parent_id = #{parentId}
      </if>
      <if test="type!=null and type == 'apply'">
        and (area_status = 'open' or area_status = 'keyOpen')
      </if>
      <if test="type!=null and type == 'reservation'">
        and (area_status='notOpen' or area_status = 'saturated')
      </if>
    </where>
  </select>

  <select id="getChildrenCount" resultType="java.lang.Integer">
    select count(1) from
    hy_open_area_info
    <where>
      <if test="parentId!=null">
        and parent_id = #{parentId}
      </if>
      <if test="type!=null and type == 'apply'">
        and (area_status = 'open' or area_status = 'keyOpen')
      </if>
      <if test="type!=null and type == 'reservation'">
        and (area_status='notOpen' or area_status = 'saturated')
      </if>
    </where>
  </select>

  <select id="selectByIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"></include>
    from hy_open_area_info
    <where>
      <if test="idList !=null and idList.size>0">
        <foreach collection="idList" item="id" open="and id in (" close=")" separator=",">
          #{id}
        </foreach>
      </if>
    </where>
  </select>

  <select id="getAllOpenArea" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from hy_open_area_info
  </select>

  <select id="filterLeafNode" resultType="long">
    select
      id
    from
      hy_open_area_info
    where
      deleted = '0' and province_city_flag = '0' and id in <foreach collection="openAreaIds" item="openAreaId" separator="," open="(" close=")">#{openAreaId}</foreach>
  </select>
  <select id="getSonArea" resultType="com.cool.store.entity.HyOpenAreaInfoDO">
    SELECT
    <include refid="Base_Column_List"></include>
    FROM hy_open_area_info
    WHERE parent_id = #{id}
  </select>
  <select id="getProvinceAllCode"   resultMap="BaseResultMap">
    SELECT  b.* FROM `hy_open_area_info` a  inner join  hy_open_area_info b    on a.`id`=b.`parent_id`
    WHERE a.parent_id=#{id}  ORDER BY  b.id desc
  </select>

</mapper>