package com.ruoyi.system.utils;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.PosOrderLog;
import com.ruoyi.system.service.IPosOrderLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class OrderLogHelper {

    @Autowired
    private IPosOrderLogService orderLogService;

    /**
     * 异步写入订单日志
     */
    public void log(String ddId, int operatorType, Long operatorId, String operatorName, String content) {
        PosOrderLog orderLog = new PosOrderLog();
        orderLog.setDdId(ddId);
        orderLog.setOperatorType(operatorType);
        orderLog.setOperatorId(operatorId);
        orderLog.setOperatorName(operatorName);
        orderLog.setContent(content);
        ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
        executor.schedule(() -> SpringUtils.getBean(IPosOrderLogService.class).insertOrderLog(orderLog),
                10, TimeUnit.MILLISECONDS);
    }

    /**
     * 同步写入订单日志（用于定时任务等异步上下文）
     */
    public void logSync(String ddId, int operatorType, Long operatorId, String operatorName, String content) {
        PosOrderLog orderLog = new PosOrderLog();
        orderLog.setDdId(ddId);
        orderLog.setOperatorType(operatorType);
        orderLog.setOperatorId(operatorId);
        orderLog.setOperatorName(operatorName);
        orderLog.setContent(content);
        orderLogService.insertOrderLog(orderLog);
    }
}
