package com.gree.mall.manager.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gree.mall.manager.enums.LeaseOrderStatusEnum;
import com.gree.mall.manager.plus.entity.LeaseOrder;
import com.gree.mall.manager.plus.entity.LeaseOrderItem;
import com.gree.mall.manager.plus.service.LeaseOrderItemService;
import com.gree.mall.manager.plus.service.LeaseOrderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 租赁订单
 */
@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Component
public class LeaseOrderSchedule {
    @Value("${spring.profiles.active}")
    private String env;
    @Resource
    LeaseOrderService leaseOrderService;
    @Resource
    LeaseOrderItemService leaseOrderItemService;

    @Scheduled(cron = "0 0 12 * * ?")
    @Transactional
    public void task() {
        if(env.equals("dev")){
            return;
        }
        List<LeaseOrder> orderList = leaseOrderService.lambdaQuery()
                .eq(LeaseOrder::getPayStatus, true)
                .in(LeaseOrder::getStatus, LeaseOrderStatusEnum.WAIT_START.getKey(), LeaseOrderStatusEnum.LEASE.getKey())
                .list();
        if (CollectionUtil.isNotEmpty(orderList)) {
            for (LeaseOrder order : orderList) {
                LeaseOrderItem item = leaseOrderItemService.lambdaQuery()
                        .eq(LeaseOrderItem::getLeaseOrderId, order.getId())
                        .last("limit 1")
                        .one();
                final DateTime curDate = DateUtil.date();
                if (order.getStatus().equals(LeaseOrderStatusEnum.WAIT_START.getKey())) {
                    if (curDate.getTime() <= item.getStartDate().getTime()) {
                        leaseOrderService.lambdaUpdate()
                                .set(LeaseOrder::getStatus, LeaseOrderStatusEnum.LEASE.getKey())
                                .eq(LeaseOrder::getId, order.getId())
                                .update();
                    }
                } else if (order.getStatus().equals(LeaseOrderStatusEnum.LEASE.getKey())) {
                    long leaseDay = DateUtil.betweenDay(Objects.isNull(item.getRealEndDate()) ? item.getEndDate() : item.getRealEndDate(), curDate, true) + 1;

                    if (leaseDay > 0) {
                        // 已超到期日期
                        leaseOrderService.lambdaUpdate()
                                .set(LeaseOrder::getStatus, LeaseOrderStatusEnum.OVER.getKey())
                                .eq(LeaseOrder::getId, order.getId())
                                .update();
                    }

                }
            }
        }
    }
}
