package cn.reghao.tnb.file.app.zorder.rpc;

import cn.reghao.file.api.dto.BuyDto;
import cn.reghao.file.api.iface.NewOrderService;
import cn.reghao.file.api.iface.JobService;
import cn.reghao.jutil.jdk.string.SnowFlake;
import cn.reghao.tnb.file.app.zorder.db.OrderMapper;
import cn.reghao.tnb.file.app.zorder.model.po.Order;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2025-02-24 11:00:51
 */
@DubboService
@Service
public class NewOrderServiceImpl implements NewOrderService {
    private final JobService jobService;
    private final OrderMapper orderMapper;
    private final SnowFlake idGenerator;

    public NewOrderServiceImpl(JobService jobService, OrderMapper orderMapper) {
        this.jobService = jobService;
        this.orderMapper = orderMapper;
        this.idGenerator = new SnowFlake(1L, 1L);
    }

    public long createOrder(BuyDto buyDto) {
        long orderId = idGenerator.nextId();
        Order order = new Order(orderId, buyDto);
        orderMapper.save(order);

        long timeout = 15*60;
        long jobId = jobService.addOrderTimeoutJob(orderId, timeout);
        return orderId;
    }
}
