/*
 * Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
 *
 * Licensed under the GNU Affero General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.gnu.org/licenses/agpl-3.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.iocoder.yudao.module.langchat.controller.admin.aigc;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.langchat.annotation.ApiLog;
import cn.iocoder.yudao.module.langchat.entity.AigcDocs;
import cn.iocoder.yudao.module.langchat.mapper.AigcDocsMapper;
import cn.iocoder.yudao.module.langchat.utils.MybatisUtil;
import cn.iocoder.yudao.module.langchat.utils.QueryPage;
import cn.iocoder.yudao.module.langchat.service.core.EmbeddingService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tycoding
 * @since 2024/4/15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/chat/aigc/docs")
public class AigcDocsController {

    private final AigcDocsMapper docsMapper;
    private final EmbeddingService embeddingService;

    @GetMapping("/list")
    @TenantIgnore
    public CommonResult<List<AigcDocs>> list(AigcDocs data) {
        return CommonResult.success(docsMapper.selectList(Wrappers.<AigcDocs>lambdaQuery().orderByDesc(AigcDocs::getCreateTime)));
    }

    @GetMapping("/page")
    @TenantIgnore
    public CommonResult list(AigcDocs data, QueryPage queryPage) {
        Page<AigcDocs> page = new Page<>(queryPage.getPageNo(), queryPage.getPageSize());
        return CommonResult.success(MybatisUtil.getData(docsMapper.selectPage(page, Wrappers.<AigcDocs>lambdaQuery()
                .eq(data.getKnowledgeId() != null, AigcDocs::getKnowledgeId, data.getKnowledgeId())
                .eq(data.getSliceStatus() != null, AigcDocs::getSliceStatus, data.getSliceStatus())
                .orderByDesc(AigcDocs::getCreateTime)
        )));
    }

    @GetMapping("/{id}")
    @TenantIgnore
    public CommonResult<AigcDocs> findById(@PathVariable String id) {
        return CommonResult.success(docsMapper.selectById(id));
    }

    @PostMapping
    @ApiLog("新增文档")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:docs:add')")
    public CommonResult add(@RequestBody AigcDocs data) {
        docsMapper.insert(data);
        return CommonResult.success("success");
    }

    @PutMapping
    @ApiLog("修改文档")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:docs:update')")
    public CommonResult update(@RequestBody AigcDocs data) {
        docsMapper.updateById(data);
        return CommonResult.success("success");
    }

    @DeleteMapping("/{id}")
    @ApiLog("删除文档")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:docs:delete')")
    @Transactional
    public CommonResult delete(@PathVariable String id) {
        // 删除切面数据
        embeddingService.clearDocSlices(id);

        // 删除文档
        docsMapper.deleteById(id);
        return CommonResult.success("success");
    }
}

