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.IOrg;
import com.ibiz.client.org.config.OrgClientProperties;
import com.ibiz.core.client.service.IOrgClientService;
import com.ibiz.client.org.model.OrgImpl;
import com.ibiz.client.org.feign.OrgFeignClient;
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;

/**
 * 实体[Org] 服务对象接口
 */
@Service
public class OrgClientServiceImpl implements IOrgClientService {

    OrgFeignClient orgFeignClient;

    @Autowired
    public OrgClientServiceImpl(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.orgFeignClient = nameBuilder.target(OrgFeignClient.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.orgFeignClient = nameBuilder.target(OrgFeignClient.class,orgClientProperties.getServiceUrl()) ;
		}
    }
    @Override
    public IOrg createModel() {
		return new OrgImpl();
	}


    public void getDraft(IOrg org){
        IOrg clientModel = orgFeignClient.getDraft(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void getOrgPrent(IOrg org){
        IOrg clientModel = orgFeignClient.getOrgPrent(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void remove(IOrg org){
        orgFeignClient.remove(org.getOrgId()) ;
    }


    public void update(IOrg org){
        IOrg clientModel = orgFeignClient.update(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void get(IOrg org){
        IOrg clientModel = orgFeignClient.get(org.getOrgId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void create(IOrg org){
        IOrg clientModel = orgFeignClient.create((OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void save(IOrg org){
        IOrg clientModel = orgFeignClient.save(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void syncToUserGroup(IOrg org){
        IOrg clientModel = orgFeignClient.syncToUserGroup(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void checkKey(IOrg org){
        IOrg clientModel = orgFeignClient.checkKey(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public void getOrgChild(IOrg org){
        IOrg clientModel = orgFeignClient.getOrgChild(org.getOrgId(),(OrgImpl)org) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), org.getClass(), false);
        copier.copy(clientModel, org, null);
    }


    public Page<IOrg> fetchDefault(SearchContext context){
        Page<OrgImpl> page = this.orgFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

