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.ITAppWcsTaskService;
import com.wms_main.mapper.AppWcsTaskMapper;
import com.wms_main.model.po.TAppWcsTask;
import com.wms_main.repository.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * wcs任务表(TAppWcsTask)表服务实现类
 */
@Service
public class TAppWcsTaskServiceImpl extends ServiceImpl<AppWcsTaskMapper, TAppWcsTask> implements ITAppWcsTaskService {
    @Override
    public Boolean existsTaskWithLocation(String Location) {
        if(StringUtils.isEmpty(Location)) {
            return false;
        }
        try {
            LambdaQueryWrapper<TAppWcsTask> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TAppWcsTask::getDestination, Location)
                    .or()
                    .eq(TAppWcsTask::getOrigin, Location);
            List<TAppWcsTask> list = list(queryWrapper);
            if(list == null) {
                return null;
            }
            return !list.isEmpty();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Integer getEquipmentByLocation(String locationId) {
        String[] str_lst = locationId.split("-");
        int row = Integer.parseInt(str_lst[0].replace("A", ""));
        return (int) Math.floor((double) (row + 1) / 2);
    }

    @Override
    public Integer getDepthByLocation(String locationId) {
        String[] str_lst = locationId.split("-");
        int depth = Integer.parseInt(str_lst[str_lst.length - 1]);
        return depth;
    }
}
