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.IUserRoleDetail;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleDetail;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDetailSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户角色明细] 服务对象接口实现
 */
@Service
public class UserRoleDetailServiceImpl implements IUserRoleDetailService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleDetailClientService userRoleDetailClientService;

    @Override
    public boolean remove(String userroledetailid) {
        boolean bOk = false ;
        IUserRoleDetail clientModel = userRoleDetailClientService.createModel();
        clientModel.setUserRoleDetailId(userroledetailid);
        userRoleDetailClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(UserRoleDetail userroledetail) {
        boolean bOk = false ;
        IUserRoleDetail clientModel = this.convert2Model(userroledetail,null);
        userRoleDetailClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroledetail) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean save(UserRoleDetail userroledetail) {
        if(userroledetail.getUserRoleDetailId()==null){
    		return create(userroledetail);
    	}else{
            if(this.get(userroledetail.getUserRoleDetailId())!=null){
    			return update(userroledetail);
    		}
    		return create(userroledetail);
    	}
    }

    @Override
    public boolean create(UserRoleDetail userroledetail) {
        boolean bOk = false ;
        IUserRoleDetail clientModel = this.convert2Model(userroledetail,null);
        userRoleDetailClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroledetail);
        bOk = true ;

        return bOk ;
    }

    @Override
    public UserRoleDetail getDraft(UserRoleDetail userroledetail) {
        return userroledetail;
    }

    @Override
    public boolean checkKey(UserRoleDetail userroledetail) {
        return true;
    }

    @Override
    public UserRoleDetail get(String userroledetailid) {
        IUserRoleDetail clientModel = userRoleDetailClientService.createModel();
        clientModel.setUserRoleDetailId(userroledetailid);
        userRoleDetailClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleDetail get(UserRoleDetail userroledetail) {
        UserRoleDetail retuserroledetail = this.get(userroledetail.getUserRoleDetailId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroledetail.getClass(), userroledetail.getClass(), false);
        copier.copy(retuserroledetail, userroledetail, null);
        return userroledetail ;
    }

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


    /**
     * 根据用户对象查询
     */
    @Override
    public Page<UserRoleDetail> selectByUserObject(String userobjectid){
        return null ;
    }

    /**
     * 根据用户对象查询
     */
    @Override
    public List<UserRoleDetail> selectByUserObject2(String userobjectid){
        return null ;
    }
    /**
     * 根据用户角色查询
     */
    @Override
    public Page<UserRoleDetail> selectByUserRole(String userroleid){
        return null ;
    }

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


    public IUserRoleDetail convert2Model(UserRoleDetail domain , IUserRoleDetail model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleDetailClientService.createModel() ;
        }

         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUserRoleDetailIdDirtyFlag())
            model.setUserRoleDetailId(domain.getUserRoleDetailId());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUserRoleDetailNameDirtyFlag())
            model.setUserRoleDetailName(domain.getUserRoleDetailName());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserObjectNameDirtyFlag())
            model.setUserObjectName(domain.getUserObjectName());
         if(domain.getUserRoleNameDirtyFlag())
            model.setUserRoleName(domain.getUserRoleName());
         if(domain.getUserObjectIdDirtyFlag())
            model.setUserObjectId(domain.getUserObjectId());
         if(domain.getUserRoleIdDirtyFlag())
            model.setUserRoleId(domain.getUserRoleId());
        return model ;
    }

    public UserRoleDetail convert2Domain( IUserRoleDetail model ,UserRoleDetail domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleDetail();
        }

        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUserRoleDetailIdDirtyFlag())
            domain.setUserRoleDetailId(model.getUserRoleDetailId());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUserRoleDetailNameDirtyFlag())
            domain.setUserRoleDetailName(model.getUserRoleDetailName());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserObjectNameDirtyFlag())
            domain.setUserObjectName(model.getUserObjectName());
        if(model.getUserRoleNameDirtyFlag())
            domain.setUserRoleName(model.getUserRoleName());
        if(model.getUserObjectIdDirtyFlag())
            domain.setUserObjectId(model.getUserObjectId());
        if(model.getUserRoleIdDirtyFlag())
            domain.setUserRoleId(model.getUserRoleId());
        return domain ;
    }


}
