package com.wms_main.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms_main.constant.enums.wms.OrderStatusEnum;
import com.wms_main.dao.ITAppOrderInService;
import com.wms_main.mapper.AppOrderInMapper;
import com.wms_main.model.po.TAppOrderIn;
import org.springframework.stereotype.Service;

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

@Service
public class TAppOrderInServiceImpl extends ServiceImpl<AppOrderInMapper, TAppOrderIn> implements ITAppOrderInService {


    @Override
    public List<TAppOrderIn> getWithOrderId(String orderId) {
        try {
            LambdaQueryWrapper<TAppOrderIn> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TAppOrderIn::getOrderId, orderId);
            return list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<TAppOrderIn> getWithVehicleNoAndStatus(String vehicleNo, OrderStatusEnum status) {
        try {
            LambdaQueryWrapper<TAppOrderIn> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TAppOrderIn::getVehicleNo, vehicleNo);
            queryWrapper.eq(TAppOrderIn::getOrderStatus, status.getCode());
            return list(queryWrapper);

        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean updateStatusAndDestinationWithVehicleNo(String vehicleNo, OrderStatusEnum status, String destination) {
        try {
            LambdaUpdateWrapper<TAppOrderIn> queryWrapper = new LambdaUpdateWrapper<>();
            queryWrapper.eq(TAppOrderIn::getVehicleNo, vehicleNo);
            queryWrapper.set(TAppOrderIn::getOrderStatus, status.getCode());
            if(destination != null) {
                queryWrapper.set(TAppOrderIn::getDestination, destination);
            }
            queryWrapper.set(TAppOrderIn::getUpdateTime, LocalDateTime.now());
            return update(queryWrapper);
        } catch (Exception e) {
            return false;
        }
    }


}
