<?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.tmerclub.cloud.seckill.mapper.SeckillCategoryMapper">
  <resultMap id="seckillCategoryMap" type="com.tmerclub.cloud.seckill.model.SeckillCategory">
    <id column="category_id" property="categoryId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="name" property="name"/>
    <result column="seq" property="seq"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `category_id`,`create_time`,`update_time`,`name`,`seq`
  </sql>
  <select id="list" resultMap="seckillCategoryMap">
    select <include refid="Vo_Column_List"/> from seckill_category
    order by seq desc
  </select>
  <select id="getByCategoryId" resultMap="seckillCategoryMap">
    select <include refid="Vo_Column_List"/> from seckill_category where category_id = #{categoryId}
  </select>

  <select id="countByName" resultType="int">
    select ifnull(count(*),0) from seckill_category
    <where>
      <if test="name != null and name != ''">
        and `name` = #{name}
      </if>
      <if test="categoryId != null">
        and category_id <![CDATA[<>]]> #{categoryId}
      </if>
    </where>
  </select>

  <insert id="save">
    insert into seckill_category (`name`,`seq`)
    values (#{seckillCategory.name},#{seckillCategory.seq});
  </insert>

  <update id="update">
    update seckill_category
    <set>
      <if test="seckillCategory.name != null">
        `name` = #{seckillCategory.name},
      </if>
      <if test="seckillCategory.seq != null">
        `seq` = #{seckillCategory.seq},
      </if>
        update_time = now()
    </set>
    where category_id = #{seckillCategory.categoryId}
  </update>

  <update id="updateBatch">
    <foreach collection="seckillCategoryList" item="seckillCategory" separator=";">
      update seckill_category
      <set>
        <if test="seckillCategory.name != null">
          `name` = #{seckillCategory.name},
        </if>
        <if test="seckillCategory.seq != null">
          `seq` = #{seckillCategory.seq},
        </if>
          update_time = now()
      </set>
      where category_id = #{seckillCategory.categoryId}
    </foreach>
  </update>

  <delete id="deleteById">
    delete from seckill_category where category_id = #{categoryId}
  </delete>

</mapper>
