package cn.iocoder.byzs.module.web.service.aiCourseProgress;

import cn.iocoder.byzs.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.byzs.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.byzs.module.aicourse.dal.mysql.aiCourseType.AiCourseTypeMapper;
import cn.iocoder.byzs.module.bjdx.dal.mysql.reportmanage.ReportManageMapper;
import cn.iocoder.byzs.module.web.controller.admin.aiCourseProgress.vo.WebAiCourseProgressDO;
import cn.iocoder.byzs.module.web.dal.mysql.aiCourseProgress.WebAiCourseProgressMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 评估报告-课程开课率 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class WebAiCourseProgressServiceImpl {

    @Resource
    private WebAiCourseProgressMapper progressMapper;
    @Resource
    private ReportManageMapper reportManageMapper;
    @Resource
    private AiCourseTypeMapper aiCourseTypeMapper;

    /**
     * 保存评估报告进度
     *
     * @param progress 评估报告进度
     */
    public void saveReportProgress(WebAiCourseProgressDO progress) {

        LambdaQueryWrapperX<WebAiCourseProgressDO> progressLambdaQueryWrapperX = new LambdaQueryWrapperX<WebAiCourseProgressDO>()
                .eqIfPresent(WebAiCourseProgressDO::getArpUserId, progress.getArpUserId())
                .eqIfPresent(WebAiCourseProgressDO::getArpType, progress.getArpType())
                .eqIfPresent(WebAiCourseProgressDO::getArpZtId, progress.getArpZtId());

        switch (progress.getArpType()) {
            case "course":// 课程开课率
                progressLambdaQueryWrapperX
                        .eqIfPresent(WebAiCourseProgressDO::getArpCtId, progress.getArpCtId())
                        .eqIfPresent(WebAiCourseProgressDO::getArpCourseId, progress.getArpCourseId());
                WebAiCourseProgressDO courseProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);
                if (courseProgressDo == null) {
                    progressMapper.insert(progress);
                }else if (progress.getArpProgress() > courseProgressDo.getArpProgress()) {
                    courseProgressDo.setArpProgress(progress.getArpProgress());
                    progressMapper.updateById(courseProgressDo);
                }
                break;

            case "courseQuest":// 课程互动率
                progressLambdaQueryWrapperX
                        .eqIfPresent(WebAiCourseProgressDO::getArpCtId, progress.getArpCtId())
                        .eqIfPresent(WebAiCourseProgressDO::getArpCourseId, progress.getArpCourseId())
                        .eqIfPresent(WebAiCourseProgressDO::getArpCourseConfigId, progress.getArpCourseConfigId());
                WebAiCourseProgressDO courseConfigProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);

                if (courseConfigProgressDo == null) {
                    progressMapper.insert(progress);
                }
                break;

            case "aiCount":// ai问答次数
                WebAiCourseProgressDO aiCountProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);

                if (aiCountProgressDo == null) {
                    progressMapper.insert(progress);
                }else {
                    aiCountProgressDo.setArpProgress(aiCountProgressDo.getArpProgress()+1);
                    progressMapper.updateById(aiCountProgressDo);
                }
                break;

            case "aiCourse":// blocklu编程课通关
                progressLambdaQueryWrapperX
                        .eqIfPresent(WebAiCourseProgressDO::getArpCtId, progress.getArpCtId())
                        .eqIfPresent(WebAiCourseProgressDO::getArpCourseId, progress.getArpCourseId());
                WebAiCourseProgressDO aiCourseProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);

                if (aiCourseProgressDo == null) {
                    progressMapper.insert(progress);
                }else {
                    if (progress.getArpProgress() > aiCourseProgressDo.getArpProgress()){
                        aiCourseProgressDo.setArpProgress(progress.getArpProgress());
                        progressMapper.updateById(aiCourseProgressDo);
                    }
                }
                break;
        }
    }

    /**
     * 根据类型id查询进度
     *
     * @param typeId 类型id
     */
    public List<WebAiCourseProgressDO> getAiCourseProgressByTypeId(Long typeId) {
        WebAiCourseProgressDO webAiCourseProgressDO = new WebAiCourseProgressDO().setArpCtId(typeId).setArpUserId(WebFrameworkUtils.getLoginUserId());
        List<WebAiCourseProgressDO> aiCourseProgressList = progressMapper.getAiCourseProgressByTypeId(webAiCourseProgressDO);
        return aiCourseProgressList;
    }

    /**
     * 根据主题id查询进度
     *
     * @param themeId 主题id
     * @return 进度列表
     */
    public List<WebAiCourseProgressDO> getAiCourseProgressByThemeId(Long themeId) {
        WebAiCourseProgressDO webAiCourseProgressDO = new WebAiCourseProgressDO().setArpZtId(themeId).setArpUserId(WebFrameworkUtils.getLoginUserId());
        List<WebAiCourseProgressDO> aiCourseProgressList = progressMapper.getAiCourseProgressByThemeId(webAiCourseProgressDO);
        return aiCourseProgressList;
    }
}