package cn.iocoder.yudao.module.server.service.interviewarticle;

import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.server.controller.admin.interviewarticle.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.interviewarticle.InterviewArticleDO;
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.server.dal.mysql.interviewarticle.InterviewArticleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.server.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;

/**
 * 访谈稿 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class InterviewArticleServiceImpl implements InterviewArticleService {

    @Resource
    private InterviewArticleMapper interviewArticleMapper;

    @LogRecord(
            type = SYSTEM_INTERVIEW_TYPE,
            subType = SYSTEM_INTERVIEW_CREATE_SUB_TYPE,
            bizNo = "220",
            success = SYSTEM_INTERVIEW_CREATE_SUCCESS
    )
    public Long createInterviewArticle(InterviewArticleSaveReqVO createReqVO) {
        // 插入
        InterviewArticleDO interviewArticle = BeanUtils.toBean(createReqVO, InterviewArticleDO.class);
        interviewArticleMapper.insert(interviewArticle);
        // 返回
        return interviewArticle.getId();
    }

    @LogRecord(
            type = SYSTEM_INTERVIEW_TYPE,
            subType = SYSTEM_INTERVIEW_UPDATE_SUB_TYPE,
            bizNo = "220",
            success = SYSTEM_INTERVIEW_UPDATE_SUCCESS
    )
    public void updateInterviewArticle(InterviewArticleSaveReqVO updateReqVO) {
        // 校验存在
        validateInterviewArticleExists(updateReqVO.getId());
        // 更新
        InterviewArticleDO updateObj = BeanUtils.toBean(updateReqVO, InterviewArticleDO.class);
        interviewArticleMapper.updateById(updateObj);
    }

    @LogRecord(
            type = SYSTEM_INTERVIEW_TYPE,
            subType = SYSTEM_INTERVIEW_DELETE_SUB_TYPE,
            bizNo = "220",
            success = SYSTEM_INTERVIEW_DELETE_SUCCESS
    )
    public void deleteInterviewArticle(Long id) {
        // 校验存在
        validateInterviewArticleExists(id);
        // 删除
        interviewArticleMapper.deleteById(id);
    }

    private void validateInterviewArticleExists(Long id) {
        if (interviewArticleMapper.selectById(id) == null) {
            throw exception(INTERVIEW_ARTICLE_NOT_EXISTS);
        }
    }

    @Override
    public InterviewArticleDO getInterviewArticle(Long id) {
        return interviewArticleMapper.selectById(id);
    }

    @Override
    public PageResult<InterviewArticleDO> getInterviewArticlePage(InterviewArticlePageReqVO pageReqVO) {
        return interviewArticleMapper.selectPage(pageReqVO);
    }

}