<?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.goafanti.common.dao.JtBusinessCategoryMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.JtBusinessCategory">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="summary" jdbcType="VARCHAR" property="summary" />
    <result column="number" jdbcType="VARCHAR" property="number" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="super_id" jdbcType="VARCHAR" property="superId" />
    <result column="img_url" jdbcType="VARCHAR" property="imgUrl" />
    <result column="layer" jdbcType="INTEGER" property="layer" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="module" jdbcType="INTEGER" property="module" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    <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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    <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">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    id, name, summary, number, create_time, super_id, img_url, layer, sort, module
  </sql>
  <select id="selectByExample" parameterType="com.goafanti.common.model.JtBusinessCategoryExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from jt_business_category
    <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.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from jt_business_category
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    delete from jt_business_category
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.goafanti.common.model.JtBusinessCategoryExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    delete from jt_business_category
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.JtBusinessCategory">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    insert into jt_business_category (id, name, summary, 
      number, create_time, super_id, 
      img_url, layer, sort, 
      module)
    values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{summary,jdbcType=VARCHAR}, 
      #{number,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{superId,jdbcType=VARCHAR}, 
      #{imgUrl,jdbcType=VARCHAR}, #{layer,jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}, 
      #{module,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.JtBusinessCategory">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    insert into jt_business_category
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="summary != null">
        summary,
      </if>
      <if test="number != null">
        number,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="superId != null">
        super_id,
      </if>
      <if test="imgUrl != null">
        img_url,
      </if>
      <if test="layer != null">
        layer,
      </if>
      <if test="sort != null">
        sort,
      </if>
      <if test="module != null">
        module,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="summary != null">
        #{summary,jdbcType=VARCHAR},
      </if>
      <if test="number != null">
        #{number,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="superId != null">
        #{superId,jdbcType=VARCHAR},
      </if>
      <if test="imgUrl != null">
        #{imgUrl,jdbcType=VARCHAR},
      </if>
      <if test="layer != null">
        #{layer,jdbcType=INTEGER},
      </if>
      <if test="sort != null">
        #{sort,jdbcType=INTEGER},
      </if>
      <if test="module != null">
        #{module,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.goafanti.common.model.JtBusinessCategoryExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    select count(*) from jt_business_category
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    update jt_business_category
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.summary != null">
        summary = #{record.summary,jdbcType=VARCHAR},
      </if>
      <if test="record.number != null">
        number = #{record.number,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.superId != null">
        super_id = #{record.superId,jdbcType=VARCHAR},
      </if>
      <if test="record.imgUrl != null">
        img_url = #{record.imgUrl,jdbcType=VARCHAR},
      </if>
      <if test="record.layer != null">
        layer = #{record.layer,jdbcType=INTEGER},
      </if>
      <if test="record.sort != null">
        sort = #{record.sort,jdbcType=INTEGER},
      </if>
      <if test="record.module != null">
        module = #{record.module,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    update jt_business_category
    set id = #{record.id,jdbcType=VARCHAR},
      name = #{record.name,jdbcType=VARCHAR},
      summary = #{record.summary,jdbcType=VARCHAR},
      number = #{record.number,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      super_id = #{record.superId,jdbcType=VARCHAR},
      img_url = #{record.imgUrl,jdbcType=VARCHAR},
      layer = #{record.layer,jdbcType=INTEGER},
      sort = #{record.sort,jdbcType=INTEGER},
      module = #{record.module,jdbcType=INTEGER}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.JtBusinessCategory">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    update jt_business_category
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="summary != null">
        summary = #{summary,jdbcType=VARCHAR},
      </if>
      <if test="number != null">
        number = #{number,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="superId != null">
        super_id = #{superId,jdbcType=VARCHAR},
      </if>
      <if test="imgUrl != null">
        img_url = #{imgUrl,jdbcType=VARCHAR},
      </if>
      <if test="layer != null">
        layer = #{layer,jdbcType=INTEGER},
      </if>
      <if test="sort != null">
        sort = #{sort,jdbcType=INTEGER},
      </if>
      <if test="module != null">
        module = #{module,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.JtBusinessCategory">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jul 17 09:14:43 CST 2018.
    -->
    update jt_business_category
    set name = #{name,jdbcType=VARCHAR},
      summary = #{summary,jdbcType=VARCHAR},
      number = #{number,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      super_id = #{superId,jdbcType=VARCHAR},
      img_url = #{imgUrl,jdbcType=VARCHAR},
      layer = #{layer,jdbcType=INTEGER},
      sort = #{sort,jdbcType=INTEGER},
      module = #{module,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="getBusinessCategoryByLayerAndName" resultType="com.goafanti.common.model.JtBusinessCategory">
select
	id,
	name,
	super_id as superId,module
