package com.ynfy.buss.course.usercoursecatalog.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynfy.buss.course.usercoursecatalog.entity.vo.UserCourseStudyVO;
import com.ynfy.buss.course.coursecatalog.entity.CourseCatalog;
import com.ynfy.buss.course.coursecatalog.service.ICourseCatalogService;
import com.ynfy.buss.course.usercoursecatalog.entity.UserCourseCatalog;
import com.ynfy.buss.course.usercoursecatalog.entity.vo.CatalogTimeVO;
import com.ynfy.buss.course.usercoursecatalog.mapper.UserCourseCatalogMapper;
import com.ynfy.buss.course.usercoursecatalog.service.IUserCourseCatalogService;
import com.ynfy.buss.practice.userpractice.entity.UserPractice;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 用户课程任务学习情况
 * @Author: jeecg-boot
 * @Date: 2023-10-25
 * @Version: V1.0
 */
@Service
public class UserCourseCatalogServiceImpl extends ServiceImpl<UserCourseCatalogMapper, UserCourseCatalog> implements IUserCourseCatalogService {

    @Autowired
    private UserCourseCatalogMapper userCourseCatalogMapper;

    @Lazy
    @Autowired
    private ICourseCatalogService courseCatalogService;

    /**
     * 计算任务学习时长
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void calcStudyTime(CatalogTimeVO catalogTimeVO) {
        if (Objects.isNull(catalogTimeVO.getCourseCatalog()) || StringUtils.isBlank(catalogTimeVO.getCourseCatalog().getId())
                || Objects.isNull(catalogTimeVO.getStartLearnTime()) || Objects.isNull(catalogTimeVO.getEndLearnTime())) {
            return;
        }
        if (DateUtil.compare(catalogTimeVO.getEndLearnTime(), catalogTimeVO.getStartLearnTime()) > 0) {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            CourseCatalog courseCatalog = catalogTimeVO.getCourseCatalog();
            UserCourseCatalog userCourseCatalog = findUserCatalog(sysUser.getId(), courseCatalog.getId());
            if (Objects.isNull(userCourseCatalog)) {
                userCourseCatalog = new UserCourseCatalog();
            }
            userCourseCatalog.setUserId(sysUser.getId());
            userCourseCatalog.setCourseCatalogId(courseCatalog.getId());
            long between = DateUtil.between(catalogTimeVO.getStartLearnTime(), catalogTimeVO.getEndLearnTime(), DateUnit.SECOND);
            userCourseCatalog.setTotalLearnTime(Integer.parseInt(String.valueOf(((!Objects.isNull(userCourseCatalog.getTotalLearnTime())
                    ? userCourseCatalog.getTotalLearnTime() : 0) + between))));

            //如果有必须时长的话则以其分母，如果没有的话取总时长
            Long number = !Objects.isNull(courseCatalog.getMustLearnTime()) ? courseCatalog.getMustLearnTime() :
                    !Objects.isNull(courseCatalog.getVideoHour()) ? courseCatalog.getVideoHour() : 0L;
            if (number.intValue() > 0) {
                //计算学习进度
                userCourseCatalog.setLearnProcess(NumberUtil.div(userCourseCatalog.getTotalLearnTime(), number).doubleValue());
            } else {
                userCourseCatalog.setLearnProcess(1.00);
            }

            saveOrUpdate(userCourseCatalog);
        }
    }


    @Override
    public UserCourseCatalog findUserCatalog(String userId, String courseCatalogId) {
        LambdaQueryWrapper<UserCourseCatalog> query = new LambdaQueryWrapper<UserCourseCatalog>()
                .eq(UserCourseCatalog::getUserId, userId).eq(UserCourseCatalog::getCourseCatalogId, courseCatalogId);
        return getOne(query);
    }


    /**
     * 获取课程任务学习进度
     *
     * @return
     */
    @Override
    public List<UserCourseCatalog> getCourseStudyProcess(String courseId) {
        List<CourseCatalog> catalogList = courseCatalogService.listCourseCatalog(courseId);
        List<String> courseCatalogIdList = catalogList.stream().map(CourseCatalog::getId).collect(Collectors.toList());
        LambdaQueryWrapper<UserCourseCatalog> query = new LambdaQueryWrapper<UserCourseCatalog>()
                .in(UserCourseCatalog::getCourseCatalogId, courseCatalogIdList);
        return list(query);
    }

    /**
     * 获取用户课程学习情况
     *
     * @param courseIds
     * @param userId
     * @return
     */
    @Override
    public List<UserCourseStudyVO> getUserCourseStudy(List<String> courseIds, String userId) {
        return userCourseCatalogMapper.getUserCourseStudy(courseIds, userId);
    }

    @Override
    public IPage<UserCourseCatalog> selectPageList(IPage<UserCourseCatalog> page, UserCourseCatalog userCourseCatalog){
        return userCourseCatalogMapper.selectPageList(page, userCourseCatalog);
    }
}
