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.IApplication;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.Application;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.ApplicationSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[业务系统前端应用] 服务对象接口实现
 */
@Service
public class ApplicationServiceImpl implements IApplicationService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IApplicationClientService applicationClientService;

    @Override
    public boolean remove(String applicationid) {
        boolean bOk = false ;
        IApplication clientModel = applicationClientService.createModel();
        clientModel.setApplicationId(applicationid);
        applicationClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean checkKey(Application application) {
        return true;
    }

    @Override
    public boolean save(Application application) {
        if(application.getApplicationId()==null){
    		return create(application);
    	}else{
            if(this.get(application.getApplicationId())!=null){
    			return update(application);
    		}
    		return create(application);
    	}
    }

    @Override
    public Application getDraft(Application application) {
        return application;
    }

    @Override
    public boolean update(Application application) {
        boolean bOk = false ;
        IApplication clientModel = this.convert2Model(application,null);
        applicationClientService.update(clientModel) ;
        this.convert2Domain(clientModel,application) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Application get(String applicationid) {
        IApplication clientModel = applicationClientService.createModel();
        clientModel.setApplicationId(applicationid);
        applicationClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Application get(Application application) {
        Application retapplication = this.get(application.getApplicationId()) ;
    	BeanCopier copier=BeanCopier.create(retapplication.getClass(), application.getClass(), false);
        copier.copy(retapplication, application, null);
        return application ;
    }

    @Override
    public boolean create(Application application) {
        boolean bOk = false ;
        IApplication clientModel = this.convert2Model(application,null);
        applicationClientService.create(clientModel) ;
        this.convert2Domain(clientModel,application);
        bOk = true ;

        return bOk ;
    }

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


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

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


    public IApplication convert2Model(Application domain , IApplication model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = applicationClientService.createModel() ;
        }

         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getApplicationNameDirtyFlag())
            model.setApplicationName(domain.getApplicationName());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getApplicationIdDirtyFlag())
            model.setApplicationId(domain.getApplicationId());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getSystemIdDirtyFlag())
            model.setSystemId(domain.getSystemId());
        return model ;
    }

    public Application convert2Domain( IApplication model ,Application domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Application();
        }

        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getApplicationNameDirtyFlag())
            domain.setApplicationName(model.getApplicationName());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getApplicationIdDirtyFlag())
            domain.setApplicationId(model.getApplicationId());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getSystemIdDirtyFlag())
            domain.setSystemId(model.getSystemId());
        return domain ;
    }


}
