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

@Service
public class ORGUSERService {

    ORGUSERFeignClient client;

    @Autowired
    public ORGUSERService(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(ORGUSERFeignClient.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(ORGUSERFeignClient.class, "http://" + webClientProperties.getServiceUrl() + "/") ;
		}
    }

	public ORGUSER create(ORGUSER orguser) {
        return client.create(orguser);
    }

    public ORGUSER getDraft(String orguser_id, ORGUSER orguser) {
        return client.getDraft(orguser_id, orguser);
    }

    public ORGUSER update(String orguser_id, ORGUSER orguser) {
        return client.update(orguser_id, orguser);
    }

    public ORGUSER get( String orguser_id) {
        return client.get( orguser_id);
    }

    public boolean checkKey(ORGUSER orguser) {
        return client.checkKey(orguser);
    }

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

    public ORGUSER save(String orguser_id, ORGUSER orguser) {
        return client.save(orguser_id, orguser);
    }

	public Page<ORGUSER> fetchCurOrgUser(ORGUSERSearchContext searchContext) {
        return client.fetchCurOrgUser(searchContext);
    }

	public Page<ORGUSER> fetchDefault(ORGUSERSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
