package com.sonic.frog.service.impl;

import com.alibaba.fastjson.JSON;
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.sonic.frog.dao.HeartbeatLevelDictDao;
import com.sonic.frog.domain.entity.HeartbeatLevelDict;
import com.sonic.frog.domain.output.HeartbeatLevelDictOutput;
import com.sonic.frog.enums.HeartbeatLevelEnum;
import com.sonic.frog.service.HeartbeatLevelDictService;
import com.sonic.frog.utils.BeanConver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 心动等级字典表服务实现类
 */
@Service
@Slf4j
public class HeartbeatLevelDictServiceImpl extends ServiceImpl<HeartbeatLevelDictDao, HeartbeatLevelDict> implements HeartbeatLevelDictService {
    public List<HeartbeatLevelEnum> getUnlockListByCode(HeartbeatLevelEnum code) {
        if (code == null) {
            return Collections.emptyList();
        }
        HeartbeatLevelDict heartbeatLevelDict = getOne(Wrappers.<HeartbeatLevelDict>lambdaQuery().eq(HeartbeatLevelDict::getCode, code).last("limit 1"));
        if (heartbeatLevelDict != null) {
            String unlockCode = heartbeatLevelDict.getUnlockCode();
            return JSON.parseArray(unlockCode, HeartbeatLevelEnum.class);
        }
        return Collections.emptyList();
    }

    @Override
    public List<HeartbeatLevelDictOutput> getHearbeatLevelDictList(HeartbeatLevelEnum aiUserHeartbeatLevel) {
        //获取字典列表
        List<HeartbeatLevelDict> list = list(Wrappers.<HeartbeatLevelDict>lambdaQuery()
                .eq(HeartbeatLevelDict::getIsDelete, false)
                .orderByAsc(HeartbeatLevelDict::getSort)
        );
        //获取用户解锁的心动等级
        List<HeartbeatLevelEnum> unlockList = getUnlockListByCode(aiUserHeartbeatLevel);
        //构建输出
        List<HeartbeatLevelDictOutput> heartbeatLevelDictOutputList = Lists.newArrayList();
        for (HeartbeatLevelDict heartbeatLevelDict : list) {
            HeartbeatLevelDictOutput heartbeatLevelDictOutput = BeanConver.copeBean(heartbeatLevelDict, HeartbeatLevelDictOutput.class);
            //是否解锁
            boolean isUnlock = unlockList.contains(heartbeatLevelDict.getCode());
            heartbeatLevelDictOutput.setIsUnlock(isUnlock);
            //未解锁，显示未解锁图片
            if (!isUnlock) {
                heartbeatLevelDictOutput.setImgUrl(heartbeatLevelDict.getUnlockImgUrl());
            }
            heartbeatLevelDictOutput.setNum(heartbeatLevelDictOutput.getCode().getNum());
            heartbeatLevelDictOutputList.add(heartbeatLevelDictOutput);
        }
        return heartbeatLevelDictOutputList;
    }

    @Override
    public HeartbeatLevelDict getHeartbeatLevelByVal(BigDecimal heartbeatVal) {
        return getOne(Wrappers.<HeartbeatLevelDict>lambdaQuery()
                .le(HeartbeatLevelDict::getStartVal, heartbeatVal)
                .ge(HeartbeatLevelDict::getEndVal, heartbeatVal)
                .last("limit 1")
        );
    }

    @Override
    public HeartbeatLevelDict getHeartbeatLevelDictByLevel(HeartbeatLevelEnum heartbeatLevelEnum) {
        return getOne(Wrappers.<HeartbeatLevelDict>lambdaQuery()
                .eq(HeartbeatLevelDict::getCode, heartbeatLevelEnum)
                .last("limit 1")
        );
    }

    @Override
    public Map<HeartbeatLevelEnum, HeartbeatLevelDict> mapByLevel(Set<HeartbeatLevelEnum> heartbeatLevelEnums) {
        if (CollectionUtils.isEmpty(heartbeatLevelEnums)) {
            return Collections.emptyMap();
        }
        List<HeartbeatLevelDict> list = list(Wrappers.<HeartbeatLevelDict>lambdaQuery().in(HeartbeatLevelDict::getCode, heartbeatLevelEnums));
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().collect(Collectors.toMap(HeartbeatLevelDict::getCode, Function.identity()));
        }
        return Collections.emptyMap();
    }
}