
package cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.stereotype.Service;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;

import com.alibaba.fastjson.JSONObject;

import org.springframework.util.DigestUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import cn.ibizlab.r7.runtime.util.feign.suport.SearchContextFeignEncode;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.domain.DataEntity;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.feign.DataEntityFeignClient;
import cn.ibizlab.r7.runtime.runtimemgr.RuntimeMgrApplication.RuntimeMgrClientProperties;
import cn.ibizlab.r7.runtime.util.feign.FeignRequestInterceptor;

@Service
public class DataEntityService {

    DataEntityFeignClient client;

    @Autowired
    public DataEntityService(Decoder decoder, Encoder encoder, Client client, Contract contract, FeignRequestInterceptor feignRequestInterceptor,
                                    RuntimeMgrClientProperties runtimemgrClientProperties) {
        if (runtimemgrClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.client = nameBuilder.target(DataEntityFeignClient.class,"http://" + runtimemgrClientProperties.getServiceId() + "/") ;
		} else if (runtimemgrClientProperties.getServiceUrl()!=null) {
			if (client instanceof LoadBalancerFeignClient) {
				client = ((LoadBalancerFeignClient) client).getDelegate();
			}
			Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.client = nameBuilder.target(DataEntityFeignClient.class, "http://" + runtimemgrClientProperties.getServiceUrl() + "/") ;
		}
    }

    public boolean remove( String dataentity_id) {
        return client.remove( dataentity_id);
    }

	public DataEntity create(DataEntity dataentity) {
        return client.create(dataentity);
    }

    public DataEntity update(String dataentity_id, DataEntity dataentity) {
        return client.update(dataentity_id, dataentity);
    }

    public DataEntity initAll(String dataentity_id, DataEntity dataentity) {
        return client.initAll(dataentity_id, dataentity);
    }

    public DataEntity getDraft(String dataentity_id, DataEntity dataentity) {
        return client.getDraft(dataentity_id, dataentity);
    }

    public DataEntity get( String dataentity_id) {
        return client.get( dataentity_id);
    }

    public DataEntity save(String dataentity_id, DataEntity dataentity) {
        return client.save(dataentity_id, dataentity);
    }

    public boolean checkKey(DataEntity dataentity) {
        return client.checkKey(dataentity);
    }

	public Page<DataEntity> fetchSelectDEAll(DataEntitySearchContext searchContext) {
        return client.fetchSelectDEAll(searchContext);
    }

	public Page<DataEntity> fetchDefault(DataEntitySearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }

}
