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.IUserRole;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.IUserRoleClientService;
import cn.ibizlab.r7.runtime.client.base.model.UserRoleImpl;
import cn.ibizlab.r7.runtime.client.base.feign.UserRoleFeignClient;
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;

/**
 * 实体[UserRole] 服务对象接口
 */
@Service
public class UserRoleClientServiceImpl implements IUserRoleClientService {

    UserRoleFeignClient userRoleFeignClient;

    @Autowired
    public UserRoleClientServiceImpl(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.userRoleFeignClient = nameBuilder.target(UserRoleFeignClient.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.userRoleFeignClient = nameBuilder.target(UserRoleFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public IUserRole createModel() {
		return new UserRoleImpl();
	}


    public void create(IUserRole userrole){
        IUserRole clientModel = userRoleFeignClient.create((UserRoleImpl)userrole) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userrole.getClass(), false);
        copier.copy(clientModel, userrole, null);
    }


    public void save(IUserRole userrole){
        IUserRole clientModel = userRoleFeignClient.save(userrole.getUserRoleId(),(UserRoleImpl)userrole) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userrole.getClass(), false);
        copier.copy(clientModel, userrole, null);
    }


    public void update(IUserRole userrole){
        IUserRole clientModel = userRoleFeignClient.update(userrole.getUserRoleId(),(UserRoleImpl)userrole) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userrole.getClass(), false);
        copier.copy(clientModel, userrole, null);
    }


    public void checkKey(IUserRole userrole){
        IUserRole clientModel = userRoleFeignClient.checkKey(userrole.getUserRoleId(),(UserRoleImpl)userrole) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userrole.getClass(), false);
        copier.copy(clientModel, userrole, null);
    }


    public void get(IUserRole userrole){
        IUserRole clientModel = userRoleFeignClient.get(userrole.getUserRoleId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userrole.getClass(), false);
        copier.copy(clientModel, userrole, null);
    }


    public void remove(IUserRole userrole){
        userRoleFeignClient.remove(userrole.getUserRoleId()) ;
    }


    public void getDraft(IUserRole userrole){
        IUserRole clientModel = userRoleFeignClient.getDraft(userrole.getUserRoleId(),(UserRoleImpl)userrole) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), userrole.getClass(), false);
        copier.copy(clientModel, userrole, null);
    }


    public Page<IUserRole> fetchDefault(SearchContext context){
        Page<UserRoleImpl> page = this.userRoleFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

