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.IUserRoleDatas;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDatas;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDatasSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDatasService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户角色数据能力] 服务对象接口实现
 */
@Service
public class UserRoleDatasServiceImpl implements IUserRoleDatasService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleDatasClientService userRoleDatasClientService;

    @Override
    public boolean save(UserRoleDatas userroledatas) {
        if(userroledatas.getUserRoleDatasId()==null){
    		return create(userroledatas);
    	}else{
            if(this.get(userroledatas.getUserRoleDatasId())!=null){
    			return update(userroledatas);
    		}
    		return create(userroledatas);
    	}
    }

    @Override
    public boolean create(UserRoleDatas userroledatas) {
        boolean bOk = false ;
        IUserRoleDatas clientModel = this.convert2Model(userroledatas,null);
        userRoleDatasClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroledatas);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(UserRoleDatas userroledatas) {
        boolean bOk = false ;
        IUserRoleDatas clientModel = this.convert2Model(userroledatas,null);
        userRoleDatasClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroledatas) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean checkKey(UserRoleDatas userroledatas) {
        return true;
    }

    @Override
    public UserRoleDatas get(String userroledatasid) {
        IUserRoleDatas clientModel = userRoleDatasClientService.createModel();
        clientModel.setUserRoleDatasId(userroledatasid);
        userRoleDatasClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleDatas get(UserRoleDatas userroledatas) {
        UserRoleDatas retuserroledatas = this.get(userroledatas.getUserRoleDatasId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroledatas.getClass(), userroledatas.getClass(), false);
        copier.copy(retuserroledatas, userroledatas, null);
        return userroledatas ;
    }

    @Override
    public UserRoleDatas getDraft(UserRoleDatas userroledatas) {
        return userroledatas;
    }

    @Override
    public boolean remove(String userroledatasid) {
        boolean bOk = false ;
        IUserRoleDatas clientModel = userRoleDatasClientService.createModel();
        clientModel.setUserRoleDatasId(userroledatasid);
        userRoleDatasClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

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


    /**
     * 根据数据对象能力查询
     */
    @Override
    public Page<UserRoleDatas> selectByUserRoleData(String userroledataid){
        return null ;
    }

    /**
     * 根据数据对象能力查询
     */
    @Override
    public List<UserRoleDatas> selectByUserRoleData2(String userroledataid){
        return null ;
    }
    /**
     * 根据用户角色查询
     */
    @Override
    public Page<UserRoleDatas> selectByUserRole(String userroleid){
        return null ;
    }

    /**
     * 根据用户角色查询
     */
    @Override
    public List<UserRoleDatas> selectByUserRole2(String userroleid){
        return null ;
    }


    public IUserRoleDatas convert2Model(UserRoleDatas domain , IUserRoleDatas model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleDatasClientService.createModel() ;
        }

         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUserRoleDatasNameDirtyFlag())
            model.setUserRoleDatasName(domain.getUserRoleDatasName());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getUserRoleDatasIdDirtyFlag())
            model.setUserRoleDatasId(domain.getUserRoleDatasId());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUserRoleNameDirtyFlag())
            model.setUserRoleName(domain.getUserRoleName());
         if(domain.getUserRoleDataNameDirtyFlag())
            model.setUserRoleDataName(domain.getUserRoleDataName());
         if(domain.getUserRoleIdDirtyFlag())
            model.setUserRoleId(domain.getUserRoleId());
         if(domain.getUserRoleDataIdDirtyFlag())
            model.setUserRoleDataId(domain.getUserRoleDataId());
        return model ;
    }

    public UserRoleDatas convert2Domain( IUserRoleDatas model ,UserRoleDatas domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleDatas();
        }

        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUserRoleDatasNameDirtyFlag())
            domain.setUserRoleDatasName(model.getUserRoleDatasName());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getUserRoleDatasIdDirtyFlag())
            domain.setUserRoleDatasId(model.getUserRoleDatasId());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUserRoleNameDirtyFlag())
            domain.setUserRoleName(model.getUserRoleName());
        if(model.getUserRoleDataNameDirtyFlag())
            domain.setUserRoleDataName(model.getUserRoleDataName());
        if(model.getUserRoleIdDirtyFlag())
            domain.setUserRoleId(model.getUserRoleId());
        if(model.getUserRoleDataIdDirtyFlag())
            domain.setUserRoleDataId(model.getUserRoleDataId());
        return domain ;
    }


}
