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.IUserRoleData;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserRoleData;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserRoleDataSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserRoleDataService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[数据对象能力] 服务对象接口实现
 */
@Service
public class UserRoleDataServiceImpl implements IUserRoleDataService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserRoleDataClientService userRoleDataClientService;

    @Override
    public boolean checkKey(UserRoleData userroledata) {
        return true;
    }

    @Override
    public UserRoleData getDraft(UserRoleData userroledata) {
        return userroledata;
    }

    @Override
    public UserRoleData getUserRoleDataAPI(UserRoleData userroledata) {
        IUserRoleData clientModel = this.convert2Model(userroledata,null);
        userRoleDataClientService.getUserRoleDataAPI(clientModel) ;
        this.convert2Domain(clientModel,null);
         return userroledata;
    }

    @Override
    public UserRoleData get(String userroledataid) {
        IUserRoleData clientModel = userRoleDataClientService.createModel();
        clientModel.setUserRoleDataId(userroledataid);
        userRoleDataClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserRoleData get(UserRoleData userroledata) {
        UserRoleData retuserroledata = this.get(userroledata.getUserRoleDataId()) ;
    	BeanCopier copier=BeanCopier.create(retuserroledata.getClass(), userroledata.getClass(), false);
        copier.copy(retuserroledata, userroledata, null);
        return userroledata ;
    }

    @Override
    public boolean remove(String userroledataid) {
        boolean bOk = false ;
        IUserRoleData clientModel = userRoleDataClientService.createModel();
        clientModel.setUserRoleDataId(userroledataid);
        userRoleDataClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public UserRoleData hasPermissionGV(UserRoleData userroledata) {
        IUserRoleData clientModel = this.convert2Model(userroledata,null);
        userRoleDataClientService.hasPermissionGV(clientModel) ;
        this.convert2Domain(clientModel,null);
         return userroledata;
    }

    @Override
    public boolean create(UserRoleData userroledata) {
        boolean bOk = false ;
        IUserRoleData clientModel = this.convert2Model(userroledata,null);
        userRoleDataClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userroledata);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(UserRoleData userroledata) {
        boolean bOk = false ;
        IUserRoleData clientModel = this.convert2Model(userroledata,null);
        userRoleDataClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userroledata) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public UserRoleData hasPermissionEV(UserRoleData userroledata) {
        IUserRoleData clientModel = this.convert2Model(userroledata,null);
        userRoleDataClientService.hasPermissionEV(clientModel) ;
        this.convert2Domain(clientModel,null);
         return userroledata;
    }

    @Override
    public boolean save(UserRoleData userroledata) {
        if(userroledata.getUserRoleDataId()==null){
    		return create(userroledata);
    	}else{
            if(this.get(userroledata.getUserRoleDataId())!=null){
    			return update(userroledata);
    		}
    		return create(userroledata);
    	}
    }

    @Override
    public UserRoleData syncData(UserRoleData userroledata) {
        IUserRoleData clientModel = this.convert2Model(userroledata,null);
        userRoleDataClientService.syncData(clientModel) ;
        this.convert2Domain(clientModel,null);
         return userroledata;
    }

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


    /**
     * 根据业务实体查询
     */
    @Override
    public Page<UserRoleData> selectByDataEntity(String dataentityid){
        return null ;
    }

    /**
     * 根据业务实体查询
     */
    @Override
    public List<UserRoleData> selectByDataEntity2(String dataentityid){
        return null ;
    }


    public IUserRoleData convert2Model(UserRoleData domain , IUserRoleData model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userRoleDataClientService.createModel() ;
        }

         if(domain.getUserRoleDataIdDirtyFlag())
            model.setUserRoleDataId(domain.getUserRoleDataId());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getDeptDRDirtyFlag())
            model.setDeptDR(domain.getDeptDR());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUserRoleDataNameDirtyFlag())
            model.setUserRoleDataName(domain.getUserRoleDataName());
         if(domain.getOrgDRDirtyFlag())
            model.setOrgDR(domain.getOrgDR());
         if(domain.getCondcontentDirtyFlag())
            model.setCondcontent(domain.getCondcontent());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUserDRDirtyFlag())
            model.setUserDR(domain.getUserDR());
         if(domain.getIsAllDataDirtyFlag())
            model.setIsAllData(domain.getIsAllData());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getSysPresetQueryDirtyFlag())
            model.setSysPresetQuery(domain.getSysPresetQuery());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getDataEntityNameDirtyFlag())
            model.setDataEntityName(domain.getDataEntityName());
         if(domain.getDataEntityIdDirtyFlag())
            model.setDataEntityId(domain.getDataEntityId());
        return model ;
    }

    public UserRoleData convert2Domain( IUserRoleData model ,UserRoleData domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserRoleData();
        }

        if(model.getUserRoleDataIdDirtyFlag())
            domain.setUserRoleDataId(model.getUserRoleDataId());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getDeptDRDirtyFlag())
            domain.setDeptDR(model.getDeptDR());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUserRoleDataNameDirtyFlag())
            domain.setUserRoleDataName(model.getUserRoleDataName());
        if(model.getOrgDRDirtyFlag())
            domain.setOrgDR(model.getOrgDR());
        if(model.getCondcontentDirtyFlag())
            domain.setCondcontent(model.getCondcontent());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUserDRDirtyFlag())
            domain.setUserDR(model.getUserDR());
        if(model.getIsAllDataDirtyFlag())
            domain.setIsAllData(model.getIsAllData());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getSysPresetQueryDirtyFlag())
            domain.setSysPresetQuery(model.getSysPresetQuery());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getDataEntityNameDirtyFlag())
            domain.setDataEntityName(model.getDataEntityName());
        if(model.getDataEntityIdDirtyFlag())
            domain.setDataEntityId(model.getDataEntityId());
        return domain ;
    }


}
