<?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.njzscloud.supervisory.money.mapper.MoneyChangeDetailMapper">
    <!-- 分页查询资金账户（关联用户或企业信息） -->
    <select id="page" resultType="com.njzscloud.supervisory.money.pojo.entity.MoneyChangeDetailEntity">
        SELECT
        mcd.id,
        mcd.user_id,
        mcd.money_account_id,
        mcd.old_money,
        mcd.delta,
        mcd.new_money,
        mcd.money_change_category,
        mcd.extend_info,
        mcd.memo,
        mcd.company_id,
        mcd.creator_id,
        mcd.modifier_id,
        mcd.create_time,
        mcd.modify_time,
        mcd.deleted,
        CASE
            WHEN mcd.user_id IS NOT NULL THEN u.nickname
            WHEN mcd.company_id IS NOT NULL THEN bc.company_name
            ELSE NULL
            END as nickname
        FROM money_change_detail mcd
        LEFT JOIN sys_user u ON mcd.user_id = u.id
        LEFT JOIN biz_company bc ON mcd.company_id = bc.id
        <where>
            <if test="entity.nickname != null and entity.nickname != ''">
                AND u.nickname LIKE CONCAT('%', #{entity.nickname}, '%')
            </if>
            <if test="entity.moneyChangeCategory != null">
                AND mcd.money_change_category = #{entity.moneyChangeCategory}
            </if>
        </where>
        ORDER BY mcd.modify_time DESC
    </select>
</mapper>