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

import cn.iocoder.yudao.module.llm.controller.admin.datarefluxconfig.vo.DataRefluxConfigPageReqVO;
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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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.datarefluxdata.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.datarefluxdata.DataRefluxDataDO;
import cn.iocoder.yudao.module.llm.service.datarefluxdata.DataRefluxDataService;

@Tag(name = "管理后台 - 数据回流 —— 数据")
@RestController
@RequestMapping("/llm/data-reflux-data")
@Validated
public class DataRefluxDataController {

    @Resource
    private DataRefluxDataService dataRefluxDataService;

    @PostMapping("/create")
    @Operation(summary = "创建数据回流 —— 数据")
//    @PreAuthorize("@ss.hasPermission('llm:data-reflux-data:create')")
    public CommonResult<Long> createDataRefluxData(@Valid @RequestBody DataRefluxDataSaveReqVO createReqVO) {
        return success(dataRefluxDataService.createDataRefluxData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据回流 —— 数据")
//    @PreAuthorize("@ss.hasPermission('llm:data-reflux-data:update')")
    public CommonResult<Boolean> updateDataRefluxData(@Valid @RequestBody DataRefluxDataSaveReqVO updateReqVO) {
        dataRefluxDataService.updateDataRefluxData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据回流 —— 数据")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:data-reflux-data:delete')")
    public CommonResult<Boolean> deleteDataRefluxData(@RequestParam("id") Long id) {
        dataRefluxDataService.deleteDataRefluxData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据回流 —— 数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:data-reflux-data:query')")
    public CommonResult<DataRefluxDataRespVO> getDataRefluxData(@RequestParam("id") Long id) {
        //TODO 查看按钮后面没具体内容
        DataRefluxDataDO dataRefluxData = dataRefluxDataService.getDataRefluxData(id);
        return success(BeanUtils.toBean(dataRefluxData, DataRefluxDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据回流 —— 数据分页")
//    @PreAuthorize("@ss.hasPermission('llm:data-reflux-data:query')")
    public CommonResult<PageResult<DataRefluxDataRespVO>> getDataRefluxDataPage(@Valid DataRefluxDataPageReqVO pageReqVO) {
        PageResult<DataRefluxDataDO> pageResult = dataRefluxDataService.getDataRefluxDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataRefluxDataRespVO.class));
    }

    @GetMapping("/review-the-data")
    @Operation(summary = "数据回流配置保存数据")
    public CommonResult<Boolean> saveTheData(@Valid DataRefluxDataReqVO pageReqVO){
        if (pageReqVO.getDataid() != null){
            pageReqVO.setDataids(Collections.singletonList(pageReqVO.getDataid()));
        }
        dataRefluxDataService.getDataRefluxDataIDs(pageReqVO);
        return success(true);
    }

    @GetMapping("/save-all-the-data")
    @Operation(summary = "数据回流配置保存全部数据")
    public CommonResult<Boolean> saveAllTheData(@Valid DataRefluxDataReqVO pageReqVO, DataRefluxConfigPageReqVO page){
        page.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DataRefluxDataDO> list = dataRefluxDataService.getTheDetails(page);
        List<DataRefluxDataDO> doList = list.getList();
        List<Long> collect = doList.stream().map(DataRefluxDataDO::getId).collect(Collectors.toList());
        pageReqVO.setDataids(collect);
        dataRefluxDataService.getDataRefluxDataIDs(pageReqVO);
        return success(true);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出数据回流 —— 数据 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:data-reflux-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataRefluxDataExcel(@Valid DataRefluxDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataRefluxDataDO> list = dataRefluxDataService.getDataRefluxDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据回流 —— 数据.xls", "数据", DataRefluxDataRespVO.class,
                        BeanUtils.toBean(list, DataRefluxDataRespVO.class));
    }

}
