package com.sinosig.core.external.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 com.sinosig.core.client.model.IORGUSER;
import com.sinosig.core.external.domain.ORGUSER;
import com.sinosig.core.external.filter.ORGUSERSearchContext;
import com.sinosig.core.external.service.IORGUSERService;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[ORGUSER] 服务对象接口实现
 */
@Service
public class ORGUSERServiceImpl implements IORGUSERService {

    @Autowired
    private com.sinosig.core.client.service.IORGUSERClientService oRGUSERClientService;




    @Override
    public boolean create(ORGUSER orguser) {
        boolean bOk = false ;
        IORGUSER clientModel = this.convert2Model(orguser,null);
        oRGUSERClientService.create(clientModel) ;
        this.convert2Domain(clientModel,orguser);
        bOk = true ;
        return bOk ;
    }

    @Override
    public ORGUSER getDraft(ORGUSER orguser) {
        return orguser;
    }

    @Override
    public boolean update(ORGUSER orguser) {
        boolean bOk = false ;
        IORGUSER clientModel = this.convert2Model(orguser,null);
        oRGUSERClientService.update(clientModel) ;
        this.convert2Domain(clientModel,orguser) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public ORGUSER get(String orguserid) {
        IORGUSER clientModel = oRGUSERClientService.createModel();
        clientModel.setORGUSERId(orguserid);
        oRGUSERClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public ORGUSER get(ORGUSER orguser) {
        ORGUSER retorguser = this.get(orguser.getORGUSERId()) ;
    	BeanCopier copier=BeanCopier.create(retorguser.getClass(), orguser.getClass(), false);
        copier.copy(retorguser, orguser, null);
        return orguser ;
    }

    @Override
    public boolean checkKey(ORGUSER orguser) {
        return true;
    }
    @Override
    public boolean remove(String orguserid) {
        boolean bOk = false ;
        IORGUSER clientModel = oRGUSERClientService.createModel();
        clientModel.setORGUSERId(orguserid);
        oRGUSERClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean save(ORGUSER orguser) {
        if(orguser.getORGUSERId()==null){
    		return create(orguser);
    	}else{
            if(this.get(orguser.getORGUSERId())!=null){
    			return update(orguser);
    		}
    		return create(orguser);
    	}
    }

    /**
     * 查询集合 获取当前用户
     */
    @Override
    public Page<ORGUSER> searchCurOrgUser(ORGUSERSearchContext context) {
        List<ORGUSER> list = new ArrayList<ORGUSER>();
        Page<com.sinosig.core.client.model.IORGUSER> clientModelList = oRGUSERClientService.fetchCurOrgUser(context) ;
        for(com.sinosig.core.client.model.IORGUSER clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<ORGUSER>(list, context.getPageable(), clientModelList.getTotalElements());
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<ORGUSER> searchDefault(ORGUSERSearchContext context) {
        List<ORGUSER> list = new ArrayList<ORGUSER>();
        Page<com.sinosig.core.client.model.IORGUSER> clientModelList = oRGUSERClientService.fetchDefault(context) ;
        for(com.sinosig.core.client.model.IORGUSER clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<ORGUSER>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据机构查询
     */
    @Override
    public Page<ORGUSER> selectByORG(String orgid ){
        return null ;
    }

    /**
     * 根据机构查询
     */
    @Override
    public List<ORGUSER> selectByORG2(String orgid ){

        return null ;
    }


    public IORGUSER convert2Model(ORGUSER domain , IORGUSER model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = oRGUSERClientService.createModel() ;
        }

         if(domain.getORGUSERNameDirtyFlag())
            model.setORGUSERName(domain.getORGUSERName());
         if(domain.getORGUSERIdDirtyFlag())
            model.setORGUSERId(domain.getORGUSERId());
        return model ;
    }

    public ORGUSER convert2Domain( IORGUSER model ,ORGUSER domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new ORGUSER();
        }

        if(model.getORGUSERNameDirtyFlag())
            domain.setORGUSERName(model.getORGUSERName());
        if(model.getORGUSERIdDirtyFlag())
            domain.setORGUSERId(model.getORGUSERId());
        return domain ;
    }

}
