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.ILoginAccountPwd;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.LoginAccountPwd;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.LoginAccountPwdSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.ILoginAccountPwdService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[登录账号密码] 服务对象接口实现
 */
@Service
public class LoginAccountPwdServiceImpl implements ILoginAccountPwdService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.ILoginAccountPwdClientService loginAccountPwdClientService;

    @Override
    public boolean checkKey(LoginAccountPwd loginaccountpwd) {
        return true;
    }

    @Override
    public boolean update(LoginAccountPwd loginaccountpwd) {
        boolean bOk = false ;
        ILoginAccountPwd clientModel = this.convert2Model(loginaccountpwd,null);
        loginAccountPwdClientService.update(clientModel) ;
        this.convert2Domain(clientModel,loginaccountpwd) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(String loginaccountpwdid) {
        boolean bOk = false ;
        ILoginAccountPwd clientModel = loginAccountPwdClientService.createModel();
        clientModel.setLoginAccountPwdId(loginaccountpwdid);
        loginAccountPwdClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public LoginAccountPwd get(String loginaccountpwdid) {
        ILoginAccountPwd clientModel = loginAccountPwdClientService.createModel();
        clientModel.setLoginAccountPwdId(loginaccountpwdid);
        loginAccountPwdClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public LoginAccountPwd get(LoginAccountPwd loginaccountpwd) {
        LoginAccountPwd retloginaccountpwd = this.get(loginaccountpwd.getLoginAccountPwdId()) ;
    	BeanCopier copier=BeanCopier.create(retloginaccountpwd.getClass(), loginaccountpwd.getClass(), false);
        copier.copy(retloginaccountpwd, loginaccountpwd, null);
        return loginaccountpwd ;
    }

    @Override
    public boolean save(LoginAccountPwd loginaccountpwd) {
        if(loginaccountpwd.getLoginAccountPwdId()==null){
    		return create(loginaccountpwd);
    	}else{
            if(this.get(loginaccountpwd.getLoginAccountPwdId())!=null){
    			return update(loginaccountpwd);
    		}
    		return create(loginaccountpwd);
    	}
    }

    @Override
    public boolean create(LoginAccountPwd loginaccountpwd) {
        boolean bOk = false ;
        ILoginAccountPwd clientModel = this.convert2Model(loginaccountpwd,null);
        loginAccountPwdClientService.create(clientModel) ;
        this.convert2Domain(clientModel,loginaccountpwd);
        bOk = true ;

        return bOk ;
    }

    @Override
    public LoginAccountPwd getDraft(LoginAccountPwd loginaccountpwd) {
        return loginaccountpwd;
    }

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


    /**
     * 根据登录账号查询
     */
    @Override
    public Page<LoginAccountPwd> selectByLoginacccoutDR(String loginaccountid){
        return null ;
    }

    /**
     * 根据登录账号查询
     */
    @Override
    public List<LoginAccountPwd> selectByLoginacccoutDR2(String loginaccountid){
        return null ;
    }


    public ILoginAccountPwd convert2Model(LoginAccountPwd domain , ILoginAccountPwd model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = loginAccountPwdClientService.createModel() ;
        }

         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getPwdDirtyFlag())
            model.setPwd(domain.getPwd());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getLoginAccountPwdNameDirtyFlag())
            model.setLoginAccountPwdName(domain.getLoginAccountPwdName());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getLoginAccountPwdIdDirtyFlag())
            model.setLoginAccountPwdId(domain.getLoginAccountPwdId());
         if(domain.getLoginAccountIdDirtyFlag())
            model.setLoginAccountId(domain.getLoginAccountId());
        return model ;
    }

    public LoginAccountPwd convert2Domain( ILoginAccountPwd model ,LoginAccountPwd domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new LoginAccountPwd();
        }

        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getPwdDirtyFlag())
            domain.setPwd(model.getPwd());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getLoginAccountPwdNameDirtyFlag())
            domain.setLoginAccountPwdName(model.getLoginAccountPwdName());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getLoginAccountPwdIdDirtyFlag())
            domain.setLoginAccountPwdId(model.getLoginAccountPwdId());
        if(model.getLoginAccountIdDirtyFlag())
            domain.setLoginAccountId(model.getLoginAccountId());
        return domain ;
    }


}
