package com.hotent.uc.manager.impl;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryField;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.dao.UcGroupUserDao;
import com.hotent.uc.exception.BaseException;
import com.hotent.uc.manager.DemensionManager;
import com.hotent.uc.manager.UcGroupManager;
import com.hotent.uc.manager.UcGroupUserManager;
import com.hotent.uc.model.UcGroup;
import com.hotent.uc.model.UcGroupUser;
import com.hotent.uc.util.ContextUtil;
import com.hotent.uc.vo.UcGroupUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 群组和用户关系表 服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @since 2021-01-15
 */
@Service
public class UcGroupUserManagerImpl extends BaseManagerImpl<UcGroupUserDao, UcGroupUser> implements UcGroupUserManager {

	@Resource
	DemensionManager demensionManager;

	@Autowired
	UcGroupManager ucGroupManager;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveJson(Map<String,Object> map) {

		String groupId=map.get("groupId").toString();
		List ids= (List) map.get("ids");

		QueryWrapper<UcGroupUser> wrapper = new QueryWrapper();
		wrapper.eq("GROUP_ID_",groupId);
		wrapper.in("USER_ID",ids);
		this.remove(wrapper);
		wrapper.clear();
		wrapper.eq("GROUP_ID_", groupId);
		List<UcGroupUser> groups = this.list(wrapper);
		Integer maxIndex = 0;
		for (UcGroupUser group : groups) {
			if (group.getSn() >= maxIndex) {
				maxIndex = group.getSn() + 1;
			}
		}
		List<UcGroupUser> list = new ArrayList<>();
		for (Object id : ids) {
			UcGroupUser ucGroupUser=new UcGroupUser();
			ucGroupUser.setSn(maxIndex++);
			ucGroupUser.setGroupId(groupId);
			ucGroupUser.setUserId(id.toString());
			list.add(ucGroupUser);
		}
		this.saveBatch(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void swap(String s1, String s2) {
		UcGroupUser u1 = this.getById(s1);
		UcGroupUser u2 = this.getById(s2);
		if (BeanUtils.isEmpty(u1) || BeanUtils.isEmpty(u2)) {
			throw new BaseException("顺序交换失败，记录为空");
		}
		Integer t1 = u1.getSn();
		u1.setSn(u2.getSn());
		u2.setSn(t1);
		UcGroupUser[] ucGroupUsers = {u1, u2};
		this.updateBatchById(Arrays.asList(ucGroupUsers));
	}

	@Override
	public PageList<Map<String, Object>> loadUserByGroupId(String groupIds, String demId, QueryFilter queryFilter) throws Exception {
		Class<?> aClass = Class.forName("com.hotent.uc.model.User");
		for (Object query : queryFilter.getQuerys()) {
			QueryField queryField = (QueryField) query;
			Field field = aClass.getDeclaredField(queryField.getProperty());
			if (field != null) {
				TableField annotation = field.getAnnotation(TableField.class);
				if (null != annotation) {
					queryField.setProperty(annotation.value());
				}
			}
		}
		List<String> groupIdList = new ArrayList<>();
		if (StringUtil.isNotEmpty(groupIds)) {
			groupIdList = Lists.newArrayList(groupIds.split(","));
		}
		if (StringUtil.isEmpty(demId)) {
			demId = demensionManager.getDefaultDemension().getId();
		}
		String userId = ContextUtil.getCurrentUserId();
		queryFilter.addParams("userId", userId);
		IPage<Map<String, Object>> page = baseMapper.loadUserByGroupId(convert2IPage(queryFilter.getPageBean()), convert2Wrapper(queryFilter, currentModelClass()) , groupIdList, demId);
		return new PageList<>(page);
	}
}
