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.IUserGroupDetail;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserGroupDetail;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserGroupDetailSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserGroupDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户对象组明细] 服务对象接口实现
 */
@Service
public class UserGroupDetailServiceImpl implements IUserGroupDetailService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserGroupDetailClientService userGroupDetailClientService;

    @Override
    public UserGroupDetail get(String usergroupdetailid) {
        IUserGroupDetail clientModel = userGroupDetailClientService.createModel();
        clientModel.setUserGroupDetailId(usergroupdetailid);
        userGroupDetailClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserGroupDetail get(UserGroupDetail usergroupdetail) {
        UserGroupDetail retusergroupdetail = this.get(usergroupdetail.getUserGroupDetailId()) ;
    	BeanCopier copier=BeanCopier.create(retusergroupdetail.getClass(), usergroupdetail.getClass(), false);
        copier.copy(retusergroupdetail, usergroupdetail, null);
        return usergroupdetail ;
    }

    @Override
    public UserGroupDetail getDraft(UserGroupDetail usergroupdetail) {
        return usergroupdetail;
    }

    @Override
    public boolean create(UserGroupDetail usergroupdetail) {
        boolean bOk = false ;
        IUserGroupDetail clientModel = this.convert2Model(usergroupdetail,null);
        userGroupDetailClientService.create(clientModel) ;
        this.convert2Domain(clientModel,usergroupdetail);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean save(UserGroupDetail usergroupdetail) {
        if(usergroupdetail.getUserGroupDetailId()==null){
    		return create(usergroupdetail);
    	}else{
            if(this.get(usergroupdetail.getUserGroupDetailId())!=null){
    			return update(usergroupdetail);
    		}
    		return create(usergroupdetail);
    	}
    }

    @Override
    public boolean checkKey(UserGroupDetail usergroupdetail) {
        return true;
    }

    @Override
    public boolean update(UserGroupDetail usergroupdetail) {
        boolean bOk = false ;
        IUserGroupDetail clientModel = this.convert2Model(usergroupdetail,null);
        userGroupDetailClientService.update(clientModel) ;
        this.convert2Domain(clientModel,usergroupdetail) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(String usergroupdetailid) {
        boolean bOk = false ;
        IUserGroupDetail clientModel = userGroupDetailClientService.createModel();
        clientModel.setUserGroupDetailId(usergroupdetailid);
        userGroupDetailClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

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


    /**
     * 根据用户对象组查询
     */
    @Override
    public Page<UserGroupDetail> selectByUserGroup(String usergroupid){
        return null ;
    }

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

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


    public IUserGroupDetail convert2Model(UserGroupDetail domain , IUserGroupDetail model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userGroupDetailClientService.createModel() ;
        }

         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserGroupDetailNameDirtyFlag())
            model.setUserGroupDetailName(domain.getUserGroupDetailName());
         if(domain.getUserGroupDetailIdDirtyFlag())
            model.setUserGroupDetailId(domain.getUserGroupDetailId());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUserObjectNameDirtyFlag())
            model.setUserObjectName(domain.getUserObjectName());
         if(domain.getUserGroupNameDirtyFlag())
            model.setUserGroupName(domain.getUserGroupName());
         if(domain.getUserGroupIdDirtyFlag())
            model.setUserGroupId(domain.getUserGroupId());
         if(domain.getUserObjectIdDirtyFlag())
            model.setUserObjectId(domain.getUserObjectId());
        return model ;
    }

    public UserGroupDetail convert2Domain( IUserGroupDetail model ,UserGroupDetail domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserGroupDetail();
        }

        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserGroupDetailNameDirtyFlag())
            domain.setUserGroupDetailName(model.getUserGroupDetailName());
        if(model.getUserGroupDetailIdDirtyFlag())
            domain.setUserGroupDetailId(model.getUserGroupDetailId());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUserObjectNameDirtyFlag())
            domain.setUserObjectName(model.getUserObjectName());
        if(model.getUserGroupNameDirtyFlag())
            domain.setUserGroupName(model.getUserGroupName());
        if(model.getUserGroupIdDirtyFlag())
            domain.setUserGroupId(model.getUserGroupId());
        if(model.getUserObjectIdDirtyFlag())
            domain.setUserObjectId(model.getUserObjectId());
        return domain ;
    }


}
