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

import cn.iocoder.byzs.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.byzs.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.byzs.module.bjdx.dal.mysql.reportmanage.ReportManageMapper;
import cn.iocoder.byzs.module.blockly.dal.mysql.blocklyType.BlocklyTypeMapper;
import cn.iocoder.byzs.module.web.controller.admin.blocklyprogress.vo.WebBlocklyProgressDO;
import cn.iocoder.byzs.module.web.dal.mysql.blocklyprogress.WebBlocklyProgressMapper;
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 WebBlocklyProgressServiceImpl {

    @Resource
    private WebBlocklyProgressMapper progressMapper;
    @Resource
    private ReportManageMapper reportManageMapper;
    @Resource
    private BlocklyTypeMapper blocklyTypeMapper;

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

        LambdaQueryWrapperX<WebBlocklyProgressDO> progressLambdaQueryWrapperX = new LambdaQueryWrapperX<WebBlocklyProgressDO>()
                .eqIfPresent(WebBlocklyProgressDO::getBrpUserId, progress.getBrpUserId())
                .eqIfPresent(WebBlocklyProgressDO::getBrpType, progress.getBrpType())
                .eqIfPresent(WebBlocklyProgressDO::getBrpZtId, progress.getBrpZtId());

        switch (progress.getBrpType()) {
            case "course":// 课程开课率
                progressLambdaQueryWrapperX
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCtId, progress.getBrpCtId())
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCourseId, progress.getBrpCourseId());
                WebBlocklyProgressDO courseProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);
                if (courseProgressDo == null) {
                    progressMapper.insert(progress);
                }else if (progress.getBrpProgress() > courseProgressDo.getBrpProgress()) {
                    courseProgressDo.setBrpProgress(progress.getBrpProgress());
                    progressMapper.updateById(courseProgressDo);
                }
                break;

            case "courseQuest":// 课程互动率
                progressLambdaQueryWrapperX
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCtId, progress.getBrpCtId())
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCourseId, progress.getBrpCourseId())
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCourseConfigId, progress.getBrpCourseConfigId());
                WebBlocklyProgressDO courseConfigProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);

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

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

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

            case "blockly":// blocklu编程课通关
                progressLambdaQueryWrapperX
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCtId, progress.getBrpCtId())
                        .eqIfPresent(WebBlocklyProgressDO::getBrpCourseId, progress.getBrpCourseId());
                WebBlocklyProgressDO blocklyProgressDo = progressMapper.selectOne(progressLambdaQueryWrapperX);

                if (blocklyProgressDo == null) {
                    progressMapper.insert(progress);
                }else {
                    if (progress.getBrpProgress() > blocklyProgressDo.getBrpProgress()){
                        blocklyProgressDo.setBrpProgress(progress.getBrpProgress());
                        progressMapper.updateById(blocklyProgressDo);
                    }
                }
                break;
        }
    }

    /**
     * 根据类型id查询进度
     *
     * @param typeId 类型id
     * @return 进度列表
     */
    public List<WebBlocklyProgressDO> getBlocklyProgressByTypeId(Long typeId) {
        WebBlocklyProgressDO webBlocklyProgressDO = new WebBlocklyProgressDO().setBrpCtId(typeId).setBrpUserId(WebFrameworkUtils.getLoginUserId());
        List<WebBlocklyProgressDO> blocklyProgressList = progressMapper.getBlocklyProgressByTypeId(webBlocklyProgressDO);
        return blocklyProgressList;
    }

    /**
     * 根据主题id查询进度
     *
     * @param themeId 主题id
     * @return 进度列表
     */
    public List<WebBlocklyProgressDO> getBlocklyProgressByThemeId(Long themeId) {
        WebBlocklyProgressDO webBlocklyProgressDO = new WebBlocklyProgressDO().setBrpZtId(themeId).setBrpUserId(WebFrameworkUtils.getLoginUserId());
        List<WebBlocklyProgressDO> blocklyProgressList = progressMapper.getBlocklyProgressByThemeId(webBlocklyProgressDO);
        return blocklyProgressList;
    }
}