<?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.BigRegionMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.BigRegionDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="region_id" jdbcType="BIGINT" property="regionId" />
    <result column="region_name" jdbcType="VARCHAR" property="regionName" />
    <result column="org_code" jdbcType="VARCHAR" property="orgCode" />
    <result column="org_id" jdbcType="VARCHAR" property="orgId" />
    <result column="org_name" jdbcType="VARCHAR" property="orgName" />
    <result column="join_mode" jdbcType="TINYINT" property="joinMode" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="group_name" jdbcType="VARCHAR" property="groupName" />
    <result column="store_manage_region_id" jdbcType="BIGINT" property="storeManageRegionId" />
    <result column="hqt_region_code" jdbcType="VARCHAR" property="hqtRegionCode" />
    <result column="hqt_region_name" jdbcType="VARCHAR" property="hqtRegionName" />
  </resultMap>
    <update id="updateHqtDate">
      update xfsg_big_region
      set hqt_region_code = #{hqtRegionCode},
      hqt_region_name = #{hqtRegionName}
      where id = #{id}
    </update>


    <select id="queryAllBigRegion" resultType="com.cool.store.dto.region.BigRegionDTO">
    select  DISTINCT
    region_id as regionId,
    region_name as regionName,
    group_name as groupName,
    enable_flag as enableFlag,
    store_manage_region_id as storeManageRegionId
    FROM `xfsg_big_region`
    <where>
      <if test="keyword!=null and keyword !=''">
        and region_name like CONCAT('%',#{keyword},'%')
      </if>
      <if test="joinBrand != null">
        and join_brand = #{joinBrand}
      </if>
      <if test="type != null">
        and type = #{type}
      </if>
    </where>
      order by
      enable_flag desc,
      <choose>
        <when test="type != null and type == 2">
          id asc
        </when>
        <when test="type != null and type == 1">
          id desc
        </when>
        <otherwise>
          id asc
        </otherwise>
      </choose>
  </select>

  <select id="queryOrgInfoByBigRegionAndJoinMode" resultMap="BaseResultMap">
    select  *
    FROM `xfsg_big_region`
    <where>
      <if test="regionId !=null ">
        and region_id = #{regionId}
      </if>
      <if test="joinMode !=null ">
        and join_mode = #{joinMode}
      </if>
    </where>
    limit 1
  </select>
  <select id="queryBigRegion" resultType="com.cool.store.dto.region.BigRegionDTO">
    select *
    from `xfsg_big_region`
    where join_mode = 0
    <if test="request.keyword!=null and request.keyword !=''">
      and region_name like CONCAT('%',#{request.keyword},'%')
    </if>
    <if test="request.groupNameList!=null and request.groupNameList.size()>0">
      and group_name in
      <foreach item="item" collection="request.groupNameList" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
    <choose>
      <when test="request.type == null or request.type == 1">
        AND type = 1
      </when>
      <otherwise>
        AND type = 2
      </otherwise>
    </choose>
  </select>
</mapper>