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.IUserRoleRes;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleRes;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleResSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleResService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户角色资源能力] 服务对象接口实现
 */
@Service
public class UserRoleResServiceImpl implements IUserRoleResService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleResClientService userRoleResClientService;

    @Override
    public UserRoleRes getDraft(UserRoleRes userroleres) {
        return userroleres;
    }

    @Override
    public boolean checkKey(UserRoleRes userroleres) {
        return true;
    }

    @Override
    public boolean update(UserRoleRes userroleres) {
        boolean bOk = false ;
        IUserRoleRes clientModel = this.convert2Model(userroleres,null);
        userRoleResClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroleres) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public UserRoleRes get(String userroleresid) {
        IUserRoleRes clientModel = userRoleResClientService.createModel();
        clientModel.setUserRoleResId(userroleresid);
        userRoleResClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleRes get(UserRoleRes userroleres) {
        UserRoleRes retuserroleres = this.get(userroleres.getUserRoleResId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroleres.getClass(), userroleres.getClass(), false);
        copier.copy(retuserroleres, userroleres, null);
        return userroleres ;
    }

    @Override
    public boolean create(UserRoleRes userroleres) {
        boolean bOk = false ;
        IUserRoleRes clientModel = this.convert2Model(userroleres,null);
        userRoleResClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroleres);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean save(UserRoleRes userroleres) {
        if(userroleres.getUserRoleResId()==null){
    		return create(userroleres);
    	}else{
            if(this.get(userroleres.getUserRoleResId())!=null){
    			return update(userroleres);
    		}
    		return create(userroleres);
    	}
    }

    @Override
    public boolean remove(String userroleresid) {
        boolean bOk = false ;
        IUserRoleRes clientModel = userRoleResClientService.createModel();
        clientModel.setUserRoleResId(userroleresid);
        userRoleResClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

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


    /**
     * 根据统一资源查询
     */
    @Override
    public Page<UserRoleRes> selectByUniRes(String uniresid){
        return null ;
    }

    /**
     * 根据统一资源查询
     */
    @Override
    public List<UserRoleRes> selectByUniRes2(String uniresid){
        return null ;
    }
    /**
     * 根据用户角色查询
     */
    @Override
    public Page<UserRoleRes> selectByUserRole(String userroleid){
        return null ;
    }

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


    public IUserRoleRes convert2Model(UserRoleRes domain , IUserRoleRes model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleResClientService.createModel() ;
        }

         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUserRoleResIdDirtyFlag())
            model.setUserRoleResId(domain.getUserRoleResId());
         if(domain.getUserRoleResNameDirtyFlag())
            model.setUserRoleResName(domain.getUserRoleResName());
         if(domain.getUserRoleNameDirtyFlag())
            model.setUserRoleName(domain.getUserRoleName());
         if(domain.getUniResNameDirtyFlag())
            model.setUniResName(domain.getUniResName());
         if(domain.getUserRoleIdDirtyFlag())
            model.setUserRoleId(domain.getUserRoleId());
         if(domain.getUniResIdDirtyFlag())
            model.setUniResId(domain.getUniResId());
        return model ;
    }

    public UserRoleRes convert2Domain( IUserRoleRes model ,UserRoleRes domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleRes();
        }

        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUserRoleResIdDirtyFlag())
            domain.setUserRoleResId(model.getUserRoleResId());
        if(model.getUserRoleResNameDirtyFlag())
            domain.setUserRoleResName(model.getUserRoleResName());
        if(model.getUserRoleNameDirtyFlag())
            domain.setUserRoleName(model.getUserRoleName());
        if(model.getUniResNameDirtyFlag())
            domain.setUniResName(model.getUniResName());
        if(model.getUserRoleIdDirtyFlag())
            domain.setUserRoleId(model.getUserRoleId());
        if(model.getUniResIdDirtyFlag())
            domain.setUniResId(model.getUniResId());
        return domain ;
    }


}
