package com.foreverwin.mesnac.dataimport.client;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.constant.DataImportConstant;
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.BasicStatusEnum;
import com.sap.me.common.ObjectAliasEnum;
import com.sap.me.common.ObjectReference;
import com.sap.me.nonconformance.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

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

    /**
     * 查询不合格代码
     *
     * @param jsonObject
     * @return
     */
    public static NcCodeFullConfiguration find(JSONObject jsonObject) {
        NcCodeFullConfiguration ncCodeFullConfiguration = null;

        try {
            String site = jsonObject.getString("site");
            String ncCode = jsonObject.getString("ncCode");
            String ncCodeBo = HandleEnum.NC_CODE.getHandle(site, ncCode);

            //------------------------------------------------------------------------------------------------
            NcCodeConfigurationServiceInterface ncCodeConfigurationService = MEServices.create("com.sap.me.nonconformance", "NcCodeConfigurationService", site);
            ObjectReference objectReference = new ObjectReference(ncCodeBo);
            ncCodeFullConfiguration = ncCodeConfigurationService.readNcCode(objectReference);
        } catch (Exception e) {
        }

        return ncCodeFullConfiguration;
    }


    /**
     * 不合格代码创建
     *
     * @param jsonObject
     * @return
     */
    public static String create(JSONObject jsonObject) {
        try {
            String site = jsonObject.getString("site");
            String ncCode = jsonObject.getString("ncCode");
            String ncCodeBo = HandleEnum.NC_CODE.getHandle(site, ncCode);
            String category = jsonObject.getString("category");
            String dataType = jsonObject.getString("dataType");
            String description = jsonObject.getString("descZH");
            String closureRequired = jsonObject.getString("closureRequired");
            String canBePrimaryCode = jsonObject.getString("canBePrimaryCode");
            String autoClosePrimary = jsonObject.getString("autoClosePrimary");
            String autoCloseIncident = jsonObject.getString("autoCloseIncident");
            String secondaryRequireForClose = jsonObject.getString("secondaryRequireForClose");

            //------------------------------------------------------------------------------------------------
            NcCodeConfiguration ncCodeConfiguration = new NcCodeConfiguration();
            ncCodeConfiguration.setRef(ncCodeBo);
            ncCodeConfiguration.setNcCode(ncCode);
            ncCodeConfiguration.setDescription(description);
            ncCodeConfiguration.setStatus(BasicStatusEnum.ENABLED);

            //不合格代码组
            String ncGroup = jsonObject.getString("ncGroup");
            NcGroupMember ncGroupMember = new NcGroupMember();
            String ncGroupBo = HandleEnum.NC_GROUP.getHandle(site, ncGroup);
            ncGroupMember.setNcGroupRef(ncGroupBo);
            ncCodeConfiguration.getNcGroupMemberList().add(ncGroupMember);

            //不合格代码类型
            NCCategory ncCodeCategory = NCCategory.FAILURE;
            switch (category.trim()) {
                case DataImportConstant.NC_CODE_CATEGORY_FAILURE :
                    ncCodeCategory = NCCategory.FAILURE;
                    break;
                case DataImportConstant.NC_CODE_CATEGORY_DEFECT :
                    ncCodeCategory = NCCategory.DEFECT;
                    break;
                case DataImportConstant.NC_CODE_CATEGORY_REPAIR :
                    ncCodeCategory = NCCategory.REPAIR;
                    break;
                    default: break;
            }
            ncCodeConfiguration.setNcCategory(ncCodeCategory);

            //不合格数据类型
            if (StringUtil.isBlank(dataType)) {
                dataType = "SIMPLE";
            } else if ("SIMPLE".equals(dataType) || DataImportConstant.NC_CODE_DATA_TYPE_SIMPLE.equals(dataType)) {
                dataType = "SIMPLE";
            }
            String ncDataTypeRef = HandleEnum.DATA_TYPE.getHandle(site, "NC", dataType);
            ncCodeConfiguration.setNcDataTypeRef(ncDataTypeRef);

            //需要关闭
            ncCodeConfiguration.setClosureRequired("是".equals(closureRequired) ? true : false);
            //是否可以是主要代码
            ncCodeConfiguration.setCanBePrimaryCode("是".equals(canBePrimaryCode) ? true : false);
            ncCodeConfiguration.setAutoClosePrimary("是".equals(autoClosePrimary) ? true : false);
            ncCodeConfiguration.setAutoCloseIncident("是".equals(autoCloseIncident) ? true : false);
            ncCodeConfiguration.setSecondaryRequiredForClose("是".equals(secondaryRequireForClose) ? true : false);

            //次要不良项目
            String secondaryCodes = jsonObject.getString("secondaryCode");
            if (StringUtil.notBlank(secondaryCodes)) {
                List<NcSecondaryCode> ncSecondaryCodeList = new ArrayList<>();

                if (!secondaryCodes.contains(";")) {
                    NcSecondaryCode ncSecondaryCode = new NcSecondaryCode();
                    String ncCodeRef = HandleEnum.NC_CODE.getHandle(site, secondaryCodes);
                    ncSecondaryCode.setNcCodeRef(ncCodeRef);
                    ncSecondaryCodeList.add(ncSecondaryCode);
                } else {
                    String [] secondaryCodeArray = secondaryCodes.split(";");
                    for (String secondaryCode: secondaryCodeArray) {
                        NcSecondaryCode ncSecondaryCode = new NcSecondaryCode();
                        String ncCodeRef = HandleEnum.NC_CODE.getHandle(site, secondaryCode);
                        ncSecondaryCode.setNcCodeRef(ncCodeRef);
                        ncSecondaryCodeList.add(ncSecondaryCode);
                    }
                }

                ncCodeConfiguration.setNcSecondaryCodeList(ncSecondaryCodeList);
            }

            //NC最大限制值
            String maxNcLimit = jsonObject.getString("maxNcLimit");
            maxNcLimit = StringUtil.notBlank(maxNcLimit) ? maxNcLimit : "10";
            ncCodeConfiguration.setMaxNcLimit(new BigDecimal(maxNcLimit));

            //------------------------------------------------------------------------------------------------
            NcCodeConfigurationServiceInterface ncCodeConfigurationService = MEServices.create("com.sap.me.nonconformance", "NcCodeConfigurationService", site);
            ncCodeConfigurationService.createNcCode(ncCodeConfiguration);

            //自定义字段保存
            CustomDataWSClient.update(jsonObject, ncCodeConfiguration.getRef(), ObjectAliasEnum.NC_CODE.value());
        } catch (Exception e) {
            return ExceptionUtil.getExceptionMsg(e);
        }

        return null;
    }

    /**
     * 不合格代码更新
     *
     * @param jsonObject
     * @param ncCodeFullConfiguration
     * @return
     */
    public static String update(JSONObject jsonObject, NcCodeFullConfiguration ncCodeFullConfiguration) {
        try {
            String site = jsonObject.getString("site");
            String category = jsonObject.getString("category");
            String dataType = jsonObject.getString("dataType");
            String description = jsonObject.getString("descZH");
            String closureRequired = jsonObject.getString("closureRequired");
            String canBePrimaryCode = jsonObject.getString("canBePrimaryCode");
            String autoClosePrimary = jsonObject.getString("autoClosePrimary");
            String autoCloseIncident = jsonObject.getString("autoCloseIncident");
            String secondaryRequireForClose = jsonObject.getString("secondaryRequireForClose");

            //------------------------------------------------------------------------------------------------
            ncCodeFullConfiguration.setDescription(description);
            ncCodeFullConfiguration.setStatus(BasicStatusEnum.ENABLED);

            //不合格代码组
            String ncGroup = jsonObject.getString("ncGroup");
            String ncGroupBo = HandleEnum.NC_GROUP.getHandle(site, ncGroup);
            List<NcGroupMember> ncGroupMemberList = ncCodeFullConfiguration.getNcGroupMemberList();
            if (ncGroupMemberList == null || ncGroupMemberList.size() <= 0) {
                NcGroupMember ncGroupMember = new NcGroupMember();
                ncGroupMember.setNcGroupRef(ncGroupBo);
                ncCodeFullConfiguration.getNcGroupMemberList().add(ncGroupMember);
            } else {
                for (NcGroupMember ncGroupMemberModel : ncGroupMemberList) {
                    if (!ncGroupBo.equals(ncGroupMemberModel.getNcGroupRef())) {
                        NcGroupMember ncGroupMember = new NcGroupMember();
                        ncGroupMember.setNcGroupRef(ncGroupBo);
                        ncCodeFullConfiguration.getNcGroupMemberList().add(ncGroupMember);
                    }
                }
            }


            //不合格代码类型
            NCCategory ncCodeCategory = NCCategory.FAILURE;
            switch (category.trim()) {
                case DataImportConstant.NC_CODE_CATEGORY_FAILURE :
                    ncCodeCategory = NCCategory.FAILURE;
                    break;
                case DataImportConstant.NC_CODE_CATEGORY_DEFECT :
                    ncCodeCategory = NCCategory.DEFECT;
                    break;
                case DataImportConstant.NC_CODE_CATEGORY_REPAIR :
                    ncCodeCategory = NCCategory.REPAIR;
                    break;
                default: break;
            }
            ncCodeFullConfiguration.setNcCategory(ncCodeCategory);

            //不合格数据类型
            if (StringUtil.isBlank(dataType)) {
                dataType = "SIMPLE";
            } else if ("SIMPLE".equals(dataType) || DataImportConstant.NC_CODE_DATA_TYPE_SIMPLE.equals(dataType)) {
                dataType = "SIMPLE";
            }
            String ncDataTypeRef = HandleEnum.DATA_TYPE.getHandle(site, "NC", dataType);
            ncCodeFullConfiguration.setNcDataTypeRef(ncDataTypeRef);

            //需要关闭
            ncCodeFullConfiguration.setClosureRequired("是".equals(closureRequired) ? true : false);
            //是否可以是主要代码
            ncCodeFullConfiguration.setCanBePrimaryCode("是".equals(canBePrimaryCode) ? true : false);
            ncCodeFullConfiguration.setAutoClosePrimary("是".equals(autoClosePrimary) ? true : false);
            ncCodeFullConfiguration.setAutoCloseIncident("是".equals(autoCloseIncident) ? true : false);
            ncCodeFullConfiguration.setSecondaryRequiredForClose("是".equals(secondaryRequireForClose) ? true : false);

            //次要不良项目
            String secondaryCodes = jsonObject.getString("secondaryCode");
            if (StringUtil.notBlank(secondaryCodes)) {
                List<NcSecondaryCode> ncSecondaryCodeList = new ArrayList<>();

                if (!secondaryCodes.contains(";")) {
                    NcSecondaryCode ncSecondaryCode = new NcSecondaryCode();
                    String ncCodeRef = HandleEnum.NC_CODE.getHandle(site, secondaryCodes);
                    ncSecondaryCode.setNcCodeRef(ncCodeRef);
                    ncSecondaryCodeList.add(ncSecondaryCode);
                } else {
                    String [] secondaryCodeArray = secondaryCodes.split(";");
                    for (String secondaryCode: secondaryCodeArray) {
                        NcSecondaryCode ncSecondaryCode = new NcSecondaryCode();
                        String ncCodeRef = HandleEnum.NC_CODE.getHandle(site, secondaryCode);
                        ncSecondaryCode.setNcCodeRef(ncCodeRef);
                        ncSecondaryCodeList.add(ncSecondaryCode);
                    }
                }

                ncCodeFullConfiguration.setNcSecondaryCodeList(ncSecondaryCodeList);
            }

            //NC最大限制值
            String maxNcLimit = jsonObject.getString("maxNcLimit");
            maxNcLimit = StringUtil.notBlank(maxNcLimit) ? maxNcLimit : "10";
            ncCodeFullConfiguration.setMaxNcLimit(new BigDecimal(maxNcLimit));

            //------------------------------------------------------------------------------------------------
            NcCodeConfigurationServiceInterface ncCodeConfigurationService = MEServices.create("com.sap.me.nonconformance", "NcCodeConfigurationService", site);
            ncCodeConfigurationService.updateNcCode(ncCodeFullConfiguration);

            //自定义字段保存
            CustomDataWSClient.update(jsonObject, ncCodeFullConfiguration.getRef(), ObjectAliasEnum.NC_CODE.value());
        } catch (Exception e) {
            return ExceptionUtil.getExceptionMsg(e);
        }

        return null;
    }
}
