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

@Service
public class SJZDXService {

    SJZDXFeignClient client;

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

	public SJZDX create(SJZDX sjzdx) {
        return client.create(sjzdx);
    }

    public SJZDX get( String sjzdx_id) {
        return client.get( sjzdx_id);
    }

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

    public boolean checkKey(SJZDX sjzdx) {
        return client.checkKey(sjzdx);
    }

    public SJZDX update(String sjzdx_id, SJZDX sjzdx) {
        return client.update(sjzdx_id, sjzdx);
    }

    public SJZDX getDraft(String sjzdx_id, SJZDX sjzdx) {
        return client.getDraft(sjzdx_id, sjzdx);
    }

    public SJZDX save(String sjzdx_id, SJZDX sjzdx) {
        return client.save(sjzdx_id, sjzdx);
    }

	public Page<SJZDX> fetchCLZL(SJZDXSearchContext searchContext) {
        return client.fetchCLZL(searchContext);
    }

	public Page<SJZDX> fetchYWLY(SJZDXSearchContext searchContext) {
        return client.fetchYWLY(searchContext);
    }

	public Page<SJZDX> fetchCLDL(SJZDXSearchContext searchContext) {
        return client.fetchCLDL(searchContext);
    }

	public Page<SJZDX> fetchDefault(SJZDXSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
