package com.wms_main.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms_main.dao.ITAppAgvLockService;
import com.wms_main.mapper.AppAgvLockMapper;
import com.wms_main.model.po.TAppAgvLock;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AGV互锁表数据访问实现
 */
@Service
public class TAppAgvLockServiceImpl extends ServiceImpl<AppAgvLockMapper, TAppAgvLock> implements ITAppAgvLockService {

    @Override
    public List<TAppAgvLock> getByAgvAndStation(String agvId, String feedStation) {
        return list(new LambdaQueryWrapper<TAppAgvLock>()
                .eq(TAppAgvLock::getAgvId, agvId)
                .eq(TAppAgvLock::getFeedStation, feedStation)
                .eq(TAppAgvLock::getLockStatus, 1)
                .orderByAsc(TAppAgvLock::getCreateTime)
        );
    }

    @Override
    public List<TAppAgvLock> getByStation(String feedStation) {
        return list(new LambdaQueryWrapper<TAppAgvLock>()
                .eq(TAppAgvLock::getFeedStation, feedStation)
                .eq(TAppAgvLock::getLockStatus, 1)
                .orderByAsc(TAppAgvLock::getPriority)
                .orderByAsc(TAppAgvLock::getCreateTime)
        );
    }

    @Override
    public List<TAppAgvLock> getTimeoutLocks() {
        return list(new LambdaQueryWrapper<TAppAgvLock>()
                .eq(TAppAgvLock::getLockStatus, 1)
                .lt(TAppAgvLock::getLockTime, LocalDateTime.now().minusSeconds(300)) // 超过5分钟的锁定
        );
    }
}