package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
import cn.wisenergy.model.enums.MemberPercentEnum;
import cn.wisenergy.model.enums.TradeRecordEnum;
import cn.wisenergy.model.enums.TradeStatusEnum;
import cn.wisenergy.model.enums.UserLevelEnum;
import cn.wisenergy.model.vo.TeamPerformanceSortVo;
import cn.wisenergy.service.Manager.AccountManager;
import cn.wisenergy.service.Manager.PublicManager;
import cn.wisenergy.service.app.AccountService;
import cn.wisenergy.service.app.MonthTaskService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author 86187
 */
@Slf4j
@Service
public class MonthTaskServiceImpl implements MonthTaskService {

    @Autowired
    private LastAccountMapper lastAccountMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private MemberPercentMapper memberPercentMapper;

    @Autowired
    private AccountManager accountManager;

    @Autowired
    private PublicManager publicManager;

    @Autowired
    private TradeRecordMapper tradeRecordMapper;

    @Autowired
    private TeamPerformanceMapper teamPerformanceMapper;

    @Autowired
    private ProgressPrizeMapper progressPrizeMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    private MonthManureMapper monthManureMapper;


    @Autowired
    private OrderMapper orderMapper;

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

    private static final Integer TWENTY = 20;

    @XxlJob(value = "monthManureMonthTask")
    @Override
    public R<Boolean> performanceCount() {
        //获取上月订单
        Date lastMonth = DateUtil.getLastMonth(new Date());
        List<OrderInfo> list = orderMapper.getByCreateTime(lastMonth);
        log.info("shop-mall[]MonthTaskServiceImpl[]performanceCount[]input.param.list:{}", list.size());
        if (CollectionUtils.isEmpty(list)) {
            return R.ok(0, true);
        }

        String yearMonth = DateUtil.convertDateToStr(lastMonth, PATTERN);

        //计算上月所有订单成交额
        BigDecimal totalMoney = new BigDecimal(0);

        //统计出出每个用户上月订单成交额 key:userId  value:用户上月订单成交额
        Map<String, Double> map = new HashMap<>();
        for (OrderInfo orderInfo : list) {
            String userId = orderInfo.getUserId();
            double payMoney = orderInfo.getPayment().doubleValue();

            //key 存在  累加订单金额 到 value
            if (map.containsKey(userId)) {
                double money = payMoney + map.get(orderInfo.getUserId());
                map.put(orderInfo.getUserId(), money);
            } else {
                //key 不存在,加入集合
                map.put(userId, payMoney);
            }

            //累加所以订单成交额
            totalMoney = totalMoney.add(orderInfo.getPayment());
        }

        //累计用户和上级用户-团队业绩
        Map<String, Double> tempMap = new HashMap<>();

        //遍历订单
        for (Map.Entry<String, Double> entity : map.entrySet()) {
            String userId = entity.getKey();
            //1)、统计当前用户月度业绩
            double userCount = entity.getValue();
            //key 存在  当前用户团队绩效  + 上级用户团队绩效
            if (tempMap.containsKey(entity.getKey())) {
                double teamMoney = userCount + tempMap.get(entity.getKey());
                tempMap.put(entity.getKey(), teamMoney);
            } else {
                //key 不存在,加入集合 当前用户团队绩效
                tempMap.put(entity.getKey(), userCount);
            }

            //2)、获取当前用户的上级用户列表
            List<User> userList = accountService.getByList(userId);
            if (CollectionUtils.isEmpty(userList)) {
                continue;
            }

            for (User userInfo : userList) {
                //3)、统计当前用户的上级用户团队绩效
                //key 存在  当前用户团队绩效  + 上级用户团队绩效
                if (tempMap.containsKey(userInfo.getUserId())) {
                    double teamMoney = userCount + tempMap.get(userInfo.getUserId());
                    tempMap.put(userInfo.getUserId(), teamMoney);
                } else {
                    //key 不存在,加入集合 当前用户团队绩效
                    tempMap.put(userInfo.getUserId(), userCount);
                }
            }
        }

        //3、获取用户上月绩效信息   新增 or 更新
        List<TeamPerformance> addList = new ArrayList<>();
        List<TeamPerformance> updateList = new ArrayList<>();
        for (Map.Entry<String, Double> entity : tempMap.entrySet()) {
            //获取团队业绩信息
            TeamPerformance teamPerformance = teamPerformanceMapper.getByUserIdAndTime(entity.getKey(), yearMonth);
            if (null == teamPerformance) {
                //获取用户信息
                User user = usersMapper.getByUserId(entity.getKey());

                //添加用户团队业绩信息
                TeamPerformance performance = new TeamPerformance();
                performance.setUserId(user.getUserId());
                performance.setMonthTeamPerformance(BigDecimal.valueOf(entity.getValue()));
                performance.setUserLevel(user.getUserLevel());
                performance.setYearMonth(yearMonth);
                addList.add(performance);
            } else {
                teamPerformance.setMonthTeamPerformance(BigDecimal.valueOf(entity.getValue()));
                updateList.add(teamPerformance);
            }
        }

        //4、更新账户月度绩效
        boolean updateBool = accountManager.updateAccountPerformanceMonth(addList, updateList);
        if (!updateBool) {
            return R.ok(1, false);
        }

        //5、获取所有用户,如果会员等级是黄金以上,计算月度收益
        List<User> userList = usersMapper.getAllGoldUser();
        if (CollectionUtils.isEmpty(userList)) {
            //添加月度肥料剩余奖金
            MonthManure monthManure = new MonthManure();
            monthManure.setManureAward(totalMoney.doubleValue());
            monthManure.setYearMonth(yearMonth);
            int count = monthManureMapper.add(monthManure);
            if (count == 0) {
                return R.ok(1, false);
            }
            return R.ok(0, true);
        }

        //6、计算收益
        boolean bool = monthlyIncome(totalMoney, userList);
        if (!bool) {
            return R.ok(1, false);
        }
        return R.ok(0, true);
    }

