package org.springblade.manager.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
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.ArchiveTreeDTO;
import org.springblade.manager.dto.WbsTreeDTO;
import org.springblade.manager.entity.ArchiveTree;
import org.springblade.manager.entity.WbsTree;
import org.springblade.manager.service.IArchiveTreeService;
import org.springblade.manager.vo.ArchiveTreeVO;
import org.springblade.manager.vo.WbsTreeVO;
import org.springblade.manager.wrapper.ArchiveTreeWrapper;
import org.springblade.manager.wrapper.WbsTreeWrapper;
import org.springblade.system.cache.DictCache;
import org.springblade.system.enums.DictEnum;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

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

/**
 * 归档树节点控制层
 *
 * @author liuyc
 * @since 2022-05-16
 */

@RestController
@AllArgsConstructor
@RequestMapping("/archiveTree")
@Api(value = "归档树配置", tags = "归档树配置接口")
public class ArchiveTreeController extends BladeController {

    private final IArchiveTreeService archiveTreeService;

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

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

    /**
     * 获取项目节点树形结构 (新增、修改加载、分配合同wbs树加载type=1)
     *
     * @return
     */
    @GetMapping("/tree")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "归档节点树形结构", notes = "传入租户Id、roleTypeNum、请求头token")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "tenantId", value = "租户id", required = true),
            @ApiImplicitParam(name = "roleTypeNum", value = "角色类型", required = true)
    })
    public R<List<ArchiveTreeVO>> tree(String roleTypeNum, String tenantId, BladeUser bladeUser) {
        List<ArchiveTreeVO> tree = archiveTreeService.tree(roleTypeNum , 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(@Valid @RequestBody ArchiveTreeDTO archiveTreeDTO) {
        if (archiveTreeService.submit(archiveTreeDTO)) {
            CacheUtil.clear(SYS_CACHE);
            // 返回懒加载树更新节点所需字段
            Kv kv = Kv.create().set("id", String.valueOf(archiveTreeDTO.getId())).set("tenantId", archiveTreeDTO.getTenantId());
            return R.data(kv);
        }
        return R.fail("操作失败");
    }


}
