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.IOrgUser;
import com.ibiz.client.org.config.OrgClientProperties;
import com.ibiz.core.client.service.IOrgUserClientService;
import com.ibiz.client.org.model.OrgUserImpl;
import com.ibiz.client.org.feign.OrgUserFeignClient;
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;

/**
 * 实体[OrgUser] 服务对象接口
 */
@Service
public class OrgUserClientServiceImpl implements IOrgUserClientService {

    OrgUserFeignClient orgUserFeignClient;

    @Autowired
    public OrgUserClientServiceImpl(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.orgUserFeignClient = nameBuilder.target(OrgUserFeignClient.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.orgUserFeignClient = nameBuilder.target(OrgUserFeignClient.class,orgClientProperties.getServiceUrl()) ;
		}
    }
    @Override
    public IOrgUser createModel() {
		return new OrgUserImpl();
	}


    public void remove(IOrgUser orguser){
        orgUserFeignClient.remove(orguser.getOrgUserId()) ;
    }


    public void save(IOrgUser orguser){
        IOrgUser clientModel = orgUserFeignClient.save(orguser.getOrgUserId(),(OrgUserImpl)orguser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orguser.getClass(), false);
        copier.copy(clientModel, orguser, null);
    }


    public void update(IOrgUser orguser){
        IOrgUser clientModel = orgUserFeignClient.update(orguser.getOrgUserId(),(OrgUserImpl)orguser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orguser.getClass(), false);
        copier.copy(clientModel, orguser, null);
    }


    public void checkKey(IOrgUser orguser){
        IOrgUser clientModel = orgUserFeignClient.checkKey(orguser.getOrgUserId(),(OrgUserImpl)orguser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orguser.getClass(), false);
        copier.copy(clientModel, orguser, null);
    }


    public void getDraft(IOrgUser orguser){
        IOrgUser clientModel = orgUserFeignClient.getDraft(orguser.getOrgUserId(),(OrgUserImpl)orguser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orguser.getClass(), false);
        copier.copy(clientModel, orguser, null);
    }


    public void create(IOrgUser orguser){
        IOrgUser clientModel = orgUserFeignClient.create((OrgUserImpl)orguser) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orguser.getClass(), false);
        copier.copy(clientModel, orguser, null);
    }


    public void get(IOrgUser orguser){
        IOrgUser clientModel = orgUserFeignClient.get(orguser.getOrgUserId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), orguser.getClass(), false);
        copier.copy(clientModel, orguser, null);
    }


    public Page<IOrgUser> fetchDefault(SearchContext context){
        Page<OrgUserImpl> page = this.orgUserFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

