package com.prolog.eis.service.pms;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.prolog.eis.dto.outbound.WmsOutCbReqDto;
import com.prolog.eis.dto.pms.InBoundCarryTaskReq;
import com.prolog.eis.dto.pms.PmsResult;
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.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;

/**
 * @desc:
 * @author: zhangJie
 * @date: 2023/7/11 10:48
 */
@Service
@Slf4j
@PrologInterface(key = "inCarryTask", name = "pms入库搬运", type = InterfaceType.Send)
public class PmsInCarryTaskService implements StandardInterface<InBoundCarryTaskReq, PmsResult> {

    @Value("${pms.pms-in-carryTask}")
    private String eis2pmsInCarryTask;

    @Autowired
    private RestTemplate newRestTemplate;

    @Override
    public PmsResult execute(InBoundCarryTaskReq inBoundCarryTaskReq)  {
        try {
            PmsResult pmsResult = newRestTemplate.postForObject(eis2pmsInCarryTask, inBoundCarryTaskReq, PmsResult.class);
            if (!pmsResult.isSuccess()) {
                throw new Exception("请求pms失败" + pmsResult.getMessage());
            }
            return pmsResult;
        } catch (Exception e) {
            log.error("容器【{}】请求pms入库搬运失败：" + e.getMessage(),inBoundCarryTaskReq.getContainerNo(), e);
            throw new RetryException(e.getMessage());
        }
    }

    @Override
    public SimpleLog toLog(InBoundCarryTaskReq inBoundCarryTaskReqs) {
        SimpleLog simpleLog = new SimpleLog();
        try {
            simpleLog.setData(JsonUtils.toString(inBoundCarryTaskReqs));
        } catch (JsonProcessingException e) {
            log.error(e.getMessage());
        }
        return simpleLog;
    }

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