/*
 *      Copyright (c) 2018-2028, Chill Zhuang 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 dreamlu.net 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: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.manager.service.impl;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.manager.mapper.WbsTreeMapper;
import org.springblade.manager.service.IWbsTreeService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.manager.entity.WbsFormElement;
import org.springblade.manager.entity.WbsTree;
import org.springblade.manager.vo.WbsNodeTableVO;
import org.springblade.manager.vo.WbsTreeVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.*;

/**
 *  服务实现类
 *
 * @author liuyc
 * @since 2022-04-25
 */
@Service
public class WbsTreeServiceImpl extends BaseServiceImpl<WbsTreeMapper,WbsTree> implements IWbsTreeService {

	@Resource
	private WbsTreeMapper wbsTreeMapper;

	@Override
	public IPage<WbsTreeVO> selectWbsTreePage(IPage<WbsTreeVO> page, WbsTreeVO wbsTree) {
		return page.setRecords(baseMapper.selectWbsTreePage(page, wbsTree));
	}

	@Override
	public List<WbsTreeVO> lazyTree(String tenantId, Long parentId) {
		if (AuthUtil.isAdministrator()) {
			tenantId = StringPool.EMPTY;
		}
		return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId));
	}

	@Override
	public List<WbsTreeVO> tree(String tenantId) {
		return ForestNodeMerger.merge(baseMapper.tree(tenantId));
	}

	@Override
	public boolean submit(WbsTree dept) {
		if (Func.isEmpty(dept.getParentId())) {
			dept.setTenantId(AuthUtil.getTenantId());
			dept.setParentId(BladeConstant.TOP_PARENT_ID);
			dept.setAncestors(String.valueOf(BladeConstant.TOP_PARENT_ID));
		}
		if (dept.getParentId() > 0) {
			WbsTree parent = getById(dept.getParentId());
			if (Func.toLong(dept.getParentId()) == Func.toLong(dept.getId())) {
				throw new ServiceException("父节点不可选择自身!");
			}
			dept.setTenantId(parent.getTenantId());
			String ancestors = parent.getAncestors() + StringPool.COMMA + dept.getParentId();
			dept.setAncestors(ancestors);
		}
		dept.setIsDeleted(BladeConstant.DB_NOT_DELETED);
		return saveOrUpdate(dept);
	}

	@Override
	public List<WbsNodeTableVO> selectByNodeTable(String id) {
		//查询节点下的所有表单
		List<WbsNodeTableVO> wbsNodeTableVOS = wbsTreeMapper.selectByNodeTable(id);
		/*List<String> ids = new ArrayList<>();
		for (int i = 0; i < wbsNodeTableVOS.size(); i++) {
			String id1 = wbsNodeTableVOS.get(i).getId();
			ids.add(id1+",");
			if ( i == (wbsNodeTableVOS.size()-1)){
				ids.remove(",");
			}
		}
		//根据ID查询元素
		List<String> strings = wbsTreeMapper.selectByElementsTotal(ids);*/
		return wbsNodeTableVOS;
	}

	@Override
	public boolean removeTableById(String id) {
		Integer integer = wbsTreeMapper.removeTableById(id);
		if (integer > 0){
			return true;
		}
		return false;
	}

	@Override
	public List<WbsFormElement> selectFormElements(String id) {
		List<WbsFormElement> wbsFormElements = wbsTreeMapper.selectFormElements(id);
		return wbsFormElements;
	}




}
