package com.yc.ship.module.trade.service.tradeOrderaudit;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.trade.controller.admin.tradeOrderaudit.vo.TradeOrderAuditPageReqVO;
import com.yc.ship.module.trade.controller.admin.tradeOrderaudit.vo.TradeOrderAuditSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.tradeOrderaudit.TradeOrderAuditDO;
import com.yc.ship.module.trade.dal.mysql.tradeOrderaudit.TradeOrderAuditMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 订单审核记录 Service 实现类
 *
 * @author qsl
 */
@Service
@Validated
public class TradeOrderAuditServiceImpl implements TradeOrderAuditService {

    @Resource
    private TradeOrderAuditMapper orderAuditMapper;

    @Override
    public Long createOrderAudit(TradeOrderAuditSaveReqVO createReqVO) {
        // 插入
        TradeOrderAuditDO orderAudit = BeanUtils.toBean(createReqVO, TradeOrderAuditDO.class);
        orderAuditMapper.insert(orderAudit);
        // 返回
        return orderAudit.getId();
    }

    @Override
    public void updateOrderAudit(TradeOrderAuditSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderAuditExists(updateReqVO.getId());
        // 更新
        TradeOrderAuditDO updateObj = BeanUtils.toBean(updateReqVO, TradeOrderAuditDO.class);
        orderAuditMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderAudit(Long id) {
        // 校验存在
        validateOrderAuditExists(id);
        // 删除
        orderAuditMapper.deleteById(id);
    }

    private void validateOrderAuditExists(Long id) {
        if (orderAuditMapper.selectById(id) == null) {
            throw exception(500, "订单审核记录不存在");
        }
    }

    @Override
    public TradeOrderAuditDO getOrderAudit(Long id) {
        return orderAuditMapper.selectById(id);
    }

    @Override
    public PageResult<TradeOrderAuditDO> getOrderAuditPage(TradeOrderAuditPageReqVO pageReqVO) {
        return orderAuditMapper.selectPage(pageReqVO);
    }

}