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.IDataEntity;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.DataEntity;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.DataEntitySearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IDataEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[业务实体] 服务对象接口实现
 */
@Service
public class DataEntityServiceImpl implements IDataEntityService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IDataEntityClientService dataEntityClientService;

    @Override
    public boolean remove(String dataentityid) {
        boolean bOk = false ;
        IDataEntity clientModel = dataEntityClientService.createModel();
        clientModel.setDataEntityId(dataentityid);
        dataEntityClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(DataEntity dataentity) {
        boolean bOk = false ;
        IDataEntity clientModel = this.convert2Model(dataentity,null);
        dataEntityClientService.create(clientModel) ;
        this.convert2Domain(clientModel,dataentity);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(DataEntity dataentity) {
        boolean bOk = false ;
        IDataEntity clientModel = this.convert2Model(dataentity,null);
        dataEntityClientService.update(clientModel) ;
        this.convert2Domain(clientModel,dataentity) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public DataEntity initAll(DataEntity dataentity) {
        
        IDataEntity clientModel = this.convert2Model(dataentity,null);
        dataEntityClientService.initAll(clientModel) ;
        this.convert2Domain(clientModel,null);
        return dataentity;
    }

    @Override
    public DataEntity getDraft(DataEntity dataentity) {
        return dataentity;
    }

    @Override
    public DataEntity get(String dataentityid) {
        IDataEntity clientModel = dataEntityClientService.createModel();
        clientModel.setDataEntityId(dataentityid);
        dataEntityClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public DataEntity get(DataEntity dataentity) {
        DataEntity retdataentity = this.get(dataentity.getDataEntityId()) ;
    	BeanCopier copier=BeanCopier.create(retdataentity.getClass(), dataentity.getClass(), false);
        copier.copy(retdataentity, dataentity, null);
        return dataentity ;
    }

    @Override
    public boolean save(DataEntity dataentity) {
        if(dataentity.getDataEntityId()==null){
    		return create(dataentity);
    	}else{
            if(this.get(dataentity.getDataEntityId())!=null){
    			return update(dataentity);
    		}
    		return create(dataentity);
    	}
    }

    @Override
    public boolean checkKey(DataEntity dataentity) {
        return true;
    }

    /**
     * 查询集合 SelectDEAll
     */
    @Override
    public Page<DataEntity> searchSelectDEAll(DataEntitySearchContext context) {
        List<DataEntity> list = new ArrayList<DataEntity>();
        Page<cn.ibizlab.r7.runtime.core.client.model.IDataEntity> clientModelList = dataEntityClientService.fetchSelectDEAll(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.IDataEntity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<DataEntity>(list, context.getPageable(), clientModelList.getTotalElements());
    }

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


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

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


    public IDataEntity convert2Model(DataEntity domain , IDataEntity model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = dataEntityClientService.createModel() ;
        }

         if(domain.getRelationalDataDirtyFlag())
            model.setRelationalData(domain.getRelationalData());
         if(domain.getDataEntityIdDirtyFlag())
            model.setDataEntityId(domain.getDataEntityId());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getDataEntityNameDirtyFlag())
            model.setDataEntityName(domain.getDataEntityName());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getSystemNameDirtyFlag())
            model.setSystemName(domain.getSystemName());
         if(domain.getSystemIdDirtyFlag())
            model.setSystemId(domain.getSystemId());
        return model ;
    }

    public DataEntity convert2Domain( IDataEntity model ,DataEntity domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new DataEntity();
        }

        if(model.getRelationalDataDirtyFlag())
            domain.setRelationalData(model.getRelationalData());
        if(model.getDataEntityIdDirtyFlag())
            domain.setDataEntityId(model.getDataEntityId());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getDataEntityNameDirtyFlag())
            domain.setDataEntityName(model.getDataEntityName());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getSystemNameDirtyFlag())
            domain.setSystemName(model.getSystemName());
        if(model.getSystemIdDirtyFlag())
            domain.setSystemId(model.getSystemId());
        return domain ;
    }


}
