package com.gxwebsoft.shop.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.entity.OrderInfo;
import com.gxwebsoft.shop.mapper.OrderMapper;
import com.gxwebsoft.shop.param.OrderParam;
import com.gxwebsoft.shop.service.OrderInfoService;
import com.gxwebsoft.shop.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单结算任务
 *
 * @author 科技小王子
 * @since 2024-05-14 13:20:04
 */
@Api(tags = "订单结算任务")
@RestController
@RequestMapping("/api/shop/order")
public class OrderTask extends BaseController {
  @Value("${spring.profiles.active}")
  String active;
  @Resource
  private OrderService orderService;
  @Resource
  private OrderMapper orderMapper;
  @Resource
  private OrderInfoService orderInfoService;


  /**
   * 订单任务列表
   */
  @Scheduled(cron = "*/10 * * * * *")
  public void orderTask() {
      // 自动处理过期的预定订单
//      this.settledExpirationOrder();
  }

  /**
   * 自动核销订单
   */
  @Scheduled(cron = "*/30 * * * * *")
  public void writeOff() {
      // 自动核销订单
//      this.orderWriteOff();
  }

  // 自动处理过期订单任务
  public void settledExpirationOrder() {
    if (active.equals("dev")) {
      return;
    }
    final OrderParam p = new OrderParam();
    p.setOrderStatus(0);
    p.setExpirationTime(DateUtil.date());
    final List<Order> list = orderMapper.selectExpirationOrder(p);
    System.out.println("自动处理过期订单list = " + list);
    list.forEach(d -> {
      d.setOrderStatus(2);
    });
    if (!CollectionUtils.isEmpty(list)) {
      orderService.updateBatchById(list);
      System.out.println("自动处理过期订单 = " + list.size());
    }
  }

  public void settledExpirationOrderBy3028() {
    if (active.equals("dev")) {
      return;
    }
    final Order order = new Order();
    final List<OrderInfo> list = orderInfoService.list(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getTenantId, 10150).eq(OrderInfo::getOrderStatus, 0).eq(OrderInfo::getMerchantId, 3028).eq(OrderInfo::getDateTime, DateUtil.today()).lt(OrderInfo::getStartTime, "21:30:00"));
    list.forEach(d -> {
      d.setOrderStatus(1);
      order.setOrderId(d.getOrderId());
    });
    orderInfoService.updateBatchById(list);
    if (!CollectionUtils.isEmpty(list)) {
      orderService.updateById(order);
    }
    System.out.println("自动核销订单(游泳馆) = " + list.size());
  }

  // 自动核销订单
  public void orderWriteOff() {
    if (active.equals("dev")) {
      return;
    }
    final Order order = new Order();
    final int hour = DateUtil.hour(DateUtil.date(), true);
    final int minute = DateUtil.minute(DateUtil.date());
    final int second = DateUtil.second(DateUtil.date());
    final String startTime = String.format("%02d", hour).concat(":").concat(String.format("%02d", minute)).concat(":").concat(String.format("%02d", second));
    final List<OrderInfo> list = orderInfoService.list(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderStatus, 0).ne(OrderInfo::getMerchantId, 3028).eq(OrderInfo::getDateTime, DateUtil.today()).lt(OrderInfo::getStartTime, startTime));
    list.forEach(d -> {
      d.setOrderStatus(1);
      order.setOrderId(d.getOrderId());
    });
    orderInfoService.updateBatchById(list);
    order.setOrderStatus(1);
    orderService.updateById(order);
    System.out.println("自动核销订单 = " + list.size());
  }

}
