<?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.gic.haoban.manage.service.dao.mapper.TabHaobanMaterialCategoryMapper" >
  <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanMaterialCategory" >
    <id column="category_id" property="categoryId" jdbcType="VARCHAR" />
    <result column="category_name" property="categoryName" jdbcType="VARCHAR" />
    <result column="category_parent_id" property="categoryParentId" jdbcType="VARCHAR" />
    <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR" />
    <result column="status_flag" property="statusFlag" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="sort_num" property="sortNum" />
  </resultMap>
  <sql id="Base_Column_List" >
    category_id, category_name, category_parent_id, wx_enterprise_id, status_flag, create_time, 
    update_time , sort_num
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material_category
    where category_id = #{categoryId,jdbcType=VARCHAR}
  </select>

  <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanMaterialCategory" >
    insert into tab_haoban_material_category (category_id, category_name, category_parent_id, 
      wx_enterprise_id, status_flag, create_time,  sort_num ,
      update_time)
    values (#{categoryId,jdbcType=VARCHAR}, #{categoryName,jdbcType=VARCHAR}, #{categoryParentId,jdbcType=VARCHAR}, 
      #{wxEnterpriseId,jdbcType=VARCHAR}, #{statusFlag,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},  #{sortNum} ,
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanMaterialCategory" >
    update tab_haoban_material_category
    <set >
      <if test="categoryName != null" >
        category_name = #{categoryName,jdbcType=VARCHAR},
      </if>
      <if test="statusFlag != null" >
        status_flag = #{statusFlag,jdbcType=INTEGER},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where category_id = #{categoryId,jdbcType=VARCHAR}
  </update>

  <update id="updateSortNum">
    <foreach collection="list" item="item" separator=";">
      UPDATE tab_haoban_material_category
      SET sort_num = #{item.sortNum}
      WHERE category_id = #{item.categoryId}
    </foreach>
  </update>

  <select id="selectByCategoryNameAndParentId" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material_category
    where category_name = #{categoryName,jdbcType=VARCHAR}
    and status_flag = 1
    and category_parent_id = #{categoryParentId}
    and wx_enterprise_id = #{wxEnterpriseId}
  </select>
  
  <select id="listCategory" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material_category
    where status_flag = 1
    and wx_enterprise_id = #{wxEnterpriseId}  order by sort_num asc , create_time desc
  </select>
  
  <select id="listByParentCategory" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material_category
    where status_flag = 1
    and category_parent_id = #{categoryId}  order by sort_num asc , create_time desc
  </select>

  <select id="getMaxSortNum" resultType="java.lang.Integer">
    select ifnull(max(sort_num),0) from tab_haoban_material_category
    where wx_enterprise_id = #{wxEnterpriseId}
    and category_parent_id = #{parentId} and status_flag = 1
  </select>
</mapper>