package cn.wisenergy.service.Manager;

import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
import cn.wisenergy.model.enums.RebateStatusEnum;
import cn.wisenergy.model.enums.TradeRecordEnum;
import cn.wisenergy.model.vo.TeamPerformanceSortVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 86187
 * @ Description: 账户管理公共类
 * @ Author     : 86187
 * @ Date       : 2021/2/23 10:43
 */
@Component
@Slf4j
public class AccountManager {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TradeRecordMapper recordMapper;

    @Autowired
    private TeamPerformanceMapper teamPerformanceMapper;

    @Autowired
    private ProgressPrizeMapper progressPrizeMapper;

    @Autowired
    private TradeRecordMapper tradeRecordMapper;

    private static final String PATTERN = "yyyy-MM";


    /**
     * 保存用户佣金
     *
     * @param orderInfo     订单信息
     * @param accountInfo   账户信息
     * @param memberPercent 会员等级百比分
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateOrderAddMoney(OrderInfo orderInfo, AccountInfo accountInfo, MemberPercent memberPercent) {
        //1、计算返佣金额
        BigDecimal bigDecimal = orderInfo.getPayment().multiply(memberPercent.getPercent());
        BigDecimal extractMoney = accountInfo.getExtractMoney().add(bigDecimal);
        accountInfo.setExtractMoney(extractMoney);

        BigDecimal performanceMonth = accountInfo.getEarningsMonth().add(bigDecimal);
        accountInfo.setEarningsMonth(performanceMonth);

        BigDecimal performanceTotal = accountInfo.getEarningsMonth().add(bigDecimal);
        accountInfo.setEarningsMonth(performanceTotal);
        //2、修改订单返佣状态:已返佣 1
        orderInfo.setRebateStatus(RebateStatusEnum.ALREADY_REBATE.getCode());

        orderMapper.updateById(orderInfo);

        //3、增加账户可用金额
        accountMapper.updateById(accountInfo);

        //4、添加交易流水记录
        TradeRecord tradeRecord = new TradeRecord();
        tradeRecord.setUserId(orderInfo.getBuyerId());
        tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode());
        tradeRecord.setTradeNo(orderInfo.getTid());
        recordMapper.add(tradeRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAccountPerformanceMonth(List<TeamPerformance> list) {
        for (TeamPerformance teamPerformance : list) {
            teamPerformanceMapper.updateById(teamPerformance);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfos, List<ProgressPrize> prizes) {

        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
        //判断 prizes 是否为空集合,是新增
        //新增
        if (CollectionUtils.isEmpty(prizes)) {
            for (TeamPerformanceSortVo sortVo : listVo) {
                String userId = sortVo.getTeamPerformance().getUserId();
                ProgressPrize progressPrize = new ProgressPrize();
                progressPrize.setUserId(userId);
                progressPrize.setGrowthRate(sortVo.getGrowthRate());
                progressPrize.setAwardMoney(sortVo.getMonthPerformance());
                progressPrize.setYearMonth(yearMonth);
                progressPrizeMapper.add(progressPrize);
            }
        } else {

        }
    }
}