package cn.iocoder.yudao.module.portal.service.jobpost;

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.portal.controller.admin.jobpost.vo.*;
import cn.iocoder.yudao.module.portal.dal.dataobject.jobpost.JobPostDO;
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.portal.dal.mysql.jobpost.JobPostMapper;

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

/**
 * 职位 Service 实现类
 *
 * @author 篮智科技
 */
@Service
@Validated
public class JobPostServiceImpl implements JobPostService {

    @Resource
    private JobPostMapper jobPostMapper;

    @Override
    public Long createJobPost(JobPostSaveReqVO createReqVO) {
        // 插入
        JobPostDO jobPost = BeanUtils.toBean(createReqVO, JobPostDO.class);
        jobPostMapper.insert(jobPost);
        // 返回
        return jobPost.getId();
    }

    @Override
    public void updateJobPost(JobPostSaveReqVO updateReqVO) {
        // 校验存在
        validateJobPostExists(updateReqVO.getId());
        // 更新
        JobPostDO updateObj = BeanUtils.toBean(updateReqVO, JobPostDO.class);
        jobPostMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobPost(Long id) {
        // 校验存在
        validateJobPostExists(id);
        // 删除
        jobPostMapper.deleteById(id);
    }

    private void validateJobPostExists(Long id) {
        if (jobPostMapper.selectById(id) == null) {
            throw exception(JOB_POST_NOT_EXISTS);
        }
    }

    @Override
    public JobPostDO getJobPost(Long id) {
        return jobPostMapper.selectById(id);
    }

    @Override
    public PageResult<JobPostDO> getJobPostPage(JobPostPageReqVO pageReqVO) {
        return jobPostMapper.selectPage(pageReqVO);
    }

}