package cn.iocoder.yudao.module.llm.controller.admin.learningresources;

import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.llm.controller.admin.learningresources.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.learningresources.LearningResourcesDO;
import cn.iocoder.yudao.module.llm.service.learningresources.LearningResourcesService;

@Tag(name = "管理后台 - 学习资源")
@RestController
@RequestMapping("/llm/learning-resources")
@Validated
public class LearningResourcesController {

    @Resource
    private LearningResourcesService learningResourcesService;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建学习资源")
//    @PreAuthorize("@ss.hasPermission('llm:learning-resources:create')")
    public CommonResult<Long> createLearningResources(@Valid @RequestBody LearningResourcesSaveReqVO createReqVO) {
        return success(learningResourcesService.createLearningResources(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学习资源")
//    @PreAuthorize("@ss.hasPermission('llm:learning-resources:update')")
    public CommonResult<Boolean> updateLearningResources(@Valid @RequestBody LearningResourcesSaveReqVO updateReqVO) {
        learningResourcesService.updateLearningResources(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学习资源")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:learning-resources:delete')")
    public CommonResult<Boolean> deleteLearningResources(@RequestParam("id") Long id) {
        learningResourcesService.deleteLearningResources(id);
        return success(true);
    }

    @GetMapping("/down/{id}")
    @Operation(summary = "学习资源下载")
    @PermitAll
    public CommonResult<String> downLearningResources(@PathVariable  Long id) {
        return success(learningResourcesService.downLearningResources(id));
    }
    @GetMapping("/get")
    @Operation(summary = "获得学习资源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:learning-resources:query')")
    public CommonResult<LearningResourcesRespVO> getLearningResources(@RequestParam("id") Long id) {

        LearningResourcesDO learningResources = learningResourcesService.getLearningResources(id);
        return success(BeanUtils.toBean(learningResources, LearningResourcesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学习资源分页")
//    @PreAuthorize("@ss.hasPermission('llm:learning-resources:query')")
    public CommonResult<PageResult<LearningResourcesRespVO>> getLearningResourcesPage(@Valid LearningResourcesPageReqVO pageReqVO) {
        PageResult<LearningResourcesDO> pageResult = learningResourcesService.getLearningResourcesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LearningResourcesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学习资源 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:learning-resources:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLearningResourcesExcel(@Valid LearningResourcesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LearningResourcesDO> list = learningResourcesService.getLearningResourcesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学习资源.xls", "数据", LearningResourcesRespVO.class,
                        BeanUtils.toBean(list, LearningResourcesRespVO.class));
    }

    @GetMapping("/getUserNumber")
    @Operation(summary = "获得用户数量")
    public CommonResult<Long> getNumber() {
        Long aLong = adminUserApi.getUserNumber();
        return success(aLong);
    }

    @GetMapping("/getContainerNumber")
    @Operation(summary = "获得容器数量")
    public CommonResult<Long> getContainerNumber() {
        return success(learningResourcesService.getContainerNumber());
    }

    @PutMapping("/videoViewsUpdate")
    @Operation(summary = "视频查看量更新")
    //    @PreAuthorize("@ss.hasPermission('llm:learning-resources:update')")
    public CommonResult<Boolean> videoViewsUpdate(@Valid @RequestBody LearningResourcesSaveReqVO updateReqVO) {
        learningResourcesService.videoViewsUpdate(updateReqVO);
        return success(true);
    }
}
