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

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.plate.vo.*;
import com.cf.imes.module.executor.dal.dataobject.plate.PlateDO;
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.plate.PlateMapper;

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

/**
 * 生产单板件 Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class PlateServiceImpl implements PlateService {

    @Resource
    private PlateMapper plateMapper;

    @Override
    public Long createPlate(PlateSaveReqVO createReqVO) {
        // 插入
        PlateDO plate = BeanUtils.toBean(createReqVO, PlateDO.class);
        plateMapper.insert(plate);
        // 返回
        return plate.getId();
    }

    @Override
    public void updatePlate(PlateSaveReqVO updateReqVO) {
        // 校验存在
        validatePlateExists(updateReqVO.getId());
        // 更新
        PlateDO updateObj = BeanUtils.toBean(updateReqVO, PlateDO.class);
        plateMapper.updateById(updateObj);
    }

    @Override
    public void deletePlate(Long id) {
        // 校验存在
        validatePlateExists(id);
        // 删除
        plateMapper.deleteById(id);
    }

    private void validatePlateExists(Long id) {
        if (plateMapper.selectById(id) == null) {
            throw exception(PLATE_NOT_EXISTS);
        }
    }

    @Override
    public PlateDO getPlate(Long id) {
        return plateMapper.selectById(id);
    }

    @Override
    public PageResult<PlateDO> getPlatePage(PlatePageReqVO pageReqVO) {
        return plateMapper.selectPage(pageReqVO);
    }

}