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.IUniRes;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UniRes;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UniResSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUniResService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[统一资源] 服务对象接口实现
 */
@Service
public class UniResServiceImpl implements IUniResService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUniResClientService uniResClientService;

    @Override
    public boolean remove(String uniresid) {
        boolean bOk = false ;
        IUniRes clientModel = uniResClientService.createModel();
        clientModel.setUniResId(uniresid);
        uniResClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean save(UniRes unires) {
        if(unires.getUniResId()==null){
    		return create(unires);
    	}else{
            if(this.get(unires.getUniResId())!=null){
    			return update(unires);
    		}
    		return create(unires);
    	}
    }

    @Override
    public boolean update(UniRes unires) {
        boolean bOk = false ;
        IUniRes clientModel = this.convert2Model(unires,null);
        uniResClientService.update(clientModel) ;
        this.convert2Domain(clientModel,unires) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public UniRes get(String uniresid) {
        IUniRes clientModel = uniResClientService.createModel();
        clientModel.setUniResId(uniresid);
        uniResClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UniRes get(UniRes unires) {
        UniRes retunires = this.get(unires.getUniResId()) ;
    	BeanCopier copier=BeanCopier.create(retunires.getClass(), unires.getClass(), false);
        copier.copy(retunires, unires, null);
        return unires ;
    }

    @Override
    public boolean create(UniRes unires) {
        boolean bOk = false ;
        IUniRes clientModel = this.convert2Model(unires,null);
        uniResClientService.create(clientModel) ;
        this.convert2Domain(clientModel,unires);
        bOk = true ;

        return bOk ;
    }

    @Override
    public UniRes getDraft(UniRes unires) {
        return unires;
    }

    @Override
    public UniRes getResource(UniRes unires) {
        
        IUniRes clientModel = this.convert2Model(unires,null);
        uniResClientService.getResource(clientModel) ;
        this.convert2Domain(clientModel,null);
        return unires;
    }

    @Override
    public boolean checkKey(UniRes unires) {
        return true;
    }

    /**
     * 查询集合 查询全部实体结果
     */
    @Override
    public Page<UniRes> searchSelectUniresAll(UniResSearchContext context) {
        List<UniRes> list = new ArrayList<UniRes>();
        Page<cn.ibizlab.r7.runtime.core.client.model.IUniRes> clientModelList = uniResClientService.fetchSelectUniresAll(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.IUniRes clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<UniRes>(list, context.getPageable(), clientModelList.getTotalElements());
    }

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


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

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


    public IUniRes convert2Model(UniRes domain , IUniRes model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = uniResClientService.createModel() ;
        }

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

    public UniRes convert2Domain( IUniRes model ,UniRes domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UniRes();
        }

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


}
