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.IUserRoleDataScope;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDataScope;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDataScopeSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDataScopeService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[数据范围] 服务对象接口实现
 */
@Service
public class UserRoleDataScopeServiceImpl implements IUserRoleDataScopeService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleDataScopeClientService userRoleDataScopeClientService;

    @Override
    public boolean create(UserRoleDataScope userroledatascope) {
        boolean bOk = false ;
        IUserRoleDataScope clientModel = this.convert2Model(userroledatascope,null);
        userRoleDataScopeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroledatascope);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean save(UserRoleDataScope userroledatascope) {
        if(userroledatascope.getUserRoleDataScopeId()==null){
    		return create(userroledatascope);
    	}else{
            if(this.get(userroledatascope.getUserRoleDataScopeId())!=null){
    			return update(userroledatascope);
    		}
    		return create(userroledatascope);
    	}
    }

    @Override
    public boolean checkKey(UserRoleDataScope userroledatascope) {
        return true;
    }

    @Override
    public UserRoleDataScope getDraft(UserRoleDataScope userroledatascope) {
        return userroledatascope;
    }

    @Override
    public boolean update(UserRoleDataScope userroledatascope) {
        boolean bOk = false ;
        IUserRoleDataScope clientModel = this.convert2Model(userroledatascope,null);
        userRoleDataScopeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroledatascope) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public UserRoleDataScope get(String userroledatascopeid) {
        IUserRoleDataScope clientModel = userRoleDataScopeClientService.createModel();
        clientModel.setUserRoleDataScopeId(userroledatascopeid);
        userRoleDataScopeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleDataScope get(UserRoleDataScope userroledatascope) {
        UserRoleDataScope retuserroledatascope = this.get(userroledatascope.getUserRoleDataScopeId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroledatascope.getClass(), userroledatascope.getClass(), false);
        copier.copy(retuserroledatascope, userroledatascope, null);
        return userroledatascope ;
    }

    @Override
    public boolean remove(String userroledatascopeid) {
        boolean bOk = false ;
        IUserRoleDataScope clientModel = userRoleDataScopeClientService.createModel();
        clientModel.setUserRoleDataScopeId(userroledatascopeid);
        userRoleDataScopeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

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


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

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


    public IUserRoleDataScope convert2Model(UserRoleDataScope domain , IUserRoleDataScope model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleDataScopeClientService.createModel() ;
        }

         if(domain.getUserRoleDataScopeIdDirtyFlag())
            model.setUserRoleDataScopeId(domain.getUserRoleDataScopeId());
         if(domain.getUserRoleDataScopeNameDirtyFlag())
            model.setUserRoleDataScopeName(domain.getUserRoleDataScopeName());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserRoleDataIdDirtyFlag())
            model.setUserRoleDataId(domain.getUserRoleDataId());
        return model ;
    }

    public UserRoleDataScope convert2Domain( IUserRoleDataScope model ,UserRoleDataScope domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleDataScope();
        }

        if(model.getUserRoleDataScopeIdDirtyFlag())
            domain.setUserRoleDataScopeId(model.getUserRoleDataScopeId());
        if(model.getUserRoleDataScopeNameDirtyFlag())
            domain.setUserRoleDataScopeName(model.getUserRoleDataScopeName());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserRoleDataIdDirtyFlag())
            domain.setUserRoleDataId(model.getUserRoleDataId());
        return domain ;
    }


}
