package com.wms.service.serviceImplements;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms.constants.enums.TaskType;
import com.wms.entity.table.Stand;
import com.wms.mapper.StandMapper;
import com.wms.service.StandService;
import com.wms.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StandServiceImplements extends ServiceImpl<StandMapper, Stand> implements StandService {
    private final StandMapper standMapper;

    /**
     * 验证站台正确性
     * @param standId 待验证站台
     * @param taskType 任务类型---1：入库，2：出库，4：盘点
     * @return 验证结果
     */
    public boolean validateStand(String standId, Integer taskType) {
        if (StringUtils.isEmpty(standId) || taskType == null) {
            return false;
        }
        LambdaQueryWrapper<Stand> lambdaQueryWrapper = new LambdaQueryWrapper<Stand>()
                .eq(Stand::getStandId, standId)
                .eq(Stand::getStandStatus, 0)// 是否可用
                .eq(Stand::getIsLock, 0);// 是否锁定
        if (taskType.equals(TaskType.IN.getCode())) {// 入库
            lambdaQueryWrapper.eq(Stand::getAllowIn, 1);// 入库
        } else if (taskType.equals(TaskType.OUT.getCode())) {// 出库
            lambdaQueryWrapper.eq(Stand::getAllowOut, 1);// 出库
        } else if (taskType.equals(TaskType.INVENTORY.getCode())) {// 拣选或者盘点
            lambdaQueryWrapper.eq(Stand::getStandType, 2);
        } else {
            return false;
        }
        return standMapper.exists(lambdaQueryWrapper);
    }

    /**
     * 根据ip地址获取站台号
     * @param ip ip地址
     * @return 站台id
     */
    @Override
    public String getStandIdByIp(String ip) {
        if (StringUtils.isEmpty(ip)) {
            return "";
        }
        Stand stand = standMapper.selectOne(new LambdaQueryWrapper<Stand>().eq(Stand::getStandIp, ip));
        return stand != null ? stand.getStandId() : "";
    }
}
