package com.cy.guoyan.admin.module.ai.controller.admin.chatapp;

import com.cy.guoyan.admin.module.ai.controller.admin.chatapp.vo.ChatAppPageReqVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatapp.vo.ChatAppRespVO;
import com.cy.guoyan.admin.module.ai.controller.admin.chatapp.vo.ChatAppSaveReqVO;
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.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import com.cy.guoyan.admin.framework.common.pojo.PageParam;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.pojo.CommonResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import static com.cy.guoyan.admin.framework.common.pojo.CommonResult.success;
import com.cy.guoyan.admin.framework.excel.core.util.ExcelUtils;
import com.cy.guoyan.admin.framework.apilog.core.annotation.ApiAccessLog;
import static com.cy.guoyan.admin.framework.apilog.core.enums.OperateTypeEnum.*;
import com.cy.guoyan.admin.module.ai.dal.dataobject.chatapp.ChatAppDO;
import com.cy.guoyan.admin.module.ai.service.chatapp.ChatAppService;

@Tag(name = "管理后台 - AI 应用")
@RestController
@RequestMapping("/chatAi/chat-app")
@Validated
public class ChatAppController {

    @Resource
    private ChatAppService chatAppService;

    @PostMapping("/create")
    @Operation(summary = "创建AI 应用")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-app:create')")
    public CommonResult<Long> createChatApp(@Valid @RequestBody ChatAppSaveReqVO createReqVO) {
        return success(chatAppService.createChatApp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI 应用")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-app:update')")
    public CommonResult<Boolean> updateChatApp(@Valid @RequestBody ChatAppSaveReqVO updateReqVO) {
        chatAppService.updateChatApp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI 应用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('chatAi:chat-app:delete')")
    public CommonResult<Boolean> deleteChatApp(@RequestParam("id") Long id) {
        chatAppService.deleteChatApp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI 应用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-app:query')")
    public CommonResult<ChatAppRespVO> getChatApp(@RequestParam("id") Long id) {
        ChatAppDO chatApp = chatAppService.getChatApp(id);
        return success(BeanUtils.toBean(chatApp, ChatAppRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI 应用分页")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-app:query')")
    public CommonResult<PageResult<ChatAppRespVO>> getChatAppPage(@Valid ChatAppPageReqVO pageReqVO) {
        PageResult<ChatAppDO> pageResult = chatAppService.getChatAppPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatAppRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI 应用 Excel")
    @PreAuthorize("@ss.hasPermission('chatAi:chat-app:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChatAppExcel(@Valid ChatAppPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChatAppDO> list = chatAppService.getChatAppPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI 应用.xls", "数据", ChatAppRespVO.class,
                        BeanUtils.toBean(list, ChatAppRespVO.class));
    }

}