package com.cf.imes.module.executor.service.processStep;

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 com.cf.imes.module.executor.controller.admin.processStep.vo.*;
import com.cf.imes.module.executor.dal.dataobject.processStep.ProcessStepDO;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.util.object.BeanUtils;

import com.cf.imes.module.executor.dal.mysql.processStep.ProcessStepMapper;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;

/**
 * 生产单工序步骤 Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class ProcessStepServiceImpl implements ProcessStepService {

    @Resource
    private ProcessStepMapper processStepMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createProcessStep(ProcessStepSaveReqVO createReqVO) {
        // 插入
        ProcessStepDO processStep = BeanUtils.toBean(createReqVO, ProcessStepDO.class);
        processStepMapper.insert(processStep);
        // 返回
        return processStep.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProcessStep(ProcessStepSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessStepExists(updateReqVO.getId());
        // 更新
        ProcessStepDO updateObj = BeanUtils.toBean(updateReqVO, ProcessStepDO.class);
        processStepMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcessStep(Long id) {
        // 校验存在
        validateProcessStepExists(id);
        // 删除
        processStepMapper.deleteById(id);
    }

    private void validateProcessStepExists(Long id) {
        if (processStepMapper.selectById(id) == null) {
//            throw exception(PROCESS_STEP_NOT_EXISTS);
        }
    }

    @Override
    public ProcessStepDO getProcessStep(Long id) {
        return processStepMapper.selectById(id);
    }

    @Override
    public PageResult<ProcessStepDO> getProcessStepPage(ProcessStepPageReqVO pageReqVO) {
        return processStepMapper.selectPage(pageReqVO);
    }

}