/*
 * 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.AigcDocsSlice;
import cn.iocoder.yudao.module.langchat.mapper.AigcDocsSliceMapper;
import cn.iocoder.yudao.module.langchat.utils.MybatisUtil;
import cn.iocoder.yudao.module.langchat.utils.QueryPage;
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.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

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

    private final AigcDocsSliceMapper docsSliceMapper;

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

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

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

    @PostMapping
    @ApiLog("新增切片数据")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:docs:slice:add')")
    public CommonResult add(@RequestBody AigcDocsSlice data) {
        data.setCreateTime(new Date());
        docsSliceMapper.insert(data);
        return CommonResult.success("success");
    }

    @PutMapping
    @ApiLog("修改切片数据")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:docs:slice:update')")
    public CommonResult update(@RequestBody AigcDocsSlice data) {
        docsSliceMapper.updateById(data);
        return CommonResult.success("success");
    }

    @DeleteMapping("/{id}")
    @ApiLog("删除切片数据")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:docs:slice:delete')")
    public CommonResult delete(@PathVariable String id) {
        docsSliceMapper.deleteById(id);
        return CommonResult.success("success");
    }
}