from
	jt_business_category
where
	name = #{1}
	and layer = #{0}
	order by create_time desc
  </select>
  
  <select id="getBusinessCategoryBySuperId" resultType="com.goafanti.common.model.JtBusinessCategory">
  select
	id,
	name,
	super_id as superId,
	sort,module
from
	jt_business_category
where

<if test="superId!=null">
	super_id = #{superId,jdbcType=VARCHAR}
	</if>
	<if test="superId==null">
	layer=1
	</if>

	order by sort
	<if test="size!=null">
	limit #{size,jdbcType=INTEGER}
	</if>
  </select>
  
 <select id="getCategoryTree" resultType="com.goafanti.business.bo.JtBusinessCategoryTree">
  select
	jt1.name as topLevel,
	jt2.name as secondLevel
from
	jt_business_category jt2
left join jt_business_category jt1 on
	jt2.super_id = jt1.id
where
	jt2.layer = 2
	and jt1.layer = 1
	and jt2.id = #{0}
 </select>
 <select id="getCategoryBySuperId" resultType="com.goafanti.common.model.JtBusinessCategory">
  select
	id,
	name,
	super_id as superId,
	sort,module
from
	jt_business_category
where
	super_id = #{0}
	order by sort
  </select>
  <resultMap id="JtBusinessCategoryBoResult" type="com.goafanti.business.bo.JtBusinessCategoryBo">  
        <id column="name" property="topLevel" />  
        <id column="topLevelId" property="topLevelId" />
        <id column="summary" property="summary"/>
        <collection column="topLevelId" ofType="com.goafanti.common.model.JtBusinessCategory" property="children" select="getCategoryBySuperId">
        </collection>  
    </resultMap>  
    
    <select id="getCategoryBoList" resultMap="JtBusinessCategoryBoResult">
    select 
    name as topLevel,
    id as topLevelId,
    img_url as topImg,
    summary
    from jt_business_category
    where layer=1
    <if test="module!=null">
    and module=#{module,jdbcType=INTEGER}
    </if>
    order by sort
    </select>
    
  <update id="autoIncreaseSort">
update jt_business_category set 
sort=sort+1 
where sort &gt; #{1}
and layer=#{0}
  </update>
  
    <select id="getCategoryListByCondition" resultType="com.goafanti.common.model.JtBusinessCategoryResult">
   select bc1.id,bc1.sort,bc1.number,bc1.name,bc1.super_id as superId,bc1.layer,bc2.name as superName,bc1.module as module
    from jt_business_category bc1
    left join jt_business_category bc2 on bc2.id=bc1.super_id
    where 
    1=1 
    <if test="layer!=null">
    and bc1.layer=#{layer,jdbcType=INTEGER}</if>
    <if test="name!=null">
    and bc1.name like concat("%",#{name,jdbcType=VARCHAR},"%")
    </if>
    <if test="module !=null">
    and bc1.module =#{module,jdbcType=INTEGER}
    </if>
   <if test="page_sql!=null">
		${page_sql}
	</if>
    </select>
    
    <select id="getCategoryCountByCondition" resultType="java.lang.Integer">
    select
    count(0)
      from jt_business_category bc1
    left join jt_business_category bc2 on bc2.id=bc1.super_id
    where 
    1=1 
    <if test="layer!=null">
    and bc1.layer=#{layer,jdbcType=INTEGER}</if>
    <if test="name!=null">
    and bc1.name like concat("%",#{name,jdbcType=VARCHAR},"%")
    </if>
     <if test="module !=null">
    and bc1.module =#{module,jdbcType=INTEGER}
    </if>
    </select>
    
    <!-- 根据修改后的品类id获得最大同级number -->
    <select id="getCurrentMaxNumber" resultType="java.lang.String" parameterType="java.lang.String">
    	<bind name="n" value="_parameter + '__'"/>
    	select max(`number`) maxnum from jt_business_category where `number` like #{n, jdbcType=VARCHAR}
    </select>
    <!-- 根据修改后的品类id获得最小同级number -->
    <select id="getCurrentMinNumber" resultType="java.lang.String" parameterType="java.lang.String">
    	<bind name="n" value="_parameter + '__'"/>
    	select min(`number`) minnum from jt_business_category where `number` like #{n, jdbcType=VARCHAR}
    </select>
    
    <!-- 修改品类前缀  -->
    <update id="updateNumberPrefix" parameterType="com.goafanti.common.model.JtBusinessCategory">
    	<bind name="n" value="number + '__%'"/>
    	update jt_business_category 
		set `number` = concat(#{name, jdbcType=VARCHAR},substring(`number`,#{sort, jdbcType=INTEGER}))
		where `number` like "#{n, jdbcType=VARCHAR}
    </update>
</mapper>