/*
 *
 *      Copyright (c) 2018-2025, hnqz All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: hnqz
 *
 */

package com.qunzhixinxi.hnqz.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDeptRelation;
import com.qunzhixinxi.hnqz.admin.api.vo.UserVO;
import com.qunzhixinxi.hnqz.admin.mapper.SysDeptMapper;
import com.qunzhixinxi.hnqz.admin.mapper.SysDeptRelationMapper;
import com.qunzhixinxi.hnqz.admin.service.SysDeptRelationService;
import com.qunzhixinxi.hnqz.admin.service.SysDeptService;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hnqz
 * @since 2018-02-12
 */
@Service
@AllArgsConstructor
public class SysDeptRelationServiceImpl extends ServiceImpl<SysDeptRelationMapper, SysDeptRelation>
		implements SysDeptRelationService {

	private final SysDeptRelationMapper sysDeptRelationMapper;

	private final SysDeptMapper sysDeptMapper;

	/**
	 * 维护部门关系
	 * @param sysDept 部门
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertDeptRelation(SysDept sysDept) {
		// 增加部门关系表
		List<SysDeptRelation> relationList = sysDeptRelationMapper.selectList(
				Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getDescendant, sysDept.getParentId()))
				.stream().map(relation -> {
					relation.setDescendant(sysDept.getDeptId());
					return relation;
				}).collect(Collectors.toList());
		if (CollUtil.isNotEmpty(relationList)) {
			this.saveBatch(relationList);
		}

		// 自己也要维护到关系表中
		SysDeptRelation own = new SysDeptRelation();
		own.setDescendant(sysDept.getDeptId());
		own.setAncestor(sysDept.getDeptId());
		sysDeptRelationMapper.insert(own);
	}

	/**
	 * 通过ID删除部门关系
	 * @param id
	 */
	@Override
	public void deleteAllDeptRealtion(Integer id) {
		baseMapper.deleteDeptRelationsById(id);
	}

	/**
	 * 通过ID删除部门关系
	 * @param id
	 */
	@Override
	public void deleteDeptById(Integer id) {
		baseMapper.deleteDeptById(id);
	}

	@Override
	public IPage selectSysDeptList(Page page, SysDept sysDept) {
		if (sysDept.getDeptId()==null){
			sysDept.setDeptId(SecurityUtils.getUser().getDeptId());
		}
		SysDeptRelation sysDeptRelation  = new SysDeptRelation();
		sysDeptRelation.setAncestor(sysDept.getDeptId());
		List<Map<String,Object>> list1 = new ArrayList<>();
		List<SysDeptRelation> sysDeptRelationList = sysDeptRelationMapper.selectListByAncestor(sysDeptRelation);
		SysDeptRelation sysDeptRelation1  = new SysDeptRelation();
		sysDeptRelation1.setDescendant(sysDept.getDeptId());
		List<SysDeptRelation> sysDeptRelationList1 = sysDeptRelationMapper.selectListByDescendant(sysDeptRelation1);
		for (int i = 0; i < sysDeptRelationList.size(); i++) {
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(sysDeptRelationList.get(i).getDescendant());
				List<Map<String,Object>> sysDept2 = sysDeptMapper.selectDeptById(sysDept1);
				list1.addAll(sysDept2);
		}
		for (int i = 0; i < sysDeptRelationList1.size(); i++) {
			SysDept sysDept1 = new SysDept();
			sysDept1.setDeptId(sysDeptRelationList1.get(i).getAncestor());
				List<Map<String,Object>> sysDept2 = sysDeptMapper.selectDeptById(sysDept1);
				list1.addAll(sysDept2);
		}

		Page<Map<String,Object>> page1 = new Page<>();
		int current = new Long(page.getCurrent()).intValue();
		int size = new Long(page.getSize()).intValue();

		int count = list1.size();
		List<Map<String,Object>> pageList = new ArrayList<>();
		//计算当前页第一条数据的下标
		int currId = current>1 ? (current-1)*size:0;
		for (int i=0; i<size && i<count - currId;i++){
			pageList.add(list1.get(currId+i));
		}
		page1.setSize(size);
		page1.setCurrent(current);
		page1.setTotal(count);
		//计算分页总页数
		page1.setPages(count %10 == 0 ? count/10 :count/10+1);
		page1.setRecords(pageList);
		return page1;
	}


	/**
	 * 更新部门关系
	 * @param relation
	 */
	@Override
	public void updateDeptRealtion(SysDeptRelation relation) {
		baseMapper.updateDeptRelations(relation);
	}

}
