<?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.ai.da.mapper.primary.AccountMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ai.da.mapper.primary.entity.Account">
        <id column="id" property="id" />
        <result column="user_email" property="userEmail" />
        <result column="user_name" property="userName" />
        <result column="user_password" property="userPassword" />
        <result column="create_date" property="createDate" />
    </resultMap>
    <select id="findByPhoneList" resultType="com.ai.da.mapper.primary.entity.Account">
        select id , user_email AS userEmail, user_Name AS userName,
               user_password AS userPassword from t_account
        where user_Name in
        <foreach collection="phoneList" item="phone" open="(" separator="," close=")">
            #{phone}
        </foreach>
    </select>
    <select id="findById" resultType="com.ai.da.mapper.primary.entity.Account">
        select id ,user_email AS userEmail, user_phone AS userPhone,
        user_password AS userPassword from t_account
        where id =  #{id}
    </select>
    <update id="toVisitor">
        update t_account
        set is_trial = 0, credits = 0, system_user = 0,
        organization_name = null, organization_id = null, credits_usage = null, credits_usage_limit = null, sub_account_num = null,
        parent_id = null, subscription_plan_id = null
        where id = #{id}
    </update>

    <!-- 查询用户积分使用情况，可灵活按事件类型查询或查询全部 -->
    <select id="selectCreditUsage" resultType="com.ai.da.model.dto.AccountCreditsUsageDTO">
        SELECT
        a.id id,
        a.user_email,
        a.credits currentCredits,
        <if test="groupByEvent">
            cd.change_event generateFunction,
        </if>
        cd.total_changed_credits consumedCredits,
        cd.count_account_id usageCount
        FROM
        t_account a
        LEFT JOIN
        (SELECT
        account_id,
        <if test="groupByEvent">
            change_event,
        </if>
        SUM(changed_credits) AS total_changed_credits,
        COUNT(account_id) AS count_account_id
        FROM
        t_credits_detail
        WHERE
        changed_credits &lt;= 0
        <if test="changeEvent != null and changeEvent != ''">
            AND change_event = #{changeEvent}
        </if>
        <!-- 添加时间区间查询条件 -->
        <if test="startTime != null and startTime != ''">
            AND cd.create_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND cd.create_time &lt;= #{endTime}
        </if>
        GROUP BY
        account_id
        <if test="groupByEvent">
            , change_event
        </if>
        ) cd
        ON
        a.id = cd.account_id
        WHERE
        <choose>
            <when test="role == 'corp'">
                a.system_user IN (5, 6)
            </when>
            <when test="role == 'edu'">
                a.system_user IN (7, 8)
            </when>
            <when test="role == 'prsn'">
                a.system_user IN (0, 1, 2, 3, 4)
            </when>
        </choose>
        <if test="organizationName != null and organizationName != ''">
            AND a.organization_name = #{organizationName}
        </if>
        <if test="id != null">
            AND a.id = #{id}
        </if>
        <if test="userEmail != null and userEmail != ''">
            AND a.user_email = #{userEmail}
        </if>
        <!-- 添加分页查询 -->
        ORDER BY a.id
        <if test="size != null and offset != null">
            LIMIT #{size} OFFSET #{offset}
        </if>
    </select>

    <!-- 查询总记录数 (优化版) -->
    <select id="countCreditUsage" resultType="int">
        SELECT COUNT(DISTINCT a.id)
        FROM t_account a
        <if test="changeEvent != null or startTime != null or endTime != null">
            LEFT JOIN t_credits_detail cd ON a.id = cd.account_id
            AND cd.changed_credits &lt;= 0
            <if test="changeEvent != null and changeEvent != ''">
                AND cd.change_event = #{changeEvent}
            </if>
            <if test="startTime != null and startTime != ''">
                AND cd.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND cd.create_time &lt;= #{endTime}
            </if>
        </if>
        WHERE
        <choose>
            <when test="role == 'corp'">
                a.system_user IN (5, 6)
            </when>
            <when test="role == 'edu'">
                a.system_user IN (7, 8)
            </when>
            <when test="role == 'prsn'">
                a.system_user IN (0, 1, 2, 3, 4)
            </when>
        </choose>
        <if test="organizationName != null and organizationName != ''">
            AND a.organization_name = #{organizationName}
        </if>
        <if test="userEmail != null and userEmail != ''">
            AND a.user_email = #{userEmail}
        </if>
    </select>

</mapper>
