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.modular.core.meext.MEServices;
import com.sap.me.common.ObjectAliasEnum;
import com.sap.me.plant.FindResourceTypeByKeyFieldsRequest;
import com.sap.me.plant.ResourceTypeBasicConfiguration;
import com.sap.me.plant.ResourceTypeConfiguration;
import com.sap.me.plant.ResourceTypeConfigurationServiceInterface;

import java.util.Collection;


/**
 * 资源类型主数据操作类
 *
 *
 * @author Leon
 * @date 2021/7/6
 */
public class ResourceTypeWSClient {

    /**
     * 查询资源类型
     *
     * @param jsonObject
     * @return
     */
    public static Collection<ResourceTypeBasicConfiguration> find(JSONObject jsonObject) {
        Collection<ResourceTypeBasicConfiguration> collection = null;

        try {
            String site = jsonObject.getString("site");
            String resourceType = jsonObject.getString("resourceType");

            //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
            ResourceTypeConfigurationServiceInterface resourceTypeService = MEServices.create("com.sap.me.plant", "ResourceTypeConfigurationService", site);
            FindResourceTypeByKeyFieldsRequest resourceTypeRequest = new FindResourceTypeByKeyFieldsRequest();
            resourceTypeRequest.setResourceType(resourceType);
            collection = resourceTypeService.findResourceTypeByKeyFields(resourceTypeRequest);
        } catch (Exception e) {
            //----------------
        }

       return collection;
    }

    /**
     * 资源类型新增
     *
     * @param jsonObject
     * @return
     */
    public static String insert(JSONObject jsonObject) {
        try {
            String site = jsonObject.getString("site");
            String resourceType = jsonObject.getString("resourceType");
            String description = jsonObject.getString("description");
            String resourceTypeBo = HandleEnum.RESOURCE_TYPE.getHandle(site, resourceType);


            //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
            ResourceTypeConfigurationServiceInterface resourceTypeService = MEServices.create("com.sap.me.plant", "ResourceTypeConfigurationService", site);
            ResourceTypeConfiguration resourceTypeConfiguration = new ResourceTypeConfiguration();
            resourceTypeConfiguration.setRef(resourceTypeBo);
            resourceTypeConfiguration.setResourceType(resourceType);
            resourceTypeConfiguration.setDescription(description);
            resourceTypeService.createResourceType(resourceTypeConfiguration);

            //自定义字段更新
            CustomDataWSClient.update(jsonObject, resourceTypeBo, ObjectAliasEnum.RESOURCE_TYPE.value());

            return null;
        } catch (Exception e) {
            return ExceptionUtil.getExceptionMsg(e);
        }
    }
}
