package org.springblade.manager.controller;

import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.exception.ServiceException;
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.utils.Func;
import org.springblade.manager.dto.ArchiveTreeDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.manager.entity.ArchiveTree;
import org.springblade.manager.vo.ArchiveTreeVO;
import org.springblade.manager.service.IArchiveTreeService;
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-06-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/archiveTree")
@Api(value = "归档树管理", tags = "归档树管理接口")
public class ArchiveTreeController extends BladeController {

    private final IArchiveTreeService archiveTreeService;

    /**
     * 初始化归档树根节点
     */
    @PostMapping("/init")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "初始化归档树根节点")
    public R<ArchiveTree> init() {
        boolean b = archiveTreeService.initArchiveTree();
        if (b) {
            return R.success("初始化创建成功");
        }
        return R.fail(200, "初始化创建失败");
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "详情", notes = "传入archiveTree")
    public R<ArchiveTree> detail(ArchiveTree archiveTree) {
        ArchiveTree detail = archiveTreeService.getOne(Condition.getQueryWrapper(archiveTree));
        return R.data(detail);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增", notes = "传入ArchiveTreeDTO")
    public R submit(@Valid @RequestBody ArchiveTreeDTO archiveTreeDTO) {
        if (archiveTreeService.submit(archiveTreeDTO)) {
            CacheUtil.clear(SYS_CACHE);
            return R.data(archiveTreeDTO);
        }
        return R.fail(200, "操作失败");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改", notes = "传入ArchiveTree")
    public R update(@Valid @RequestBody ArchiveTree archiveTree) {
        return R.status(archiveTreeService.updateById(archiveTree));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "逻辑删除", notes = "传入id")
    public R remove(@ApiParam(value = "id", required = true) @RequestParam String id) {
        List<ArchiveTree> archiveTrees = archiveTreeService.selectByParentId(id);
        archiveTrees.stream().forEach(archiveTree -> {
            if (archiveTree.getParentId() == Long.parseLong(id)) {
                throw new ServiceException("当前节点下存在子节点,删除失败");
            }
        });
        return R.status(archiveTreeService.deleteLogic(Func.toLongList(id)));
    }

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

    /**
     * 全加载树形结构/显示树
     */
    @GetMapping("/tree")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "全加载树形结构/显示树", notes = "传入租户id、token、或disPlayTree=1加载显示树、nodeType")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "token", value = "token", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户id", required = true),
            @ApiImplicitParam(name = "disPlayTree", value = "是否加载显示树 'null'=不加载 '1'=加载"),
            @ApiImplicitParam(name = "nodeType", value = "'null'=全加载 '1'=关联电子原生文件类型树 '2'=文件上传类型树")
    })
    public R<List<ArchiveTreeVO>> tree(String tenantId, BladeUser bladeUser, Integer disPlayTree, Integer nodeType) {
        List<ArchiveTreeVO> tree = archiveTreeService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()), disPlayTree, nodeType);
        if (tree != null && tree.size() > 0) {
            return R.data(tree);
        }
        return R.fail(200, "未查询到信息");
    }

    /**
     * 保存/修改上传文件显示配置树
     */
    @PostMapping("/submitDisplayConfigTree")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "保存/修改上传文件显示配置树", notes = "传入ids")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids", value = "ids", required = true)
    })
    public R<List<ArchiveTreeVO>> submitDisplayConfigTree(@RequestBody String ids) {
        return R.status(archiveTreeService.submitDisplayConfigTree(ids));
    }


}
