package com.ycwl.basic.repository;

import com.ycwl.basic.mapper.SourceMapper;
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
import com.ycwl.basic.pricing.dto.VoucherInfo;
import com.ycwl.basic.pricing.enums.VoucherDiscountType;
import com.ycwl.basic.pricing.service.IVoucherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class SourceRepository {
    @Autowired
    private SourceMapper sourceMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private IVoucherService iVoucherService;
    @Autowired
    private FaceRepository faceRepository;

    public void addSource(SourceEntity source) {
        sourceMapper.add(source);
    }

    public void setUserIsBuyItem(Long memberId, int type, Long faceId, Long orderId) {
        MemberSourceEntity memberSource = new MemberSourceEntity();
        memberSource.setMemberId(memberId);
        memberSource.setFaceId(faceId);
        memberSource.setType(type);
        memberSource.setOrderId(orderId);
        memberSource.setIsBuy(1);
        sourceMapper.updateRelation(memberSource);
    }

    public void setUserNotBuyItem(Long memberId, int type, Long faceId) {
        MemberSourceEntity memberSource = new MemberSourceEntity();
        memberSource.setMemberId(memberId);
        memberSource.setFaceId(faceId);
        memberSource.setType(type);
        memberSource.setOrderId(null);
        memberSource.setIsBuy(0);
        sourceMapper.updateRelation(memberSource);
    }

    public boolean getUserIsBuy(Long userId, int type, Long faceId) {
        FaceEntity face = faceRepository.getFace(faceId);
        if (face == null) {
            log.info("faceId:{} is not exist", faceId);
            return false;
        }
        // 确认人员faceId是否有券码
        List<VoucherInfo> voucherDetails = iVoucherService.getVoucherDetails(faceId, face.getScenicId());
        if (voucherDetails != null && !voucherDetails.isEmpty()) {
            VoucherInfo voucherInfo = voucherDetails.getFirst();
            if (voucherInfo.getDiscountType().equals(VoucherDiscountType.FREE_ALL)) {
                return true;
            }
        }
        switch (type) {
            case 1:
                List<SourceEntity> videoSourceList = sourceMapper.listVideoByFaceRelation(userId, faceId);
                if (videoSourceList == null || videoSourceList.isEmpty()) {
                    return false;
                }
                return videoSourceList.stream().filter(Objects::nonNull).anyMatch(item -> Integer.valueOf(1).equals(item.getIsBuy()));
            case 2:
                List<SourceEntity> imageSourceList = sourceMapper.listImageByFaceRelation(userId, faceId);
                if (imageSourceList == null || imageSourceList.isEmpty()) {
                    return false;
                }
                return imageSourceList.stream().filter(Objects::nonNull).anyMatch(item -> Integer.valueOf(1).equals(item.getIsBuy()));
            default:
                return false;
        }
    }

    public SourceEntity getSource(Long id) {
        return sourceMapper.getEntity(id);
    }
}
