/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.disk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.disk.domain.CollectCourse;
import vip.xiaonuo.disk.domain.CourseInfo;
import vip.xiaonuo.disk.dto.courseInfo.CourseInfoIdParam;
import vip.xiaonuo.disk.mapper.CollectCourseMapper;
import vip.xiaonuo.disk.param.CollectCourseAddParam;
import vip.xiaonuo.disk.param.CollectCourseEditParam;
import vip.xiaonuo.disk.param.CollectCourseIdParam;
import vip.xiaonuo.disk.param.CollectCoursePageParam;
import vip.xiaonuo.disk.service.CollectCourseService;
import vip.xiaonuo.disk.service.CourseInfoService;

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

/**
 * 收藏课程Service接口实现类
 *
 * @author 金吉龙
 * @date  2025/07/01 19:01
 **/
@Service
public class CollectCourseServiceImpl extends ServiceImpl<CollectCourseMapper, CollectCourse> implements CollectCourseService {

    @Resource
    private CourseInfoService courseInfoService;


    @Override
    public Page<CollectCourse> page(CollectCoursePageParam collectCoursePageParam) {
        QueryWrapper<CollectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CollectCourse::getCollectUserId, StpLoginUserUtil.getLoginUser().getId()); // 查看自己收藏
        queryWrapper.lambda().eq(CollectCourse::getCollectState, 0); // 查看收藏状态的
        queryWrapper.lambda().orderByDesc(CollectCourse::getCollectTime);
        Page<CollectCourse> page = this.page(CommonPageRequest.defaultPage(), queryWrapper);
        // 关联课程信息
        if (page.getRecords().size() > 0) {
            for (CollectCourse record : page.getRecords()) {
                CourseInfoIdParam courseInfoIdParam = new CourseInfoIdParam();
                courseInfoIdParam.setCourseId(record.getCourseId());
                CourseInfo detail = courseInfoService.detail(courseInfoIdParam);
                if (detail != null) {
                    record.setCourseInfo(detail);
                }
            }
        }
        return this.page(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(CollectCourseAddParam collectCourseAddParam) {
        // 校验是否已经收藏
        QueryWrapper<CollectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CollectCourse::getCollectUserId, StpLoginUserUtil.getLoginUser().getId());
        queryWrapper.lambda().eq(CollectCourse::getCourseId, collectCourseAddParam.getCourseId());
        CollectCourse data = this.getOne(queryWrapper);
        if(ObjectUtil.isNotEmpty(data)) {
            throw new CommonException("该课程已经收藏");
        }
        // 执行添加
        CollectCourse collectCourse = BeanUtil.toBean(collectCourseAddParam, CollectCourse.class);
        collectCourse.setCollectId(IdUtil.fastSimpleUUID()); // 主键id
        collectCourse.setCollectUserId(StpLoginUserUtil.getLoginUser().getId()); // 当前登录用户id
        collectCourse.setCollectTime(new Date()); // 收藏时间
        this.save(collectCourse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(CollectCourseEditParam collectCourseEditParam) {
        CollectCourse collectCourse = this.queryEntity(collectCourseEditParam.getCollectId());
        BeanUtil.copyProperties(collectCourseEditParam, collectCourse);
        this.updateById(collectCourse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<CollectCourseIdParam> collectCourseIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(collectCourseIdParamList, CollectCourseIdParam::getCollectId));
    }

    @Override
    public CollectCourse detail(CollectCourseIdParam collectCourseIdParam) {
        return this.queryEntity(collectCourseIdParam.getCollectId());
    }

    @Override
    public CollectCourse queryEntity(String id) {
        CollectCourse collectCourse = this.getById(id);
        if(ObjectUtil.isEmpty(collectCourse)) {
            throw new CommonException("收藏课程不存在，id值为：{}", id);
        }
        return collectCourse;
    }

    @Override
    public void cancel(CollectCourseAddParam collectCourseAddParam) {
        // 校验数据是否存在
        QueryWrapper<CollectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CollectCourse::getCollectUserId, StpLoginUserUtil.getLoginUser().getId());
        queryWrapper.lambda().eq(CollectCourse::getCourseId, collectCourseAddParam.getCourseId());
        CollectCourse data = this.getOne(queryWrapper);
        if(ObjectUtil.isNotEmpty(data)) {
            throw new CommonException("该课程未收藏");
        }
        // 执行取消
        data.setCollectState(1);
        this.updateById(data);
    }

    @Override
    public CommonResult<Integer> checkCollect(CollectCourseAddParam collectCourseAddParam) {
        QueryWrapper<CollectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CollectCourse::getCollectUserId, StpLoginUserUtil.getLoginUser().getId());
        queryWrapper.lambda().eq(CollectCourse::getCourseId, collectCourseAddParam.getCourseId());
        CollectCourse data = this.getOne(queryWrapper);
        if(ObjectUtil.isNotEmpty(data)) {
            return CommonResult.data(0);
        }
        return CommonResult.data(1);
    }

}
