/*
 * Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yifu.cloud.plus.v1.yifu.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.*;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDataAuthVO;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysDataAuthMapper;
import com.yifu.cloud.plus.v1.yifu.admin.service.*;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.security.service.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 数据权限配置服务实现
 * @Author: hgw
 * @Date: 2022/6/8 17:20
 **/
@Service
@RequiredArgsConstructor
public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDataAuth> implements SysDataAuthService {

	// 权限-部门
	private final SysDataAuthDeptRelService authDeptRelService;
	// 权限-菜单（type：类型：1：创建人；2：结算主体；3部门）
	private final SysDataAuthMenuRelService authMenuRelService;
	// 权限-自定义sql
	private final SysDataAuthDiySqlService diySqlService;
	// 自定义sql-菜单
	private final SysDiySqlMenuRelService diySqlMenuRelService;

	/**
	 * @Description: 分页查询
	 * @Author: hgw
	 * @Date: 2022/6/7 14:49
	 * @return: java.util.List<com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDataAuth>
	 **/
	@Override
	public IPage<SysDataAuth> getSysDataAuthPage(Page<SysDataAuth> page, SysDataAuth sysDataAuth) {
		return baseMapper.getSysDataAuthPage(page, sysDataAuth);
	}

	/**
	 * @Description: 获取list
	 * @Author: hgw
	 * @Date: 2022/6/9 16:47
	 * @return: java.util.List<com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDataAuth>
	 **/
	@Override
	public List<SysDataAuth> getSysDataAuthList(SysDataAuth sysDataAuth) {
		return baseMapper.getSysDataAuthPage(sysDataAuth);
	}

	/**
	 * @Description: 新增
	 * @Author: hgw
	 * @Date: 2022/6/7 14:48
	 * @return: java.lang.Boolean
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R<String> saveDataAuth(SysDataAuthVO sysDataAuthVO) {
		YifuUser user = SecurityUtils.getUser();
		if (user == null || Common.isEmpty(user.getId())) {
			return R.failed("请登录！");
		}
		if (sysDataAuthVO != null) {
			SysDataAuth sysDataAuth = sysDataAuthVO.getSysDataAuth();
			// 校验参数
			if (sysDataAuth == null || Common.isEmpty(sysDataAuth.getLinkType())
					|| Common.isEmpty(sysDataAuth.getLinkId()) || Common.isEmpty(sysDataAuth.getLinkName())
					|| Common.isEmpty(sysDataAuth.getIsDeptAuth())) {
				return R.failed("请传参sysDataAuth以及参数！");
			} else {
				List<SysDataAuthMenuRel> saveMenuDeptList = new ArrayList<>();

				// 编辑
				String mainId = sysDataAuth.getId();
				R<String> failed = null;
				if (mainId == null) {
					// 新增
					this.save(sysDataAuth);
					mainId = sysDataAuth.getId();
				} else {
					// 删除之前的关系
					this.deleteSubInfoByMainId(mainId);
				}
				failed = doCore(sysDataAuth, saveMenuDeptList, sysDataAuthVO, mainId);
				if (failed != null) {
					return failed;
				}
				this.updateById(sysDataAuth);

				if (!saveMenuDeptList.isEmpty()) {
					authMenuRelService.saveOrUpdateBatch(saveMenuDeptList);
				}
				return R.ok("保存成功");
			}
		} else {
			return R.failed("请传参sysDataAuthVO！");
		}
	}

	/**
	 * @Description: 核心保存
	 * @Author: hgw
	 * @Date: 2022/6/8 18:41
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	private R<String> doCore(SysDataAuth sysDataAuth, List<SysDataAuthMenuRel> saveMenuDeptList
			, SysDataAuthVO sysDataAuthVO, String mainId) {
		List<SysDataAuthDeptRel> authDeptList = sysDataAuthVO.getAuthDeptList();
		List<SysDataAuthDiySql> authSqlList = sysDataAuthVO.getAuthSqlList();

		// 类型：1：创建人；2：结算主体；3部门
		List<SysDataAuthMenuRel> menuCreateList = sysDataAuthVO.getMenuCreateList();
		List<SysDataAuthMenuRel> menuSettleList = sysDataAuthVO.getMenuSettleList();
		List<SysDataAuthMenuRel> menuDeptList = sysDataAuthVO.getMenuDeptList();
		// 处理权限部门
		if (sysDataAuth.getIsDeptAuth() == 1 || sysDataAuth.getIsDeptAuth() == 2) {
			if (menuDeptList == null || menuDeptList.isEmpty()) {
				return R.failed("请选择部门关联的菜单！");
			} else if (sysDataAuth.getIsDeptAuth() == 2) {
				for (SysDataAuthDeptRel dept : authDeptList) {
					dept.setSysDataAuthId(mainId);
				}
				authDeptRelService.saveOrUpdateBatch(authDeptList);
			}
			for (SysDataAuthMenuRel menu : menuDeptList) {
				menu.setSysDataAuthId(mainId);
				menu.setType(3);
			}
			saveMenuDeptList.addAll(menuSettleList);

		}

		// 处理创建人
		if (menuCreateList != null && !menuCreateList.isEmpty()) {
			sysDataAuth.setIsCreateAuth(1);
			for (SysDataAuthMenuRel menu : menuCreateList) {
				menu.setType(1);
				menu.setSysDataAuthId(mainId);
			}
			saveMenuDeptList.addAll(menuSettleList);
		}

		// 处理结算主体
		if (menuSettleList != null && !menuSettleList.isEmpty()) {
			sysDataAuth.setIsSettleAuth(1);
			for (SysDataAuthMenuRel menu : menuSettleList) {
				menu.setType(2);
				menu.setSysDataAuthId(mainId);
			}
			saveMenuDeptList.addAll(menuSettleList);
		}

		// 处理自定义sql
		if (authSqlList != null && !authSqlList.isEmpty()) {
			sysDataAuth.setIsDiySql(1);
			List<SysDiySqlMenuRel> menuSqlList;
			for (SysDataAuthDiySql sql : authSqlList) {
				menuSqlList = sql.getSqlMenuList();
				if (menuSqlList != null && !menuSqlList.isEmpty()) {
					sql.setSysDataAuthId(mainId);
					diySqlService.save(sql);
					for (SysDiySqlMenuRel menuSql : menuSqlList) {
						menuSql.setSysDataAuthId(mainId);
						menuSql.setSysDiySqlId(sql.getId());
					}
					diySqlMenuRelService.saveBatch(menuSqlList);
				} else {
					return R.failed("自定义Sql有内容，要选择菜单！");
				}
			}
		}
		return null;
	}

	/**
	 * @param mainId 数据权限主表id
	 * @Description: 删除数据权限关联表
	 * @Author: hgw
	 * @Date: 2022/6/9 11:47
	 * @return: void
	 **/
	public void deleteSubInfoByMainId(String mainId) {
		authDeptRelService.deleteAuthDeptRelByAuthId(mainId);
		authMenuRelService.deleteAuthMenuRelByAuthId(mainId);
		diySqlService.deleteDiySqlByAuthId(mainId);
		diySqlMenuRelService.deleteSqlMenuRelByAuthId(mainId);
	}

	/**
	 * 删除
	 *
	 * @param id 部门 ID
	 * @return 成功、失败
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeDataAuthById(String id) {
		this.deleteSubInfoByMainId(id);
		this.removeById(id);
		return Boolean.TRUE;
	}

	/**
	 * @param mainId 权限主表id
	 * @Description: 查看
	 * @Author: hgw
	 * @Date: 2022/6/9 14:52
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDataAuthVO>
	 **/
	@Override
	public R<SysDataAuthVO> getByMainId(String mainId) {
		SysDataAuthVO vo = new SysDataAuthVO();
		SysDataAuth main = this.getById(mainId);
		if (main != null) {
			vo.setSysDataAuth(main);
			// 部门树
			vo.setAuthDeptList(authDeptRelService.getByMainId(mainId));
			// 菜单
			List<SysDataAuthMenuRel> menuList = authMenuRelService.getByMainId(mainId);
			List<SysDataAuthMenuRel> menuCreateList = new ArrayList<>();
			List<SysDataAuthMenuRel> menuSettleList = new ArrayList<>();
			List<SysDataAuthMenuRel> menuDeptList = new ArrayList<>();
			if (menuList != null && !menuList.isEmpty()) {
				for (SysDataAuthMenuRel menu : menuList) {
					switch (menu.getType()) {
						case 1:
							menuCreateList.add(menu);
							break;
						case 2:
							menuSettleList.add(menu);
							break;
						case 3:
							menuDeptList.add(menu);
							break;
						default:
							break;
					}
				}
				vo.setMenuCreateList(menuCreateList);
				vo.setMenuSettleList(menuSettleList);
				vo.setMenuDeptList(menuDeptList);
			}
			// 自定义sql
			List<SysDataAuthDiySql> diyList = diySqlService.getByMainId(mainId);
			if (diyList != null && !diyList.isEmpty()) {
				List<SysDiySqlMenuRel> diyMenuList = diySqlMenuRelService.getByMainId(mainId);
				if (diyMenuList != null && !diyMenuList.isEmpty()) {
					Map<String, List<SysDiySqlMenuRel>> sqlMenuMap = new HashMap<>();
					List<SysDiySqlMenuRel> baseList;
					for (SysDiySqlMenuRel menu : diyMenuList) {
						baseList = sqlMenuMap.get(menu.getSysDiySqlId());
						if (baseList == null) {
							baseList = new ArrayList<>();
						}
						baseList.add(menu);
						sqlMenuMap.put(menu.getSysDiySqlId(), baseList);
					}
					for (SysDataAuthDiySql diy : diyList) {
						if (sqlMenuMap.get(diy.getId()) != null) {
							diy.setSqlMenuList(sqlMenuMap.get(diy.getId()));
						}
					}
				}
				vo.setAuthSqlList(diyList);
			}
			return new R<>(vo);
		} else {
			return R.failed("未找到！");
		}
	}

}
