package cn.iocoder.yudao.module.llm.service.learningresources;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.llm.controller.admin.learningresources.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.learningresources.LearningResourcesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.llm.dal.mysql.learningresources.LearningResourcesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.*;
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 学习资源 Service 实现类
 *
 * @author 华大大模型
 */
@Service
@Validated
public class LearningResourcesServiceImpl implements LearningResourcesService {

    @Resource
    private LearningResourcesMapper learningResourcesMapper;

    @Override
    public Long createLearningResources(LearningResourcesSaveReqVO createReqVO) {
        // 插入
        LearningResourcesDO learningResources = BeanUtils.toBean(createReqVO, LearningResourcesDO.class);
        learningResourcesMapper.insert(learningResources);
        // 返回
        return learningResources.getId();
    }

    @Override
    public void updateLearningResources(LearningResourcesSaveReqVO updateReqVO) {
        // 校验存在
        validateLearningResourcesExists(updateReqVO.getId());
        // 更新
        LearningResourcesDO updateObj = BeanUtils.toBean(updateReqVO, LearningResourcesDO.class);
        learningResourcesMapper.updateById(updateObj);
    }

    @Override
    public void deleteLearningResources(Long id) {
        // 校验存在
        validateLearningResourcesExists(id);
        // 删除
        learningResourcesMapper.deleteById(id);
    }

    private void validateLearningResourcesExists(Long id) {
        if (learningResourcesMapper.selectById(id) == null) {
            throw exception(LEARNING_RESOURCES_NOT_EXISTS);
        }
    }

    @Override
    public LearningResourcesDO getLearningResources(Long id) {
        return learningResourcesMapper.selectById(id);
    }

    @Override
    public PageResult<LearningResourcesDO> getLearningResourcesPage(LearningResourcesPageReqVO pageReqVO) {
        return learningResourcesMapper.selectPage(pageReqVO);
    }

    @Override
    public String downLearningResources(Long id){
        LearningResourcesDO learningResourcesDO = learningResourcesMapper.selectById(id);
        learningResourcesMapper.updateDownCount(learningResourcesDO);
        return learningResourcesDO.getFileUrl();
    }
}