package com.sqx.modules.artificer.service.impl;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;

import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.artificer.dao.ArtificerDao;
import com.sqx.modules.artificer.dao.OrdersDao;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.entity.Orders;
import com.sqx.modules.artificer.entity.Period;
import com.sqx.modules.artificer.service.AppArtificerIntegralService;
import com.sqx.modules.artificer.vo.HomeVO;
import com.sqx.modules.artificer.vo.UserArtificerVO;
import com.sqx.modules.common.dao.ArtificerPartitioningDetailsMapper;
import com.sqx.modules.common.dao.CommonInfoDao;
import com.sqx.modules.common.entity.ArtificerPartitioningDetails;
import com.sqx.modules.common.entity.CommonInfo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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


import static com.sqx.common.utils.Result.success;
import static com.sqx.modules.utils.PeriodUtil.obtainCycle;
import static com.sqx.modules.utils.PeriodUtil.previousIssue;
import static java.math.BigDecimal.ROUND_CEILING;


@Service
@AllArgsConstructor
public class AppArtificerIntegralServiceImpl implements AppArtificerIntegralService {

    private final OrdersDao ordersDao;
    private final ArtificerPartitioningDetailsMapper artificerPartitioningDetailsMapper;
    private final CommonInfoDao commonInfoDao;
    private final ArtificerDao artificerDao;
    private final UserDao userDao;


