package com.foreverwin.mesnac.dataimport.client;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.constant.DataImportConstant;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.constant.KeywordConstant;
import com.foreverwin.modular.core.meext.MEServices;
import com.sap.me.common.ObjectAliasEnum;
import com.sap.me.common.ObjectReference;
import com.sap.me.productdefinition.*;

import java.lang.reflect.UndeclaredThrowableException;

public class OperationWSClient {

    /**
     * 查询工序
     * @param data
     * @return
     */
    public static OperationFullConfiguration find (JSONObject data) {
        try {
            String site = (String) data.get("site");
            String operation = data.getString("operation").toUpperCase();
            String version = data.getString("version").toUpperCase();

            ObjectReference request = new ObjectReference();
            request.setRef("OperationBO:" + site + "," + operation + "," + version);
            OperationConfigurationServiceInterface operationConfigurationService = MEServices.create("com.sap.me.productdefinition", "OperationConfigurationService", site);
            OperationFullConfiguration op = operationConfigurationService.readOperation(request);
            return op;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 新增工序主数据
     * @param data
     * @return
     */
    public static String insert(JSONObject data) {
        try {
            String site = (String) data.get("site");
            String operation = data.getString("operation").toUpperCase();
            String version = data.getString("version").toUpperCase();
            String operationRef = "OperationBO:" + site + "," + operation + "," + version;

            //资源主数据对象
            OperationConfiguration request = new OperationConfiguration();
            request.setOperation(operation);
            request.setRevision(version);

            //当前版本
            String currentVersion = data.getString("currentVersion");
            if (KeywordConstant.KW_COMMON_YES.equalsIgnoreCase(currentVersion)) {
                request.setCurrentRevision(true);
            }

            /**
             * 状态
             * 201-可下达
             * 202-冻结
             * 203-作废
             * 204-保留
             * 205-新建
             */
            String status = data.getString("status");
            if (StringUtil.notEmpty(status)) {
                status = status.substring(0, status.indexOf(":"));
                String statusRef = "StatusBO:" + site + "," + "201";
                if ("201".equalsIgnoreCase(status) || "1".equals(status)) {
                    statusRef = "StatusBO:" + site + "," + "201";
                } else if ("202".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "202";
                } else if ("203".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "203";
                } else if ("204".equalsIgnoreCase(status) || "2".equals(status)) {
                    statusRef = "StatusBO:" + site + "," + "204";
                } else if ("205".equalsIgnoreCase(status) || "0".equals(status)) {
                    statusRef = "StatusBO:" + site + "," + "205";
                }
                request.setStatusRef(statusRef);
            }

            /**
             * 操作类型
             * N-普通
             * S-特殊
             * T-测试
             */
            String operationType = data.getString("operationType");
            if (StringUtil.isEmpty(operationType)) {
                request.setOperationType(OperationType.NORMAL);
            } else if (DataImportConstant.OPERATION_WSCLIENT_OPERATION_TYPE_N.equalsIgnoreCase(operationType)) {
                request.setOperationType(OperationType.NORMAL);
            } else if (DataImportConstant.OPERATION_WSCLIENT_OPERATION_TYPE_S.equalsIgnoreCase(operationType)) {
                request.setOperationType(OperationType.SPECIAL);
            } else if (DataImportConstant.OPERATION_WSCLIENT_OPERATION_TYPE_T.equalsIgnoreCase(operationType)) {
                request.setOperationType(OperationType.TEST);
            }

            //资源类型
            String resourceType = data.getString("resourceType");
            if (StringUtil.notEmpty(resourceType)) {
                request.setResourceTypeRef("ResourceTypeBO:" + site + "," + resourceType);
            }

            //默认资源
            String defaultResource = data.getString("defaultResource");
            if (StringUtil.notEmpty(defaultResource)) {
                request.setDefaultResourceRef("ResourceBO:" + site + "," + defaultResource);
            }

            //工序描述
            String descriptionZH = data.getString("descriptionZH");
            if (StringUtil.notEmpty(descriptionZH)) {
                OperationTranslation operationTranslation = new OperationTranslation();
                operationTranslation.setLocale("zh");
                operationTranslation.setDescription(descriptionZH);
                request.getOperationTranslationList().add(operationTranslation);
            }
            String descriptionEN = data.getString("descriptionEN");
            if (StringUtil.notEmpty(descriptionEN)) {
                OperationTranslation operationTranslation = new OperationTranslation();
                operationTranslation.setLocale("en");
                operationTranslation.setDescription(descriptionEN);
                request.getOperationTranslationList().add(operationTranslation);
            }
            //request.setDescription(descriptionZH);

            OperationConfigurationServiceInterface operationConfigurationService = MEServices.create("com.sap.me.productdefinition", "OperationConfigurationService", site);
            //工序创建
            operationConfigurationService.createOperation(request);

            //自定义字段保存
            CustomDataWSClient.update(data, operationRef, ObjectAliasEnum.RESOURCE.value());

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof UndeclaredThrowableException) {
                return ((UndeclaredThrowableException) e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }

    /**
     * 更新工序主数据
     * @param data
     * @param request
     * @return
     */
    public static String update(JSONObject data, OperationFullConfiguration request) {

        try {
            String site = (String) data.get("site");
            String operation = data.getString("operation").toUpperCase();

            //当前版本
            String currentVersion = data.getString("currentVersion");
            String operationRef = "OperationBO:" + site + "," + operation + "," + currentVersion;
            if (KeywordConstant.KW_COMMON_YES.equalsIgnoreCase(currentVersion)) {
                request.setCurrentRevision(true);
            }

            /**
             * 状态
             * 201-可下达
             * 202-冻结
             * 203-作废
             * 204-保留
             * 205-新建
             */
            String status = data.getString("status");
            if (StringUtil.notEmpty(status)) {
                status = status.substring(0, status.indexOf(":"));
                String statusRef = "StatusBO:" + site + "," + "201";
                if ("201".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "201";
                } else if ("202".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "202";
                } else if ("203".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "203";
                } else if ("204".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "204";
                } else if ("205".equalsIgnoreCase(status)) {
                    statusRef = "StatusBO:" + site + "," + "205";
                }
                request.setStatusRef(statusRef);
            }

            //资源类型
            String resourceType = data.getString("resourceType");
            if (StringUtil.notEmpty(resourceType)) {
                request.setResourceTypeRef("ResourceTypeBO:" + site + "," + resourceType);
            }

            //默认资源
            String defaultResource = data.getString("defaultResource");
            if (StringUtil.notEmpty(defaultResource)) {
                request.setDefaultResourceRef("ResourceBO:" + site + "," + defaultResource);
            }

            OperationConfigurationServiceInterface operationConfigurationService = MEServices.create("com.sap.me.productdefinition", "OperationConfigurationService", site);
            //工序创建
            operationConfigurationService.updateOperation(request);

            //自定义字段保存
            CustomDataWSClient.update(data, operationRef, ObjectAliasEnum.RESOURCE.value());

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof UndeclaredThrowableException) {
                return ((UndeclaredThrowableException) e).getUndeclaredThrowable().getMessage();
            }
            return e.getMessage();
        }
    }
}
