package com.gxwebsoft.ai.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.ai.service.AiCloudDocService;
import com.gxwebsoft.ai.service.AiCloudFileService;

import cn.hutool.core.util.StrUtil;

import com.gxwebsoft.ai.entity.AiCloudDoc;
import com.gxwebsoft.ai.entity.AiCloudFile;
import com.gxwebsoft.ai.param.AiCloudDocParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.system.entity.User;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * AI云文档目录表控制器
 *
 * @author yc
 */
@Tag(name = "AI云文档目录表管理")
@RestController
@RequestMapping("/api/ai/doc")
public class AiCloudDocController extends BaseController {
    @Resource
    private AiCloudDocService aiCloudDocService;
    @Resource
    private AiCloudFileService aiCloudFileService;

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:list')")
    @Operation(summary = "分页查询AI云文档目录表")
    @GetMapping("/page")
    public ApiResult<PageResult<AiCloudDoc>> page(AiCloudDocParam param) {
        // 使用关联查询
        return success(aiCloudDocService.pageRel(param));
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:list')")
    @Operation(summary = "查询全部AI云文档目录表")
    @GetMapping()
    public ApiResult<List<AiCloudDoc>> list(AiCloudDocParam param) {
        // 使用关联查询
        return success(aiCloudDocService.listRel(param));
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:list')")
    @Operation(summary = "根据id查询AI云文档目录表")
    @GetMapping("/{id}")
    public ApiResult<AiCloudDoc> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(aiCloudDocService.getByIdRel(id));
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:list')")
    @Operation(summary = "根据ids查询AI云文档目录表")
    @GetMapping("/byIds/{ids}")
    public ApiResult<List<AiCloudDoc>> getByIds(@PathVariable("ids") String ids) {
        List<String> idList = StrUtil.split(ids, ',');
        List<AiCloudDoc> ret = aiCloudDocService.list(new LambdaQueryWrapper<AiCloudDoc>().in(AiCloudDoc::getId, idList));
        return success(ret);
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:save')")
    @Operation(summary = "添加AI云文档目录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AiCloudDoc aiCloudDoc) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            aiCloudDoc.setUserId(loginUser.getUserId());
        }
        if(aiCloudDoc.getParentId()>0) {
        	AiCloudDoc aiCloudDocParent = aiCloudDocService.getByIdRel(aiCloudDoc.getParentId());
        	aiCloudDoc.setCategoryId(aiCloudDocParent.getCategoryId());
        	aiCloudDoc.setCompanyId(aiCloudDocParent.getCompanyId());
        }
        if (aiCloudDocService.save(aiCloudDoc)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:update')")
    @Operation(summary = "修改AI云文档目录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AiCloudDoc aiCloudDoc) {
        if (aiCloudDocService.updateById(aiCloudDoc)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:remove')")
    @Operation(summary = "删除AI云文档目录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
    	if(aiCloudFileService.count(new LambdaQueryWrapper<AiCloudFile>().eq(AiCloudFile::getDocId, id))>0) {
    		return fail("删除失败，当前目录下存在文件");
    	}
        if (aiCloudDocService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:save')")
    @Operation(summary = "批量添加AI云文档目录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AiCloudDoc> list) {
        if (aiCloudDocService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:update')")
    @Operation(summary = "批量修改AI云文档目录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AiCloudDoc> batchParam) {
        if (batchParam.update(aiCloudDocService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    //@PreAuthorize("hasAuthority('ai:aiCloudDoc:remove')")
    @Operation(summary = "批量删除AI云文档目录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (aiCloudDocService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