    @Override
    public Result home(Long artificerId) {
        Artificer artificer = artificerDao.selectById(artificerId);
        //91精油 95 中医
        String classifyId = artificer.getClassifyId().toString();

        HomeVO orders = new HomeVO();
        String startDate = DateUtil.formatDateTime(obtainCycle(new Date()).getStartFundData());
        String endDate = DateUtil.formatDateTime(obtainCycle(new Date()).getEndFundData());

        String grade = artificer.getGrade();
        if (StringUtils.isEmpty(grade)) {
            grade = "新";
        }
        orders.setGrade(grade);

        //当期业绩
        Integer currentPerformance = ordersDao.selectOrdersArtificerIntegralAchievement(artificerId, endDate, startDate);
        orders.setCurrentPerformance(String.valueOf(currentPerformance));

        //当期收益
//        String earnings = ordersDao.earnings(artificerId, endDate, startDate);
        BigDecimal earnings = ordersDao.selectOrdersArtificerIntegralEarnings(artificerId, endDate, startDate);
        earnings = earnings.setScale(2,BigDecimal.ROUND_HALF_UP);
        orders.setEarnings(earnings.toString());

        //当日收益
        String todayEndIncome = DateUtil.formatDateTime(DateUtil.endOfDay(new Date()));
//        String dailyEarnings = ordersDao.earnings(artificerId, todayEndIncome, String.valueOf(DateUtil.beginOfDay(new Date())));
        BigDecimal dailyEarnings = ordersDao.selectOrdersArtificerIntegralEarnings(artificerId, todayEndIncome, String.valueOf(DateUtil.beginOfDay(new Date())));
        dailyEarnings = dailyEarnings.setScale(2,BigDecimal.ROUND_HALF_UP);
        orders.setDailyEarnings(dailyEarnings.toString());

        //当月收益
        //当月结束日
        String startFundDataformat = DateUtil.formatDateTime(DateUtil.beginOfMonth(new Date()));
        String endFundDataformat = DateUtil.formatDateTime(DateUtil.endOfMonth(new Date()));
//        String currentMonthsIncome = ordersDao.earnings(artificerId, endFundDataformat, startFundDataformat);
        BigDecimal currentMonthsIncome = ordersDao.selectOrdersArtificerIntegralEarnings(artificerId, endFundDataformat, startFundDataformat);
        currentMonthsIncome = currentMonthsIncome.setScale(2,BigDecimal.ROUND_HALF_UP);
        orders.setCurrentMonthsIncome(currentMonthsIncome.toString());

        //当期结束日
        Period period = obtainCycle(new Date());
        orders.setCurrentEndDate(DateUtil.formatDateTime(period.getEndFundData()));

//        //当前周期储值积分数
//        String storedValueIntegral = ordersDao.selectOrdersStoredValueIntegral(artificerId, endDate, startDate);
//        //当前周在线积分数
//        String onlineCredits = ordersDao.selectOnlineCredits(artificerId, endDate, startDate);
//        BigDecimal a = new BigDecimal(storedValueIntegral);
//        BigDecimal b = new BigDecimal(onlineCredits);
//        //储存积分数 加上在线积分数 等于当期积分
//        BigDecimal add = a.add(b);

        //获取所有积分方法
        Map<String,Object> jfMap = getJifenList(artificerId, startDate, endDate);
        String zxjf = jfMap.get("zxscjf").toString();
        //当前周在线积分数
        BigDecimal b = new BigDecimal(zxjf);

        //总积分
        String sumJf = jfMap.get("sumJf").toString();
        BigDecimal add = new BigDecimal(sumJf);
        orders.setIntegral(add);

        //当期订单数：计算订单发生了按摩行为的订单数，订单状态经历了开始服务到订单结束。
        String currentPeriodOrdersSum = ordersDao.selectOrdersArtificerIntegral(artificerId, endDate, startDate);
        orders.setCurrentPeriodOrdersSum(currentPeriodOrdersSum);

        //当期加钟率：本周期内，加钟数/本单数
        String v = ordersDao.selectOrdersArtificerIntegraladdNum(artificerId, endDate, startDate);
        //加钟数
        BigDecimal currentPeriodAddBellsSumBig = new BigDecimal(v);
        //订单数
        BigDecimal currentPeriodOrdersSumBig = new BigDecimal(currentPeriodOrdersSum);
        BigDecimal clockRate = null;
        //充值率
        BigDecimal divide1 = null;
        if (currentPeriodOrdersSumBig.compareTo(new BigDecimal("0")) != 0) {
            clockRate = currentPeriodAddBellsSumBig.divide(currentPeriodOrdersSumBig, 2, BigDecimal.ROUND_HALF_UP);
            orders.setCurrentPeriodAddBellsSum(clockRate.toString());

            //当前周期充值率(本周期内充值订单数/本单数)
            String currentPeriodRechargeSum = ordersDao.selectOrdersCurrentPeriodRechargeSum(artificerId, endDate, startDate,1);
            BigDecimal c = new BigDecimal(currentPeriodRechargeSum);
            divide1 = c.divide(currentPeriodOrdersSumBig, 2, BigDecimal.ROUND_HALF_UP);
            orders.setCurrentPeriodRechargeSum(divide1.toString());
        } else {
            clockRate = new BigDecimal("0");
            orders.setCurrentPeriodAddBellsSum(String.valueOf(clockRate));
            orders.setCurrentPeriodRechargeSum(String.valueOf(clockRate));
        }
        if (currentPeriodOrdersSumBig.compareTo(new BigDecimal("0")) == 0) {
            divide1 = new BigDecimal("0");
        }

        //业绩后面的显示
        List<ArtificerPartitioningDetails> lists = artificerPartitioningDetailsMapper.selectList(Wrappers.<ArtificerPartitioningDetails>lambdaQuery()
                .eq(ArtificerPartitioningDetails::getClassifyId, classifyId));
        for (int i = 0; i < lists.size(); i++) {
            if (currentPerformance >= lists.get(i).getOutstandingAchievement().intValue()) {
                if (i + 1 < lists.size()) {
                    BigDecimal bigDecimal = lists.get(i + 1).getOutstandingAchievement();
                    if (Objects.nonNull(bigDecimal)) {
                        orders.setSuffix(NumberUtil.toStr(bigDecimal));
                    }
                }
            } else if (currentPerformance <= lists.get(i).getOutstandingAchievement().intValue()) {
                orders.setSuffix(NumberUtil.toStr(artificerPartitioningDetailsMapper.selectById(5).getOutstandingAchievement()));
            }
        }
        return getResult(currentPerformance, clockRate, divide1, b, add, orders, artificer, classifyId);
    }

