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.IUserGroup;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserGroup;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserGroupSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户对象组] 服务对象接口实现
 */
@Service
public class UserGroupServiceImpl implements IUserGroupService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserGroupClientService userGroupClientService;

    @Override
    public boolean create(UserGroup usergroup) {
        boolean bOk = false ;
        IUserGroup clientModel = this.convert2Model(usergroup,null);
        userGroupClientService.create(clientModel) ;
        this.convert2Domain(clientModel,usergroup);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean save(UserGroup usergroup) {
        if(usergroup.getUserGroupId()==null){
    		return create(usergroup);
    	}else{
            if(this.get(usergroup.getUserGroupId())!=null){
    			return update(usergroup);
    		}
    		return create(usergroup);
    	}
    }

    @Override
    public boolean checkKey(UserGroup usergroup) {
        return true;
    }

    @Override
    public UserGroup getDraft(UserGroup usergroup) {
        return usergroup;
    }

    @Override
    public UserGroup get(String usergroupid) {
        IUserGroup clientModel = userGroupClientService.createModel();
        clientModel.setUserGroupId(usergroupid);
        userGroupClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserGroup get(UserGroup usergroup) {
        UserGroup retusergroup = this.get(usergroup.getUserGroupId()) ;
    	BeanCopier copier=BeanCopier.create(retusergroup.getClass(), usergroup.getClass(), false);
        copier.copy(retusergroup, usergroup, null);
        return usergroup ;
    }

    @Override
    public boolean update(UserGroup usergroup) {
        boolean bOk = false ;
        IUserGroup clientModel = this.convert2Model(usergroup,null);
        userGroupClientService.update(clientModel) ;
        this.convert2Domain(clientModel,usergroup) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(String usergroupid) {
        boolean bOk = false ;
        IUserGroup clientModel = userGroupClientService.createModel();
        clientModel.setUserGroupId(usergroupid);
        userGroupClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

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


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

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


    public IUserGroup convert2Model(UserGroup domain , IUserGroup model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userGroupClientService.createModel() ;
        }

         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
         if(domain.getUserObjectTypeDirtyFlag())
            model.setUserObjectType(domain.getUserObjectType());
         if(domain.getDataSourceTypeDirtyFlag())
            model.setDataSourceType(domain.getDataSourceType());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUserGroupNameDirtyFlag())
            model.setUserGroupName(domain.getUserGroupName());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserGroupIdDirtyFlag())
            model.setUserGroupId(domain.getUserGroupId());
        return model ;
    }

    public UserGroup convert2Domain( IUserGroup model ,UserGroup domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserGroup();
        }

        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        if(model.getUserObjectTypeDirtyFlag())
            domain.setUserObjectType(model.getUserObjectType());
        if(model.getDataSourceTypeDirtyFlag())
            domain.setDataSourceType(model.getDataSourceType());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUserGroupNameDirtyFlag())
            domain.setUserGroupName(model.getUserGroupName());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserGroupIdDirtyFlag())
            domain.setUserGroupId(model.getUserGroupId());
        return domain ;
    }


}
