package cn.ibizlab.r7.runtime.client.base.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import cn.ibizlab.r7.runtime.core.client.model.IUserRoleDataScope;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.IUserRoleDataScopeClientService;
import cn.ibizlab.r7.runtime.client.base.model.UserRoleDataScopeImpl;
import cn.ibizlab.r7.runtime.client.base.feign.UserRoleDataScopeFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import cn.ibizlab.r7.runtime.util.feign.FeignRequestInterceptor;
import cn.ibizlab.r7.runtime.util.SearchContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.r7.runtime.util.feign.suport.SearchContextFeignEncode;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;

/**
 * 实体[UserRoleDataScope] 服务对象接口
 */
@Service
public class UserRoleDataScopeClientServiceImpl implements IUserRoleDataScopeClientService {

    UserRoleDataScopeFeignClient userRoleDataScopeFeignClient;

    @Autowired
    public UserRoleDataScopeClientServiceImpl(Decoder decoder, Encoder encoder, Client client, Contract contract ,FeignRequestInterceptor feignRequestInterceptor,BaseClientProperties baseClientProperties) {
        if (baseClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.userRoleDataScopeFeignClient = nameBuilder.target(UserRoleDataScopeFeignClient.class,"http://"+baseClientProperties.getServiceId()+"/") ;
		}else if (baseClientProperties.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.userRoleDataScopeFeignClient = nameBuilder.target(UserRoleDataScopeFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public IUserRoleDataScope createModel() {
		return new UserRoleDataScopeImpl();
	}


    public void create(IUserRoleDataScope userroledatascope){
        IUserRoleDataScope clientModel = userRoleDataScopeFeignClient.create((UserRoleDataScopeImpl)userroledatascope) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledatascope.getClass(), false);
        copier.copy(clientModel, userroledatascope, null);
    }


    public void save(IUserRoleDataScope userroledatascope){
        IUserRoleDataScope clientModel = userRoleDataScopeFeignClient.save(userroledatascope.getUserRoleDataScopeId(),(UserRoleDataScopeImpl)userroledatascope) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledatascope.getClass(), false);
        copier.copy(clientModel, userroledatascope, null);
    }


    public void checkKey(IUserRoleDataScope userroledatascope){
        IUserRoleDataScope clientModel = userRoleDataScopeFeignClient.checkKey(userroledatascope.getUserRoleDataScopeId(),(UserRoleDataScopeImpl)userroledatascope) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledatascope.getClass(), false);
        copier.copy(clientModel, userroledatascope, null);
    }


    public void getDraft(IUserRoleDataScope userroledatascope){
        IUserRoleDataScope clientModel = userRoleDataScopeFeignClient.getDraft(userroledatascope.getUserRoleDataScopeId(),(UserRoleDataScopeImpl)userroledatascope) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledatascope.getClass(), false);
        copier.copy(clientModel, userroledatascope, null);
    }


    public void update(IUserRoleDataScope userroledatascope){
        IUserRoleDataScope clientModel = userRoleDataScopeFeignClient.update(userroledatascope.getUserRoleDataScopeId(),(UserRoleDataScopeImpl)userroledatascope) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledatascope.getClass(), false);
        copier.copy(clientModel, userroledatascope, null);
    }


    public void get(IUserRoleDataScope userroledatascope){
        IUserRoleDataScope clientModel = userRoleDataScopeFeignClient.get(userroledatascope.getUserRoleDataScopeId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledatascope.getClass(), false);
        copier.copy(clientModel, userroledatascope, null);
    }


    public void remove(IUserRoleDataScope userroledatascope){
        userRoleDataScopeFeignClient.remove(userroledatascope.getUserRoleDataScopeId()) ;
    }


    public Page<IUserRoleDataScope> fetchDefault(SearchContext context){
        Page<UserRoleDataScopeImpl> page = this.userRoleDataScopeFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

