package cn.iocoder.yudao.module.mes.service.subjectplan;

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.mes.controller.admin.subjectplan.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.subjectplan.SubjectPlanDO;
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.mes.dal.mysql.subjectplan.SubjectPlanMapper;

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

/**
 * 项目方案关联 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class SubjectPlanServiceImpl implements SubjectPlanService {

    @Resource
    private SubjectPlanMapper subjectPlanMapper;

    @Override
    public Long createSubjectPlan(SubjectPlanSaveReqVO createReqVO) {
        // 插入
        SubjectPlanDO subjectPlan = BeanUtils.toBean(createReqVO, SubjectPlanDO.class);
        subjectPlanMapper.insert(subjectPlan);
        // 返回
        return subjectPlan.getId();
    }

    @Override
    public void updateSubjectPlan(SubjectPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateSubjectPlanExists(updateReqVO.getId());
        // 更新
        SubjectPlanDO updateObj = BeanUtils.toBean(updateReqVO, SubjectPlanDO.class);
        subjectPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubjectPlan(Long id) {
        // 校验存在
        validateSubjectPlanExists(id);
        // 删除
        subjectPlanMapper.deleteById(id);
    }

    private void validateSubjectPlanExists(Long id) {
        if (subjectPlanMapper.selectById(id) == null) {
            throw exception(SUBJECT_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public SubjectPlanDO getSubjectPlan(Long id) {
        return subjectPlanMapper.selectById(id);
    }

    @Override
    public PageResult<SubjectPlanDO> getSubjectPlanPage(SubjectPlanPageReqVO pageReqVO) {
        return subjectPlanMapper.selectPage(pageReqVO);
    }

}