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.ORGUSER;
import com.ibiz.core.external.filter.ORGUSERSearchContext;
import com.ibiz.core.external.service.IORGUSERService;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiz.util.SearchResult;

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

    @Autowired
    com.ibiz.core.repository.ORGUSERRepository oRGUSERRepository;




    @Override
    public boolean create(ORGUSER orguser) {
        boolean bOk = false ;
        com.ibiz.core.repository.po.ORGUSER t_ORGUSER = oRGUSERRepository.convert2PO(orguser,null);
        bOk = oRGUSERRepository.create(t_ORGUSER);
        oRGUSERRepository.convert2Domain(t_ORGUSER,orguser);

        return bOk ;
    }

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

    @Override
    public boolean update(ORGUSER orguser) {
        boolean bOk = false ;
        com.ibiz.core.repository.po.ORGUSER t_ORGUSER = oRGUSERRepository.convert2PO(orguser,null);
        bOk = oRGUSERRepository.update(t_ORGUSER);
        oRGUSERRepository.convert2Domain(t_ORGUSER,orguser) ;

        return bOk ;
    }

    @Override
    public ORGUSER get(String orguserid) {
        com.ibiz.core.repository.po.ORGUSER t_ORGUSER = oRGUSERRepository.get(orguserid);
        return oRGUSERRepository.convert2Domain(t_ORGUSER,null);
    }

    @Override
    public ORGUSER get(ORGUSER orguser) {
        com.ibiz.core.repository.po.ORGUSER t_ORGUSER = oRGUSERRepository.get(orguser.getORGUSERId());
        oRGUSERRepository.convert2Domain(t_ORGUSER,orguser);
        return orguser;
    }

    @Override
    public boolean checkKey(ORGUSER orguser) {
        return true;
    }
    @Override
    public boolean remove(String orguserid) {
        boolean bOk = false ;
        bOk = oRGUSERRepository.remove(orguserid);

        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.ibiz.core.repository.po.ORGUSER> t_ORGUSERs = oRGUSERRepository.searchCurOrgUser(context) ;
        for(com.ibiz.core.repository.po.ORGUSER t_ORGUSER : t_ORGUSERs){
            list.add(oRGUSERRepository.convert2Domain(t_ORGUSER,null));
        }
        return new PageImpl<ORGUSER>(list, context.getPageable(), t_ORGUSERs.getTotalElements());
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<ORGUSER> searchDefault(ORGUSERSearchContext context) {
        List<ORGUSER> list = new ArrayList<ORGUSER>();
        Page<com.ibiz.core.repository.po.ORGUSER> t_ORGUSERs = oRGUSERRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.ORGUSER t_ORGUSER : t_ORGUSERs){
            list.add(oRGUSERRepository.convert2Domain(t_ORGUSER,null));
        }
        return new PageImpl<ORGUSER>(list, context.getPageable(), t_ORGUSERs.getTotalElements());
    }


    /**
     * 根据机构查询
     */
    @Override
    public Page<ORGUSER> selectByORG(String orgid ){
        ORGUSERSearchContext context = new ORGUSERSearchContext();
        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<ORGUSER> list = new ArrayList<ORGUSER>();
        Page<com.ibiz.core.repository.po.ORGUSER> t_ORGUSERs = oRGUSERRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.ORGUSER t_ORGUSER : t_ORGUSERs){
            list.add(oRGUSERRepository.convert2Domain(t_ORGUSER,null));
        }
        return new PageImpl<ORGUSER>(list, context.getPageable(), t_ORGUSERs.getTotalElements());
    }

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

        ORGUSERSearchContext context = new ORGUSERSearchContext();
        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<ORGUSER> list = new ArrayList<ORGUSER>();
        Page<com.ibiz.core.repository.po.ORGUSER> t_ORGUSERs = oRGUSERRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.ORGUSER t_ORGUSER : t_ORGUSERs){
            list.add(oRGUSERRepository.convert2Domain(t_ORGUSER,null));
        }
        return list;
    }



}
