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

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

    @Autowired
    com.ibiz.core.repository.ORGRepository oRGRepository;




    @Override
    public boolean checkKey(ORG org) {
        return true;
    }
    @Override
    public boolean create(ORG org) {
        boolean bOk = false ;
        com.ibiz.core.repository.po.ORG t_ORG = oRGRepository.convert2PO(org,null);
        bOk = oRGRepository.create(t_ORG);
        oRGRepository.convert2Domain(t_ORG,org);

        return bOk ;
    }

    @Override
    public ORG get(String orgid) {
        com.ibiz.core.repository.po.ORG t_ORG = oRGRepository.get(orgid);
        return oRGRepository.convert2Domain(t_ORG,null);
    }

    @Override
    public ORG get(ORG org) {
        com.ibiz.core.repository.po.ORG t_ORG = oRGRepository.get(org.getORGId());
        oRGRepository.convert2Domain(t_ORG,org);
        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 ;
        bOk = oRGRepository.remove(orgid);

        return bOk ;
    }

    @Override
    public boolean update(ORG org) {
        boolean bOk = false ;
        com.ibiz.core.repository.po.ORG t_ORG = oRGRepository.convert2PO(org,null);
        bOk = oRGRepository.update(t_ORG);
        oRGRepository.convert2Domain(t_ORG,org) ;

        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.ibiz.core.repository.po.ORG> t_ORGs = oRGRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.ORG t_ORG : t_ORGs){
            list.add(oRGRepository.convert2Domain(t_ORG,null));
        }
        return new PageImpl<ORG>(list, context.getPageable(), t_ORGs.getTotalElements());
    }

    /**
     * 查询集合 顶级组织
     */
    @Override
    public Page<ORG> searchTopOrg(ORGSearchContext context) {
        List<ORG> list = new ArrayList<ORG>();
        Page<com.ibiz.core.repository.po.ORG> t_ORGs = oRGRepository.searchTopOrg(context) ;
        for(com.ibiz.core.repository.po.ORG t_ORG : t_ORGs){
            list.add(oRGRepository.convert2Domain(t_ORG,null));
        }
        return new PageImpl<ORG>(list, context.getPageable(), t_ORGs.getTotalElements());
    }

    /**
     * 查询集合 当前组织
     */
    @Override
    public Page<ORG> searchCurOrg(ORGSearchContext context) {
        List<ORG> list = new ArrayList<ORG>();
        Page<com.ibiz.core.repository.po.ORG> t_ORGs = oRGRepository.searchCurOrg(context) ;
        for(com.ibiz.core.repository.po.ORG t_ORG : t_ORGs){
            list.add(oRGRepository.convert2Domain(t_ORG,null));
        }
        return new PageImpl<ORG>(list, context.getPageable(), t_ORGs.getTotalElements());
    }


    /**
     * 根据机构查询
     */
    @Override
    public Page<ORG> selectByPORG(String orgid ){
        ORGSearchContext context = new ORGSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(orgid)){
            com.ibiz.util.SearchFieldFilter fieldFilter=new com.ibiz.util.SearchFieldFilter();
            fieldFilter.setParam("orgid");
            fieldFilter.setCondition(com.ibiz.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(orgid);
            context.getCondition().add(fieldFilter);
        }
        List<ORG> list = new ArrayList<ORG>();
        Page<com.ibiz.core.repository.po.ORG> t_ORGs = oRGRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.ORG t_ORG : t_ORGs){
            list.add(oRGRepository.convert2Domain(t_ORG,null));
        }
        return new PageImpl<ORG>(list, context.getPageable(), t_ORGs.getTotalElements());
    }

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

        ORGSearchContext context = new ORGSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(orgid)){
            com.ibiz.util.SearchFieldFilter fieldFilter=new com.ibiz.util.SearchFieldFilter();
            fieldFilter.setParam("orgid");
            fieldFilter.setCondition(com.ibiz.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(orgid);
            context.getCondition().add(fieldFilter);
        }
        List<ORG> list = new ArrayList<ORG>();
        Page<com.ibiz.core.repository.po.ORG> t_ORGs = oRGRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.ORG t_ORG : t_ORGs){
            list.add(oRGRepository.convert2Domain(t_ORG,null));
        }
        return list;
    }



}
