package com.sonic.frog.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sonic.common.enums.AppEnv;
import com.sonic.frog.dao.AiUserAlbumDao;
import com.sonic.frog.dao.AiUserAlbumUnlockDao;
import com.sonic.frog.domain.entity.AiUserAlbum;
import com.sonic.frog.domain.entity.AiUserAlbumUnlock;
import com.sonic.frog.enums.LockStatusEnum;
import com.sonic.frog.enums.ToastResultCode;
import com.sonic.frog.service.AiUserAlbumUnlockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zzhan
 * @Description 查询话题是否对访问用户解锁
 * @Date 2023/8/28 13:57
 * @Version 1.0
 */
@Slf4j
@Service
public class AiUserAlbumUnlockServiceImpl extends ServiceImpl<AiUserAlbumUnlockDao, AiUserAlbumUnlock> implements AiUserAlbumUnlockService {

    @Value("${spring.profiles.active}")
    private String runMode;

    /**
     * TODO 这里配置的是QA环境的
     */
    private static final List<Long> OFFICIAL_UN_LOCK_USER_ID_LIST_TEST = Lists.newArrayList();

    /**
     * TODO 这里配置他们要的官方号【线上环境】
     */
    private static final List<Long> OFFICIAL_UN_LOCK_USER_ID_LIST_PROD = Lists.newArrayList();


    /**
     * 获取官方号的配置
     *
     * @return
     */
    private List<Long> getOfficialUnLockUserIdList() {
        if (AppEnv.product.name().equals(runMode)) {
            return OFFICIAL_UN_LOCK_USER_ID_LIST_PROD;
        }
        return OFFICIAL_UN_LOCK_USER_ID_LIST_TEST;
    }


    @Override
    public Map<Long, LockStatusEnum> queryAlbumUnlock(Long currentUserId, List<AiUserAlbum> aiUserAlbumList) {
        if (CollectionUtils.isEmpty(aiUserAlbumList)) {
            return Maps.newHashMap();
        }
        //批量查询用户是对相册图片付费解锁
        List<Long> albumIds = aiUserAlbumList.stream().map(AiUserAlbum::getId).collect(Collectors.toList());
        List<AiUserAlbumUnlock> list = list(Wrappers.<AiUserAlbumUnlock>lambdaQuery().eq(AiUserAlbumUnlock::getUserId, currentUserId).in(AiUserAlbumUnlock::getAlbumId, albumIds));
        List<Long> unlockAlbumIds = list.stream().map(AiUserAlbumUnlock::getAlbumId).collect(Collectors.toList());
        //解锁状态map
        Map<Long, LockStatusEnum> aiUserAlbumLockStatusMap = Maps.newHashMap();
        for (AiUserAlbum aiUserAlbum : aiUserAlbumList) {
            Long userId = aiUserAlbum.getUserId();
            Long id = aiUserAlbum.getId();
            //如果没有加锁，默认为null
            if (aiUserAlbum.getUnlockPrice() == 0L) {
                aiUserAlbumLockStatusMap.put(id, null);
                continue;
            }
            //官方用户访问，解锁
            if (getOfficialUnLockUserIdList().contains(currentUserId)) {
                aiUserAlbumLockStatusMap.put(id, LockStatusEnum.UNLOCK);
                continue;
            }
            //是相册主人访问，解锁
            if (userId.equals(currentUserId)) {
                aiUserAlbumLockStatusMap.put(id, LockStatusEnum.UNLOCK);
                continue;
            }
            //查看用户是否对相册图片付费解锁
            if (unlockAlbumIds.contains(id)) {
                aiUserAlbumLockStatusMap.put(id, LockStatusEnum.UNLOCK);
                continue;
            }
            aiUserAlbumLockStatusMap.put(id, LockStatusEnum.LOCK);
        }
        return aiUserAlbumLockStatusMap;
    }

    @Override
    public void checkAlbumLockStatus(Long currentUserId, AiUserAlbum aiUserAlbum) {
        Map<Long, LockStatusEnum> aiUserAlbumLockStatusMap = queryAlbumUnlock(currentUserId, Lists.newArrayList(aiUserAlbum));
        //提示是否有操作权限
        ToastResultCode.SYS_PERMISSION_DENIED.check(LockStatusEnum.LOCK == aiUserAlbumLockStatusMap.get(aiUserAlbum.getId()));
    }

    @Override
    public List<Long> getUnlockAlbumIds(Long currentUserId) {
        List<AiUserAlbumUnlock> list = list(Wrappers.<AiUserAlbumUnlock>lambdaQuery().eq(AiUserAlbumUnlock::getUserId, currentUserId));
        return list.stream().map(AiUserAlbumUnlock::getAlbumId).collect(Collectors.toList());
    }

    @Override
    public void addUnlockAlbumRecord(Long currentUserId, Long albumId, String orderNo) {
        AiUserAlbumUnlock aiUserAlbumUnlock = AiUserAlbumUnlock.builder().albumId(albumId).userId(currentUserId).orderNo(orderNo).build();
        save(aiUserAlbumUnlock);
    }

    @Override
    public Boolean isUnlockAlbumId(Long currentUserId, Long albumId) {
        int count = count(Wrappers.<AiUserAlbumUnlock>lambdaQuery().eq(AiUserAlbumUnlock::getUserId, currentUserId).eq(AiUserAlbumUnlock::getAlbumId, albumId));
        return count > 0;
    }
}
