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.*;
import com.foreverwin.mesnac.integration.model.IntegrationLog;
import com.foreverwin.mesnac.integration.service.ERPAPIWebService;
import com.foreverwin.mesnac.integration.service.IntegrationLogService;
import com.foreverwin.mesnac.integration.service.InterfaceService;
import com.foreverwin.modular.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 = "ERPAPIWebService",
            targetNamespace = "http://service.integration.mesnac.com",
            endpointInterface = "com.foreverwin.mesnac.integration.service.ERPAPIWebService")
public class ERPAPIWebServiceImpl implements ERPAPIWebService {

    private static Logger logger = LoggerFactory.getLogger(ERPAPIWebServiceImpl.class);

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


    @Override
    public WebServiceResponse bomSync(BomSyncRequest bomSyncRequest) {
        WebServiceResponse response = new WebServiceResponse();

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

            site = bomSyncRequest.getSITE();
            String shopOrder = bomSyncRequest.getSHOP_ORDER();
            if (StringUtil.isBlank(site)) {
                throw BusinessException.build("参数【SITE】不能为空");
            }

            //调用业务接口
            interfaceService.bomSync(site, bomSyncRequest);
            response.setMESSAGE("工单【" + shopOrder + "】BOM数据同步成功");

        } 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.BOM);
            log.setCategory("RESPONSE");
            log.setIntegrationWay("ERP");
            log.setIntegrationMethod("InterfaceService.shopOrderSync");
            log.setParam(bomSyncRequest.toString());
            log.setStatus(response.getSTATUS());
            log.setResultMessage(response.getMESSAGE());
            log.setTransactionId(bomSyncRequest.getTRANID());
            log.setRequestDateTime(LocalDateTime.parse(bomSyncRequest.getDATE_TIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.setCreatedDateTime(LocalDateTime.now());
            integrationLogService.saveIntegrationLog(log);
        }

        //接口返回
        return response;
    }

    @Override
    public WebServiceResponse routerSync(RouterSyncRequest routerSyncRequest) {
        WebServiceResponse response = new WebServiceResponse();

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

            site = routerSyncRequest.getSITE();
            String shopOrder = routerSyncRequest.getSHOP_ORDER();
            if (StringUtil.isBlank(site)) {
                throw BusinessException.build("站点参数不能为空");
            }

            //调用业务接口
            interfaceService.routerSync(site, routerSyncRequest);
            response.setMESSAGE("工单【" + shopOrder + "】工艺路线数据同步成功");

        } 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.ROUTER);
            log.setCategory("RESPONSE");
            log.setIntegrationWay("ERP");
            log.setIntegrationMethod("InterfaceService.routerSync");
            log.setParam(routerSyncRequest.toString());
            log.setStatus(response.getSTATUS());
            log.setResultMessage(response.getMESSAGE());
            log.setTransactionId(routerSyncRequest.getTRANID());
            log.setRequestDateTime(LocalDateTime.parse(routerSyncRequest.getDATE_TIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.setCreatedDateTime(LocalDateTime.now());
            integrationLogService.saveIntegrationLog(log);
        }

        //接口返回
        return response;
    }

    @Override
    public WebServiceResponse shopOrderSync(ShopOrderSyncRequest shopOrderSyncRequest) {
        WebServiceResponse response = new WebServiceResponse();

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


            site = shopOrderSyncRequest.getSITE();
            String shopOrder = shopOrderSyncRequest.getSHOP_ORDER();
            if (StringUtil.isBlank(site)) {
                throw BusinessException.build("参数【SITE】不能为空");
            }

            //调用业务接口
            interfaceService.shopOrderSync(site, shopOrderSyncRequest);
            response.setMESSAGE("工单【" + shopOrder + "】同步成功");

        } 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.SHOP_ORDER);
            log.setCategory("RESPONSE");
            log.setIntegrationWay("ERP");
            log.setIntegrationMethod("InterfaceService.shopOrderSync");
            log.setParam(shopOrderSyncRequest.toString());
            log.setStatus(response.getSTATUS());
            log.setResultMessage(response.getMESSAGE());
            log.setTransactionId(shopOrderSyncRequest.getTRANID());
            log.setRequestDateTime(LocalDateTime.parse(shopOrderSyncRequest.getDATE_TIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.setCreatedDateTime(LocalDateTime.now());
            integrationLogService.saveIntegrationLog(log);
        }

        //接口返回
        return response;
    }

    @Override
    public WebServiceResponse itemBatchReceive(ItemBatchReceiveRequest itemBatchReceiveRequest) {
        logger.error("ERP物料接受接口：" + itemBatchReceiveRequest);
        WebServiceResponse response = new WebServiceResponse();

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


            site = itemBatchReceiveRequest.getSITE();
            String purchaseOrder = itemBatchReceiveRequest.getPURCHASE_ORDER();
            if (StringUtil.isBlank(site)) {
                throw BusinessException.build("参数【SITE】不能为空");
            }

            //调用业务接口
            interfaceService.saveItemBatch(site, itemBatchReceiveRequest);
            response.setMESSAGE("采购订单【" + purchaseOrder + "】的行项数据接收成功");

        } 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_BATCH_RECEIVE);
            log.setCategory("RESPONSE");
            log.setIntegrationWay("ERP");
            log.setIntegrationMethod("InterfaceService.saveItemBatch");
            log.setParam(itemBatchReceiveRequest.toString());
            log.setStatus(response.getSTATUS());
            log.setResultMessage(response.getMESSAGE());
            log.setTransactionId(itemBatchReceiveRequest.getTRANID());
            log.setRequestDateTime(LocalDateTime.parse(itemBatchReceiveRequest.getDATE_TIME(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.setCreatedDateTime(LocalDateTime.now());
            integrationLogService.saveIntegrationLog(log);
        }

        //接口返回
        return response;
    }
}
