package com.mcwl.myInvitation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.utils.ShareCodeUtils;
import com.mcwl.myInvitation.domain.Invitation;
import com.mcwl.myInvitation.domain.dto.EarningsDisplay;
import com.mcwl.myInvitation.domain.dto.EarningsDisplayDto;
import com.mcwl.myInvitation.domain.dto.SysUserDto;
import com.mcwl.myInvitation.mapper.InvitationMapper;
import com.mcwl.myInvitation.service.ConsumeService;
import com.mcwl.myInvitation.service.InvitationService;
import com.mcwl.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation> implements InvitationService {


    private final ConsumeService consumeService;

    private final ISysUserService sysUserService;


    @Override
    public String getInvitationCode(Long userId) {
        if (userId == null) {
            return null;
        }
        // 生成邀请码
        return ShareCodeUtils.idToCode(userId);
    }

    @Override
    public List<EarningsDisplayDto> getEarningsDisplay(Long userId) {
        List<EarningsDisplay> earningsDisplay = baseMapper.getEarningsDisplay(userId);
        if (earningsDisplay == null || earningsDisplay.isEmpty()) {
            return Collections.emptyList();
        }

        List<EarningsDisplayDto> list = new ArrayList<>();
        earningsDisplay.forEach(item -> {
            EarningsDisplayDto edd = new EarningsDisplayDto();
            SysUser sysUser = sysUserService.selectUserById(item.getUserId());
            edd.setUser(BeanUtil.copyProperties(sysUser, SysUserDto.class));
            edd.setCount(item.getCount());
            list.add(edd);
        });

        return list;
    }

    @Override
    public Double getTotalAmount(Long userId) {
        return baseMapper.getTotalAmount(userId);
    }
}
