package com.prolog.eis.service.wms;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.prolog.eis.dto.item.ItemSyncVO;
import com.prolog.eis.dto.page.ItemReqDto;
import com.prolog.eis.dto.page.PageItem;
import com.prolog.eis.dto.wms.WmsTaskFinishDto;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.fx.inf.core.InterfaceType;
import com.prolog.fx.inf.core.StandardInterface;
import com.prolog.fx.inf.core.annotation.PrologInterface;
import com.prolog.fx.inf.core.dto.SimpleLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;

@Service
@Slf4j
@PrologInterface(key = "itemPage", name = "查询商品信息分页", type = InterfaceType.Send)
public class WmsItemPageService implements StandardInterface<ItemReqDto, RestMessage> {

    @Value("${wms.wms-item-page}")
    private String eis2wmsItemPage;
    @Autowired
    private RestTemplate newRestTemplate;

    @Override
    public RestMessage execute(ItemReqDto dataObject) throws Exception {
        RestMessage restMessage = newRestTemplate.postForObject(eis2wmsItemPage, dataObject, RestMessage.class);
        if (!restMessage.isSuccess()) {
            log.error("请求wms失败" + restMessage.getMessage());
            throw new Exception("请求wms失败" + restMessage.getMessage());
        }
        return restMessage;
    }

    @Override
    public SimpleLog toLog(ItemReqDto dataObject) {
        SimpleLog simpleLog = new SimpleLog();
        try {
            simpleLog.setData(JsonUtils.toString(dataObject));
        } catch (JsonProcessingException e) {
            log.error(e.getMessage());
        }
//        simpleLog.setBussinessKey1(dataObject.getContainerNo());
        return simpleLog;
    }

    @Override
    public ItemReqDto toDataObject(SimpleLog simpleLog) {
        if (simpleLog != null) {
            try {
                ItemReqDto dataObject = JsonUtils.parseJsonString(simpleLog.getData(), new TypeReference<ItemReqDto>() {
                });
                return dataObject;
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
        return null;
    }
}
