package com.baosight.xservices.xs.service;

import com.baosight.hggp.aspect.annotation.OperationLogAnnotation;
import com.baosight.hggp.core.constant.CommonConstant;
import com.baosight.hggp.core.dao.DaoUtils;
import com.baosight.hggp.core.security.UserSessionUtils;
import com.baosight.hggp.hg.wd.domain.HGWD001;
import com.baosight.hggp.hg.wd.domain.HGWD003;
import com.baosight.hggp.hg.wd.tools.HGWDTools;
import com.baosight.hggp.util.AssertUtils;
import com.baosight.hggp.util.DateUtils;
import com.baosight.hggp.util.EiInfoUtils;
import com.baosight.hggp.util.LogUtils;
import com.baosight.hggp.util.MapUtils;
import com.baosight.hggp.util.ObjectUtils;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.xservices.xs.domain.XS02;
import com.baosight.xservices.xs.tools.XsTools;
import com.baosight.xservices.xs.util.LoginUserDetails;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 复制角色权限
 *
 * @author:songx
 * @date:2024/5/9,11:04
 */
public class ServiceXS3201B extends ServiceEPBase {
	
	/**
	 * 画面初始化
	 *
	 * @param inInfo
	 * @return
	 */
	@Override
	@OperationLogAnnotation(operModul = "文档权限", operType = "查询", operDesc = "初始化")
	public EiInfo initLoad(EiInfo inInfo) {
		try {
		} catch (Exception e) {
			LogUtils.setDetailMsg(inInfo, e, "初始化失败");
		}
		return inInfo;
	}
	
	/**
	 * 查询
	 *
	 * @param inInfo
	 * @return
	 */
	public EiInfo query(EiInfo inInfo) {
		try {
			Map queryMap = EiInfoUtils.getFirstRow(inInfo);
			// 非超级管理只能看到所属企业的信息 added by songx at 2024-01-16
			if (!LoginUserDetails.isUserAdmin(UserSessionUtils.getLoginName())) {
				queryMap.put("accountCode", UserSessionUtils.getAccountCode());
			}
			// 排除自身
			queryMap.put("excludeEnames", new String[]{MapUtils.getString(queryMap, "mainGroupEname")});
			inInfo = super.query(inInfo, "XS02.query", new XS02());
		} catch (Exception e) {
			LogUtils.setMsg(inInfo, e, "查询失败");
		}
		return inInfo;
	}
	
	/**
	 * 确认
	 *
	 * @param inInfo
	 * @return
	 */
	@OperationLogAnnotation(operModul = "文档权限", operType = "保存", operDesc = "操作")
	public EiInfo confirm(EiInfo inInfo) {
		try {
			Map queryMap = EiInfoUtils.getFirstRow(inInfo);
			// 获取Group_ID信息
			String mainGroupEname = MapUtils.getString(queryMap, "mainGroupEname");
			Map resultMap = XsTools.Xs02.getGroup(mainGroupEname);
			AssertUtils.isEmpty(resultMap, String.format("【%s】角色不存在", mainGroupEname));
			// 获取Group_ID授权信息
			List<Map> dbMainAuths = XsTools.Xs07.listBySubject(MapUtils.getString(resultMap, "groupId"));
			AssertUtils.isEmpty(dbMainAuths, String.format("【%s】角色还未进行授权,请授权后再进行复制", mainGroupEname));
			// 复制授权信息
			List<Map> fGroupMaps = inInfo.getBlock(EiConstant.resultBlock).getRows();
			this.copyAuthUser(fGroupMaps, dbMainAuths);
			inInfo.setStatus(EiConstant.STATUS_SUCCESS);
			inInfo.setMsg("复制角色权限成功!");
		} catch (Exception e) {
			LogUtils.setDetailMsg(inInfo, e, "复制角色权限失败");
		}
		return inInfo;
	}
	
	/**
	 * 复制授权用户
	 *
	 * @param fGroupMaps
	 * @param dbMainAuths
	 */
	private void copyAuthUser(List<Map> fGroupMaps, List<Map> dbMainAuths) {
		List<String> groupIds = ObjectUtils.listKey(fGroupMaps, "groupId");
		// 已授权信息
		Map<String, List<Map>> dbAuthMap = XsTools.Xs07.mapBySubject(groupIds);
		for (String groupId : groupIds) {
			// 已存在的授权用户信息
			List<Map> dbAuths = dbAuthMap == null ? null : dbAuthMap.get(groupId);
			List<String> objectIds = CollectionUtils.isEmpty(dbAuths) ? new ArrayList<>()
					: dbAuths.stream().map(item -> item.get("objectId").toString()).collect(Collectors.toList());
			// 过滤出未授权的用户,并重新赋值FILE_ID
			List<Map> filterAuths = dbMainAuths.stream()
					.filter(dbMainAuth -> !objectIds.contains(dbMainAuth.get("objectId")))
					.map(dbMainAuth -> {
						dbMainAuth.put("subjectId", groupId);
						dbMainAuth.put("recCreator", UserSessionUtils.getLoginName());
						dbMainAuth.put("recCreateTime", DateUtils.shortDateTime());
						dbMainAuth.put("sortIndex", 0);
						return dbMainAuth;
					}).collect(Collectors.toList());
			if (CollectionUtils.isNotEmpty(filterAuths)) {
				DaoUtils.insertBatch("XS07.insert", filterAuths);
			}
		}
	}
	
}