package com.czg.account.entity;

import com.czg.utils.CzgRandomUtils;
import com.mybatisflex.annotation.*;
import com.mybatisflex.core.mask.Masks;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * 商户储值会员 实体类。
 *
 * @author Administrator
 * @since 2025-02-08
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("tb_shop_user")
@Accessors(chain = true)
public class ShopUser implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * (随机)
     */
    @Id(keyType = KeyType.Auto)
    private Long id;

    /**
     * 用户头像
     */
    private String headImg;

    /**
     * 用户昵称
     */
//    @ColumnMask(Masks.CHINESE_NAME)
    private String nickName;

    /**
     * 电话号码
     */
//    @ColumnMask(Masks.MOBILE)
    private String phone;

    /**
     * 会员生日
     */
    private String birthDay;
    /**
     * 会员生日提醒
     */
    private Integer birthDayRemind;

    /**
     * 0-女 1男
     */
    private Integer sex;

    /**
     * 主店铺Id
     */
    private Long mainShopId;

    /**
     * 来源店铺Id
     */
    private Long sourceShopId;

    /**
     * 用户Id
     */
    private Long userId;
//
//    /**
//     * 账户积分
//     */
//    private Integer accountPoints;

    /**
     * 钱包余额
     */
    private BigDecimal amount;
    /**
     * 充值次数
     */
    private Integer rechargeCount;
    /**
     * 消费累计
     */
    private BigDecimal consumeAmount;

    /**
     * 消费次数累计
     */
    private Integer consumeCount;

    /**
     * 0-不可使用 1可使用
     */
    private Integer status;

    /**
     * 是否会员，
     */
    private Integer isVip;

    /**
     * 会员编号
     */
    private String code;

//    /**
//     * 最近一次积分变动时间
//     */
//    private LocalDateTime lastPointsChangeTime;
//
//    /**
//     * 最近一次浮动积分
//     */
//    private Integer lastFloatPoints;

    /**
     * 成为会员的时间
     */
    private LocalDateTime joinTime;

    @Column(onInsertValue = "now()")
    private LocalDateTime createTime;

    @Column(onInsertValue = "now()", onUpdateValue = "now()")
    private LocalDateTime updateTime;

    private Long memberLevelId;

    private Long experience;
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    private LocalDateTime deliverTime;
    private LocalDateTime nextDeliverTime;
    // 是否分销员
    private String distributionShops;
    //分销店铺确认
    private String shopConfirm;

    private String memberCircleName;
    private Integer memberCircleReward;

    @Column(ignore = true)
    private String memberName;
    private String openType;
    @Column(ignore = true)
    private Integer discount;
    @Column(ignore = true)
    private Integer isMemberPrice;

    /**
     * 添加或更新分销店铺：若shopId已存在（无论后缀），则替换为新的id_后缀；否则插入
     *
     * @param shopId 店铺ID
     * @param suffix 新后缀
     */
    public void upDistributionShop(Long shopId, int suffix) {
        if (shopId == null) {
            return;
        }
        String shopIdStr = shopId.toString().trim();
        if (shopIdStr.isEmpty()) {
            return;
        }

        // 解析原有字符串为Set
        Set<String> shopSet = new HashSet<>();
        if (distributionShops != null && !distributionShops.trim().isEmpty()) {
            for (String idWithSuffix : distributionShops.split(",")) {
                String trimmed = idWithSuffix.trim();
                if (trimmed.isEmpty()) {
                    continue;
                }
                // 判断：如果不是以 "shopId_" 开头的记录，则保留
                if (!trimmed.startsWith(shopIdStr + "_")) {
                    shopSet.add(trimmed);
                }
            }
        }
        // 添加新的id_后缀
        shopSet.add(shopIdStr + "_" + suffix);
        // 重新拼接字符串
        distributionShops = String.join(",", shopSet);
    }

    public static String generateCode() {
        return CzgRandomUtils.randomNumber(10, true);
    }
}
