package top.continew.admin.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.AssignmentsMapper;
import top.continew.admin.business.model.entity.AssignmentsDO;
import top.continew.admin.business.model.req.AssignmentsReq;
import top.continew.admin.business.model.resp.AssignmentsDetailResp;
import top.continew.admin.business.service.AssignmentsService;
import top.continew.starter.core.util.validate.CheckUtils;

/**
 * 作业表业务实现
 *
 * @author 张波
 * @since 2024/10/22 11:45
 */
@Service
@RequiredArgsConstructor
public class AssignmentsServiceImpl extends ServiceImpl<AssignmentsMapper, AssignmentsDO> implements AssignmentsService {

    @Override
    public Long add(AssignmentsReq assignmentsReq) {
        // 检查名字是否存在
        boolean exists = lambdaQuery().eq(AssignmentsDO::getCourseId, assignmentsReq.getCourseId())
            .eq(AssignmentsDO::getName, assignmentsReq.getName())
            .exists();
        CheckUtils.throwIf(exists, "作业名称已存在，请检查");

        // copy数据 保存到数据库
        AssignmentsDO saveDb = BeanUtil.copyProperties(assignmentsReq, AssignmentsDO.class);
        save(saveDb);

        // 返回存储的ID
        return saveDb.getId();
    }

    @Override
    public AssignmentsDetailResp detail(Long assignmentsId) {
        // 查询库中数据
        AssignmentsDO assignmentsDO = getById(assignmentsId);

        // 转为Resp
        return BeanUtil.copyProperties(assignmentsDO, AssignmentsDetailResp.class);
    }
}