package com.gxwebsoft.apps.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxwebsoft.apps.entity.Equipment;
import com.gxwebsoft.apps.service.EquipmentGoodsService;
import com.gxwebsoft.apps.service.EquipmentService;
import com.gxwebsoft.common.core.config.MybatisPlusConfig;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.love.entity.UserPlanLog;
import com.gxwebsoft.love.param.UserPlanLogParam;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.param.OrderParam;
import com.gxwebsoft.shop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.gxwebsoft.common.core.constants.OrderConstants.*;

/**
 * 定时任务：订单处理
 */
@Component
@Slf4j
public class OrderTask {

    @Resource
    private OrderService orderService;

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentGoodsService goodsService;

    @Resource
    private UserService userService;

    /**
     * 删除30分钟未下单的订单
     */
    @Scheduled(cron="0 0/30 * * * ? ")
    public void removeTimeoutOrder() {
        Date newDate = DateUtil.offset(DateUtil.date(), DateField.MINUTE, -30);
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery(Order.class)
                .eq(Order::getPayStatus, PAY_STATUS_NO_PAY)
                .lt(Order::getCreateTime, newDate);

        boolean remove = orderService.remove(wrapper);

    }

    /**
     * 计算分润
     */
    @Scheduled(cron="0 0/30 * * * ? ")
    @Transactional
    public void CalcProfit() {
        log.info("开始计算分润");
        // 查询所有未结算订单
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery(Order.class)
                .eq(Order::getIsSettled, ORDER_SETTLED_NO)
                .eq(Order::getPayStatus, PAY_STATUS_SUCCESS)
                .eq(Order::getOrderStatus, ORDER_STATUS_COMPLETED);

        List<Order> orderList = orderService.list(wrapper);

        // 查询所有关联的设备
        Set<Integer> equipmentIds = new HashSet<>();
        Set<String> tuijianUserPhones = new HashSet<>();

        for (Order order : orderList) {
            equipmentIds.add(order.getEquipmentId());
            if(order.getDealerPhone() != null){
                tuijianUserPhones.add(order.getDealerPhone());
            }
        }
        List<Equipment> equipmentList = equipmentService.list(Wrappers.lambdaQuery(Equipment.class).in(Equipment::getEquipmentId, equipmentIds));
        Map<Integer, Equipment> equipmentMap = new HashMap<>();
        Set<Integer> touziUserIds = new HashSet<>();
        for (Equipment equipment : equipmentList) {
            equipmentMap.put(equipment.getEquipmentId(),equipment);
            if(equipment.getTouziUserId() != null) {
                touziUserIds.add(equipment.getTouziUserId());
            }
        }

        // 查询所有投资人
        Map<Integer, User> touziUserMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(touziUserIds)){
            List<User> list = userService.list(Wrappers.lambdaQuery(User.class).in(User::getUserId, touziUserIds));
            for (User user : list) {
                touziUserMap.put(user.getUserId(), user);
            }
        }

        // 查询所有推荐人
        Map<String, User> tuijianUserMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(tuijianUserPhones)){
            List<User> tuijianUserList = userService.list(Wrappers.lambdaQuery(User.class).in(User::getPhone, tuijianUserPhones));
            for (User user : tuijianUserList) {
                tuijianUserMap.put(user.getPhone(), user);
            }
        }


        // 开始结算
        for (Order order : orderList) {
            // 计算投资人收益
            Equipment equipment = equipmentMap.get(order.getEquipment());
            User touziUser = touziUserMap.get(equipment.getTouziUserId());
            if(touziUser != null){

            }
            //获取推荐人身份、 计算推荐人收益
            User tuijianUser = tuijianUserMap.get(order.getDealerPhone());
            if(tuijianUser != null){

            }
            // 计算门店收益

            // 计算区域经理收益
            System.out.println(equipment);

        }

    }
}