    private Result getResult(Integer currentPerformance, BigDecimal clockRate, BigDecimal divide1, BigDecimal b, BigDecimal add, HomeVO orders, Artificer artificer, String classifyId) {
        List<ArtificerPartitioningDetails> artificerPartitioningDetails = artificerPartitioningDetailsMapper.selectList(Wrappers.<ArtificerPartitioningDetails>lambdaQuery()
                .eq(ArtificerPartitioningDetails::getClassifyId, classifyId)
                .ne(ArtificerPartitioningDetails::getGrade, "A0"));

        for (int i = 0; i < artificerPartitioningDetails.size(); i++) {
            ArtificerPartitioningDetails item = artificerPartitioningDetails.get(i);
            //技师业绩
            BigDecimal outstandingAchievement = item.getOutstandingAchievement();
            //加钟率
            BigDecimal clockRate1 = item.getClockRate();
            //充值率
            BigDecimal depositRate = item.getDepositRate();
            //在线时长
            Integer durationOnline = item.getDurationOnline();
            //积分
            String integral = item.getIntegral();

            //多少分钟等1积分
            CommonInfo commonInfo = commonInfoDao.findOne(100000);
            String value = commonInfo.getValue();

            if (i + 1 < artificerPartitioningDetails.size()) {

                ArtificerPartitioningDetails items = artificerPartitioningDetails.get(i + 1);

                if (currentPerformance >= outstandingAchievement.intValue()
                        && clockRate.compareTo(clockRate1) > -1
                        && divide1.compareTo(depositRate) > -1
                        && b.intValue() >= durationOnline
                        && add.intValue() >= Integer.valueOf(integral)
                        && Objects.nonNull(items)
                ) {
                    BigDecimal p1 = new BigDecimal(currentPerformance);

                    //还差多少业绩加钟率积分提升等级
                    BigDecimal siq = new BigDecimal(items.getOutstandingAchievement().toString());
                    BigDecimal jf = new BigDecimal(items.getIntegral());
                    BigDecimal jzl = new BigDecimal(items.getClockRate().toString());
                    BigDecimal czl = new BigDecimal(items.getDepositRate().toString());

                    //差的业绩
                    BigDecimal subtract = siq.subtract(p1);
                    if (p1.intValue() >= siq.intValue()) {
                        subtract = new BigDecimal("0");
                    }
                    //差的加钟率
                    BigDecimal clockRateJzl1 = jzl.subtract(clockRate);
                    if (clockRate.compareTo(jzl) > -1) {
                        clockRateJzl1 = new BigDecimal("0");
                    }
                    //差的充值率
                    BigDecimal divide11 = czl.subtract(divide1);
                    if (clockRate.compareTo(jzl) > -1) {
                        divide11 = new BigDecimal("0");
                    }
                    //差的在线时长
                    BigDecimal shi = new BigDecimal(value);
                    BigDecimal multiply1 = b.multiply(shi);
                    BigDecimal divide = multiply1.divide(new BigDecimal("60"), ROUND_CEILING);
                    BigDecimal bs = new BigDecimal(items.getDurationOnline().toString());
                    BigDecimal subtract2 = bs.subtract(divide);

                    if (divide.compareTo(bs) > -1) {
                        subtract2 = new BigDecimal("0");
                    }

                    //差的积分
                    BigDecimal subtract1 = jf.subtract(add);
                    if (add.intValue() >= jf.intValue()) {
                        subtract1 = new BigDecimal("0");
                    }

                    //差的业绩
                    orders.setDifferenceOutstandingAchievement(subtract.stripTrailingZeros().toPlainString());
                    //差的积分
                    orders.setDifferenceIntegral(NumberUtil.toStr(subtract1));
                    //差的加钟率
                    orders.setDifferenceClockRate(String.valueOf(clockRateJzl1));
                    //差的充值率
                    orders.setDepositRate(String.valueOf(divide11));
                    //差的在线时长
                    orders.setDifferenceDurationOnline(String.valueOf(subtract2));
                    //差的等级
                    orders.setDifferenceGrade(items.getGrade());
                    String ultimately = "上周期等级：" + artificer.getGrade() + ";" + "本周期等级：" + item.getGrade() + ";" + "还差" + subtract.stripTrailingZeros().toPlainString() + "业绩;" + subtract1 + "积分;" + clockRateJzl1 + "加钟率;" + divide11 + "充值率;" + subtract2 + "在线时长;" + "可以升级" + items.getGrade();
                    orders.setUltimately(ultimately);
                    if (subtract.stripTrailingZeros().toPlainString().equals("0")
                            && subtract1.stripTrailingZeros().toPlainString().equals("0")
                            && clockRateJzl1.stripTrailingZeros().toPlainString().equals("0")
                            && divide11.stripTrailingZeros().toPlainString().equals("0")
                            && subtract2.stripTrailingZeros().toPlainString().equals("0")
                            && items.getGrade().equals("A5")
                    ) {
                        orders.setMaxStatus(true);
                    } else {
                        orders.setMaxStatus(false);
                    }

                }
            }
        }
        return Result.success().put("data", orders);
    }

