package com.hotent.uc.manager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.dao.UserUniteDao;
import com.hotent.uc.manager.UserUniteManager;
import com.hotent.uc.model.UserUnite;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户与外部通讯录关系表 服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author pangquan
 * @since 2020-10-30
 */
@Service
public class UserUniteManagerImpl extends BaseManagerImpl<UserUniteDao, UserUnite> implements UserUniteManager {

	@Override
	public void updateUserUnite(UserUnite userUnite) {
		QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>().eq("user_id_", userUnite.getUserId());
		UserUnite exist = this.getOne(queryWrapper);
		if(BeanUtils.isNotEmpty(exist)){
			if(StringUtil.isNotEmpty(userUnite.getOpenId())){
				exist.setOpenId(userUnite.getOpenId());
			}
			if(StringUtil.isNotEmpty(userUnite.getWxWorkId())){
				exist.setWxWorkId(userUnite.getWxWorkId());
			}
			if(StringUtil.isNotEmpty(userUnite.getDingtalkId())){
				exist.setDingtalkId(userUnite.getDingtalkId());
			}
			if(StringUtil.isNotEmpty(userUnite.getMpOpenId())){
				exist.setMpOpenId(userUnite.getMpOpenId());
			}
			this.update(exist);
		}else{
			this.create(userUnite);
		}
	}

	@Override
	public void updateUserUniteList(List<UserUnite> userUniteList) {
		Map<String,UserUnite> map = new HashMap<>();
		List<String> userIds = new ArrayList<String>();
		for (UserUnite userUnite : userUniteList) {
			map.put(userUnite.getUserId(), userUnite);
			userIds.add(userUnite.getUserId());
		}
		QueryWrapper<UserUnite> qw = new QueryWrapper<UserUnite>().in("user_id_", userIds);
		List<UserUnite> existList = this.baseMapper.selectList(qw);
		for (UserUnite exist : existList) {
			String userId = exist.getUserId();
			UserUnite uu = map.get(userId);
			if(uu!=null){
				boolean needToUpdate = false;
				if(BeanUtils.isNotEmpty(uu.getOpenId()) && !uu.getOpenId().equals(exist.getOpenId())){
					exist.setOpenId(uu.getOpenId());
					needToUpdate = true;
				}
				if(BeanUtils.isNotEmpty(uu.getWxWorkId()) && !uu.getWxWorkId().equals(exist.getWxWorkId())){
					exist.setWxWorkId(uu.getWxWorkId());
					needToUpdate = true;
				}
				if(BeanUtils.isNotEmpty(uu.getDingtalkId()) && !uu.getDingtalkId().equals(exist.getDingtalkId())){
					exist.setDingtalkId(uu.getDingtalkId());
					needToUpdate = true;
				}
				if(needToUpdate){
					this.baseMapper.updateById(exist);
				}
			}
			map.remove(userId);
		}
		//把不存在的批量创建
		if(BeanUtils.isNotEmpty(map.values())){
			this.saveBatch(map.values());
		}
	}

	@Override
	public String getDingtalkIdsByUserIds(String userIds) {
		if(BeanUtils.isNotEmpty(userIds)){
			Object[] userIdList = userIds.split(",");
			QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>();
			queryWrapper.in("USER_ID_", userIdList);
			List<UserUnite> list = list(queryWrapper);
			if(BeanUtils.isNotEmpty(list)){
				List<String> dingtalkIds = list.stream().filter(e->BeanUtils.isNotEmpty(e.getDingtalkId())).
						map(UserUnite::getDingtalkId).collect(Collectors.toList());
				if(BeanUtils.isNotEmpty(dingtalkIds)){
					return String.join(",", dingtalkIds);
				}
			}
		}
		return "";
	}

	@Override
	public String getWxWorkIdsByUserIds(String userIds) {
		if(BeanUtils.isNotEmpty(userIds)){
			Object[] userIdList = userIds.split(",");
			QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>();
			queryWrapper.in("USER_ID_", userIdList);
			List<UserUnite> list = list(queryWrapper);
			if(BeanUtils.isNotEmpty(list)){
				List<String> wxWorkIds = list.stream().filter(e->BeanUtils.isNotEmpty(e.getWxWorkId())).
						map(UserUnite::getWxWorkId).collect(Collectors.toList());
				if(BeanUtils.isNotEmpty(wxWorkIds)){
					return String.join(",", wxWorkIds);
				}
			}
		}
		return "";
	}

	@Override
	public String getOpenIdsByUserIds(String userIds) {
		if(BeanUtils.isNotEmpty(userIds)){
			Object[] userIdList = userIds.split(",");
			QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>();
			queryWrapper.in("USER_ID_", userIdList);
			List<UserUnite> list = list(queryWrapper);
			if(BeanUtils.isNotEmpty(list)){
				List<String> getOpenId = list.stream().filter(e->BeanUtils.isNotEmpty(e.getOpenId())).
						map(UserUnite::getOpenId).collect(Collectors.toList());
				if(BeanUtils.isNotEmpty(getOpenId)){
					return String.join(",", getOpenId);
				}
			}
		}
		return "";
	}

}
