package com.foreverwin.mesnac.integration.service.impl;

import com.foreverwin.mesnac.common.constant.IntegrationTypeConstant;
import com.foreverwin.mesnac.common.util.ExceptionUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.integration.dto.DisRouterSyncRequest;
import com.foreverwin.mesnac.integration.dto.ItemSyncRequest;
import com.foreverwin.mesnac.integration.dto.RouterSyncRequest;
import com.foreverwin.mesnac.integration.dto.WebServiceResponse;
import com.foreverwin.mesnac.integration.model.IntegrationLog;
import com.foreverwin.mesnac.integration.service.CAPPAPIWebService;
import com.foreverwin.mesnac.integration.service.IntegrationLogService;
import com.foreverwin.mesnac.integration.service.InterfaceService;
import com.foreverwin.modular.core.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Component
@WebService(serviceName = "CAPPAPIWebService",
        targetNamespace = "http://service.integration.mesnac.com",
        endpointInterface = "com.foreverwin.mesnac.integration.service.CAPPAPIWebService")
public class CAPPAPIWebServiceImpl implements CAPPAPIWebService {

    @Autowired
    private InterfaceService interfaceService;
    @Autowired
    private IntegrationLogService integrationLogService;

    @Override
    public WebServiceResponse itemSync(ItemSyncRequest itemSyncRequest) {
        WebServiceResponse response = new WebServiceResponse();

        String site = null;
        try {
            if (itemSyncRequest == null) {
                throw BusinessException.build("传递的参数格式不正确，或者为空");
            }
            if (StringUtil.isBlank(itemSyncRequest.getTRANID())) {
                throw BusinessException.build("参数【TRANID】不能为空");
            }
            if (StringUtil.isBlank(itemSyncRequest.getDATE_TIME())) {
                throw BusinessException.build("参数【DATE_TIME】不能为空");
            }
            response.setHANDLE(itemSyncRequest.getTRANID());
            response.setSTATUS("S");

            site = itemSyncRequest.getSITE();
            String item = itemSyncRequest.getITEM();
            if (StringUtil.isBlank(site)) {
                throw BusinessException.build("站点不能为空");
            }
            if (StringUtil.isBlank(item)) {
                throw BusinessException.build("物料编号不能为空");
            }

            //调用业务接口
            interfaceService.itemSync(site, itemSyncRequest);
            response.setMESSAGE("物料【" + item + "】数据同步成功");

        } catch (Exception e) {
            response.setSTATUS("E");
            response.setMESSAGE(ExceptionUtil.getExceptionMsg(e));
        } finally {

            //记录接口日志
            IntegrationLog log = new IntegrationLog();
            log.setHandle(UUID.randomUUID().toString());
            log.setSite(site);
            log.setIntegrationType(IntegrationTypeConstant.ITEM);
            log.setCategory("RESPONSE");
            log.setIntegrationWay("CAPP");
            log.setIntegrationMethod("InterfaceService.itemSync");
            log.setParam(itemSyncRequest.toString());
            log.setStatus(response.getSTATUS());
            log.setResultMessage(response.getMESSAGE());
            log.setTransactionId(itemSyncRequest.getTRANID());
            log.setRequestDateTime(LocalDateTime.parse(itemSyncRequest.getDATE_TIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.setCreatedDateTime(LocalDateTime.now());
            integrationLogService.saveIntegrationLog(log);
        }

        //接口返回
        return response;
    }

    @Override
    public WebServiceResponse disRouterSync(DisRouterSyncRequest disRouterSyncRequest) {
        WebServiceResponse response = new WebServiceResponse();

        String site = null;
        try {
            if (disRouterSyncRequest == null) {
                throw BusinessException.build("传递的参数格式不正确，或者为空");
            }
            if (StringUtil.isBlank(disRouterSyncRequest.getTRANID())) {
                throw BusinessException.build("参数【TRANID】不能为空");
            }
            if (StringUtil.isBlank(disRouterSyncRequest.getDATE_TIME())) {
                throw BusinessException.build("参数【DATE_TIME】不能为空");
            }
            response.setHANDLE(disRouterSyncRequest.getTRANID());
            response.setSTATUS("S");

            site = disRouterSyncRequest.getSITE();
            String router = disRouterSyncRequest.getROUTER();
            if (StringUtil.isBlank(site)) {
                throw BusinessException.build("参数【SITE】不能为空");
            }
            if (StringUtil.isBlank(router)) {
                throw BusinessException.build("参数【ROUTER】不能为空");
            }

            //调用业务接口
            interfaceService.disRouterSync(site, disRouterSyncRequest);
            response.setMESSAGE("返工工艺路线【" + router + "】同步成功");

        } catch (Exception e) {
            response.setSTATUS("E");
            response.setMESSAGE(ExceptionUtil.getExceptionMsg(e));
        } finally {

            //记录接口日志
            IntegrationLog log = new IntegrationLog();
            log.setHandle(UUID.randomUUID().toString());
            log.setSite(site);
            log.setIntegrationType(IntegrationTypeConstant.DIS_ROUTER);
            log.setCategory("RESPONSE");
            log.setIntegrationWay("CAPP");
            log.setIntegrationMethod("InterfaceService.disRouterSync");
            log.setParam(disRouterSyncRequest.toString());
            log.setStatus(response.getSTATUS());
            log.setResultMessage(response.getMESSAGE());
            log.setTransactionId(disRouterSyncRequest.getTRANID());
            log.setRequestDateTime(LocalDateTime.parse(disRouterSyncRequest.getDATE_TIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.setCreatedDateTime(LocalDateTime.now());
            integrationLogService.saveIntegrationLog(log);
        }

        //接口返回
        return response;
    }
}
