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.IUserRole;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRole;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户角色] 服务对象接口实现
 */
@Service
public class UserRoleServiceImpl implements IUserRoleService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleClientService userRoleClientService;

    @Override
    public boolean create(UserRole userrole) {
        boolean bOk = false ;
        IUserRole clientModel = this.convert2Model(userrole,null);
        userRoleClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userrole);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean save(UserRole userrole) {
        if(userrole.getUserRoleId()==null){
    		return create(userrole);
    	}else{
            if(this.get(userrole.getUserRoleId())!=null){
    			return update(userrole);
    		}
    		return create(userrole);
    	}
    }

    @Override
    public boolean update(UserRole userrole) {
        boolean bOk = false ;
        IUserRole clientModel = this.convert2Model(userrole,null);
        userRoleClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userrole) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean checkKey(UserRole userrole) {
        return true;
    }

    @Override
    public UserRole get(String userroleid) {
        IUserRole clientModel = userRoleClientService.createModel();
        clientModel.setUserRoleId(userroleid);
        userRoleClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRole get(UserRole userrole) {
        UserRole retuserrole = this.get(userrole.getUserRoleId()) ;
    	BeanCopier copier=BeanCopier.create(retuserrole.getClass(), userrole.getClass(), false);
        copier.copy(retuserrole, userrole, null);
        return userrole ;
    }

    @Override
    public boolean remove(String userroleid) {
        boolean bOk = false ;
        IUserRole clientModel = userRoleClientService.createModel();
        clientModel.setUserRoleId(userroleid);
        userRoleClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public UserRole getDraft(UserRole userrole) {
        return userrole;
    }

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


    /**
     * 根据业务系统查询
     */
    @Override
    public Page<UserRole> selectBySystem(String systemid){
        return null ;
    }

    /**
     * 根据业务系统查询
     */
    @Override
    public List<UserRole> selectBySystem2(String systemid){
        return null ;
    }


    public IUserRole convert2Model(UserRole domain , IUserRole model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleClientService.createModel() ;
        }

         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
         if(domain.getUserRoleIdDirtyFlag())
            model.setUserRoleId(domain.getUserRoleId());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getDataSourceTypeDirtyFlag())
            model.setDataSourceType(domain.getDataSourceType());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUserRoleNameDirtyFlag())
            model.setUserRoleName(domain.getUserRoleName());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserRoleTypeDirtyFlag())
            model.setUserRoleType(domain.getUserRoleType());
         if(domain.getSystemNameDirtyFlag())
            model.setSystemName(domain.getSystemName());
         if(domain.getSystemIdDirtyFlag())
            model.setSystemId(domain.getSystemId());
        return model ;
    }

    public UserRole convert2Domain( IUserRole model ,UserRole domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRole();
        }

        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        if(model.getUserRoleIdDirtyFlag())
            domain.setUserRoleId(model.getUserRoleId());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getDataSourceTypeDirtyFlag())
            domain.setDataSourceType(model.getDataSourceType());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUserRoleNameDirtyFlag())
            domain.setUserRoleName(model.getUserRoleName());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserRoleTypeDirtyFlag())
            domain.setUserRoleType(model.getUserRoleType());
        if(model.getSystemNameDirtyFlag())
            domain.setSystemName(model.getSystemName());
        if(model.getSystemIdDirtyFlag())
            domain.setSystemId(model.getSystemId());
        return domain ;
    }


}
