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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
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.dal.dataobject.modelassesstaskautoinfo.ModelAssessTaskAutoInfoBackupDO;
import cn.iocoder.yudao.module.llm.service.modelassesstaskautoinfo.ModelAssessTaskAutoInfoBackupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

    @Resource
    private ModelAssessTaskAutoInfoBackupService modelAssessTaskAutoInfoBackupService;

    @GetMapping("/get/count")
    @Operation(summary = "获得雷达图数据")
    public CommonResult<Map<Integer,Double>> count(@RequestParam("taskId") Long taskId) {
        ModelAssessTaskAutoInfoPageReqVO modelAssessTaskAutoInfoPageReqVO = new ModelAssessTaskAutoInfoPageReqVO();
        modelAssessTaskAutoInfoPageReqVO.setTaskId(taskId);
        List<ModelAssessTaskAutoInfoBackupDO> list = modelAssessTaskAutoInfoBackupService.getListByTaskId(modelAssessTaskAutoInfoPageReqVO);
        List<Integer> res = list.stream()
                .map(ModelAssessTaskAutoInfoBackupDO::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<ModelAssessTaskAutoInfoBackupDO> list = modelAssessTaskAutoInfoBackupService.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<ModelAssessTaskAutoInfoBackupDO> pageResult = modelAssessTaskAutoInfoBackupService.getModelAssessTaskAutoInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ModelAssessTaskAutoInfoRespVO.class));
    }


}
