<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mall.category.mapper.CategoryMapper">
  <select id="findAll" resultType="com.zioinfo.mall.category.MallCategory">
    SELECT * FROM mall_category WHERE active = TRUE ORDER BY COALESCE(parent_id,0), sort_order, id
  </select>
  <select id="findByParent" resultType="com.zioinfo.mall.category.MallCategory">
    SELECT * FROM mall_category
    WHERE active = TRUE AND
    <choose><when test="parentId != null">parent_id = #{parentId}</when><otherwise>parent_id IS NULL</otherwise></choose>
    ORDER BY sort_order, id
  </select>
  <select id="findById" resultType="com.zioinfo.mall.category.MallCategory">SELECT * FROM mall_category WHERE id = #{id}</select>
  <insert id="insert" parameterType="com.zioinfo.mall.category.MallCategory" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_category (parent_id, name, code, sort_order, active)
    VALUES (#{parentId}, #{name}, #{code}, COALESCE(#{sortOrder},0), COALESCE(#{active},TRUE))
  </insert>
  <update id="update">
    UPDATE mall_category SET parent_id=#{parentId}, name=#{name}, code=#{code},
      sort_order=COALESCE(#{sortOrder},sort_order), active=COALESCE(#{active},active) WHERE id=#{id}
  </update>
  <delete id="delete">DELETE FROM mall_category WHERE id=#{id}</delete>
</mapper>
