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.ISystem;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.System;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.SystemSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[业务系统] 服务对象接口实现
 */
@Service
public class SystemServiceImpl implements ISystemService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.ISystemClientService systemClientService;

    @Override
    public boolean create(System system) {
        boolean bOk = false ;
        ISystem clientModel = this.convert2Model(system,null);
        systemClientService.create(clientModel) ;
        this.convert2Domain(clientModel,system);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(System system) {
        boolean bOk = false ;
        ISystem clientModel = this.convert2Model(system,null);
        systemClientService.update(clientModel) ;
        this.convert2Domain(clientModel,system) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public System get(String systemid) {
        ISystem clientModel = systemClientService.createModel();
        clientModel.setSystemId(systemid);
        systemClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public System get(System system) {
        System retsystem = this.get(system.getSystemId()) ;
    	BeanCopier copier=BeanCopier.create(retsystem.getClass(), system.getClass(), false);
        copier.copy(retsystem, system, null);
        return system ;
    }

    @Override
    public boolean remove(String systemid) {
        boolean bOk = false ;
        ISystem clientModel = systemClientService.createModel();
        clientModel.setSystemId(systemid);
        systemClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean save(System system) {
        if(system.getSystemId()==null){
    		return create(system);
    	}else{
            if(this.get(system.getSystemId())!=null){
    			return update(system);
    		}
    		return create(system);
    	}
    }

    @Override
    public boolean checkKey(System system) {
        return true;
    }

    @Override
    public System getDraft(System system) {
        return system;
    }

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




    public ISystem convert2Model(System domain , ISystem model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = systemClientService.createModel() ;
        }

         if(domain.getSystemNameDirtyFlag())
            model.setSystemName(domain.getSystemName());
         if(domain.getSystemIdDirtyFlag())
            model.setSystemId(domain.getSystemId());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
        return model ;
    }

    public System convert2Domain( ISystem model ,System domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new System();
        }

        if(model.getSystemNameDirtyFlag())
            domain.setSystemName(model.getSystemName());
        if(model.getSystemIdDirtyFlag())
            domain.setSystemId(model.getSystemId());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        return domain ;
    }


}
