package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.cultivate.mapper.FtbCultivateCourseSettingMapper;
import jnpf.cultivate.service.FtbCultivateChapterTestResultService;
import jnpf.cultivate.service.FtbCultivateCourseSettingService;
import jnpf.model.cultivate.dto.course.FtbCultivateCourseSettingDTO;
import jnpf.model.cultivate.po.course.FtbCultivateChapterTestResult;
import jnpf.model.cultivate.po.course.FtbCultivateCourseSetting;
import jnpf.model.cultivate.vo.chapter.FtbCultivateChapterTestResultVO;
import jnpf.util.UserProvider;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FtbCultivateCourseSettingServiceImpl extends ServiceImpl<FtbCultivateCourseSettingMapper, FtbCultivateCourseSetting> implements FtbCultivateCourseSettingService {

    @Resource
    private FtbCultivateChapterTestResultService fileResultService;

    @Override
    public void buildUpCourseLearningSettings(FtbCultivateCourseSettingDTO ftbCultivateCourseSettingDTO, String courseId) {
        LambdaQueryWrapper<FtbCultivateCourseSetting> courseSettingLambdaQueryWrapper = Wrappers.lambdaQuery();
        courseSettingLambdaQueryWrapper.eq(FtbCultivateCourseSetting::getEnableMark, 0);
        courseSettingLambdaQueryWrapper.eq(FtbCultivateCourseSetting::getCourseId, courseId);
        courseSettingLambdaQueryWrapper.last("limit 1");
        FtbCultivateCourseSetting ftbCultivateCourseSetting = getBaseMapper().selectOne(courseSettingLambdaQueryWrapper);
        FtbCultivateCourseSettingDTO.convertFtbCultivateCourseSetting(ftbCultivateCourseSettingDTO, ftbCultivateCourseSetting);
    }

    @Override
    public void onSiteTestResultStorage(List<FtbCultivateChapterTestResultVO> ftbChapterTestDTOs, String courseId, String chapterId) {
        if (CollUtil.isNotEmpty(ftbChapterTestDTOs)) {
            FtbCultivateChapterTestResult fileResult = new FtbCultivateChapterTestResult();
            fileResult.setCourseId(courseId);
            fileResult.setCourseChapterId(chapterId);
            fileResult.setUserId(UserProvider.getLoginUserId());
            fileResult.setChapterTestOptions(JSON.toJSONString(ftbChapterTestDTOs));
            fileResultService.save(fileResult);
        }
    }
    @Override
    public void onSiteTestResultStorageV2(List<FtbCultivateChapterTestResultVO> ftbChapterTestDTOs, String courseId, String chapterId, String userId) {
        if (CollUtil.isNotEmpty(ftbChapterTestDTOs)) {

            LambdaQueryWrapper<FtbCultivateChapterTestResult> testWrapper = Wrappers.lambdaQuery();
            testWrapper.eq(FtbCultivateChapterTestResult::getEnableMark, 0);
            testWrapper.eq(FtbCultivateChapterTestResult::getCourseId, courseId);
            testWrapper.eq(FtbCultivateChapterTestResult::getCourseChapterId, chapterId);
            testWrapper.eq(FtbCultivateChapterTestResult::getUserId, userId);
            fileResultService.remove(testWrapper);

            FtbCultivateChapterTestResult fileResult = new FtbCultivateChapterTestResult();
            fileResult.setCourseId(courseId);
            fileResult.setCourseChapterId(chapterId);
            fileResult.setUserId(userId);
            fileResult.setChapterTestOptions(JSON.toJSONString(ftbChapterTestDTOs));
            fileResultService.save(fileResult);
        }
    }

}
