
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.SJZD;
import com.sinosig.web.module.filter.*;
import com.sinosig.web.module.feign.SJZDFeignClient;
import com.sinosig.web.WebApplication.WebClientProperties;
import com.sinosig.util.feign.FeignRequestInterceptor;

@Service
public class SJZDService {

    SJZDFeignClient client;

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

    public SJZD update(String sjzd_id, SJZD sjzd) {
        return client.update(sjzd_id, sjzd);
    }

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

    public SJZD getDraft(String sjzd_id, SJZD sjzd) {
        return client.getDraft(sjzd_id, sjzd);
    }

    public SJZD get( String sjzd_id) {
        return client.get( sjzd_id);
    }

    public boolean checkKey(SJZD sjzd) {
        return client.checkKey(sjzd);
    }

	public SJZD create(SJZD sjzd) {
        return client.create(sjzd);
    }

    public SJZD save(String sjzd_id, SJZD sjzd) {
        return client.save(sjzd_id, sjzd);
    }

	public Page<SJZD> fetchDefault(SJZDSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