    @XxlJob(value = "growthAwardMonthTask")
    @Override
    public R<Boolean> progressPrizeCount() {
        log.info("shop-mall[]MonthTaskServiceImpl[]performanceCount[]input.method");
        Date date = DateUtil.getLastMonth(new Date());
        String lastMonth = DateUtil.convertDateToStr(date, PATTERN);

        //1、判断上月是否是业绩开始的第一个月
        List<TeamPerformance> teamPerformances = teamPerformanceMapper.getByBeforeTime(lastMonth);

        //获取上月月所有人业绩总额
        Double totalMoney = teamPerformanceMapper.countByTime(lastMonth);
        double number = Math.floor(totalMoney / 3980 / 12);
        //2、集合为空 是业绩开始的第一个月
        if (CollectionUtils.isEmpty(teamPerformances)) {
            if (number != 0) {
                //获取上月业绩前20用户
                List<TeamPerformance> list = teamPerformanceMapper.userTwenty(lastMonth);
                if (CollectionUtils.isEmpty(list)) {
                    return R.ok(0, true);
                }

                //获取20名进步最大的月业绩和
                Double twentyTotal = teamPerformanceMapper.countTwenty(lastMonth);
                List<AccountInfo> accountInfoList = new ArrayList<>();
                List<TradeRecord> tradeRecordList = new ArrayList<>();
                List<ProgressPrize> prizeList = new ArrayList<>();
                for (TeamPerformance user : list) {
                    //获取最大进步奖 百分比
                    MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(user.getUserLevel(), 3);

                    //计算收益
                    double userTeamPerformance = user.getMonthTeamPerformance().doubleValue();
                    double percent = memberPercent.getPercent().doubleValue();
                    double income = Math.floor(number * 3980 * percent * userTeamPerformance / twentyTotal);

                    //获取账户信息  用户月收益  =其他收益+最大进步奖收益
                    AccountInfo accountInfo = accountMapper.getByUserId(user.getUserId());
                    BigDecimal earningsMonth = accountInfo.getEarningsMonth().add(BigDecimal.valueOf(income));
                    accountInfo.setEarningsMonth(earningsMonth.setScale(2, RoundingMode.HALF_UP));

                    //用户总收益 =其他总收益 + earningsMonth
                    BigDecimal earningsTotal = accountInfo.getEarningsTotal().add(BigDecimal.valueOf(income));
                    accountInfo.setEarningsTotal(earningsTotal.setScale(2, RoundingMode.HALF_UP));
                    accountInfoList.add(accountInfo);

                    //添加最大进步奖信息
                    ProgressPrize prize = new ProgressPrize();
                    BigDecimal awardMoney = new BigDecimal(income).setScale(2, RoundingMode.HALF_UP);
                    prize.setAwardMoney(awardMoney.doubleValue());
                    prize.setUserId(user.getUserId());
                    prize.setGrowthRate(0.0);
                    prize.setYearMonth(lastMonth);
                    prizeList.add(prize);

                    //添加交易流水记录
                    TradeRecord tradeRecord = new TradeRecord();
                    tradeRecord.setUserId(user.getUserId());
                    tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
                    tradeRecord.setTradeNo(null);
                    tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
                    tradeRecord.setMoney(BigDecimal.valueOf(income).setScale(2, RoundingMode.HALF_UP));
                    tradeRecordList.add(tradeRecord);
                }

                //更新账户信息,添加交易流水记录
                boolean bool = accountManager.updateAccountAddRecordAddPrize(accountInfoList, tradeRecordList, prizeList);
                if (!bool) {
                    return R.ok(1, false);
                }
            }
            return R.ok(0, true);
        }

        //3、集合不为空 不是业绩开始的第一个月
        //获取用户列表
        List<User> userList = usersMapper.findAllNotZeroUser();
        if (CollectionUtils.isEmpty(userList)) {
            return R.ok(0, true);
        }

        //4计算每个用户本月的业绩增长率  如果集合大于20 ,取前二十名,小于,取全部
        List<TeamPerformanceSortVo> listVo = progressPrizeStatistics(userList);
        if (listVo.size() >= TWENTY) {
            listVo.subList(0, TWENTY);
        }

        //5、计算前20的总业绩
        double total = listVo.stream().mapToDouble(TeamPerformanceSortVo::getMonthPerformance).sum();
        boolean bool = totalPerformanceIncome(listVo, number, total);
        if (!bool) {
            return R.ok(1, false);
        }
        return R.ok(0, true);
    }

