package com.sqx.modules.job.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.invite.service.InviteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author GYJoker
 */
@Slf4j
@Component
public class SupplyAgainSignRewardTask{

    @Autowired
    private UserService userService;
    @Autowired
    private InviteService inviteService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private CommonInfoService commonRepository;
    @Autowired
    private UserMoneyService userMoneyService;

    public void run(String params) {
        if (StringUtils.isBlank(params)) {
            log.error("参数为空");
        }

        Long userId = Long.parseLong(params);

        UserEntity userEntity = userService.selectUserById(userId);
        if (userEntity == null) {
            log.error("用户不存在");
        }

        List<Long> userIdList = inviteService.selectInviteSignByUser(userEntity.getInvitationCode());

        if (userIdList == null || userIdList.isEmpty()) {
            log.error("没有邀请的用户 用户 ID {}", userId);
        }

        Double inviteAmount = 0.0;
        String amount = commonRepository.findOne(912).getValue();
        double doubleAmount = Double.parseDouble(amount);
        for (Long inviteeUserId : userIdList) {
            QueryWrapper<UserMoneyDetails> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("classify", 6);
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("by_user_id", inviteeUserId);
            int count = userMoneyDetailsService.count(queryWrapper);
            if (count > 0) {
                log.info("已经发放过奖励, 用户 ID {}， 邀请 ID {}", userId, inviteeUserId);
                continue;
            }

            UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
            userMoneyDetails.setClassify(6);
            userMoneyDetails.setMoney(new BigDecimal(amount));
            userMoneyDetails.setUserId(userId);
            userMoneyDetails.setByUserId(inviteeUserId);
            userMoneyDetails.setCreateTime(DateUtil.now());
            userMoneyDetails.setContent("分享达标奖励" + amount + "元");
            userMoneyDetails.setTitle("分享达标奖励");
            userMoneyDetails.setState(2);
            userMoneyDetails.setType(1);
            userMoneyDetails.setMoneyType(1);
            userMoneyDetailsService.save(userMoneyDetails);
            //存入余额 钱
            inviteAmount += doubleAmount;
            log.info("发放过奖励, 用户 ID {}， 邀请 ID {}", userId, inviteeUserId);
        }

        if (inviteAmount > 0) {
            userMoneyService.updateAmount(1, userId, inviteAmount);
        }
        log.info("补发奖励结束");
    }

}
