
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.LoginAccount;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.feign.LoginAccountFeignClient;
import cn.ibizlab.r7.runtime.runtimemgr.RuntimeMgrApplication.RuntimeMgrClientProperties;
import cn.ibizlab.r7.runtime.util.feign.FeignRequestInterceptor;

@Service
public class LoginAccountService {

    LoginAccountFeignClient client;

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

    public LoginAccount get( String loginaccount_id) {
        return client.get( loginaccount_id);
    }

    public boolean checkKey(LoginAccount loginaccount) {
        return client.checkKey(loginaccount);
    }

    public LoginAccount updatePWD(String loginaccount_id, LoginAccount loginaccount) {
        return client.updatePWD(loginaccount_id, loginaccount);
    }

    public LoginAccount save(String loginaccount_id, LoginAccount loginaccount) {
        return client.save(loginaccount_id, loginaccount);
    }

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

	public LoginAccount create(LoginAccount loginaccount) {
        return client.create(loginaccount);
    }

    public LoginAccount getDraft(String loginaccount_id, LoginAccount loginaccount) {
        return client.getDraft(loginaccount_id, loginaccount);
    }

    public LoginAccount update(String loginaccount_id, LoginAccount loginaccount) {
        return client.update(loginaccount_id, loginaccount);
    }

	public Page<LoginAccount> fetchGetLoginaccount(LoginAccountSearchContext searchContext) {
        return client.fetchGetLoginaccount(searchContext);
    }

	public Page<LoginAccount> fetchDefault(LoginAccountSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


    public LoginAccount getByUser(String user_id,  String loginaccount_id) {
        return client.getByUser(user_id,  loginaccount_id);
    }

    public boolean checkKeyByUser(String user_id, LoginAccount loginaccount) {
        return client.checkKeyByUser(user_id, loginaccount);
    }

    public LoginAccount updatePWDByUser(String user_id, String loginaccount_id, LoginAccount loginaccount) {
        return client.updatePWDByUser(user_id, loginaccount_id, loginaccount);
    }

    public LoginAccount saveByUser(String user_id, String loginaccount_id, LoginAccount loginaccount) {
        return client.saveByUser(user_id, loginaccount_id, loginaccount);
    }

    public boolean removeByUser(String user_id,  String loginaccount_id) {
        return client.removeByUser(user_id,  loginaccount_id);
    }

	public LoginAccount createByUser(String user_id, LoginAccount loginaccount) {
        return client.createByUser(user_id, loginaccount);
    }

    public LoginAccount getDraftByUser(String user_id, String loginaccount_id, LoginAccount loginaccount) {
        return client.getDraftByUser(user_id, loginaccount_id, loginaccount);
    }

    public LoginAccount updateByUser(String user_id, String loginaccount_id, LoginAccount loginaccount) {
        return client.updateByUser(user_id, loginaccount_id, loginaccount);
    }

	public Page<LoginAccount> fetchGetLoginaccountByUser(String user_id , LoginAccountSearchContext searchContext) {
        return client.fetchGetLoginaccountByUser(user_id , searchContext);
    }

	public Page<LoginAccount> fetchDefaultByUser(String user_id , LoginAccountSearchContext searchContext) {
        return client.fetchDefaultByUser(user_id , searchContext);
    }

}