    @Override
    public Result upgrade(String dateTime) {
        Date newDate = null;
        if (StringUtils.isNotEmpty(dateTime) && !dateTime.equals("null")) {
            newDate = DateUtil.parse(dateTime);
        } else {
            newDate = new Date();
        }
        List<Artificer> artificers = artificerDao.selectList(Wrappers.<Artificer>lambdaQuery()
                .eq(Artificer::getStatus, 1)
                .eq(Artificer::getClassifyId, 91));
        for (Artificer list : artificers) {
            Long artificerId = list.getArtificerId();
            String createTime = list.getCreateTime();
            //每十天定时扫描一次
            //查询当前周期的 上周期 在线时长是否达到10小时 并且至少有一个订单
            Period period1 = previousIssue(newDate);
            Date startFundData = period1.getStartFundData();
            Date endFundData = period1.getEndFundData();
            String date = DateUtil.formatDateTime(endFundData);
            //startFundData转换String
            String cycleTime = DateUtil.formatDateTime(startFundData);

            //当前一周期业绩
            BigDecimal currentPerformance = ordersDao.selectOrdersArtificerIntegralEarnings(artificerId, date, cycleTime);
            currentPerformance = currentPerformance.setScale(2,BigDecimal.ROUND_HALF_UP);

            //当前一周期储值积分数
            String storedValueIntegral = ordersDao.selectOrdersStoredValueIntegral(artificerId, date, cycleTime);
            //当前一周在线积分数
            String onlineCredits = ordersDao.selectOnlineCredits(artificerId, date, cycleTime);
            BigDecimal a = new BigDecimal(storedValueIntegral);
            BigDecimal b = new BigDecimal(onlineCredits);
            //储存积分数 加上在线积分数等于当期积分
            BigDecimal add = a.add(b);

            //当期订单数：计算订单发生了按摩行为的订单数，订单状态经历了开始服务到订单结束。
            String currentPeriodOrdersSum = ordersDao.selectOrdersArtificerIntegral(artificerId, date, cycleTime);

            //当期加钟率：本周期内，加钟数/本单数
            String v = ordersDao.selectOrdersArtificerIntegraladdNum(artificerId, date, cycleTime);
            //加钟数
            BigDecimal currentPeriodAddBellsSumBig = new BigDecimal(v);
            //订单数
            BigDecimal currentPeriodOrdersSumBig = new BigDecimal(currentPeriodOrdersSum);
            //加钟率
            BigDecimal clockRate = null;
            //充值率
            BigDecimal divide1 = null;
            if (currentPeriodOrdersSumBig.compareTo(new BigDecimal("0")) != 0) {
                clockRate = currentPeriodAddBellsSumBig.divide(currentPeriodOrdersSumBig, 2, BigDecimal.ROUND_HALF_UP);
                //当前周期充值率(本周期内充值订单数/本单数)
                String currentPeriodRechargeSum = ordersDao.selectOrdersCurrentPeriodRechargeSum(artificerId, date, cycleTime,1);
                BigDecimal c = new BigDecimal(currentPeriodRechargeSum);
                divide1 = c.divide(currentPeriodOrdersSumBig, 2, BigDecimal.ROUND_HALF_UP);
            } else {
                clockRate = new BigDecimal("0");
                divide1 = new BigDecimal("0");
            }

            Date date1 = DateUtil.parse(createTime);
            long betweenDay = DateUtil.between(date1, new Date(), DateUnit.DAY);

            List<ArtificerPartitioningDetails> artificerPartitioningDetails = artificerPartitioningDetailsMapper.selectList(Wrappers.<ArtificerPartitioningDetails>lambdaQuery()
                    .eq(ArtificerPartitioningDetails::getClassifyId, 91));
            //判断betweenDay大于10天 说明过了 新人扶持阶段 设置等级为A0
            if (betweenDay > 10) {
                artificerPartitioningDetails.remove(0);
                extractedRate(artificerId, artificerPartitioningDetailsMapper.selectById(3).getProportionalSharing(), artificerPartitioningDetailsMapper.selectById(3).getGrade());
            }

            for (ArtificerPartitioningDetails item : artificerPartitioningDetails) {
                //技师等级
                String grade = item.getGrade();
                //技师业绩
                BigDecimal outstandingAchievement = item.getOutstandingAchievement();
                //加钟率
                BigDecimal clockRate1 = item.getClockRate();
                //充值率
                BigDecimal depositRate = item.getDepositRate();
                //在线时长
                Integer durationOnline = item.getDurationOnline();
                //积分
                String integral = item.getIntegral();
                //分成比例
                String proportionalSharing = item.getProportionalSharing();
                if (
                    //1.业绩要求
                        currentPerformance.compareTo(outstandingAchievement) >= 0
                                //2.加钟率要求
                                && clockRate.compareTo(clockRate1) > -1
                                //充值率要求
                                && divide1.compareTo(depositRate) > -1
                                //3.在线时长要求
                                && b.intValue() >= durationOnline
                                //4.积分要求
                                && add.intValue() >= Integer.valueOf(integral)
                ) {
                    //更新技师表分成比例和等级
                    extractedRate(artificerId, proportionalSharing, grade);
                }
            }
        }


        return success();
    }

