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.IUserObject;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.UserObject;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserObjectSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户对象] 服务对象接口实现
 */
@Service
public class UserObjectServiceImpl implements IUserObjectService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserObjectClientService userObjectClientService;

    @Override
    public boolean checkKey(UserObject userobject) {
        return true;
    }

    @Override
    public boolean save(UserObject userobject) {
        if(userobject.getUserObjectId()==null){
    		return create(userobject);
    	}else{
            if(this.get(userobject.getUserObjectId())!=null){
    			return update(userobject);
    		}
    		return create(userobject);
    	}
    }

    @Override
    public boolean create(UserObject userobject) {
        boolean bOk = false ;
        IUserObject clientModel = this.convert2Model(userobject,null);
        userObjectClientService.create(clientModel) ;
        this.convert2Domain(clientModel,userobject);
        bOk = true ;

        return bOk ;
    }

    @Override
    public UserObject getDraft(UserObject userobject) {
        return userobject;
    }

    @Override
    public boolean remove(String userobjectid) {
        boolean bOk = false ;
        IUserObject clientModel = userObjectClientService.createModel();
        clientModel.setUserObjectId(userobjectid);
        userObjectClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public UserObject get(String userobjectid) {
        IUserObject clientModel = userObjectClientService.createModel();
        clientModel.setUserObjectId(userobjectid);
        userObjectClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public UserObject get(UserObject userobject) {
        UserObject retuserobject = this.get(userobject.getUserObjectId()) ;
    	BeanCopier copier=BeanCopier.create(retuserobject.getClass(), userobject.getClass(), false);
        copier.copy(retuserobject, userobject, null);
        return userobject ;
    }

    @Override
    public boolean update(UserObject userobject) {
        boolean bOk = false ;
        IUserObject clientModel = this.convert2Model(userobject,null);
        userObjectClientService.update(clientModel) ;
        this.convert2Domain(clientModel,userobject) ;
        bOk = true;
        return bOk ;
    }

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




    public IUserObject convert2Model(UserObject domain , IUserObject model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userObjectClientService.createModel() ;
        }

         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUserObjectTypeDirtyFlag())
            model.setUserObjectType(domain.getUserObjectType());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUserObjectIdDirtyFlag())
            model.setUserObjectId(domain.getUserObjectId());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUserObjectNameDirtyFlag())
            model.setUserObjectName(domain.getUserObjectName());
         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
        return model ;
    }

    public UserObject convert2Domain( IUserObject model ,UserObject domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new UserObject();
        }

        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUserObjectTypeDirtyFlag())
            domain.setUserObjectType(model.getUserObjectType());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUserObjectIdDirtyFlag())
            domain.setUserObjectId(model.getUserObjectId());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUserObjectNameDirtyFlag())
            domain.setUserObjectName(model.getUserObjectName());
        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        return domain ;
    }


}
