<?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.zsElectric.boot.business.mapper.UserInfoMapper">

    <!-- 获取个人用户信息分页列表 -->
    <select id="getUserInfoPage" resultType="com.zsElectric.boot.business.model.vo.UserInfoVO">
        SELECT
        ui.id,
        ui.nick_name,
        fi.name AS firmName,
        ui.phone,
        ui.openid,
        IFNULL(ua.balance, 0) AS balance,
        IFNULL(log.total_consumption, 0) AS totalConsumption,
        ui.create_time
        FROM
        c_user_info ui
        LEFT JOIN c_user_firm uf ON uf.user_id = ui.id AND uf.is_deleted = 0
        LEFT JOIN c_firm_info fi ON fi.id = uf.firm_id AND fi.is_deleted = 0
        LEFT JOIN c_user_account ua ON ua.user_id = ui.id AND ua.is_deleted = 0
        LEFT JOIN (
            SELECT user_id, SUM(IFNULL(change_balance, 0)) AS total_consumption
            FROM c_user_account_log
            WHERE is_deleted = 0 AND change_type = 1
            GROUP BY user_id
        ) log ON ui.id = log.user_id
        <where>
            ui.is_deleted = 0
            <if test="queryParams.phone != null and queryParams.phone != ''">
                AND ui.phone LIKE CONCAT('%', #{queryParams.phone}, '%')
            </if>
            <if test="queryParams.firmId != null">
                AND uf.firm_id = #{queryParams.firmId}
            </if>
        </where>
        <choose>
            <when test="queryParams.sortField == 'balance' and queryParams.sortOrder == 'ascending'">
                ORDER BY balance ASC
            </when>
            <when test="queryParams.sortField == 'balance' and queryParams.sortOrder == 'descending'">
                ORDER BY balance DESC
            </when>
            <when test="queryParams.sortField == 'totalConsumption' and queryParams.sortOrder == 'ascending'">
                ORDER BY totalConsumption ASC
            </when>
            <when test="queryParams.sortField == 'totalConsumption' and queryParams.sortOrder == 'descending'">
                ORDER BY totalConsumption DESC
            </when>
            <otherwise>
                ORDER BY ui.create_time DESC
            </otherwise>
        </choose>
    </select>
    <select id="getAppletUserInfo" resultType="com.zsElectric.boot.business.model.vo.applet.AppUserInfoVO">
        SELECT
                ui.id AS appletUserId,
                ui.nick_name,
                ui.phone,
                ui.openid,
                ua.integral,
                ua.balance,
                ua.redeem_balance,
                (ua.balance + ua.redeem_balance) AS accountBalance,
                uf.firm_id,
                uf.type AS firmUserType,
                fi.name AS firmName
        FROM
            c_user_info ui
        LEFT JOIN c_user_account ua ON ui.id = ua.user_id
        LEFT JOIN c_user_firm uf ON ui.id = uf.user_id
        LEFT JOIN c_firm_info fi ON uf.firm_id = fi.id
        WHERE
            ui.is_deleted = 0
            AND ui.id = #{userId}
    </select>

    <!-- 查询在订单或账户中存在但UserInfo表中不存在的用户信息（用于恢复物理删除的数据） -->
    <select id="selectMissingUserInfoFromOrderAndAccount" resultType="com.zsElectric.boot.business.model.entity.UserInfo">
        SELECT 
            t.user_id AS id,
            t.openid,
            t.phone,
            COALESCE(t.phone, CONCAT('用户_', t.user_id)) AS nick_name
        FROM (
            SELECT DISTINCT uoi.user_id, uoi.openid, NULL AS phone
            FROM c_user_order_info uoi
            WHERE uoi.is_deleted = 0
            AND uoi.user_id IS NOT NULL
            AND NOT EXISTS (SELECT 1 FROM c_user_info ui WHERE ui.id = uoi.user_id)
            UNION
            SELECT DISTINCT ua.user_id, NULL AS openid, NULL AS phone
            FROM c_user_account ua
            WHERE ua.is_deleted = 0
            AND ua.user_id IS NOT NULL
            AND NOT EXISTS (SELECT 1 FROM c_user_info ui WHERE ui.id = ua.user_id)
        ) t
        GROUP BY t.user_id
    </select>

    <!-- 插入恢复的用户信息（指定ID） -->
    <insert id="insertUserInfoWithId">
        INSERT INTO c_user_info (id, nick_name, phone, openid, create_time, update_time, is_deleted)
        VALUES (#{id}, #{nickName}, #{phone}, #{openid}, NOW(), NOW(), 0)
    </insert>

</mapper>
