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.ILoginAccount;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.ILoginAccountClientService;
import cn.ibizlab.r7.runtime.client.base.model.LoginAccountImpl;
import cn.ibizlab.r7.runtime.client.base.feign.LoginAccountFeignClient;
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;

/**
 * 实体[LoginAccount] 服务对象接口
 */
@Service
public class LoginAccountClientServiceImpl implements ILoginAccountClientService {

    LoginAccountFeignClient loginAccountFeignClient;

    @Autowired
    public LoginAccountClientServiceImpl(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.loginAccountFeignClient = nameBuilder.target(LoginAccountFeignClient.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.loginAccountFeignClient = nameBuilder.target(LoginAccountFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public ILoginAccount createModel() {
		return new LoginAccountImpl();
	}


    public void get(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.get(loginaccount.getLoginAccountId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public void checkKey(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.checkKey(loginaccount.getLoginAccountId(),(LoginAccountImpl)loginaccount) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public void updatePWD(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.updatePWD(loginaccount.getLoginAccountId(),(LoginAccountImpl)loginaccount) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public void save(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.save(loginaccount.getLoginAccountId(),(LoginAccountImpl)loginaccount) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public void remove(ILoginAccount loginaccount){
        loginAccountFeignClient.remove(loginaccount.getLoginAccountId()) ;
    }


    public void create(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.create((LoginAccountImpl)loginaccount) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public void getDraft(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.getDraft(loginaccount.getLoginAccountId(),(LoginAccountImpl)loginaccount) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public void update(ILoginAccount loginaccount){
        ILoginAccount clientModel = loginAccountFeignClient.update(loginaccount.getLoginAccountId(),(LoginAccountImpl)loginaccount) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), loginaccount.getClass(), false);
        copier.copy(clientModel, loginaccount, null);
    }


    public Page<ILoginAccount> fetchGetLoginaccount(SearchContext context){
        Page<LoginAccountImpl> page = this.loginAccountFeignClient.fetchGetLoginaccount(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }


    public Page<ILoginAccount> fetchDefault(SearchContext context){
        Page<LoginAccountImpl> page = this.loginAccountFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

