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.IOrgDeptUser;
import cn.ibizlab.r7.runtime.core.r7rt_org.domain.OrgDeptUser;
import cn.ibizlab.r7.runtime.core.r7rt_org.filter.OrgDeptUserSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_org.service.IOrgDeptUserService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[部门用户] 服务对象接口实现
 */
@Service
public class OrgDeptUserServiceImpl implements IOrgDeptUserService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IOrgDeptUserClientService orgDeptUserClientService;

    @Override
    public OrgDeptUser get(String orgdeptuserid) {
        IOrgDeptUser clientModel = orgDeptUserClientService.createModel();
        clientModel.setOrgDeptUserId(orgdeptuserid);
        orgDeptUserClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public OrgDeptUser get(OrgDeptUser orgdeptuser) {
        OrgDeptUser retorgdeptuser = this.get(orgdeptuser.getOrgDeptUserId()) ;
    	BeanCopier copier=BeanCopier.create(retorgdeptuser.getClass(), orgdeptuser.getClass(), false);
        copier.copy(retorgdeptuser, orgdeptuser, null);
        return orgdeptuser ;
    }

    @Override
    public OrgDeptUser getDraft(OrgDeptUser orgdeptuser) {
        return orgdeptuser;
    }

    @Override
    public boolean update(OrgDeptUser orgdeptuser) {
        boolean bOk = false ;
        IOrgDeptUser clientModel = this.convert2Model(orgdeptuser,null);
        orgDeptUserClientService.update(clientModel) ;
        this.convert2Domain(clientModel,orgdeptuser) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(OrgDeptUser orgdeptuser) {
        boolean bOk = false ;
        IOrgDeptUser clientModel = this.convert2Model(orgdeptuser,null);
        orgDeptUserClientService.create(clientModel) ;
        this.convert2Domain(clientModel,orgdeptuser);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean save(OrgDeptUser orgdeptuser) {
        if(orgdeptuser.getOrgDeptUserId()==null){
    		return create(orgdeptuser);
    	}else{
            if(this.get(orgdeptuser.getOrgDeptUserId())!=null){
    			return update(orgdeptuser);
    		}
    		return create(orgdeptuser);
    	}
    }

    @Override
    public boolean remove(String orgdeptuserid) {
        boolean bOk = false ;
        IOrgDeptUser clientModel = orgDeptUserClientService.createModel();
        clientModel.setOrgDeptUserId(orgdeptuserid);
        orgDeptUserClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean checkKey(OrgDeptUser orgdeptuser) {
        return true;
    }

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

    /**
     * 查询集合 getorgdeptuser
     */
    @Override
    public Page<OrgDeptUser> searchGetorgdeptuser(OrgDeptUserSearchContext context) {
        List<OrgDeptUser> list = new ArrayList<OrgDeptUser>();
        Page<cn.ibizlab.r7.runtime.core.client.model.IOrgDeptUser> clientModelList = orgDeptUserClientService.fetchGetorgdeptuser(context) ;
        for(cn.ibizlab.r7.runtime.core.client.model.IOrgDeptUser clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<OrgDeptUser>(list, context.getPageable(), clientModelList.getTotalElements());
    }


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

    /**
     * 根据部门查询
     */
    @Override
    public List<OrgDeptUser> selectByOrgDept2(String orgdeptid){
        return null ;
    }
    /**
     * 根据组织用户查询
     */
    @Override
    public Page<OrgDeptUser> selectByOrgUser(String orguserid){
        return null ;
    }

    /**
     * 根据组织用户查询
     */
    @Override
    public List<OrgDeptUser> selectByOrgUser2(String orguserid){
        return null ;
    }
    /**
     * 根据组织查询
     */
    @Override
    public Page<OrgDeptUser> selectByOrg(String orgid){
        return null ;
    }

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


    public IOrgDeptUser convert2Model(OrgDeptUser domain , IOrgDeptUser model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = orgDeptUserClientService.createModel() ;
        }

         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getOrgDeptUserNameDirtyFlag())
            model.setOrgDeptUserName(domain.getOrgDeptUserName());
         if(domain.getOrgDeptUserIdDirtyFlag())
            model.setOrgDeptUserId(domain.getOrgDeptUserId());
         if(domain.getOrgNameDirtyFlag())
            model.setOrgName(domain.getOrgName());
         if(domain.getOrgDeptNameDirtyFlag())
            model.setOrgDeptName(domain.getOrgDeptName());
         if(domain.getOrgUserNameDirtyFlag())
            model.setOrgUserName(domain.getOrgUserName());
         if(domain.getOrgUserIdDirtyFlag())
            model.setOrgUserId(domain.getOrgUserId());
         if(domain.getOrgDeptIdDirtyFlag())
            model.setOrgDeptId(domain.getOrgDeptId());
         if(domain.getOrgIdDirtyFlag())
            model.setOrgId(domain.getOrgId());
        return model ;
    }

    public OrgDeptUser convert2Domain( IOrgDeptUser model ,OrgDeptUser domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new OrgDeptUser();
        }

        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getOrgDeptUserNameDirtyFlag())
            domain.setOrgDeptUserName(model.getOrgDeptUserName());
        if(model.getOrgDeptUserIdDirtyFlag())
            domain.setOrgDeptUserId(model.getOrgDeptUserId());
        if(model.getOrgNameDirtyFlag())
            domain.setOrgName(model.getOrgName());
        if(model.getOrgDeptNameDirtyFlag())
            domain.setOrgDeptName(model.getOrgDeptName());
        if(model.getOrgUserNameDirtyFlag())
            domain.setOrgUserName(model.getOrgUserName());
        if(model.getOrgUserIdDirtyFlag())
            domain.setOrgUserId(model.getOrgUserId());
        if(model.getOrgDeptIdDirtyFlag())
            domain.setOrgDeptId(model.getOrgDeptId());
        if(model.getOrgIdDirtyFlag())
            domain.setOrgId(model.getOrgId());
        return domain ;
    }


}
