
package com.ibiz.web.external.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 com.ibiz.util.feign.suport.SearchContextFeignEncode;
import com.ibiz.web.external.domain.ORG;
import com.ibiz.web.external.filter.*;
import com.ibiz.web.external.feign.ORGFeignClient;
import com.ibiz.web.WebApplication.WebClientProperties;
import com.ibiz.util.feign.FeignRequestInterceptor;

@Service
public class ORGService {

    ORGFeignClient client;

    @Autowired
    public ORGService(Decoder decoder, Encoder encoder, Client client, Contract contract, FeignRequestInterceptor feignRequestInterceptor,
                                    WebClientProperties webClientProperties) {
        if (webClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.client = nameBuilder.target(ORGFeignClient.class,"http://" + webClientProperties.getServiceId() + "/") ;
		} else if (webClientProperties.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(ORGFeignClient.class, "http://" + webClientProperties.getServiceUrl() + "/") ;
		}
    }

    public boolean checkKey(ORG org) {
        return client.checkKey(org);
    }

	public ORG create(ORG org) {
        return client.create(org);
    }

    public ORG get( String org_id) {
        return client.get( org_id);
    }

    public ORG save(String org_id, ORG org) {
        return client.save(org_id, org);
    }

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

    public ORG update(String org_id, ORG org) {
        return client.update(org_id, org);
    }

    public ORG getDraft(String org_id, ORG org) {
        return client.getDraft(org_id, org);
    }

	public Page<ORG> fetchDefault(ORGSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }

	public Page<ORG> fetchTopOrg(ORGSearchContext searchContext) {
        return client.fetchTopOrg(searchContext);
    }

	public Page<ORG> fetchCurOrg(ORGSearchContext searchContext) {
        return client.fetchCurOrg(searchContext);
    }


}
