
package cn.ibizlab.r7.runtime.runtimemgr.r7rt_org.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 org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import cn.ibizlab.r7.runtime.util.feign.suport.SearchContextFeignEncode;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_org.domain.OrgUser;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_org.filter.*;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_org.feign.OrgUserFeignClient;
import cn.ibizlab.r7.runtime.runtimemgr.RuntimeMgrApplication.RuntimeMgrClientProperties;
import cn.ibizlab.r7.runtime.util.feign.FeignRequestInterceptor;

@Service
public class OrgUserService {

    OrgUserFeignClient client;

    @Autowired
    public OrgUserService(Decoder decoder, Encoder encoder, Client client, Contract contract, FeignRequestInterceptor feignRequestInterceptor,
                                    RuntimeMgrClientProperties runtimemgrClientProperties) {
        if (runtimemgrClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.client = nameBuilder.target(OrgUserFeignClient.class,"http://" + runtimemgrClientProperties.getServiceId() + "/") ;
		} else if (runtimemgrClientProperties.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(OrgUserFeignClient.class, "http://" + runtimemgrClientProperties.getServiceUrl() + "/") ;
		}
    }

	public OrgUser create(OrgUser orguser) {
        return client.create(orguser);
    }

    public OrgUser save(String orguser_id, OrgUser orguser) {
        return client.save(orguser_id, orguser);
    }

    public OrgUser update(String orguser_id, OrgUser orguser) {
        return client.update(orguser_id, orguser);
    }

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

    public OrgUser get( String orguser_id) {
        return client.get( orguser_id);
    }

    public boolean checkKey(OrgUser orguser) {
        return client.checkKey(orguser);
    }

    public OrgUser getDraft(String orguser_id, OrgUser orguser) {
        return client.getDraft(orguser_id, orguser);
    }

	public Page<OrgUser> fetchDefault(OrgUserSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }

}