    @XxlJob(value = "mirrorImageMonthTask")
    @Override
    public void mirrorImage() {
        //1、把账户表account_info复制给表account_image  CREATE TABLE table_2  SELECT * FROM table_1;
        lastAccountMapper.copyTable("account_info", "account_image");

        //删除上月备份
        lastAccountMapper.deleteTable("account_backup");

        //备份
        lastAccountMapper.copyTable("account_info", "account_backup");

        //2、把上月账户表last_month_account 复制给month_account_image
        lastAccountMapper.copyTable("last_month_account", "month_account_image");

        //3、删除last_month_account
        lastAccountMapper.deleteTable("last_month_account");

        //4、把account_image 更名为 last_month_account  rename table table_2 to table_1;
        lastAccountMapper.updateTableName("account_image", "last_month_account");

        //5、删除month_account_image   DROP table table_2;
        lastAccountMapper.deleteTable("month_account_image");
    }


    /**
     * 如果会员等级是黄金以上,计算月度收益
     *
     * @param totalMoney 月金额总额
     * @param userList   用户列表
     * @return
     */
    private boolean monthlyIncome(BigDecimal totalMoney, List<User> userList) {
        //上月
        Date date = DateUtil.getLastMonth(new Date());
        String lastMonth = DateUtil.convertDateToStr(date, PATTERN);

        //上上月
        String time = DateUtil.convertDateToStr(date, "yyyy-MM-dd");
        Date timeString = DateUtil.convertStrToDate(time, "yyyy-MM-dd");
        Date lastAndLast = DateUtil.getLastMonth(timeString);
        String yearMonthTime = DateUtil.convertDateToStr(lastAndLast, PATTERN);

        //当月所有人订单成交总金额/3980/12  向下取整
        double total = totalMoney.doubleValue();
        double moneyMonth = Math.floor(total / 3980 / 12);

        //获取上上月未分配完的奖金
        double lastMonthAward;
        MonthManure monthManure = monthManureMapper.queryByTime(yearMonthTime);
        if (null == monthManure) {
            lastMonthAward = 0.00;
        } else {
            lastMonthAward = monthManure.getManureAward();
        }
        moneyMonth = moneyMonth + lastMonthAward;

        //1、判断每个等级是否都有用户,没有用户的,记录下剩余奖金
        MonthManure manure = checkUserLevel(moneyMonth);
        //获取上月是否有月度剩余奖金信息
        MonthManure lastMonthManure = monthManureMapper.queryByTime(lastMonth);

        List<AccountInfo> accountInfoList = new ArrayList<>();
        List<TradeRecord> tradeRecordList = new ArrayList<>();
        for (User user : userList) {
            if (moneyMonth != 0) {
                //获取等级优惠百分比
                MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(user.getUserLevel(),
                        MemberPercentEnum.MONTH_MANURE.getCode());
                if (null != memberPercent) {
                    //获取上月月团队总金额
                    TeamPerformance teamPerformance = teamPerformanceMapper.getByUserIdAndTime(user.getUserId(), lastMonth);

                    //获取上月该等级团队总金额
                    Double teamTotal = teamPerformanceMapper.countTeamMoney(user.getUserLevel(), lastMonth);
                    if (null != teamPerformance && null != teamTotal) {
                        double month = teamPerformance.getMonthTeamPerformance().doubleValue();
                        double percent = memberPercent.getPercent().doubleValue();
                        //计算收益
                        double income = moneyMonth * 3980 * percent * month / teamTotal;

                        //获取账户信息
                        AccountInfo accountInfo = accountMapper.getByUserId(user.getUserId());
                        BigDecimal bigDecimal = new BigDecimal(income);
                        BigDecimal performanceMonth = accountInfo.getEarningsMonth().add(bigDecimal);
                        accountInfo.setEarningsMonth(performanceMonth.setScale(2, RoundingMode.HALF_UP));

                        BigDecimal performanceTotal = accountInfo.getEarningsMonth().add(bigDecimal);
                        accountInfo.setEarningsMonth(performanceTotal.setScale(2, RoundingMode.HALF_UP));
                        accountInfoList.add(accountInfo);


                        //5、添加交易流水记录
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(user.getUserId());
                        tradeRecord.setTradeType(TradeRecordEnum.MONTHLY_FERTILIZER.getCode());
                        tradeRecord.setTradeNo(null);
                        BigDecimal big = new BigDecimal(income);
                        tradeRecord.setMoney(big.setScale(2, RoundingMode.HALF_UP));
                        tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
                        tradeRecordList.add(tradeRecord);
                    }
                }
            }
        }
        //更新账户信息,新增交易流水记录
        return accountManager.updateAccountAddRecordAndManure(accountInfoList, tradeRecordList, manure, lastMonthManure);
    }

