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

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.orderModuleExtra.vo.*;
import com.cf.imes.module.executor.dal.dataobject.orderModuleExtra.OrderModuleExtraDO;
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.orderModuleExtra.OrderModuleExtraMapper;

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

/**
 * 生产单模块扩充属性表 order_module_extra_N Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class OrderModuleExtraServiceImpl implements OrderModuleExtraService {

    @Resource
    private OrderModuleExtraMapper orderModuleExtraMapper;

    @Override
    public Long createOrderModuleExtra(OrderModuleExtraSaveReqVO createReqVO) {
        // 插入
        OrderModuleExtraDO orderModuleExtra = BeanUtils.toBean(createReqVO, OrderModuleExtraDO.class);
        orderModuleExtraMapper.insert(orderModuleExtra);
        // 返回
        return orderModuleExtra.getId();
    }

    @Override
    public void updateOrderModuleExtra(OrderModuleExtraSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderModuleExtraExists(updateReqVO.getId());
        // 更新
        OrderModuleExtraDO updateObj = BeanUtils.toBean(updateReqVO, OrderModuleExtraDO.class);
        orderModuleExtraMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderModuleExtra(Long id) {
        // 校验存在
        validateOrderModuleExtraExists(id);
        // 删除
        orderModuleExtraMapper.deleteById(id);
    }

    private void validateOrderModuleExtraExists(Long id) {
        if (orderModuleExtraMapper.selectById(id) == null) {
            throw exception(ORDER_MODULE_EXTRA_NOT_EXISTS);
        }
    }

    @Override
    public OrderModuleExtraDO getOrderModuleExtra(Long id) {
        return orderModuleExtraMapper.selectById(id);
    }

    @Override
    public PageResult<OrderModuleExtraDO> getOrderModuleExtraPage(OrderModuleExtraPageReqVO pageReqVO) {
        return orderModuleExtraMapper.selectPage(pageReqVO);
    }

}