<?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.BillMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.bunny.dao.entity.financial.Bill">
        <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="type" property="type"/>
        <id column="amount" property="amount"/>
        <id column="description" property="description"/>
        <id column="transaction_date" property="transactionDate"/>
        <id column="category_id" property="categoryId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, create_time, update_time, create_user, update_user, is_deleted, type, amount, description, transaction_date, category_id
    </sql>

    <!-- 分页查询账单信息内容 -->
    <select id="selectListByPage" resultType="cn.bunny.dao.vo.financial.user.BillUserVo">
        select
        base.*,
        category.category_name,
        category.id as category_id,
        create_user.username as create_username,
        update_user.username as update_username
        from t_bill base
        left join t_category category on category.id = base.category_id
        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.create_user = #{userId}
            <if test="dto.type != null and dto.type != ''">
                and base.type like CONCAT('%',#{dto.type},'%')
            </if>
            <if test="dto.description != null and dto.description != ''">
                and base.description like CONCAT('%',#{dto.description},'%')
            </if>
            <if test="dto.startDate != null and dto.endDate != null">
                and base.transaction_date between #{dto.startDate} and #{dto.endDate}
            </if>
        </where>
    </select>

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

</mapper>
