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

import cn.hutool.core.collection.CollectionUtil;
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.storegoodsinventory.vo.AddReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storeorder.StoreOrderDO;
import cn.iocoder.yudao.module.member.service.async.MemberAsyncService;
import cn.iocoder.yudao.module.member.service.storeorder.StoreOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * @Author:qjq 计算门店流水
 * @Date:2024/4/24 下午3:22
 */
@Component
@Slf4j
public class StoreOrderCancelJob implements JobHandler {
    @Resource
    private StoreOrderService storeOrderService;
    @Resource
    private MemberAsyncService memberAsyncService;
    /**
     * 执行任务
     *
     * @param param 参数
     * @return 结果
     * @throws Exception 异常
     */
    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        try {
            List<StoreOrderDO> storeOrderDOS = storeOrderService.selectPayOrder();
            if(CollectionUtil.isNotEmpty(storeOrderDOS)){
                List<Integer> collect = storeOrderDOS.stream().map(StoreOrderDO::getOrderId).collect(Collectors.toList());
                for (Integer orderId:collect){
                    AddReqVO cancel = storeOrderService.cancel(orderId);
                    memberAsyncService.batchRecord(cancel);
                }
            }


        } catch (Exception e) {
            return "订单取消失败";
        }
        return "订单取消成功";
    }
}
