package com.ibiz.client.org.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 com.ibiz.core.client.model.IOrgDeptUser;
import com.ibiz.client.org.config.OrgClientProperties;
import com.ibiz.core.client.service.IOrgDeptUserClientService;
import com.ibiz.client.org.model.OrgDeptUserImpl;
import com.ibiz.client.org.feign.OrgDeptUserFeignClient;
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 com.ibiz.util.feign.FeignRequestInterceptor;
import com.ibiz.util.SearchContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import com.ibiz.util.feign.suport.SearchContextFeignEncode;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;

/**
 * 实体[OrgDeptUser] 服务对象接口
 */
@Service
public class OrgDeptUserClientServiceImpl implements IOrgDeptUserClientService {

    OrgDeptUserFeignClient orgDeptUserFeignClient;

    @Autowired
    public OrgDeptUserClientServiceImpl(Decoder decoder, Encoder encoder, Client client, Contract contract ,FeignRequestInterceptor feignRequestInterceptor,OrgClientProperties orgClientProperties) {
        if (orgClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.orgDeptUserFeignClient = nameBuilder.target(OrgDeptUserFeignClient.class,"http://"+orgClientProperties.getServiceId()+"/") ;
		}else if (orgClientProperties.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.orgDeptUserFeignClient = nameBuilder.target(OrgDeptUserFeignClient.class,orgClientProperties.getServiceUrl()) ;
		}
    }
    @Override
    public IOrgDeptUser createModel() {
		return new OrgDeptUserImpl();
	}


    public void getDraft(IOrgDeptUser orgdeptuser){
        IOrgDeptUser clientModel = orgDeptUserFeignClient.getDraft(orgdeptuser.getOrgDeptUserId(),(OrgDeptUserImpl)orgdeptuser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdeptuser.getClass(), false);
        copier.copy(clientModel, orgdeptuser, null);
    }


    public void get(IOrgDeptUser orgdeptuser){
        IOrgDeptUser clientModel = orgDeptUserFeignClient.get(orgdeptuser.getOrgDeptUserId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdeptuser.getClass(), false);
        copier.copy(clientModel, orgdeptuser, null);
    }


    public void save(IOrgDeptUser orgdeptuser){
        IOrgDeptUser clientModel = orgDeptUserFeignClient.save(orgdeptuser.getOrgDeptUserId(),(OrgDeptUserImpl)orgdeptuser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdeptuser.getClass(), false);
        copier.copy(clientModel, orgdeptuser, null);
    }


    public void create(IOrgDeptUser orgdeptuser){
        IOrgDeptUser clientModel = orgDeptUserFeignClient.create((OrgDeptUserImpl)orgdeptuser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdeptuser.getClass(), false);
        copier.copy(clientModel, orgdeptuser, null);
    }


    public void remove(IOrgDeptUser orgdeptuser){
        orgDeptUserFeignClient.remove(orgdeptuser.getOrgDeptUserId()) ;
    }


    public void update(IOrgDeptUser orgdeptuser){
        IOrgDeptUser clientModel = orgDeptUserFeignClient.update(orgdeptuser.getOrgDeptUserId(),(OrgDeptUserImpl)orgdeptuser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdeptuser.getClass(), false);
        copier.copy(clientModel, orgdeptuser, null);
    }


    public void checkKey(IOrgDeptUser orgdeptuser){
        IOrgDeptUser clientModel = orgDeptUserFeignClient.checkKey(orgdeptuser.getOrgDeptUserId(),(OrgDeptUserImpl)orgdeptuser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdeptuser.getClass(), false);
        copier.copy(clientModel, orgdeptuser, null);
    }


    public Page<IOrgDeptUser> fetchGetorgdeptuser(SearchContext context){
        Page<OrgDeptUserImpl> page = this.orgDeptUserFeignClient.fetchGetorgdeptuser(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }


    public Page<IOrgDeptUser> fetchDefault(SearchContext context){
        Page<OrgDeptUserImpl> page = this.orgDeptUserFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

