package cn.iocoder.yudao.module.order.service.orderOperateLog;

import java.util.*;
import java.util.concurrent.Future;
import javax.annotation.Resource;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.order.vo.orderOperateLog.*;
import cn.iocoder.yudao.module.order.dal.dataobject.orderOperateLog.OrderOperateLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.order.convert.orderOperateLog.OrderOperateLogConvert;
import cn.iocoder.yudao.module.order.dal.mysql.orderOperateLog.OrderOperateLogMapper;


/**
 * 订单操作日志 Service 实现类
 *
 * @author 郑屹
 */
@Service
@Validated
@Slf4j
public class OrderOperateLogServiceImpl extends AbstractService<OrderOperateLogMapper, OrderOperateLogDO> implements OrderOperateLogService {

    @Resource
    private OrderOperateLogMapper orderOperateLogMapper;

    @Override
    @Async
    public Future<Boolean> createOrderAsyncOperateLog(OrderOperateLogCreateReqVO createReqVO) {
        boolean success = false;
        try {
            // 插入
            OrderOperateLogDO orderOperateLog = OrderOperateLogConvert.INSTANCE.convert(createReqVO);
            orderOperateLog.setCreateTime(new Date());
            orderOperateLog.setUpdateTime(new Date());
            orderOperateLogMapper.insert(orderOperateLog);
        } catch (Throwable throwable) {
            // 仅仅打印日志，不对外抛出。原因是，还是要保留现场数据。
            log.error("[createOrderAsyncOperateLog][记录操作日志异常，日志为 ({})]", createReqVO, throwable);
        }
        // 返回
        return new AsyncResult<>(success);
    }

//    @Override
//    public void updateOrderOperateLog(OrderOperateLogUpdateReqVO updateReqVO) {
//        // 校验存在
////        this.validateOrderOperateLogExists(updateReqVO.getId());
//        // 更新
//        OrderOperateLogDO updateObj = OrderOperateLogConvert.INSTANCE.convert(updateReqVO);
//        orderOperateLogMapper.updateById(updateObj);
//    }

//    @Override
//    public void deleteOrderOperateLog(Long id) {
//        // 校验存在
////        this.validateOrderOperateLogExists(id);
//        // 删除
//        orderOperateLogMapper.deleteById(id);
//    }

//    private void validateOrderOperateLogExists(Long id) {
//        if (orderOperateLogMapper.selectById(id) == null) {
//            throw exception(ORDER_OPERATE_LOG_NOT_EXISTS);
//        }
//    }

    @Override
    public OrderOperateLogDO getOrderOperateLog(Long id) {
        return orderOperateLogMapper.selectById(id);
    }

    @Override
    public List<OrderOperateLogDO> getOrderOperateLogList(Collection<Long> ids) {
        return orderOperateLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderOperateLogDO> getOrderOperateLogPage(OrderOperateLogQueryVO query, PageVO page) {
        return orderOperateLogMapper.selectPage(page, query);
    }

    @Override
    public List<OrderOperateLogDO> getOrderOperateLogList(OrderOperateLogQueryVO query) {
        return orderOperateLogMapper.selectList(query);
    }
}