    /**
     * 计算上月未分配完的月度肥料奖金
     *
     * @param moneyMonth 奖金总额
     * @return 月度肥料奖金
     */
    private MonthManure checkUserLevel(double moneyMonth) {
        MonthManure monthManure = new MonthManure();
        double total = 0.00;
        //1、判断是否有黄金等级的用户
        List<User> goldList = usersMapper.getByLevel(UserLevelEnum.GOLD_TREE.getCode());
        if (CollectionUtils.isEmpty(goldList)) {
            //获取等级优惠百分比
            MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(UserLevelEnum.GOLD_TREE.getCode(),
                    MemberPercentEnum.MONTH_MANURE.getCode());
            double percent = memberPercent.getPercent().doubleValue();

            //计算该等级剩余奖金
            double goldAward;
            if (moneyMonth != 0) {
                goldAward = moneyMonth * 3980 * percent;
                total = total + goldAward;
            }
        }

        //2、判断是否有农场主等级的用户
        List<User> farmerList = usersMapper.getByLevel(UserLevelEnum.FARMER.getCode());
        if (CollectionUtils.isEmpty(farmerList)) {
            //获取等级优惠百分比
            MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(UserLevelEnum.FARMER.getCode(),
                    MemberPercentEnum.MONTH_MANURE.getCode());
            double percent = memberPercent.getPercent().doubleValue();

            //计算该等级剩余奖金
            double goldAward;
            if (moneyMonth != 0) {
                goldAward = moneyMonth * 3980 * percent;
                total = total + goldAward;
            }
        }

        //3、判断是否有森林之星等级的用户
        List<User> startList = usersMapper.getByLevel(UserLevelEnum.FOREST_START.getCode());
        if (!CollectionUtils.isEmpty(startList)) {
            //获取等级优惠百分比
            MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(UserLevelEnum.FOREST_START.getCode(),
                    MemberPercentEnum.MONTH_MANURE.getCode());
            double percent = memberPercent.getPercent().doubleValue();

            //计算该等级剩余奖金
            double goldAward;
            if (moneyMonth != 0) {
                goldAward = moneyMonth * 3980 * percent;
                total = total + goldAward;
            }
        }

        //4、判断是否有西田森等级的用户
        List<User> partnerList = usersMapper.getByLevel(UserLevelEnum.PARTNER.getCode());
        if (CollectionUtils.isEmpty(partnerList)) {
            //获取等级优惠百分比
            MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(UserLevelEnum.FOREST_START.getCode(),
                    MemberPercentEnum.MONTH_MANURE.getCode());
            double percent = memberPercent.getPercent().doubleValue();

            //计算该等级剩余奖金
            double goldAward;
            if (moneyMonth != 0) {
                goldAward = moneyMonth * 3980 * percent;
                total = total + goldAward;
            }
        }

        //获取当前时间上一月
        Date date = DateUtil.getLastMonth(new Date());
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
        monthManure.setYearMonth(yearMonth);
        monthManure.setManureAward(total);
        return monthManure;
    }

