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

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.orderParts.vo.*;
import com.cf.imes.module.executor.dal.dataobject.orderParts.OrderPartsDO;
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.orderParts.OrderPartsMapper;

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

/**
 * 生产单配件表 order_parts_{N} Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class OrderPartsServiceImpl implements OrderPartsService {

    @Resource
    private OrderPartsMapper orderPartsMapper;

    @Override
    public Long createOrderParts(OrderPartsSaveReqVO createReqVO) {
        // 插入
        OrderPartsDO orderParts = BeanUtils.toBean(createReqVO, OrderPartsDO.class);
        orderPartsMapper.insert(orderParts);
        // 返回
        return orderParts.getId();
    }

    @Override
    public void updateOrderParts(OrderPartsSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderPartsExists(updateReqVO.getId());
        // 更新
        OrderPartsDO updateObj = BeanUtils.toBean(updateReqVO, OrderPartsDO.class);
        orderPartsMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderParts(Long id) {
        // 校验存在
        validateOrderPartsExists(id);
        // 删除
        orderPartsMapper.deleteById(id);
    }

    private void validateOrderPartsExists(Long id) {
        if (orderPartsMapper.selectById(id) == null) {
            throw exception(ORDER_PARTS_NOT_EXISTS);
        }
    }

    @Override
    public OrderPartsDO getOrderParts(Long id) {
        return orderPartsMapper.selectById(id);
    }

    @Override
    public PageResult<OrderPartsDO> getOrderPartsPage(OrderPartsPageReqVO pageReqVO) {
        return orderPartsMapper.selectPage(pageReqVO);
    }

}