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

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.jobcategory.vo.*;
import cn.iocoder.yudao.module.portal.dal.dataobject.jobcategory.JobCategoryDO;
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.jobcategory.JobCategoryMapper;

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 JobCategoryServiceImpl implements JobCategoryService {

    @Resource
    private JobCategoryMapper jobCategoryMapper;

    @Override
    public Long createJobCategory(JobCategorySaveReqVO createReqVO) {
        // 插入
        JobCategoryDO jobCategory = BeanUtils.toBean(createReqVO, JobCategoryDO.class);
        jobCategoryMapper.insert(jobCategory);
        // 返回
        return jobCategory.getId();
    }

    @Override
    public void updateJobCategory(JobCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateJobCategoryExists(updateReqVO.getId());
        // 更新
        JobCategoryDO updateObj = BeanUtils.toBean(updateReqVO, JobCategoryDO.class);
        jobCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobCategory(Long id) {
        // 校验存在
        validateJobCategoryExists(id);
        // 删除
        jobCategoryMapper.deleteById(id);
    }

    private void validateJobCategoryExists(Long id) {
        if (jobCategoryMapper.selectById(id) == null) {
            throw exception(JOB_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public JobCategoryDO getJobCategory(Long id) {
        return jobCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<JobCategoryDO> getJobCategoryPage(JobCategoryPageReqVO pageReqVO) {
        return jobCategoryMapper.selectPage(pageReqVO);
    }

}