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.IUserRoleData;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.IUserRoleDataClientService;
import cn.ibizlab.r7.runtime.client.base.model.UserRoleDataImpl;
import cn.ibizlab.r7.runtime.client.base.feign.UserRoleDataFeignClient;
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;

/**
 * 实体[UserRoleData] 服务对象接口
 */
@Service
public class UserRoleDataClientServiceImpl implements IUserRoleDataClientService {

    UserRoleDataFeignClient userRoleDataFeignClient;

    @Autowired
    public UserRoleDataClientServiceImpl(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.userRoleDataFeignClient = nameBuilder.target(UserRoleDataFeignClient.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.userRoleDataFeignClient = nameBuilder.target(UserRoleDataFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public IUserRoleData createModel() {
		return new UserRoleDataImpl();
	}


    public void checkKey(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.checkKey(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void getDraft(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.getDraft(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void getUserRoleDataAPI(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.getUserRoleDataAPI(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void get(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.get(userroledata.getUserRoleDataId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void remove(IUserRoleData userroledata){
        userRoleDataFeignClient.remove(userroledata.getUserRoleDataId()) ;
    }


    public void hasPermissionGV(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.hasPermissionGV(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void create(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.create((UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void update(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.update(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void hasPermissionEV(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.hasPermissionEV(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void save(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.save(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public void syncData(IUserRoleData userroledata){
        IUserRoleData clientModel = userRoleDataFeignClient.syncData(userroledata.getUserRoleDataId(),(UserRoleDataImpl)userroledata) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userroledata.getClass(), false);
        copier.copy(clientModel, userroledata, null);
    }


    public Page<IUserRoleData> fetchDefault(SearchContext context){
        Page<UserRoleDataImpl> page = this.userRoleDataFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

