package cn.wisenergy.service.app.impl;

import cn.wisenergy.mapper.OrderMapper;
import cn.wisenergy.model.app.OrderInfo;
import cn.wisenergy.service.app.UserLevelService;
import cn.wisenergy.service.app.UserLevelTaskService;
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 java.util.List;

@Service
@Slf4j
public class UserLevelTaskServiceImpl implements UserLevelTaskService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserLevelService userLevelService;

    //获取订单
    @XxlJob(value = "userLevelUpjobhandler")
    @Override
    public void userLevelUp() {
        //1.获取数据库订单数据
        log.info("------------------------日定时任务+5分钟执行一次+用户升级开始----------------------------------");
        List<OrderInfo> ordersByLevelStatus = orderMapper.getByLevelStatus(0);
        for (OrderInfo orderInfo : ordersByLevelStatus) {
            String userId = orderInfo.getUserId();
            userLevelService.userLevelUpgrade(userId);
            //当前订单升级状态置为1
            orderInfo.setLevelStatus(1);
            orderMapper.updateById(orderInfo);
        }
        log.info("-------------------------日定时任务+5分钟执行一次+用户升级结束--------------------------------------");
    }

    @XxlJob("monthUserLevelUpjobhandler")
    @Override
    public void monthUserLevelUp(){
        //1.获取数据库订单数据
        log.info("------------------------月定时任务：====用户升级开始----------------------------------");
        List<OrderInfo> ordersByLevelStatus = orderMapper.getByMonthOrderStatus(0);
        for (OrderInfo orderInfo : ordersByLevelStatus) {
            String userId = orderInfo.getUserId();
            userLevelService.userLevelUpgrade(userId);
            //当前订单升级状态置为1
            orderInfo.setLevelStatus(1);
            orderMapper.updateById(orderInfo);
        }
        log.info("-------------------------月定时任务：====用户升级结束--------------------------------------");
    }

}
