
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.SYXBJ;
import com.ibiz.web.external.filter.*;
import com.ibiz.web.external.feign.SYXBJFeignClient;
import com.ibiz.web.WebApplication.WebClientProperties;
import com.ibiz.util.feign.FeignRequestInterceptor;

@Service
public class SYXBJService {

    SYXBJFeignClient client;

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

    public SYXBJ save(String syxbj_id, SYXBJ syxbj) {
        return client.save(syxbj_id, syxbj);
    }

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

    public SYXBJ getDraft(String syxbj_id, SYXBJ syxbj) {
        return client.getDraft(syxbj_id, syxbj);
    }

    public boolean checkKey(SYXBJ syxbj) {
        return client.checkKey(syxbj);
    }

    public SYXBJ update(String syxbj_id, SYXBJ syxbj) {
        return client.update(syxbj_id, syxbj);
    }

	public SYXBJ create(SYXBJ syxbj) {
        return client.create(syxbj);
    }

    public SYXBJ get( String syxbj_id) {
        return client.get( syxbj_id);
    }

	public Page<SYXBJ> fetchDefault(SYXBJSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
