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.IUserRoleDataAction;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDataAction;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDataActionSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDataActionService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户角色数据操作] 服务对象接口实现
 */
@Service
public class UserRoleDataActionServiceImpl implements IUserRoleDataActionService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleDataActionClientService userRoleDataActionClientService;

    @Override
    public boolean save(UserRoleDataAction userroledataaction) {
        if(userroledataaction.getUserRoleDataActionId()==null){
    		return create(userroledataaction);
    	}else{
            if(this.get(userroledataaction.getUserRoleDataActionId())!=null){
    			return update(userroledataaction);
    		}
    		return create(userroledataaction);
    	}
    }

    @Override
    public boolean create(UserRoleDataAction userroledataaction) {
        boolean bOk = false ;
        IUserRoleDataAction clientModel = this.convert2Model(userroledataaction,null);
        userRoleDataActionClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroledataaction);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(UserRoleDataAction userroledataaction) {
        boolean bOk = false ;
        IUserRoleDataAction clientModel = this.convert2Model(userroledataaction,null);
        userRoleDataActionClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroledataaction) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public UserRoleDataAction getDraft(UserRoleDataAction userroledataaction) {
        return userroledataaction;
    }

    @Override
    public UserRoleDataAction get(String userroledataactionid) {
        IUserRoleDataAction clientModel = userRoleDataActionClientService.createModel();
        clientModel.setUserRoleDataActionId(userroledataactionid);
        userRoleDataActionClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleDataAction get(UserRoleDataAction userroledataaction) {
        UserRoleDataAction retuserroledataaction = this.get(userroledataaction.getUserRoleDataActionId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroledataaction.getClass(), userroledataaction.getClass(), false);
        copier.copy(retuserroledataaction, userroledataaction, null);
        return userroledataaction ;
    }

    @Override
    public boolean checkKey(UserRoleDataAction userroledataaction) {
        return true;
    }

    @Override
    public boolean remove(String userroledataactionid) {
        boolean bOk = false ;
        IUserRoleDataAction clientModel = userRoleDataActionClientService.createModel();
        clientModel.setUserRoleDataActionId(userroledataactionid);
        userRoleDataActionClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

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


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

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


    public IUserRoleDataAction convert2Model(UserRoleDataAction domain , IUserRoleDataAction model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleDataActionClientService.createModel() ;
        }

         if(domain.getUserRoleDataActionIdDirtyFlag())
            model.setUserRoleDataActionId(domain.getUserRoleDataActionId());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUserRoleDataActionNameDirtyFlag())
            model.setUserRoleDataActionName(domain.getUserRoleDataActionName());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserRoleDataNameDirtyFlag())
            model.setUserRoleDataName(domain.getUserRoleDataName());
         if(domain.getUserRoleDataIdDirtyFlag())
            model.setUserRoleDataId(domain.getUserRoleDataId());
        return model ;
    }

    public UserRoleDataAction convert2Domain( IUserRoleDataAction model ,UserRoleDataAction domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleDataAction();
        }

        if(model.getUserRoleDataActionIdDirtyFlag())
            domain.setUserRoleDataActionId(model.getUserRoleDataActionId());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUserRoleDataActionNameDirtyFlag())
            domain.setUserRoleDataActionName(model.getUserRoleDataActionName());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserRoleDataNameDirtyFlag())
            domain.setUserRoleDataName(model.getUserRoleDataName());
        if(model.getUserRoleDataIdDirtyFlag())
            domain.setUserRoleDataId(model.getUserRoleDataId());
        return domain ;
    }


}
