package com.cyksj.service.quota.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.constant.Constant;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.*;
import com.cyksj.model.entity.*;
import com.cyksj.service.quota.NanoUserQuotaService;
import com.cyksj.service.user.UserBindRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户固定配额管理服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NanoUserQuotaServiceImpl extends ServiceImpl<NanoUserQuotaMapper, NanoUserQuota> implements NanoUserQuotaService {
    private final UserBindRelationService userBindRelationService;

    private final GroupsRelationMapper groupsRelationMapper;

    private final GroupsMapper groupsMapper;

    private final UserMapper userMapper;

    private final GoodsDonSkuMapper skuMapper;

    @Override
    public NanoUserQuota getNanoUser(Long userId, Long relationId) {
        GroupsRelation groupsRelation = getGroupsRelation(userId, relationId);
        GroupsTrips groupsTrips = getGroupsTrips(groupsRelation);
        GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
        if (goodsDonSku != null && goodsDonSku.getGoodsId() == Constant.NANO_GOODS_ID) {
            return getNanoUser(userId, relationId, groupsRelation, goodsDonSku);
        } else {
            throw BusinessRuntimeException.getInstance("服务器出了点问题");
        }
    }

    private GroupsRelation getGroupsRelation(Long userId, Long relationId) {
        List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
        GroupsRelation groupsRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).in(GroupsRelation::getUserId, userIdList).eq(GroupsRelation::getId, relationId));
        if (groupsRelation == null) {
            throw BusinessRuntimeException.getInstance("车票不存在");
        }
        return groupsRelation;
    }

    /**
     * 获取车队信息
     *
     * @param groupsRelation
     * @return
     */
    private GroupsTrips getGroupsTrips(GroupsRelation groupsRelation) {
        GroupsTrips groupsTrips = groupsMapper.selectById(groupsRelation.getGroupsId());
        if (groupsTrips == null) {
            throw BusinessRuntimeException.getInstance("车队异常");
        }
        return groupsTrips;
    }

    private NanoUserQuota getNanoUser(Long userId, Long relationId, GroupsRelation groupsRelation, GoodsDonSku goodsDonSku) {
        NanoUserQuota nanoUser = getOne(Wrappers.lambdaQuery(NanoUserQuota.class).eq(NanoUserQuota::getRelationId, relationId));
        User user = userMapper.selectById(userId);
        if (nanoUser == null) {
            nanoUser = createNanoUser(groupsRelation, user, goodsDonSku);
        } else {
            updateLumaUser(user, nanoUser);
        }
        return nanoUser;
    }


    private NanoUserQuota createNanoUser(GroupsRelation groupsRelation, User user, GoodsDonSku sku) {
        NanoUserQuota nanoUserQuota = new NanoUserQuota();
        nanoUserQuota.setUid(user.getId());
        nanoUserQuota.setRelationId(groupsRelation.getId());
        nanoUserQuota.setUserToken(UUID.randomUUID().toString());
        nanoUserQuota.setNickname(user.getNickname());
        nanoUserQuota.setAvatar(user.getHeadimgurl());
        nanoUserQuota.setPurchaseDate(DateTime.now());
        nanoUserQuota.setTotalQuota(sku.getNanoQuota());
        nanoUserQuota.setRemainingQuota(nanoUserQuota.getTotalQuota());
        try {
            save(nanoUserQuota);
        } catch (DuplicateKeyException e) {

        }
        return nanoUserQuota;
    }

    private void updateLumaUser(User user, NanoUserQuota nanoUserQuota) {
        nanoUserQuota.setNickname(user.getNickname());
        nanoUserQuota.setAvatar(user.getHeadimgurl());
        updateById(nanoUserQuota);
    }

}