    @Override
    public Result upgradeZy(String dateTime) {
        Date newDate = null;
        if (StringUtils.isNotEmpty(dateTime) && !dateTime.equals("null")) {
            newDate = DateUtil.parse(dateTime);
        } else {
            newDate = new Date();
        }

        List<Artificer> artificers = artificerDao.selectList(Wrappers.<Artificer>lambdaQuery()
                .eq(Artificer::getStatus, 1)
                .eq(Artificer::getClassifyId, 95));

        for (Artificer list : artificers) {
            Long artificerId = list.getArtificerId();
            String createTime = list.getCreateTime();
            //每十天定时扫描一次
            //查询当前周期的 上周期 在线时长是否达到10小时 并且至少有一个订单
            Period period1 = previousIssue(newDate);
            Date startFundData = period1.getStartFundData();
            Date endFundData = period1.getEndFundData();
            String date = DateUtil.formatDateTime(endFundData);
            //startFundData转换String
            String cycleTime = DateUtil.formatDateTime(startFundData);

            //当前一周期业绩
            BigDecimal currentPerformance = ordersDao.selectOrdersArtificerIntegralEarnings(artificerId, date, cycleTime);
            currentPerformance = currentPerformance.setScale(2,BigDecimal.ROUND_HALF_UP);

            //当前一周期储值积分数
            String storedValueIntegral = ordersDao.selectOrdersStoredValueIntegral(artificerId, date, cycleTime);
            //当前一周在线积分数
            String onlineCredits = ordersDao.selectOnlineCredits(artificerId, date, cycleTime);
            BigDecimal a = new BigDecimal(storedValueIntegral);
            BigDecimal b = new BigDecimal(onlineCredits);
            //储存积分数 加上在线积分数等于当期积分
            BigDecimal add = a.add(b);

            //当期订单数：计算订单发生了按摩行为的订单数，订单状态经历了开始服务到订单结束。
            String currentPeriodOrdersSum = ordersDao.selectOrdersArtificerIntegral(artificerId, date, cycleTime);

            //当期加钟率：本周期内，加钟数/本单数
            String v = ordersDao.selectOrdersArtificerIntegraladdNum(artificerId, date, cycleTime);
            //加钟数
            BigDecimal currentPeriodAddBellsSumBig = new BigDecimal(v);
            //订单数
            BigDecimal currentPeriodOrdersSumBig = new BigDecimal(currentPeriodOrdersSum);
            //加钟率
            BigDecimal clockRate = null;
            //充值率
            BigDecimal divide1 = null;
            if (currentPeriodOrdersSumBig.compareTo(new BigDecimal("0")) != 0) {
                clockRate = currentPeriodAddBellsSumBig.divide(currentPeriodOrdersSumBig, 2, BigDecimal.ROUND_HALF_UP);
                //当前周期充值率(本周期内充值订单数/本单数)
                String currentPeriodRechargeSum = ordersDao.selectOrdersCurrentPeriodRechargeSum(artificerId, date, cycleTime,1);
                BigDecimal c = new BigDecimal(currentPeriodRechargeSum);
                divide1 = c.divide(currentPeriodOrdersSumBig, 2, BigDecimal.ROUND_HALF_UP);
            } else {
                clockRate = new BigDecimal("0");
                divide1 = new BigDecimal("0");
            }

            Date date1 = DateUtil.parse(createTime);
            long betweenDay = DateUtil.between(date1, new Date(), DateUnit.DAY);


            List<ArtificerPartitioningDetails> artificerPartitioningDetails = artificerPartitioningDetailsMapper.selectList(Wrappers.<ArtificerPartitioningDetails>lambdaQuery()
                    .eq(ArtificerPartitioningDetails::getClassifyId, 95));

            //判断betweenDay大于10天 说明过了 新人扶持阶段 设置等级为A0
            if (betweenDay > 10) {
                artificerPartitioningDetails.remove(0);
                extractedRate(artificerId, artificerPartitioningDetailsMapper.selectById(29).getProportionalSharing(), artificerPartitioningDetailsMapper.selectById(29).getGrade());
            }

            for (ArtificerPartitioningDetails item : artificerPartitioningDetails) {
                //技师等级
                String grade = item.getGrade();
                //技师业绩
                BigDecimal outstandingAchievement = item.getOutstandingAchievement();
                //加钟率
                BigDecimal clockRate1 = item.getClockRate();
                //充值率
                BigDecimal depositRate = item.getDepositRate();
                //在线时长
                Integer durationOnline = item.getDurationOnline();
                //积分
                String integral = item.getIntegral();
                //分成比例
                String proportionalSharing = item.getProportionalSharing();
                if (
                    //1.业绩要求
                        currentPerformance.compareTo(outstandingAchievement) >= 0
                                //2.加钟率要求
                                && clockRate.compareTo(clockRate1) > -1
                                //充值率要求
                                && divide1.compareTo(depositRate) > -1
                                //3.在线时长要求
                                && b.intValue() >= durationOnline
                                //4.积分要求
                                && add.intValue() >= Integer.valueOf(integral)) {
                    //更新技师表分成比例和等级&订单表技师等级
                    extractedRate(artificerId, proportionalSharing, grade);
                }
            }
        }


        return success();
    }

