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.IOrgUser;
import com.ibiz.core.r7rt_org.domain.OrgUser;
import com.ibiz.core.r7rt_org.filter.OrgUserSearchContext;
import com.ibiz.core.r7rt_org.service.IOrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiz.util.SearchResult;

/**
 * 实体[组织用户] 服务对象接口实现
 */
@Service
public class OrgUserServiceImpl implements IOrgUserService {

    @Autowired
    private com.ibiz.core.client.service.IOrgUserClientService orgUserClientService;

    @Override
    public boolean remove(String orguserid) {
        boolean bOk = false ;
        IOrgUser clientModel = orgUserClientService.createModel();
        clientModel.setOrgUserId(orguserid);
        orgUserClientService.remove(clientModel) ;
        bOk = true ;
        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 boolean update(OrgUser orguser) {
        boolean bOk = false ;
        IOrgUser clientModel = this.convert2Model(orguser,null);
        orgUserClientService.update(clientModel) ;
        this.convert2Domain(clientModel,orguser) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean checkKey(OrgUser orguser) {
        return true;
    }

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

    @Override
    public boolean create(OrgUser orguser) {
        boolean bOk = false ;
        IOrgUser clientModel = this.convert2Model(orguser,null);
        orgUserClientService.create(clientModel) ;
        this.convert2Domain(clientModel,orguser);
        bOk = true ;

        return bOk ;
    }

    @Override
    public OrgUser get(String orguserid) {
        IOrgUser clientModel = orgUserClientService.createModel();
        clientModel.setOrgUserId(orguserid);
        orgUserClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public OrgUser get(OrgUser orguser) {
        OrgUser retorguser = this.get(orguser.getOrgUserId()) ;
    	BeanCopier copier=BeanCopier.create(retorguser.getClass(), orguser.getClass(), false);
        copier.copy(retorguser, orguser, null);
        return orguser ;
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<OrgUser> searchDefault(OrgUserSearchContext context) {
        List<OrgUser> list = new ArrayList<OrgUser>();
        Page<com.ibiz.core.client.model.IOrgUser> clientModelList = orgUserClientService.fetchDefault(context) ;
        for(com.ibiz.core.client.model.IOrgUser clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<OrgUser>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据组织查询
     */
    @Override
    public Page<OrgUser> selectByOrg(String orgid){
        return null ;
    }

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


    public IOrgUser convert2Model(OrgUser domain , IOrgUser model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = orgUserClientService.createModel() ;
        }

         if(domain.getSexDirtyFlag())
            model.setSex(domain.getSex());
         if(domain.getOrgUserIdDirtyFlag())
            model.setOrgUserId(domain.getOrgUserId());
         if(domain.getMobilePhoneDirtyFlag())
            model.setMobilePhone(domain.getMobilePhone());
         if(domain.getCertTypeDirtyFlag())
            model.setCertType(domain.getCertType());
         if(domain.getOrgUserNameDirtyFlag())
            model.setOrgUserName(domain.getOrgUserName());
         if(domain.getUserCodeDirtyFlag())
            model.setUserCode(domain.getUserCode());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getLoginNameDirtyFlag())
            model.setLoginName(domain.getLoginName());
         if(domain.getCertNoDirtyFlag())
            model.setCertNo(domain.getCertNo());
         if(domain.getUserIdDirtyFlag())
            model.setUserId(domain.getUserId());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
         if(domain.getOrgNameDirtyFlag())
            model.setOrgName(domain.getOrgName());
         if(domain.getOrgIdDirtyFlag())
            model.setOrgId(domain.getOrgId());
        return model ;
    }

    public OrgUser convert2Domain( IOrgUser model ,OrgUser domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new OrgUser();
        }

        if(model.getSexDirtyFlag())
            domain.setSex(model.getSex());
        if(model.getOrgUserIdDirtyFlag())
            domain.setOrgUserId(model.getOrgUserId());
        if(model.getMobilePhoneDirtyFlag())
            domain.setMobilePhone(model.getMobilePhone());
        if(model.getCertTypeDirtyFlag())
            domain.setCertType(model.getCertType());
        if(model.getOrgUserNameDirtyFlag())
            domain.setOrgUserName(model.getOrgUserName());
        if(model.getUserCodeDirtyFlag())
            domain.setUserCode(model.getUserCode());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getLoginNameDirtyFlag())
            domain.setLoginName(model.getLoginName());
        if(model.getCertNoDirtyFlag())
            domain.setCertNo(model.getCertNo());
        if(model.getUserIdDirtyFlag())
            domain.setUserId(model.getUserId());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        if(model.getOrgNameDirtyFlag())
            domain.setOrgName(model.getOrgName());
        if(model.getOrgIdDirtyFlag())
            domain.setOrgId(model.getOrgId());
        return domain ;
    }


}
