
package com.sinosig.web.module.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.module.domain.TBYDK;
import com.sinosig.web.module.filter.*;
import com.sinosig.web.module.feign.TBYDKFeignClient;
import com.sinosig.web.WebApplication.WebClientProperties;
import com.sinosig.util.feign.FeignRequestInterceptor;

@Service
public class TBYDKService {

    TBYDKFeignClient client;

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

	public TBYDK create(TBYDK tbydk) {
        return client.create(tbydk);
    }

    public TBYDK get( String tbydk_id) {
        return client.get( tbydk_id);
    }

    public TBYDK update(String tbydk_id, TBYDK tbydk) {
        return client.update(tbydk_id, tbydk);
    }

    public boolean checkKey(TBYDK tbydk) {
        return client.checkKey(tbydk);
    }

    public TBYDK save(String tbydk_id, TBYDK tbydk) {
        return client.save(tbydk_id, tbydk);
    }

    public TBYDK getDraft(String tbydk_id, TBYDK tbydk) {
        return client.getDraft(tbydk_id, tbydk);
    }

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

	public Page<TBYDK> fetchDefault(TBYDKSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }

	public Page<TBYDK> fetchJQX(TBYDKSearchContext searchContext) {
        return client.fetchJQX(searchContext);
    }

	public Page<TBYDK> fetchSYX(TBYDKSearchContext searchContext) {
        return client.fetchSYX(searchContext);
    }


}
