package cn.iocoder.yudao.module.member.job;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.orderspacecapsule.vo.OrderSpaceCapsuleSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.orderspacecapsule.OrderSpaceCapsuleDO;
import cn.iocoder.yudao.module.member.dal.mysql.orderspacecapsule.OrderSpaceCapsuleMapper;
import cn.iocoder.yudao.module.member.enums.SpaceCapsuleOrderEnum;
import cn.iocoder.yudao.module.member.service.orderspacecapsule.OrderSpaceCapsuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zt 定时任务太空舱订单结算
 * @description <description class purpose>
 * @since 2024/10/11
 */
@Slf4j
@Component
public class OrderSpaceCapsuleJob implements JobHandler {
    @Resource
    private OrderSpaceCapsuleMapper orderSpaceCapsuleMapper;
    @Resource
    private OrderSpaceCapsuleService order;
    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        List<OrderSpaceCapsuleDO> orderSpaceCapsuleDOS = orderSpaceCapsuleMapper.selectList(new LambdaQueryWrapperX<OrderSpaceCapsuleDO>()
                .eq(OrderSpaceCapsuleDO::getStatus, SpaceCapsuleOrderEnum.SPACE_CAPSULE_ORDER_ENUM_IN_USE.getCode()));
       if(ObjUtil.isEmpty(orderSpaceCapsuleDOS)){
           log.info("订单还在有效期内");
           return "太空舱订单扣款,订单还在有效期内";
       }else{
           //获得到需要扣款的数据
           List<OrderSpaceCapsuleDO> collect = orderSpaceCapsuleDOS.stream()
                   .map(m -> {
                       LocalDateTime now = LocalDateTime.now();
                       LocalDateTime orderStartTime = m.getOrderStartTime();
                       int t = m.getComboNum() * m.getComboMinutes();
                       LocalDateTime offset = LocalDateTimeUtil.offset(orderStartTime, t, ChronoUnit.MINUTES);
                       if (now.isAfter(offset)) {
                           return m;
                       }
                       return null;
                   })
                   .filter(Objects::nonNull)
                   .collect(Collectors.toList());
           log.info("太空舱订单扣款数量为{}",collect.size());
           for (OrderSpaceCapsuleDO aDo : collect) {
               aDo.setStatus(SpaceCapsuleOrderEnum.SPACE_CAPSULE_ORDER_ENUM_DONE.getCode());
               order.updateOrderSpaceCapsule(BeanUtils.toBean(aDo, OrderSpaceCapsuleSaveReqVO.class));
           }
           return "太空舱订单扣款数量为："+collect.size();
       }
    }
}
