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.IOrgDept;
import com.ibiz.client.org.config.OrgClientProperties;
import com.ibiz.core.client.service.IOrgDeptClientService;
import com.ibiz.client.org.model.OrgDeptImpl;
import com.ibiz.client.org.feign.OrgDeptFeignClient;
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;

/**
 * 实体[OrgDept] 服务对象接口
 */
@Service
public class OrgDeptClientServiceImpl implements IOrgDeptClientService {

    OrgDeptFeignClient orgDeptFeignClient;

    @Autowired
    public OrgDeptClientServiceImpl(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.orgDeptFeignClient = nameBuilder.target(OrgDeptFeignClient.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.orgDeptFeignClient = nameBuilder.target(OrgDeptFeignClient.class,orgClientProperties.getServiceUrl()) ;
		}
    }
    @Override
    public IOrgDept createModel() {
		return new OrgDeptImpl();
	}


    public void save(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.save(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void syncToUserGroup(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.syncToUserGroup(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void getOrgDeptChild(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.getOrgDeptChild(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void remove(IOrgDept orgdept){
        orgDeptFeignClient.remove(orgdept.getOrgDeptId()) ;
    }


    public void get(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.get(orgdept.getOrgDeptId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void update(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.update(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void getDraft(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.getDraft(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void create(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.create((OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void checkKey(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.checkKey(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public void getOrgDeptPrent(IOrgDept orgdept){
        IOrgDept clientModel = orgDeptFeignClient.getOrgDeptPrent(orgdept.getOrgDeptId(),(OrgDeptImpl)orgdept) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orgdept.getClass(), false);
        copier.copy(clientModel, orgdept, null);
    }


    public Page<IOrgDept> fetchDefault(SearchContext context){
        Page<OrgDeptImpl> page = this.orgDeptFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }


    public Page<IOrgDept> fetchCUR_ORG(SearchContext context){
        Page<OrgDeptImpl> page = this.orgDeptFeignClient.fetchCUR_ORG(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

