package com.ycwl.basic.service.pc.processor;

import com.ycwl.basic.biz.OrderBiz;
import com.ycwl.basic.constant.BuyStatus;
import com.ycwl.basic.constant.FreeStatus;
import com.ycwl.basic.model.mobile.order.IsBuyRespVO;
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 购买状态处理器
 * 负责处理源文件的购买状态和免费状态
 *
 * @author longbinbin
 * @date 2025-01-31
 */
@Slf4j
@Component
public class BuyStatusProcessor {

    @Autowired
    private OrderBiz orderBiz;

    /**
     * 处理购买状态逻辑
     * 设置每个源文件的购买状态和免费状态
     *
     * @param memberSourceEntityList 源文件关联列表
     * @param freeSourceIds          免费的源文件ID列表
     * @param memberId               会员ID
     * @param scenicId               景区ID
     * @param faceId                 人脸ID
     */
    public void processBuyStatus(List<MemberSourceEntity> memberSourceEntityList,
                                 List<Long> freeSourceIds,
                                 Long memberId,
                                 Long scenicId,
                                 Long faceId) {
        if (memberSourceEntityList.isEmpty()) {
            return;
        }

        // 获取用户购买状态
        IsBuyRespVO isBuy = orderBiz.isBuy(scenicId, memberId, faceId,
                memberSourceEntityList.getFirst().getType(),
                faceId);

        for (MemberSourceEntity memberSourceEntity : memberSourceEntityList) {
            // 设置购买状态
            if (isBuy.isBuy()) {
                // 如果用户买过
                memberSourceEntity.setIsBuy(BuyStatus.BOUGHT.getCode());
            } else if (isBuy.isFree()) {
                // 全免费逻辑
                memberSourceEntity.setIsBuy(BuyStatus.BOUGHT.getCode());
            } else {
                memberSourceEntity.setIsBuy(BuyStatus.NOT_BOUGHT.getCode());
            }

            // 设置免费状态
            if (freeSourceIds.contains(memberSourceEntity.getSourceId())) {
                memberSourceEntity.setIsFree(FreeStatus.FREE.getCode());
            }
        }

        log.debug("购买状态处理完成：用户购买状态 isBuy={}, isFree={}, 免费源文件数量={}",
                isBuy.isBuy(), isBuy.isFree(), freeSourceIds.size());
    }
}
