package cn.ibizlab.r7.runtime.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 cn.ibizlab.r7.runtime.core.client.model.IOrgDept;
import cn.ibizlab.r7.runtime.core.r7rt_org.domain.OrgDept;
import cn.ibizlab.r7.runtime.core.r7rt_org.filter.OrgDeptSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_org.service.IOrgDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[部门] 服务对象接口实现
 */
@Service
public class OrgDeptServiceImpl implements IOrgDeptService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IOrgDeptClientService orgDeptClientService;

    @Override
    public boolean update(OrgDept orgdept) {
        boolean bOk = false ;
        IOrgDept clientModel = this.convert2Model(orgdept,null);
        orgDeptClientService.update(clientModel) ;
        this.convert2Domain(clientModel,orgdept) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(String orgdeptid) {
        boolean bOk = false ;
        IOrgDept clientModel = orgDeptClientService.createModel();
        clientModel.setOrgDeptId(orgdeptid);
        orgDeptClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public OrgDept get(String orgdeptid) {
        IOrgDept clientModel = orgDeptClientService.createModel();
        clientModel.setOrgDeptId(orgdeptid);
        orgDeptClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public OrgDept get(OrgDept orgdept) {
        OrgDept retorgdept = this.get(orgdept.getOrgDeptId()) ;
    	BeanCopier copier=BeanCopier.create(retorgdept.getClass(), orgdept.getClass(), false);
        copier.copy(retorgdept, orgdept, null);
        return orgdept ;
    }

    @Override
    public boolean checkKey(OrgDept orgdept) {
        return true;
    }

    @Override
    public boolean create(OrgDept orgdept) {
        boolean bOk = false ;
        IOrgDept clientModel = this.convert2Model(orgdept,null);
        orgDeptClientService.create(clientModel) ;
        this.convert2Domain(clientModel,orgdept);
        bOk = true ;

        return bOk ;
    }

    @Override
    public OrgDept getDraft(OrgDept orgdept) {
        return orgdept;
    }

    @Override
    public OrgDept getOrgDeptChild(OrgDept orgdept) {
        IOrgDept clientModel = this.convert2Model(orgdept,null);
        orgDeptClientService.getOrgDeptChild(clientModel) ;
        this.convert2Domain(clientModel,null);
         return orgdept;
    }

    @Override
    public boolean save(OrgDept orgdept) {
        if(orgdept.getOrgDeptId()==null){
    		return create(orgdept);
    	}else{
            if(this.get(orgdept.getOrgDeptId())!=null){
    			return update(orgdept);
    		}
    		return create(orgdept);
    	}
    }

    @Override
    public OrgDept syncToUserGroup(OrgDept orgdept) {
        IOrgDept clientModel = this.convert2Model(orgdept,null);
        orgDeptClientService.syncToUserGroup(clientModel) ;
        this.convert2Domain(clientModel,null);
         return orgdept;
    }

    @Override
    public OrgDept getOrgDeptPrent(OrgDept orgdept) {
        IOrgDept clientModel = this.convert2Model(orgdept,null);
        orgDeptClientService.getOrgDeptPrent(clientModel) ;
        this.convert2Domain(clientModel,null);
         return orgdept;
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<OrgDept> searchDefault(OrgDeptSearchContext context) {
        List<OrgDept> list = new ArrayList<OrgDept>();
        Page<cn.ibizlab.r7.runtime.core.client.model.IOrgDept> clientModelList = orgDeptClientService.fetchDefault(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.IOrgDept clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<OrgDept>(list, context.getPageable(), clientModelList.getTotalElements());
    }

    /**
     * 查询集合 当前组织下的部门
     */
    @Override
    public Page<OrgDept> searchCUR_ORG(OrgDeptSearchContext context) {
        List<OrgDept> list = new ArrayList<OrgDept>();
        Page<cn.ibizlab.r7.runtime.core.client.model.IOrgDept> clientModelList = orgDeptClientService.fetchCUR_ORG(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.IOrgDept clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<OrgDept>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据部门查询
     */
    @Override
    public Page<OrgDept> selectByPOrgDept(String orgdeptid){
        return null ;
    }

    /**
     * 根据部门查询
     */
    @Override
    public List<OrgDept> selectByPOrgDept2(String orgdeptid){
        return null ;
    }
    /**
     * 根据组织查询
     */
    @Override
    public Page<OrgDept> selectByOrg(String orgid){
        return null ;
    }

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


    public IOrgDept convert2Model(OrgDept domain , IOrgDept model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = orgDeptClientService.createModel() ;
        }

         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getOrgDeptIdDirtyFlag())
            model.setOrgDeptId(domain.getOrgDeptId());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getOrgDeptNameDirtyFlag())
            model.setOrgDeptName(domain.getOrgDeptName());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getLevelCodeDirtyFlag())
            model.setLevelCode(domain.getLevelCode());
         if(domain.getDeptCodeDirtyFlag())
            model.setDeptCode(domain.getDeptCode());
         if(domain.getPOrgDeptNameDirtyFlag())
            model.setPOrgDeptName(domain.getPOrgDeptName());
         if(domain.getOrgNameDirtyFlag())
            model.setOrgName(domain.getOrgName());
         if(domain.getOrgIdDirtyFlag())
            model.setOrgId(domain.getOrgId());
         if(domain.getPOrgDeptIdDirtyFlag())
            model.setPOrgDeptId(domain.getPOrgDeptId());
        return model ;
    }

    public OrgDept convert2Domain( IOrgDept model ,OrgDept domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new OrgDept();
        }

        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getOrgDeptIdDirtyFlag())
            domain.setOrgDeptId(model.getOrgDeptId());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getOrgDeptNameDirtyFlag())
            domain.setOrgDeptName(model.getOrgDeptName());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getLevelCodeDirtyFlag())
            domain.setLevelCode(model.getLevelCode());
        if(model.getDeptCodeDirtyFlag())
            domain.setDeptCode(model.getDeptCode());
        if(model.getPOrgDeptNameDirtyFlag())
            domain.setPOrgDeptName(model.getPOrgDeptName());
        if(model.getOrgNameDirtyFlag())
            domain.setOrgName(model.getOrgName());
        if(model.getOrgIdDirtyFlag())
            domain.setOrgId(model.getOrgId());
        if(model.getPOrgDeptIdDirtyFlag())
            domain.setPOrgDeptId(model.getPOrgDeptId());
        return domain ;
    }


}
