package com.sonic.lion.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sonic.lion.dao.UserSubscriptionDao;
import com.sonic.lion.domain.entity.UserSubscription;
import com.sonic.lion.domain.input.PlatformGiftInput;
import com.sonic.lion.domain.output.MemberDetailOutput;
import com.sonic.lion.service.MemberPrivDictService;
import com.sonic.lion.service.MemberService;
import com.sonic.lion.service.PayTradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private UserSubscriptionDao userSubscriptionDao;
    @Autowired
    private MemberPrivDictService memberPrivDictService;
    @Autowired
    private PayTradeService payTradeService;

    @Override
    public MemberDetailOutput memberDetail(Long userId) {
        MemberDetailOutput output = new MemberDetailOutput();
        //当前用户会员信息
        output.setUserMemberInfo(userSubscriptionDao.getByUserId(userId));
        //用户会员权限列表
        output.setMemberPrivList(memberPrivDictService.getMemberPrivList());
        return output;
    }

    @Override
    public Integer userMemberGiftBuffJob() {
        //查询所有未过期的用户信息
        List<Long> userIdList = userSubscriptionDao.getNotExpiredSubscriptionList();
        if (CollectionUtils.isEmpty(userIdList)) {
            return 0;
        }
        for (Long userId : userIdList) {
            //赠送5个CrushCoin
            PlatformGiftInput platformGiftInput = new PlatformGiftInput();
            platformGiftInput.setPlatform("1");
            platformGiftInput.setUid(userId);
            platformGiftInput.setName(PlatformGiftInput.Type.VIP_BUFF_GIFT.getBizType().getDesc());
            platformGiftInput.setCreateTime(LocalDateTime.now());
            platformGiftInput.setAmount(500L);
            platformGiftInput.setType(PlatformGiftInput.Type.VIP_BUFF_GIFT);
            payTradeService.platformGift(platformGiftInput);
        }
        return userIdList.size();
    }
}
