<?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.unilife.mapper.CategoryMapper">
    <resultMap id="categoryResultMap" type="com.unilife.model.entity.Category">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="description" property="description"/>
        <result column="icon" property="icon"/>
        <result column="sort" property="sort"/>
        <result column="status" property="status"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
    </resultMap>
    
    <select id="getById" resultMap="categoryResultMap">
        SELECT id, name, description, icon, sort, status, created_at, updated_at
        FROM categories
        WHERE id = #{id}
    </select>
    
    <select id="getList" resultMap="categoryResultMap">
        SELECT id, name, description, icon, sort, status, created_at, updated_at
        FROM categories
        <where>
            <if test="status != null">
                status = #{status}
            </if>
        </where>
        ORDER BY sort ASC, id ASC
    </select>
    
    <insert id="insert" parameterType="com.unilife.model.entity.Category" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO categories (
            name, description, icon, sort, status, created_at, updated_at
        ) VALUES (
            #{name}, #{description}, #{icon}, #{sort}, #{status}, NOW(), NOW()
        )
    </insert>
    
    <update id="update" parameterType="com.unilife.model.entity.Category">
        UPDATE categories
        SET name = #{name},
            description = #{description},
            icon = #{icon},
            sort = #{sort},
            status = #{status},
            updated_at = NOW()
        WHERE id = #{id}
    </update>
    
    <update id="delete">
        UPDATE categories
        SET status = 0,
            updated_at = NOW()
        WHERE id = #{id}
    </update>
    
    <select id="getCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM categories
        <where>
            <if test="status != null">
                status = #{status}
            </if>
        </where>
    </select>
</mapper>