/*
 *      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.controller;


import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.dto.WbsTreeDTO;
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.springblade.system.cache.DictCache;
import org.springblade.system.enums.DictEnum;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.manager.wrapper.WbsTreeWrapper;
import org.springblade.manager.service.IWbsTreeService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * 控制器
 *
 * @author liuyc
 * @since 2022-04-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wbsTree")
@Api(value = "WBS动态库节点", tags = "WBS动态库节点")
public class WbsTreeController extends BladeController {

	private final IWbsTreeService wbsTreeService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入WbsTreeId,请求头token")
	@ApiImplicitParam(name = "id", value = "id", required = true)
	public R<WbsTreeVO> detail(WbsTree wbsTree) {
		WbsTree detail = wbsTreeService.getOne(Condition.getQueryWrapper(wbsTree));
		if (detail != null) {
			return R.data(WbsTreeWrapper.build().entityVO(detail));
		}
		return R.fail(200, "未查询到信息");
	}

	/**
	 * 懒加载获取项目节点树形结构
	 */
	@GetMapping("/lazy-tree")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "懒加载节点树形结构", notes = "传入tenantId，parentId，请求头token")
	@ApiImplicitParams(value = {
		@ApiImplicitParam(name = "tenantId", value = "租户id", required = true),
		@ApiImplicitParam(name = "parentId", value = "父级id", required = true),
	})
	public R<List<WbsTreeVO>> lazyTree(String tenantId, Long parentId, BladeUser bladeUser) {
		List<WbsTreeVO> tree = wbsTreeService.lazyTree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()), parentId);
		if (tree != null && tree.size() > 0) {
			return R.data(tree);
		}
		return R.fail(200, "未查询到信息");
	}

	/**
	 * 获取项目节点树形结构 (新增、修改加载)
	 *
	 * @return
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "节点树形结构", notes = "传入tenantId，请求头token")
	@ApiImplicitParams(value = {
		@ApiImplicitParam(name = "tenantId", value = "租户id", required = true),
	})
	public R<List<WbsTreeVO>> tree(String tenantId, BladeUser bladeUser) {
		List<WbsTreeVO> tree = wbsTreeService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
		if (tree != null && tree.size() > 0) {
			return R.data(tree);
		}
		return R.fail(200, "未查询到信息");
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "WbsTreeDTO")
	public R submit(@RequestBody WbsTreeDTO WbsTreeDTO) {
		if (wbsTreeService.submit(WbsTreeDTO)) {
			CacheUtil.clear(SYS_CACHE);
			// 返回懒加载树更新节点所需字段
			Kv kv = Kv.create().set("id", String.valueOf(WbsTreeDTO.getId())).set("tenantId", WbsTreeDTO.getTenantId())
				.set("deptCategoryName", DictCache.getValue(DictEnum.ORG_CATEGORY, WbsTreeDTO.getDeptCategory()));
			return R.data(kv);
		}
		return R.fail("操作失败");
	}

	/**
	 * 逻辑删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(wbsTreeService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 表单查询（根据节点ID查询当前表单）
	 */
	@GetMapping("/selectByNodeTable")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "表单查询", notes = "传入父节点id")
	public R<List<WbsNodeTableVO>> selectByNodeTable(@ApiParam(value = "父节点id", required = true) @RequestParam String id) {
		List<WbsNodeTableVO> rs = wbsTreeService.selectByNodeTable(id);
		if (!("").equals(rs) && rs.size() > 0) {
			return R.data(rs);
		}
		return R.fail(200, "未查询到元素");
	}

	/**
	 * 删除表单
	 */
	@GetMapping("/removeTableById")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "表单删除", notes = "传入id")
	@ApiImplicitParam(name = "id",value = "表单id",required = true)
	public R removeTableById(@RequestParam("id") String id) {
		boolean restul = wbsTreeService.removeTableById(id);
		if (restul){
			return R.success("删除成功");
		}
		return R.fail("删除失败");
	}

	/**
	 * 表单元素列表
	 */
	@GetMapping("/selectFormElements")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "表单元素列表", notes = "传入表单id")
	@ApiImplicitParam(name = "id",value = "表单id",required = true)
	public R<List<WbsFormElement>> selectFormElements(@RequestParam("id") String id) {
		List<WbsFormElement> wbsFormElements = wbsTreeService.selectFormElements(id);
		if (wbsFormElements.size()>0 && !"".equals(wbsFormElements)){
			return R.data(wbsFormElements);
		}
		return R.data(wbsFormElements,"未查询到元素");
	}
}