    /**
     * 更新技师表分成比例
     *
     * @param artificerId         技师id
     * @param proportionalSharing 分成明细
     * @param grade               技师等级
     */
    private void extractedRate(Long artificerId, String proportionalSharing, String grade) {
        Artificer artificer = new Artificer();
        artificer.setRate(new BigDecimal(proportionalSharing));
        artificer.setArtificerId(artificerId);
        artificer.setGrade(grade);
        artificerDao.updateById(artificer);
    }

    @Override
    public Result time() {
        return Result.success().put("data", commonInfoDao.findOne(100000).getValue());
    }

    @Override
    public Result line() {
        //每十天定时扫描一次
        //查询当前周期的 上周期 在线时长是否达到10小时 并且至少有一个订单
        Period period1 = obtainCycle(new Date());
        Date startFundData = period1.getStartFundData();
        Date endFundData = period1.getEndFundData();
        String startFundDataformat = DateUtil.formatDateTime(startFundData);
        String endFundDataformat = DateUtil.formatDateTime(endFundData);

        //多少分钟等1积分
        CommonInfo commonInfo = commonInfoDao.findOne(100000);
        Integer value = Integer.valueOf(commonInfo.getValue());

        List<UserArtificerVO> artificerInfo = userDao.getArtificerInfo();
        for (UserArtificerVO userArtificerVO : artificerInfo) {
            Long artificerId = userArtificerVO.getArtificerId();
            //上周在线积分数

            String onlineCredits = ordersDao.selectOnlineCredits(artificerId, endFundDataformat, startFundDataformat);
            String s = NumberUtil.toStr(new BigDecimal(onlineCredits));

            //积分*多少分钟等1积分*60 =多少小时
            int i = Integer.valueOf(s) * value / 60;
            //订单数
            String currentPeriodOrdersSum = ordersDao.selectOrdersArtificerIntegral(artificerId, endFundDataformat, startFundDataformat);

            Integer i1 = Integer.valueOf(currentPeriodOrdersSum);
            //更新停牌
            if (i < 10 && i1 < 1) {
                UserEntity userEntity = new UserEntity();
                userEntity.setStatus(3);
                userEntity.setUserId(userArtificerVO.getUserId());
                int ii = userDao.updateById(userEntity);
                if (ii > 0) {
                    Artificer artificer = new Artificer();
                    artificer.setGrade("A0");
                    artificer.setArtificerId(artificerId);
                    artificerDao.updateById(artificer);
                }
            }
        }


        return Result.success();
    }

