package com.cmvr.test.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.test.mapper.TeAiEvaluationMapper;
import com.cmvr.test.model.domain.TeAiEvaluation;
import com.cmvr.test.service.ITeAiEvaluationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class ITeAiEvaluationServiceImpl extends ServiceImpl<TeAiEvaluationMapper, TeAiEvaluation> implements ITeAiEvaluationService {

    @Override
    public boolean insert(TeAiEvaluation teAiEvaluation) {
        return this.save(teAiEvaluation);
    }

    @Override
    public boolean edit(TeAiEvaluation teAiEvaluation) {
        if (StrUtil.isEmpty(teAiEvaluation.getTaeId())) {
            throw new GlobalException("AI评估ID不能为空!");
        }
        String result = teAiEvaluation.getResult();
        if (StrUtil.isEmpty(result)) {
            throw new GlobalException("AI评估结果不能为空!");
        }
        JSONObject jsonObject = JSON.parseObject(result);
        String overallResult = jsonObject.getString("overall_result");
        int status ;
        if (StrUtil.isNotEmpty(overallResult) && overallResult.equals("成功")) {
            status = 3;
        }else {
            status = 4;
        }
        LambdaUpdateWrapper<TeAiEvaluation> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(TeAiEvaluation::getTaeId, teAiEvaluation.getTaeId())
                .set(TeAiEvaluation::getResult, result)
                .set(TeAiEvaluation::getStatus, status) // 评估完成/失败
                .set(TeAiEvaluation::getEndTime,System.currentTimeMillis());

        return this.update(wrapper);
    }
}


