
package cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.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_base.domain.UserRoleData;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.feign.UserRoleDataFeignClient;
import cn.ibizlab.r7.runtime.runtimemgr.RuntimeMgrApplication.RuntimeMgrClientProperties;
import cn.ibizlab.r7.runtime.util.feign.FeignRequestInterceptor;

@Service
public class UserRoleDataService {

    UserRoleDataFeignClient client;

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

    public boolean checkKey(UserRoleData userroledata) {
        return client.checkKey(userroledata);
    }

    public UserRoleData getDraft(String userroledata_id, UserRoleData userroledata) {
        return client.getDraft(userroledata_id, userroledata);
    }

    public UserRoleData getUserRoleDataAPI(String userroledata_id, UserRoleData userroledata) {
        return client.getUserRoleDataAPI(userroledata_id, userroledata);
    }

    public UserRoleData get( String userroledata_id) {
        return client.get( userroledata_id);
    }

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

    public UserRoleData hasPermissionGV(String userroledata_id, UserRoleData userroledata) {
        return client.hasPermissionGV(userroledata_id, userroledata);
    }

	public UserRoleData create(UserRoleData userroledata) {
        return client.create(userroledata);
    }

    public UserRoleData update(String userroledata_id, UserRoleData userroledata) {
        return client.update(userroledata_id, userroledata);
    }

    public UserRoleData hasPermissionEV(String userroledata_id, UserRoleData userroledata) {
        return client.hasPermissionEV(userroledata_id, userroledata);
    }

    public UserRoleData save(String userroledata_id, UserRoleData userroledata) {
        return client.save(userroledata_id, userroledata);
    }

    public UserRoleData syncData(String userroledata_id, UserRoleData userroledata) {
        return client.syncData(userroledata_id, userroledata);
    }

	public Page<UserRoleData> fetchDefault(UserRoleDataSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }

}
