package com.prolog.eis.service.shelves.impl;

import com.prolog.eis.core.model.base.area.WhArea;
import com.prolog.eis.core.model.base.area.WhAreaDirection;
import com.prolog.eis.dao.out.StationCfgMapper;
import com.prolog.eis.dao.shelves.PalletInfoMapper;
import com.prolog.eis.feign.WarehouseFeign;
import com.prolog.eis.model.container.StationCfg;
import com.prolog.eis.model.shelves.PalletInfo;
import com.prolog.eis.service.shelves.StationService;

import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StationServiceImpl implements StationService {

    @Resource
    private StationCfgMapper stationCfgMapper;

    @Resource
    PalletInfoMapper palletInfoMapper;

    @Resource
    WarehouseFeign warehouseFeign;

   /* @Resource
    RouteService routeService;

    @Resource
    WarehouseService warehouseService;*/

    @Override
    public List<StationCfg> query(String stationNo, Integer taskType, Integer isLock ) {
        return stationCfgMapper.query(stationNo, taskType,isLock);
    }

    @Override
    public void save(StationCfg stationCfg) {
        final List<StationCfg> stationCfgs = stationCfgMapper
                .findByMap(MapUtils.put("stationNo", stationCfg.getStationNo()).getMap(), StationCfg.class);
        if (!stationCfgs.isEmpty()) {
            throw new PrologException("当前站台已存在，不允许新增！");
        }

        //warehouseService.getLocatorByArea(stationCfg.getAreaNo());

        RestMessage<List<WhAreaDirection>> areaOrEndArea = warehouseFeign
                .findStartAreaOrEndArea(stationCfgs.stream().map(StationCfg::getAreaNo).collect(Collectors.toList()));

        if (!areaOrEndArea.isSuccess()) {
            throw new PrologException("查询区域方向仓库服务报错" + areaOrEndArea.getMessage());
        }

        if (null == areaOrEndArea) {
            throw new PrologException(String.format("%s区域方向不存在", stationCfg.getAreaNo()));
        }
        stationCfgMapper.save(stationCfg);
    }

    @Override
    public void update(StationCfg stationCfg) throws PrologException {
        final List<PalletInfo> palletInfos = palletInfoMapper.findByMap(MapUtils.put("subArea", stationCfg.getAreaNo()).getMap(), PalletInfo.class);
        if (palletInfos.size() > 0) {
            throw new PrologException("当前站台已有任务，不允许修改！");
        }
        List<StationCfg> stationCfgs = stationCfgMapper
                .findByMap(MapUtils.put("stationNo", stationCfg.getStationNo()).getMap(), StationCfg.class);

        if (!stationCfgs.get(0).getTaskType().equals(stationCfg.getTaskType())) {
            throw new PrologException("站台类型不允许切换，不允许修改！");
        }
       // stationCfg.setUpdateTime(new Date());
        stationCfgMapper.update(stationCfg);
    }

    @Override
    public RestMessage<?> checkWorkStationNo(String stationNo, Integer taskType) {
        List<StationCfg> stationCfgs = stationCfgMapper.findByMap(
                MapUtils.put("stationNo", stationNo).put("taskType", taskType).getMap(),
                StationCfg.class);
        if (stationCfgs.size() == 0) {
            return RestMessage.newInstance(false, "201", "站台信息未配置，请检查", null);
        }
        StationCfg stationCfg = stationCfgs.get(0);
        if (1 == stationCfg.getIsLock()) {
            return RestMessage.newInstance(false, "站台 [" + stationNo + "]已锁定");
        }
        /*if (null != stationCfg.getPickOrderId()) {
            return RestMessage.newInstance(false, "201", "站台有任务，请检查", null);
        }*/
        if (null == stationCfg.getMaxCachePosition() || stationCfg.getMaxCachePosition() <= 0) {
            return RestMessage.newInstance(false, "201", "站台缓存配置错误，请检查", null);
        }

        /*RestMessage<WhArea> whAreaRestMessage = warehouseFeign.getAreaByAreaNo(stationCfg.getAreaNo());
        if (!whAreaRestMessage.isSuccess()) {
            return RestMessage.newInstance(false, "201", "查询区域仓库服务报错" + whAreaRestMessage.getMessage(), null);
        }
        WhArea whArea = whAreaRestMessage.getData();
        if (null == whArea) {
            return RestMessage.newInstance(false, "201", stationCfg.getAreaNo() + "区域不存在", null);
        }

        RestMessage<List<WhAreaDirection>> areaOrEndArea = warehouseFeign
                .findStartAreaOrEndArea(stationCfgs.stream().map(StationCfg::getAreaNo).collect(Collectors.toList()));

        if (!areaOrEndArea.isSuccess()) {
            return RestMessage.newInstance(false, "201", "查询区域方向仓库服务报错" + areaOrEndArea.getMessage(), null);
        }

        if (null == areaOrEndArea) {
            return RestMessage.newInstance(false, "201", stationCfg.getStationNo() + "区域方向不存在", null);
        }*/
        return RestMessage.newInstance(true, "200", "验证通过", null);
    }
}
