<?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="cn.bunny.services.mapper.financial.CategoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.bunny.dao.entity.financial.Category">
        <id column="id" property="id"/>
        <id column="create_time" property="createTime"/>
        <id column="update_time" property="updateTime"/>
        <id column="create_user" property="createUser"/>
        <id column="update_user" property="updateUser"/>
        <id column="is_deleted" property="isDeleted"/>
        <id column="user_id" property="userId"/>
        <id column="category_name" property="categoryName"/>
        <id column="is_builtin" property="isBuiltin"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, create_time, update_time, create_user, update_user, is_deleted, user_id, category_name, is_builtin
    </sql>

    <!-- 分页查询分类信息内容 -->
    <select id="selectUserListByPage" resultType="cn.bunny.dao.vo.financial.user.CategoryUserVo">
        select
        base.*,
        create_user.username as create_username,
        update_user.username as update_username
        from t_category base
        left join sys_user create_user on create_user.id = base.create_user
        left join sys_user update_user on update_user.id = base.update_user
        <where>
            base.is_deleted = 0 and (base.user_id = #{userId} or base.is_builtin = 1)
            <if test="dto.categoryName != null and dto.categoryName != ''">
                and base.category_name like CONCAT('%',#{dto.categoryName},'%')
            </if>
            <if test="dto.isBuiltin != null and dto.isBuiltin != ''">
                and base.is_builtin = #{dto.isBuiltin}
            </if>
        </where>
        order by base.is_builtin desc
    </select>

    <!-- 物理删除分类信息 -->
    <delete id="deleteBatchIdsWithUserPhysics">
        delete
        from t_category
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- 分页查询分类信息内容 -->
    <select id="selectListByPage" resultType="cn.bunny.dao.vo.financial.admin.CategoryVo">
        select
        base.*,
        category_user.username as username,
        create_user.username as create_username,
        update_user.username as update_username
        from t_category base
        left join sys_user create_user on create_user.id = base.create_user
        left join sys_user update_user on update_user.id = base.update_user
        left join sys_user category_user on category_user.id = base.user_id
        <where>
            base.is_deleted = 0
            <if test="dto.userId != null and dto.userId != ''">
                and base.user_id = #{dto.userId}
            </if>
            <if test="dto.categoryName != null and dto.categoryName != ''">
                and base.category_name like CONCAT('%',#{dto.categoryName},'%')
            </if>
            <if test="dto.isBuiltin != null and dto.isBuiltin != ''">
                and base.is_builtin = #{dto.isBuiltin}
            </if>
        </where>
        order by base.is_builtin desc
    </select>

    <!-- 物理删除分类信息 -->
    <delete id="deleteBatchIdsWithPhysics">
        delete
        from t_category
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

</mapper>
