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

@Service
public class JQXBJService {

    JQXBJFeignClient client;

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

    public JQXBJ update(String jqxbj_id, JQXBJ jqxbj) {
        return client.update(jqxbj_id, jqxbj);
    }

    public JQXBJ get( String jqxbj_id) {
        return client.get( jqxbj_id);
    }

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

    public JQXBJ save(String jqxbj_id, JQXBJ jqxbj) {
        return client.save(jqxbj_id, jqxbj);
    }

	public JQXBJ create(JQXBJ jqxbj) {
        return client.create(jqxbj);
    }

    public boolean checkKey(JQXBJ jqxbj) {
        return client.checkKey(jqxbj);
    }

    public JQXBJ getDraft(String jqxbj_id, JQXBJ jqxbj) {
        return client.getDraft(jqxbj_id, jqxbj);
    }

	public Page<JQXBJ> fetchDefault(JQXBJSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
