package com.fdkankan.ucenter.controller;


import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.Folder;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.service.IFolderService;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.vo.request.FolderParam;
import com.fdkankan.ucenter.vo.request.RequestFolder;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文件夹表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/ucenter/user/folder")
public class FolderController extends BaseController {

    @Autowired
    IFolderService folderService;
    @Autowired
    IUserService userService;

    /**
     * 新增文件夹
     * name   文件夹名称
     * type   文件夹类型，0我的场景，1协作场景
     * parentId   上层文件夹id
     */
    @PostMapping("/save")
    public Result save(@RequestBody FolderParam param){
        param.setUserName(getUserName());
        folderService.add(param);
        return Result.success();
    }

    /**
     * 修改文件夹
     */
    @PostMapping("/update")
    public Result update(@RequestBody FolderParam param){
        param.setUserName(getUserName());
        folderService.updateByParam(param);
        return Result.success();
    }

    /**
     * 删除文件夹
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody FolderParam param){
        folderService.delete(param.getId(),getUserName());
        return Result.success();
    }

    /**
     * 获取该用户所有文件夹树结构
     * type   文件夹类型，0我的场景，1协作场景
     */
    @PostMapping("/findListTree")
    public Result findListTree(@RequestBody FolderParam param){
        return Result.success(folderService.findListTree(param.getFolderIds(),param.getType(),getUserName()));
    }

    /**
     * 移动文件夹或场景
     */
    @PostMapping("/move")
    public Result move(@RequestBody FolderParam param){
        folderService.move(param,getUserName());
        return Result.success();
    }

    /**
     * 根据文件夹名字获取该用户所有文件
     */
    @RequestMapping(value = "/findListByName", method = RequestMethod.POST)
    public Result findListByName(@RequestBody RequestFolder param){
        User user = userService.getByToken(getToken());
        if(user == null){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
        }

        if(param.getType() == null){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }

        List<Folder> list = folderService.findByUserIdAndType(user.getId(), param.getType(), param.getName(),param.getFolderIds());

        return Result.success(list);
    }
}

