package cn.ibizlab.r7.runtime.core.r7rt_base.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.IUser;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.User;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.UserSearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ibizlab.r7.runtime.util.SearchResult;

/**
 * 实体[用户] 服务对象接口实现
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private cn.ibizlab.r7.runtime.core.client.service.IUserClientService userClientService;

    @Override
    public boolean remove(String userid) {
        boolean bOk = false ;
        IUser clientModel = userClientService.createModel();
        clientModel.setUserId(userid);
        userClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(User user) {
        boolean bOk = false ;
        IUser clientModel = this.convert2Model(user,null);
        userClientService.create(clientModel) ;
        this.convert2Domain(clientModel,user);
        bOk = true ;

        return bOk ;
    }

    @Override
    public User syncToUser(User user) {
        IUser clientModel = this.convert2Model(user,null);
        userClientService.syncToUser(clientModel) ;
        this.convert2Domain(clientModel,null);
         return user;
    }

    @Override
    public boolean checkKey(User user) {
        return true;
    }

    @Override
    public User get(String userid) {
        IUser clientModel = userClientService.createModel();
        clientModel.setUserId(userid);
        userClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public User get(User user) {
        User retuser = this.get(user.getUserId()) ;
    	BeanCopier copier=BeanCopier.create(retuser.getClass(), user.getClass(), false);
        copier.copy(retuser, user, null);
        return user ;
    }

    @Override
    public boolean save(User user) {
        if(user.getUserId()==null){
    		return create(user);
    	}else{
            if(this.get(user.getUserId())!=null){
    			return update(user);
    		}
    		return create(user);
    	}
    }

    @Override
    public User getDraft(User user) {
        return user;
    }

    @Override
    public boolean update(User user) {
        boolean bOk = false ;
        IUser clientModel = this.convert2Model(user,null);
        userClientService.update(clientModel) ;
        this.convert2Domain(clientModel,user) ;
        bOk = true;
        return bOk ;
    }

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


    /**
     * 根据用户对象查询
     */
    @Override
    public Page<User> selectByUser(String userobjectid){
        return null ;
    }

    /**
     * 根据用户对象查询
     */
    @Override
    public List<User> selectByUser2(String userobjectid){
        return null ;
    }


    public IUser convert2Model(User domain , IUser model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = userClientService.createModel() ;
        }

         if(domain.getCertTypeDirtyFlag())
            model.setCertType(domain.getCertType());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getEnableDirtyFlag())
            model.setEnable(domain.getEnable());
         if(domain.getUserNameDirtyFlag())
            model.setUserName(domain.getUserName());
         if(domain.getLoginNameDirtyFlag())
            model.setLoginName(domain.getLoginName());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getUserCodeDirtyFlag())
            model.setUserCode(domain.getUserCode());
         if(domain.getUserIdDirtyFlag())
            model.setUserId(domain.getUserId());
         if(domain.getUserObjectTypeDirtyFlag())
            model.setUserObjectType(domain.getUserObjectType());
         if(domain.getSexDirtyFlag())
            model.setSex(domain.getSex());
         if(domain.getMobilePhoneDirtyFlag())
            model.setMobilePhone(domain.getMobilePhone());
         if(domain.getCertNoDirtyFlag())
            model.setCertNo(domain.getCertNo());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getMemoDirtyFlag())
            model.setMemo(domain.getMemo());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
        return model ;
    }

    public User convert2Domain( IUser model ,User domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new User();
        }

        if(model.getCertTypeDirtyFlag())
            domain.setCertType(model.getCertType());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getEnableDirtyFlag())
            domain.setEnable(model.getEnable());
        if(model.getUserNameDirtyFlag())
            domain.setUserName(model.getUserName());
        if(model.getLoginNameDirtyFlag())
            domain.setLoginName(model.getLoginName());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getUserCodeDirtyFlag())
            domain.setUserCode(model.getUserCode());
        if(model.getUserIdDirtyFlag())
            domain.setUserId(model.getUserId());
        if(model.getUserObjectTypeDirtyFlag())
            domain.setUserObjectType(model.getUserObjectType());
        if(model.getSexDirtyFlag())
            domain.setSex(model.getSex());
        if(model.getMobilePhoneDirtyFlag())
            domain.setMobilePhone(model.getMobilePhone());
        if(model.getCertNoDirtyFlag())
            domain.setCertNo(model.getCertNo());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getMemoDirtyFlag())
            domain.setMemo(model.getMemo());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        return domain ;
    }


}