    /**
     * 统计每个用户本月业绩增长率
     *
     * @param userList 用户列表
     * @return 每个用户本月业绩增长率
     */
    private List<TeamPerformanceSortVo> progressPrizeStatistics(List<User> userList) {
        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);

        //遍历 计算业绩增长率
        List<TeamPerformanceSortVo> listVo = new ArrayList<>();
        for (User user : userList) {
            TeamPerformanceSortVo teamVo = new TeamPerformanceSortVo();
            double growthRate;
            //获取当月业绩
            TeamPerformance teamPerformance = teamPerformanceMapper.getByUserIdAndTime(user.getUserId(), yearMonth);

            //获取上月业绩
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.MONTH, -1);
            Date lastDate = cal.getTime();
            String lastMonthTime = DateUtil.convertDateToStr(lastDate, PATTERN);
            TeamPerformance lastMonth = teamPerformanceMapper.getByUserIdAndTime(user.getUserId(), lastMonthTime);
            if (null == teamPerformance || null == lastMonth) {
                growthRate = 0.00;
            } else {
                double month = teamPerformance.getMonthTeamPerformance().doubleValue();
                double last = lastMonth.getMonthTeamPerformance().doubleValue();
                if (last >= month) {
                    growthRate = 0.00;
                } else {
                    growthRate = (month - last) / month;
                }
            }
            teamVo.setGrowthRate(growthRate);
            double monthPerformance;
            assert teamPerformance != null;
            if (null == teamPerformance.getMonthTeamPerformance()) {
                monthPerformance = 0.00;
            } else {
                monthPerformance = teamPerformance.getMonthTeamPerformance().doubleValue();
            }
            teamVo.setMonthPerformance(monthPerformance);
            teamVo.setUserId(user.getUserId());
            teamVo.setTeamPerformance(teamPerformance);
            listVo.add(teamVo);
        }

        //对集合进行排序
        listVo.sort(Comparator.comparing(TeamPerformanceSortVo::getGrowthRate).reversed());
        return listVo;
    }

    /**
     * 统计用户最大进步奖收益
     *
     * @param listVo 用户增长率列表
     * @param number 个人业绩
     * @param total  总业绩
     */
    private boolean totalPerformanceIncome(List<TeamPerformanceSortVo> listVo, double number, double total) {
        //要更新的账户列表
        List<AccountInfo> updateAccountList = new ArrayList<>();

        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < listVo.size(); i++) {
            String userId = listVo.get(i).getTeamPerformance().getUserId();
            Integer userLevel = listVo.get(i).getTeamPerformance().getUserLevel();
            //获取最大进步奖 百分比
            MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(userLevel, 3);

            //计算收益
            double userTeamPerformance = listVo.get(i).getTeamPerformance().getMonthTeamPerformance().doubleValue();
            double percent = memberPercent.getPercent().doubleValue();

            double income = number * 3980 * percent * userTeamPerformance / total;

            //获取账户信息
            AccountInfo accountInfo = accountMapper.getByUserId(userId);
            BigDecimal bigDecimal;
            if (i == listVo.size() - 1) {
                bigDecimal = new BigDecimal(total).subtract(sum);
            } else {
                bigDecimal = accountInfo.getEarningsMonth().add(new BigDecimal(income));
                sum = sum.add(new BigDecimal(income));
            }
            accountInfo.setEarningsMonth(bigDecimal);
            updateAccountList.add(accountInfo);
        }

        //判断本月是否有最大进步奖数据 无,新增  有,修改或删除
        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
        List<ProgressPrize> prizes = progressPrizeMapper.getByYearMonth(yearMonth);

        //修改或保存最大进步奖信息
        //  flag  1: 日定时任务  2:月定时任务
        int flag = 2;
        return accountManager.updateOrSavePrize(listVo, updateAccountList, prizes,flag);
    }
}