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

import com.alibaba.fastjson.JSONObject;
import com.prolog.eis.core.model.base.area.WhArea;
import com.prolog.eis.feign.WarehouseFeignService;
import com.prolog.eis.service.feign.WarehouseService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author dengj
 * @version 1.0
 * @date 2023/2/17 15:24
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class WarehouseServiceImpl  implements WarehouseService {

    private final WarehouseFeignService warehouseFeignService;

    @Override
    public WhArea getAreaByLocation(String locationNo) throws Exception {
        RestMessage<WhArea> whAreaRestMessage = warehouseFeignService.getAreaByLocation(locationNo);
        if (!whAreaRestMessage.isSuccess()){
            log.error("根据点位{}查询区域信息失败：" + whAreaRestMessage.getMessage(), locationNo);
            throw new PrologException(String.format("根据点位%s查询区域信息失败：" + whAreaRestMessage.getMessage(), locationNo));
        }
        if (Objects.isNull(whAreaRestMessage.getData())) {
            log.error("根据点位{}查询区域信息为空", locationNo);
            throw new PrologException(String.format("根据点位%s查询区域信息为空" , locationNo));
        }
        return whAreaRestMessage.getData();
    }

    @Override
    public List<WhArea> batchQueryLocation(List<String> locationNos) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("locationList", locationNos);
        RestMessage<List<WhArea>> whAreaRestMessage = warehouseFeignService.batchQueryLocation(jsonObject);
        if (!whAreaRestMessage.isSuccess()){
            log.error("根据点位{}查询区域信息失败：" + whAreaRestMessage.getMessage(), JsonUtils.toString(locationNos));
            throw new PrologException(String.format("根据点位%s查询区域信息失败：" + whAreaRestMessage.getMessage(), JsonUtils.toString(locationNos)));
        }
        if (CollectionUtils.isEmpty(whAreaRestMessage.getData())) {
            log.error("根据点位{}查询区域信息为空", JsonUtils.toString(locationNos));
            throw new PrologException(String.format("根据点位%s查询区域信息为空" , JsonUtils.toString(locationNos)));
        }
        return whAreaRestMessage.getData();
    }
}
