package com.ibiz.core.r7rt_org.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.ibiz.core.client.model.IOrg;
import com.ibiz.core.r7rt_org.domain.Org;
import com.ibiz.core.r7rt_org.filter.OrgSearchContext;
import com.ibiz.core.r7rt_org.service.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiz.util.SearchResult;

/**
 * 实体[组织] 服务对象接口实现
 */
@Service
public class OrgServiceImpl implements IOrgService {

    @Autowired
    private com.ibiz.core.client.service.IOrgClientService orgClientService;

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

    @Override
    public Org getOrgPrent(Org org) {
        IOrg clientModel = this.convert2Model(org,null);
        orgClientService.getOrgPrent(clientModel) ;
        this.convert2Domain(clientModel,null);
         return 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 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 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 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 Org syncToUserGroup(Org org) {
        IOrg clientModel = this.convert2Model(org,null);
        orgClientService.syncToUserGroup(clientModel) ;
        this.convert2Domain(clientModel,null);
         return org;
    }

    @Override
    public boolean checkKey(Org org) {
        return true;
    }

    @Override
    public Org getOrgChild(Org org) {
        IOrg clientModel = this.convert2Model(org,null);
        orgClientService.getOrgChild(clientModel) ;
        this.convert2Domain(clientModel,null);
         return org;
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<Org> searchDefault(OrgSearchContext context) {
        List<Org> list = new ArrayList<Org>();
        Page<com.ibiz.core.client.model.IOrg> clientModelList = orgClientService.fetchDefault(context) ;
        for(com.ibiz.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> selectByPOrg(String orgid){
        return null ;
    }

    /**
     * 根据组织查询
     */
    @Override
    public List<Org> selectByPOrg2(String orgid){
        return null ;
    }


    public IOrg convert2Model(Org domain , IOrg model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = orgClientService.createModel() ;
        }

         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getOrgNameDirtyFlag())
            model.setOrgName(domain.getOrgName());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getOrgCodeDirtyFlag())
            model.setOrgCode(domain.getOrgCode());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getLevelCodeDirtyFlag())
            model.setLevelCode(domain.getLevelCode());
         if(domain.getOrgIdDirtyFlag())
            model.setOrgId(domain.getOrgId());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         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.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getOrgNameDirtyFlag())
            domain.setOrgName(model.getOrgName());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getOrgCodeDirtyFlag())
            domain.setOrgCode(model.getOrgCode());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getLevelCodeDirtyFlag())
            domain.setLevelCode(model.getLevelCode());
        if(model.getOrgIdDirtyFlag())
            domain.setOrgId(model.getOrgId());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getPOrgNameDirtyFlag())
            domain.setPOrgName(model.getPOrgName());
        if(model.getPOrgIdDirtyFlag())
            domain.setPOrgId(model.getPOrgId());
        return domain ;
    }


}
