
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.CPXH;
import com.sinosig.web.external.filter.*;
import com.sinosig.web.external.feign.CPXHFeignClient;
import com.sinosig.web.WebApplication.WebClientProperties;
import com.sinosig.util.feign.FeignRequestInterceptor;

@Service
public class CPXHService {

    CPXHFeignClient client;

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

	public CPXH create(CPXH cpxh) {
        return client.create(cpxh);
    }

    public CPXH update(String cpxh_id, CPXH cpxh) {
        return client.update(cpxh_id, cpxh);
    }

    public CPXH getDraft(String cpxh_id, CPXH cpxh) {
        return client.getDraft(cpxh_id, cpxh);
    }

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

    public boolean checkKey(CPXH cpxh) {
        return client.checkKey(cpxh);
    }

    public CPXH save(String cpxh_id, CPXH cpxh) {
        return client.save(cpxh_id, cpxh);
    }

    public CPXH get( String cpxh_id) {
        return client.get( cpxh_id);
    }

	public Page<CPXH> fetchDefault(CPXHSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
