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.IORG;
import com.sinosig.core.external.domain.ORG;
import com.sinosig.core.external.filter.ORGSearchContext;
import com.sinosig.core.external.service.IORGService;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[ORG] 服务对象接口实现
 */
@Service
public class ORGServiceImpl implements IORGService {

    @Autowired
    private com.sinosig.core.client.service.IORGClientService oRGClientService;




    @Override
    public boolean checkKey(ORG org) {
        return true;
    }
    @Override
    public boolean create(ORG org) {
        boolean bOk = false ;
        IORG clientModel = this.convert2Model(org,null);
        oRGClientService.create(clientModel) ;
        this.convert2Domain(clientModel,org);
        bOk = true ;
        return bOk ;
    }

    @Override
    public ORG get(String orgid) {
        IORG clientModel = oRGClientService.createModel();
        clientModel.setORGId(orgid);
        oRGClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public ORG get(ORG org) {
        ORG retorg = this.get(org.getORGId()) ;
    	BeanCopier copier=BeanCopier.create(retorg.getClass(), org.getClass(), false);
        copier.copy(retorg, org, null);
        return org ;
    }

    @Override
    public boolean save(ORG org) {
        if(org.getORGId()==null){
    		return create(org);
    	}else{
            if(this.get(org.getORGId())!=null){
    			return update(org);
    		}
    		return create(org);
    	}
    }

    @Override
    public boolean remove(String orgid) {
        boolean bOk = false ;
        IORG clientModel = oRGClientService.createModel();
        clientModel.setORGId(orgid);
        oRGClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(ORG org) {
        boolean bOk = false ;
        IORG clientModel = this.convert2Model(org,null);
        oRGClientService.update(clientModel) ;
        this.convert2Domain(clientModel,org) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public ORG getDraft(ORG org) {
        return org;
    }

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

    /**
     * 查询集合 顶级组织
     */
    @Override
    public Page<ORG> searchTopOrg(ORGSearchContext context) {
        List<ORG> list = new ArrayList<ORG>();
        Page<com.sinosig.core.client.model.IORG> clientModelList = oRGClientService.fetchTopOrg(context) ;
        for(com.sinosig.core.client.model.IORG clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<ORG>(list, context.getPageable(), clientModelList.getTotalElements());
    }

    /**
     * 查询集合 当前组织
     */
    @Override
    public Page<ORG> searchCurOrg(ORGSearchContext context) {
        List<ORG> list = new ArrayList<ORG>();
        Page<com.sinosig.core.client.model.IORG> clientModelList = oRGClientService.fetchCurOrg(context) ;
        for(com.sinosig.core.client.model.IORG clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<ORG>(list, context.getPageable(), clientModelList.getTotalElements());
    }


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

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

        return null ;
    }


    public IORG convert2Model(ORG domain , IORG model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = oRGClientService.createModel() ;
        }

         if(domain.getORGIdDirtyFlag())
            model.setORGId(domain.getORGId());
         if(domain.getORGNameDirtyFlag())
            model.setORGName(domain.getORGName());
         if(domain.getPorgnameDirtyFlag())
            model.setPorgname(domain.getPorgname());
         if(domain.getPorgidDirtyFlag())
            model.setPorgid(domain.getPorgid());
        return model ;
    }

    public ORG convert2Domain( IORG model ,ORG domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new ORG();
        }

        if(model.getORGIdDirtyFlag())
            domain.setORGId(model.getORGId());
        if(model.getORGNameDirtyFlag())
            domain.setORGName(model.getORGName());
        if(model.getPorgnameDirtyFlag())
            domain.setPorgname(model.getPorgname());
        if(model.getPorgidDirtyFlag())
            domain.setPorgid(model.getPorgid());
        return domain ;
    }

}
