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

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.prolog.eis.dto.inbound.WmsInBackReqDto;
import com.prolog.eis.dto.result.WmsResult;
import com.prolog.eis.service.shelves.WmsInTaskRepeatBackService;
import com.prolog.eis.utils.HttpUtil;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.fx.inf.core.Exception.RetryException;
import com.prolog.fx.inf.core.InterfaceType;
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.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.util.List;


@Service
@Slf4j
@PrologInterface(key = "WmsInTaskRepeatBackService", name = "agv入库回告wms", type = InterfaceType.Send)
public class WmsInTaskRepeatBackServiceImpl implements WmsInTaskRepeatBackService {

    @Value("${wms.wms-in-back}")
    private String eis2wmsInBack;

    @Override
    public WmsResult execute(WmsInBackReqDto wmsInBackReqDtos) throws Exception {
        try {
            String  a = eis2wmsInBack;
            String restMessage = HttpUtil.postMethod(eis2wmsInBack, JSON.toJSONString(wmsInBackReqDtos));

            if (StrUtil.isBlank(restMessage)) {
                throw new Exception("入库请求wms失败,返回结果：" + restMessage);
            }
            RestMessage result = JSON.parseObject(restMessage, RestMessage.class);
            if (!result.isSuccess()) {
                throw new Exception("入库请求wms失败" + result.getMessage());
            }
            WmsResult wmsResult = new WmsResult();
            wmsResult.setMessage(result.getMessage());
            wmsResult.setStatus("200");
            return wmsResult;
        } catch (Exception e) {
            throw new RetryException(e.getMessage());
        }
    }

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

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