package cn.iocoder.yudao.module.member.service.orderdetaillog;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.orderdetaillog.vo.OrderDetailLogPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.orderdetaillog.vo.OrderDetailLogSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.orderdetaillog.OrderDetailLogDO;
import cn.iocoder.yudao.module.member.dal.mysql.orderdetaillog.OrderDetailLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ORDER_DETAIL_LOG_NOT_EXISTS;

/**
 * 打菜日志 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class OrderDetailLogServiceImpl implements OrderDetailLogService {

    @Resource
    private OrderDetailLogMapper orderDetailLogMapper;

    @Override
    public Long createOrderDetailLog(OrderDetailLogSaveReqVO createReqVO) {
        // 插入
        OrderDetailLogDO orderDetailLog = BeanUtils.toBean(createReqVO, OrderDetailLogDO.class);
        orderDetailLogMapper.insert(orderDetailLog);
        // 返回
        return orderDetailLog.getId();
    }

    @Override
    public void updateOrderDetailLog(OrderDetailLogSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderDetailLogExists(updateReqVO.getId());
        // 更新
        OrderDetailLogDO updateObj = BeanUtils.toBean(updateReqVO, OrderDetailLogDO.class);
        orderDetailLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderDetailLog(Long id) {
        // 校验存在
        validateOrderDetailLogExists(id);
        // 删除
        orderDetailLogMapper.deleteById(id);
    }

    private void validateOrderDetailLogExists(Long id) {
        if (orderDetailLogMapper.selectById(id) == null) {
            throw exception(ORDER_DETAIL_LOG_NOT_EXISTS);
        }
    }

    @Override
    public OrderDetailLogDO getOrderDetailLog(Long id) {
        return orderDetailLogMapper.selectById(id);
    }

    @Override
    public PageResult<OrderDetailLogDO> getOrderDetailLogPage(OrderDetailLogPageReqVO pageReqVO) {
        return orderDetailLogMapper.selectPage(pageReqVO);
    }

}