package cn.ibizlab.r7.runtime.core.r7rt_base.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.r7.runtime.core.client.model.ILoginAccount;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.LoginAccount;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.LoginAccountSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.ILoginAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[登录账号] 服务对象接口实现
 */
@Service
public class LoginAccountServiceImpl implements ILoginAccountService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.ILoginAccountClientService loginAccountClientService;

    @Override
    public LoginAccount get(String loginaccountid) {
        ILoginAccount clientModel = loginAccountClientService.createModel();
        clientModel.setLoginAccountId(loginaccountid);
        loginAccountClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public LoginAccount get(LoginAccount loginaccount) {
        LoginAccount retloginaccount = this.get(loginaccount.getLoginAccountId()) ;
    	BeanCopier copier=BeanCopier.create(retloginaccount.getClass(), loginaccount.getClass(), false);
        copier.copy(retloginaccount, loginaccount, null);
        return loginaccount ;
    }

    @Override
    public boolean checkKey(LoginAccount loginaccount) {
        return true;
    }

    @Override
    public LoginAccount updatePWD(LoginAccount loginaccount) {
        
        ILoginAccount clientModel = this.convert2Model(loginaccount,null);
        loginAccountClientService.updatePWD(clientModel) ;
        this.convert2Domain(clientModel,null);
        return loginaccount;
    }

    @Override
    public boolean save(LoginAccount loginaccount) {
        if(loginaccount.getLoginAccountId()==null){
    		return create(loginaccount);
    	}else{
            if(this.get(loginaccount.getLoginAccountId())!=null){
    			return update(loginaccount);
    		}
    		return create(loginaccount);
    	}
    }

    @Override
    public boolean remove(String loginaccountid) {
        boolean bOk = false ;
        ILoginAccount clientModel = loginAccountClientService.createModel();
        clientModel.setLoginAccountId(loginaccountid);
        loginAccountClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(LoginAccount loginaccount) {
        boolean bOk = false ;
        ILoginAccount clientModel = this.convert2Model(loginaccount,null);
        loginAccountClientService.create(clientModel) ;
        this.convert2Domain(clientModel,loginaccount);
        bOk = true ;

        return bOk ;
    }

    @Override
    public LoginAccount getDraft(LoginAccount loginaccount) {
        return loginaccount;
    }

    @Override
    public boolean update(LoginAccount loginaccount) {
        boolean bOk = false ;
        ILoginAccount clientModel = this.convert2Model(loginaccount,null);
        loginAccountClientService.update(clientModel) ;
        this.convert2Domain(clientModel,loginaccount) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 getLoginaccount
     */
    @Override
    public Page<LoginAccount> searchGetLoginaccount(LoginAccountSearchContext context) {
        List<LoginAccount> list = new ArrayList<LoginAccount>();
        Page<cn.ibizlab.r7.runtime.core.client.model.ILoginAccount> clientModelList = loginAccountClientService.fetchGetLoginaccount(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.ILoginAccount clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<LoginAccount>(list, context.getPageable(), clientModelList.getTotalElements());
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<LoginAccount> searchDefault(LoginAccountSearchContext context) {
        List<LoginAccount> list = new ArrayList<LoginAccount>();
        Page<cn.ibizlab.r7.runtime.core.client.model.ILoginAccount> clientModelList = loginAccountClientService.fetchDefault(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.ILoginAccount clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<LoginAccount>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<LoginAccount> selectByUser(String userid){
        return null ;
    }

    /**
     * 根据用户查询
     */
    @Override
    public List<LoginAccount> selectByUser2(String userid){
        return null ;
    }


    public ILoginAccount convert2Model(LoginAccount domain , ILoginAccount model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = loginAccountClientService.createModel() ;
        }

         if(domain.getConfirmpwdDirtyFlag())
            model.setConfirmpwd(domain.getConfirmpwd());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getLoginAccountIdDirtyFlag())
            model.setLoginAccountId(domain.getLoginAccountId());
         if(domain.getPwdDirtyFlag())
            model.setPwd(domain.getPwd());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getPwdChangedDirtyFlag())
            model.setPwdChanged(domain.getPwdChanged());
         if(domain.getLastPwfChgTimeDirtyFlag())
            model.setLastPwfChgTime(domain.getLastPwfChgTime());
         if(domain.getLoginAccountNameDirtyFlag())
            model.setLoginAccountName(domain.getLoginAccountName());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getIsSuperUserDirtyFlag())
            model.setIsSuperUser(domain.getIsSuperUser());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getLastpwdchgmanDirtyFlag())
            model.setLastpwdchgman(domain.getLastpwdchgman());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getOldPwdDirtyFlag())
            model.setOldPwd(domain.getOldPwd());
         if(domain.getUserNameDirtyFlag())
            model.setUserName(domain.getUserName());
         if(domain.getUserIdDirtyFlag())
            model.setUserId(domain.getUserId());
        return model ;
    }

    public LoginAccount convert2Domain( ILoginAccount model ,LoginAccount domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new LoginAccount();
        }

        if(model.getConfirmpwdDirtyFlag())
            domain.setConfirmpwd(model.getConfirmpwd());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getLoginAccountIdDirtyFlag())
            domain.setLoginAccountId(model.getLoginAccountId());
        if(model.getPwdDirtyFlag())
            domain.setPwd(model.getPwd());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getPwdChangedDirtyFlag())
            domain.setPwdChanged(model.getPwdChanged());
        if(model.getLastPwfChgTimeDirtyFlag())
            domain.setLastPwfChgTime(model.getLastPwfChgTime());
        if(model.getLoginAccountNameDirtyFlag())
            domain.setLoginAccountName(model.getLoginAccountName());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getIsSuperUserDirtyFlag())
            domain.setIsSuperUser(model.getIsSuperUser());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getLastpwdchgmanDirtyFlag())
            domain.setLastpwdchgman(model.getLastpwdchgman());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getOldPwdDirtyFlag())
            domain.setOldPwd(model.getOldPwd());
        if(model.getUserNameDirtyFlag())
            domain.setUserName(model.getUserName());
        if(model.getUserIdDirtyFlag())
            domain.setUserId(model.getUserId());
        return domain ;
    }


}
