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

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.orderjz.vo.OrderJzPageReqVO;
import com.yc.ship.module.trade.controller.admin.orderjz.vo.OrderJzSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.orderjz.OrderJzDO;
import com.yc.ship.module.trade.dal.mysql.orderjz.OrderJzMapper;
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 lsq
 */
@Service
@Validated
public class OrderJzServiceImpl implements OrderJzService {

    @Resource
    private OrderJzMapper orderJzMapper;

    @Override
    public Long createOrderJz(OrderJzSaveReqVO createReqVO) {
        // 插入
        OrderJzDO orderJz = BeanUtils.toBean(createReqVO, OrderJzDO.class);
        orderJzMapper.insert(orderJz);
        // 返回
        return orderJz.getId();
    }

    @Override
    public void updateOrderJz(OrderJzSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderJzExists(updateReqVO.getId());
        // 更新
        OrderJzDO updateObj = BeanUtils.toBean(updateReqVO, OrderJzDO.class);
        orderJzMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderJz(Long id) {
        // 校验存在
        validateOrderJzExists(id);
        // 删除
        orderJzMapper.deleteById(id);
    }

    private void validateOrderJzExists(Long id) {
        if (orderJzMapper.selectById(id) == null) {
            throw exception(500,"不存在");
        }
    }

    @Override
    public OrderJzDO getOrderJz(Long id) {
        return orderJzMapper.selectById(id);
    }

    @Override
    public PageResult<OrderJzDO> getOrderJzPage(OrderJzPageReqVO pageReqVO) {
        return orderJzMapper.selectPage(pageReqVO);
    }

}