package com.foreverwin.mesnac.dataimport.client;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.util.ExceptionUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.modular.core.meext.MEServices;
import com.sap.me.common.ObjectReference;
import com.sap.me.nonconformance.NcGroupConfigurationServiceInterface;
import com.sap.me.nonconformance.NcGroupFullConfiguration;
import com.sap.me.nonconformance.NcGroupValidOperation;
import com.visiprise.globalization.DateGlobalizationServiceInterface;
import com.visiprise.globalization.GlobalizationService;

import java.util.ArrayList;
import java.util.List;

/**
 * 不良代码组操作类
 *
 * @author Leon
 * @date 2021/7/6
 */
public class NcGroupWSClient {

    /**
     * 查询不合格代码组
     *
     * @param jsonObject
     * @return
     */
    public static NcGroupFullConfiguration find(JSONObject jsonObject) {
        NcGroupFullConfiguration ncGroupFullConfiguration = null;
        try {
            String site = jsonObject.getString("site");
            String ncGroup = jsonObject.getString("ncGroup");
            String ncGroupBo = HandleEnum.NC_GROUP.getHandle(site, ncGroup);

            //---------------------------------------------------------------------------------------------------
            NcGroupConfigurationServiceInterface ncGroupConfigurationService = MEServices.create("com.sap.me.nonconformance", "NcGroupConfigurationService", site);
            ObjectReference objectReference = new ObjectReference(ncGroupBo);
            ncGroupFullConfiguration = ncGroupConfigurationService.readNcGroup(objectReference);
        } catch (Exception e) {
        }

        return ncGroupFullConfiguration;
    }

    /**
     * 不良代码组创建
     *
     * @param jsonObject
     * @return
     */
    public static String create(JSONObject jsonObject) {
        try {
            String site = jsonObject.getString("site");
            String ncGroup = jsonObject.getString("ncGroup");
            String description = jsonObject.getString("description");
            String applyAll = jsonObject.getString("applyAll");
            String applyOperation = jsonObject.getString("applyOperation");
            String ncGroupBo = HandleEnum.NC_GROUP.getHandle(site, ncGroup);

            DateGlobalizationServiceInterface dateService = GlobalizationService.getInvariantService("com.visiprise.globalization.DateGlobalizationService");
            //---------------------------------------------------------------------------------------------------
            NcGroupConfigurationServiceInterface ncGroupConfigurationService = MEServices.create("com.sap.me.nonconformance", "NcGroupConfigurationService", site);
            NcGroupFullConfiguration ncGroupFullConfiguration = new NcGroupFullConfiguration();
            ncGroupFullConfiguration.setRef(ncGroupBo);
            ncGroupFullConfiguration.setNcGroup(ncGroup);
            ncGroupFullConfiguration.setDescription(description);
            if ("true".equals(applyAll) || "是".equals(applyAll)) {
                ncGroupFullConfiguration.setValidAtAllOperations(true);
            } else {
                ncGroupFullConfiguration.setValidAtAllOperations(false);
                if (StringUtil.notBlank(applyOperation) && applyOperation.contains(";")) {
                    List<NcGroupValidOperation> list = new ArrayList<>();
                    String[] operationList = applyOperation.split(";");
                    for (String operation: operationList) {
                        NcGroupValidOperation ncGroupValidOperation = new NcGroupValidOperation();
                        String operationBo = HandleEnum.OPERATION.getHandle(site, operation, "#");
                        ncGroupValidOperation.setOperationRef(operationBo);
                        list.add(ncGroupValidOperation);
                    }

                    ncGroupFullConfiguration.setNcGroupValidOperationList(list);
                } else if (StringUtil.notBlank(applyOperation)) {
                    List<NcGroupValidOperation> list = new ArrayList<>();
                    NcGroupValidOperation ncGroupValidOperation = new NcGroupValidOperation();
                    String operationBo = HandleEnum.OPERATION.getHandle(site, applyOperation, "#");
                    ncGroupValidOperation.setOperationRef(operationBo);
                    list.add(ncGroupValidOperation);
                    ncGroupFullConfiguration.setNcGroupValidOperationList(list);
                }
            }

            ncGroupFullConfiguration.setModifiedDateTime(dateService.createDateTime());
            ncGroupConfigurationService.createNcGroup(ncGroupFullConfiguration);
        } catch (Exception e) {
            return ExceptionUtil.getExceptionMsg(e);
        }

        return null;
    }

    /**
     * 不良代码组更新
     *
     * @param jsonObject
     * @param ncGroupFullConfiguration
     * @return
     */
    public static String update(JSONObject jsonObject, NcGroupFullConfiguration ncGroupFullConfiguration) {
        try {
            String site = jsonObject.getString("site");
            String description = jsonObject.getString("description");
            String applyAll = jsonObject.getString("applyAll");
            String applyOperation = jsonObject.getString("applyOperation");

            DateGlobalizationServiceInterface dateService = GlobalizationService.getInvariantService("com.visiprise.globalization.DateGlobalizationService");
            //---------------------------------------------------------------------------------------------------
            NcGroupConfigurationServiceInterface ncGroupConfigurationService = MEServices.create("com.sap.me.nonconformance", "NcGroupConfigurationService", site);
            ncGroupFullConfiguration.setDescription(description);
            if ("true".equals(applyAll) || "是".equals(applyAll)) {
                ncGroupFullConfiguration.setValidAtAllOperations(true);
            } else {
                ncGroupFullConfiguration.setValidAtAllOperations(false);
                if (StringUtil.notBlank(applyOperation) && applyOperation.contains(";")) {
                    List<NcGroupValidOperation> list = new ArrayList<>();
                    String[] operationList = applyOperation.split(";");
                    for (String operation: operationList) {
                        NcGroupValidOperation ncGroupValidOperation = new NcGroupValidOperation();
                        String operationBo = HandleEnum.OPERATION.getHandle(site, operation, "#");
                        ncGroupValidOperation.setOperationRef(operationBo);
                        list.add(ncGroupValidOperation);
                    }

                    ncGroupFullConfiguration.setNcGroupValidOperationList(list);
                } else if (StringUtil.notBlank(applyOperation)) {
                    List<NcGroupValidOperation> list = new ArrayList<>();
                    NcGroupValidOperation ncGroupValidOperation = new NcGroupValidOperation();
                    String operationBo = HandleEnum.OPERATION.getHandle(site, applyOperation, "#");
                    ncGroupValidOperation.setOperationRef(operationBo);
                    list.add(ncGroupValidOperation);
                    ncGroupFullConfiguration.setNcGroupValidOperationList(list);
                }
            }

            ncGroupConfigurationService.updateNcGroup(ncGroupFullConfiguration);
        } catch (Exception e) {
            return ExceptionUtil.getExceptionMsg(e);
        }

        return null;
    }
}
