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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskauto.vo.ModelAssessTaskAutoInfoPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskauto.vo.ModelAssessTaskAutoInfoRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskauto.vo.ModelAssessTaskAutoInfoSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskautoinfo.ModelAssessTaskAutoInfoDO;
import cn.iocoder.yudao.module.llm.service.modelassesstaskautoinfo.ModelAssessTaskAutoInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 模型评估任务 —— 自动评估-详细内容")
@RestController
@RequestMapping("/llm/model-assess-task-auto-info")
@Validated
@Slf4j
public class ModelAssessTaskAutoInfoController {

    @Resource
    private ModelAssessTaskAutoInfoService modelAssessTaskAutoInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建模型评估任务 —— 自动评估-详细内容")
    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:create')")
    public CommonResult<Long> createModelAssessTaskAutoInfo(@Valid @RequestBody ModelAssessTaskAutoInfoSaveReqVO createReqVO) {
        return success(modelAssessTaskAutoInfoService.createModelAssessTaskAutoInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型评估任务 —— 自动评估-详细内容")
    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:update')")
    public CommonResult<Boolean> updateModelAssessTaskAutoInfo(@Valid @RequestBody ModelAssessTaskAutoInfoSaveReqVO updateReqVO) {
        modelAssessTaskAutoInfoService.updateModelAssessTaskAutoInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型评估任务 —— 自动评估-详细内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:delete')")
    public CommonResult<Boolean> deleteModelAssessTaskAutoInfo(@RequestParam("id") Long id) {
        modelAssessTaskAutoInfoService.deleteModelAssessTaskAutoInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型评估任务 —— 自动评估-详细内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:query')")
    public CommonResult<ModelAssessTaskAutoInfoRespVO> getModelAssessTaskAutoInfo(@RequestParam("id") Long id) {
        ModelAssessTaskAutoInfoDO modelAssessTaskAutoInfo = modelAssessTaskAutoInfoService.getModelAssessTaskAutoInfo(id);
        return success(BeanUtils.toBean(modelAssessTaskAutoInfo, ModelAssessTaskAutoInfoRespVO.class));
    }

    @GetMapping("/get/count")
    @Operation(summary = "获得雷达图数据")
    public CommonResult<Map<Integer,Double>> count(@RequestParam("taskId") Long taskId) {
        ModelAssessTaskAutoInfoPageReqVO modelAssessTaskAutoInfoPageReqVO = new ModelAssessTaskAutoInfoPageReqVO();
        modelAssessTaskAutoInfoPageReqVO.setTaskId(taskId);
        List<ModelAssessTaskAutoInfoDO> list = modelAssessTaskAutoInfoService.getListByTaskId(modelAssessTaskAutoInfoPageReqVO);
        List<Integer> res = list.stream()
                .map(ModelAssessTaskAutoInfoDO::getEmotionTendency)
                .collect(Collectors.toList());
        int size = list.size();
        double  zero = 0;
        double  one = 0;
        double  two = 0;
        for (Integer re : res) {
            if(re == 0){
                zero++;
            }else if(re == 1){
                one++;
            }else if(re == 2){
                two++;
            }
        }
        Map<Integer,Double> map = new HashMap<>();
        map.put(0,zero * 100 / size);
        map.put(1,one * 100 / size);
        map.put(2,two * 100 / size);
        return success(map);
    }


    @GetMapping("/list")
    @Operation(summary = "根绝任务id获得模型评估任务-自动评估")
    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:query')")
    public CommonResult<List<ModelAssessTaskAutoInfoRespVO>> list(@Valid ModelAssessTaskAutoInfoPageReqVO pageReqVO) {
        List<ModelAssessTaskAutoInfoDO> list = modelAssessTaskAutoInfoService.getListByTaskId(pageReqVO);
        return success(BeanUtils.toBean(list, ModelAssessTaskAutoInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型评估任务 —— 自动评估-详细内容分页")
    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:query')")
    public CommonResult<PageResult<ModelAssessTaskAutoInfoRespVO>> getModelAssessTaskAutoInfoPage(@Valid ModelAssessTaskAutoInfoPageReqVO pageReqVO) {
        PageResult<ModelAssessTaskAutoInfoDO> pageResult = modelAssessTaskAutoInfoService.getModelAssessTaskAutoInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ModelAssessTaskAutoInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型评估任务 —— 自动评估-详细内容 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-auto-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportModelAssessTaskAutoInfoExcel(@Valid ModelAssessTaskAutoInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        
        try {
            // 设置不分页
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);

            // 获取数据
            List<ModelAssessTaskAutoInfoDO> dataList = modelAssessTaskAutoInfoService
                    .getModelAssessTaskAutoInfoPage(pageReqVO)
                    .getList();

            // 准备导出数据
            List<ModelAssessTaskAutoInfoRespVO> exportData = BeanUtils.toBean(dataList, ModelAssessTaskAutoInfoRespVO.class);

            // 写入Excel
            ExcelUtils.write(response, "模型评估任务 —— 自动评估-详细内容.xls", "数据", ModelAssessTaskAutoInfoRespVO.class, exportData);

            log.info("Excel导出完成");

        } catch (IOException e) {
            log.error("导出Excel时发生IO异常", e);
            throw new RuntimeException("导出失败", e);
        } catch (Exception e) {
            log.error("导出Excel时发生意外错误", e);
            throw new RuntimeException("导出失败", e);
        }

    }

}