    @Override
    public Result getArtificerLevels(Integer page, Integer limit, Long artificerId){
        Map<String, Object> map = new HashMap<String, Object>();
        Artificer artificer = artificerDao.selectById(artificerId);
        Period period1 = obtainCycle(new Date());
        Date startFundData = period1.getStartFundData();
        Date endFundData = period1.getEndFundData();
        artificer.setBeginTime(DateUtil.formatDate(startFundData));
        artificer.setFinishTime(DateUtil.formatDate(endFundData));
        map.put("artificerData",artificer);
        //91精油 95 中医
        String classifyId = artificer.getClassifyId().toString();
        IPage<ArtificerPartitioningDetails> page1 = new Page(page, limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("classify_id", classifyId);
        queryWrapper.orderByAsc("id");
        IPage<ArtificerPartitioningDetails> apdPage= artificerPartitioningDetailsMapper.selectPage(page1,queryWrapper);
        map.put("levelData",apdPage);
        return Result.success(map);
    }

    public Result getJifenList(Long userId, String startTime, String endTime) {
        //1.获取业绩积分
        List<Orders> yjjfList = ordersDao.getYjjfList(userId,startTime,endTime);
        int yjjf = 0;
        for (Orders par:yjjfList) {
            yjjf += (int) Double.parseDouble(par.getJifen());
        }
        //2.获取储值积分（为他充值的）
        List<Orders> czjfList = ordersDao.getCzjfList(userId,startTime,endTime);
        int czjf = 0;
        for (Orders par:czjfList) {
            czjf += (int) Double.parseDouble(par.getJifen());
        }
        //3.获取订单充值积分
        List<Orders> ddczList = ordersDao.getDdczList(userId,startTime,endTime);
        int ddczjf = 0;
        for (Orders par:ddczList) {
            ddczjf += (int) Double.parseDouble(par.getJifen());
        }
        //4.获取在线时长积分
        List<Orders> zxscList = ordersDao.getZxscList(userId,startTime,endTime);
        int zxscjf = 0;
        for (Orders par:zxscList) {
            zxscjf += (int) Double.parseDouble(par.getJifen());
        }
        int sumJf = 0;
        sumJf = yjjf + czjf + ddczjf + zxscjf;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("yjjfList",yjjfList);
        map.put("yjjf",yjjf);
        map.put("czjfList",czjfList);
        map.put("czjf",czjf);
        map.put("ddczList",ddczList);
        map.put("ddczjf",ddczjf);
        map.put("zxscList",zxscList);
        map.put("zxscjf",zxscjf);
        map.put("sumJf",sumJf);
        return Result.success(map);
    }
}