package cn.newfeifan.mall.module.distri.service.orderitemsettlement;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.distri.controller.admin.orderitemsettlement.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.orderitemsettlement.OrderItemSettlementDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.orderitemsettlement.OrderItemSettlementMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 订单结算表,记录有那些订单项参与了本次结算 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class OrderItemSettlementServiceImpl implements OrderItemSettlementService {

    @Resource
    private OrderItemSettlementMapper orderItemSettlementMapper;

    @Override
    public Long createOrderItemSettlement(OrderItemSettlementSaveReqVO createReqVO) {
        // 插入
        OrderItemSettlementDO orderItemSettlement = BeanUtils.toBean(createReqVO, OrderItemSettlementDO.class);
        orderItemSettlementMapper.insert(orderItemSettlement);
        // 返回
        return orderItemSettlement.getId();
    }

    @Override
    public void updateOrderItemSettlement(OrderItemSettlementSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderItemSettlementExists(updateReqVO.getId());
        // 更新
        OrderItemSettlementDO updateObj = BeanUtils.toBean(updateReqVO, OrderItemSettlementDO.class);
        orderItemSettlementMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderItemSettlement(Long id) {
        // 校验存在
        validateOrderItemSettlementExists(id);
        // 删除
        orderItemSettlementMapper.deleteById(id);
    }

    private void validateOrderItemSettlementExists(Long id) {
        if (orderItemSettlementMapper.selectById(id) == null) {
            throw exception(ORDER_ITEM_SETTLEMENT_NOT_EXISTS);
        }
    }

    @Override
    public OrderItemSettlementDO getOrderItemSettlement(Long id) {
        return orderItemSettlementMapper.selectById(id);
    }

    @Override
    public PageResult<OrderItemSettlementDO> getOrderItemSettlementPage(OrderItemSettlementPageReqVO pageReqVO) {
        return orderItemSettlementMapper.selectPage(pageReqVO);
    }

}