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.IUserRoleType;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleType;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleTypeSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户角色类型] 服务对象接口实现
 */
@Service
public class UserRoleTypeServiceImpl implements IUserRoleTypeService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleTypeClientService userRoleTypeClientService;

    @Override
    public boolean checkKey(UserRoleType userroletype) {
        return true;
    }

    @Override
    public UserRoleType getDraft(UserRoleType userroletype) {
        return userroletype;
    }

    @Override
    public boolean save(UserRoleType userroletype) {
        if(userroletype.getUserRoleTypeId()==null){
    		return create(userroletype);
    	}else{
            if(this.get(userroletype.getUserRoleTypeId())!=null){
    			return update(userroletype);
    		}
    		return create(userroletype);
    	}
    }

    @Override
    public boolean create(UserRoleType userroletype) {
        boolean bOk = false ;
        IUserRoleType clientModel = this.convert2Model(userroletype,null);
        userRoleTypeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroletype);
        bOk = true ;

        return bOk ;
    }

    @Override
    public UserRoleType get(String userroletypeid) {
        IUserRoleType clientModel = userRoleTypeClientService.createModel();
        clientModel.setUserRoleTypeId(userroletypeid);
        userRoleTypeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleType get(UserRoleType userroletype) {
        UserRoleType retuserroletype = this.get(userroletype.getUserRoleTypeId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroletype.getClass(), userroletype.getClass(), false);
        copier.copy(retuserroletype, userroletype, null);
        return userroletype ;
    }

    @Override
    public boolean remove(String userroletypeid) {
        boolean bOk = false ;
        IUserRoleType clientModel = userRoleTypeClientService.createModel();
        clientModel.setUserRoleTypeId(userroletypeid);
        userRoleTypeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(UserRoleType userroletype) {
        boolean bOk = false ;
        IUserRoleType clientModel = this.convert2Model(userroletype,null);
        userRoleTypeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroletype) ;
        bOk = true;
        return bOk ;
    }

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




    public IUserRoleType convert2Model(UserRoleType domain , IUserRoleType model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleTypeClientService.createModel() ;
        }

         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
         if(domain.getUserRoleTypeIdDirtyFlag())
            model.setUserRoleTypeId(domain.getUserRoleTypeId());
         if(domain.getUserRoleTypeNameDirtyFlag())
            model.setUserRoleTypeName(domain.getUserRoleTypeName());
        return model ;
    }

    public UserRoleType convert2Domain( IUserRoleType model ,UserRoleType domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleType();
        }

        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        if(model.getUserRoleTypeIdDirtyFlag())
            domain.setUserRoleTypeId(model.getUserRoleTypeId());
        if(model.getUserRoleTypeNameDirtyFlag())
            domain.setUserRoleTypeName(model.getUserRoleTypeName());
        return domain ;
    }


}
