package cn.iocoder.yudao.module.order.service.order.impl;

import java.util.*;
import javax.annotation.Resource;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.order.service.order.OrderDepartureService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.order.vo.orderDeparture.*;
import cn.iocoder.yudao.module.order.dal.dataobject.orderDeparture.OrderDepartureDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.order.convert.orderDeparture.OrderDepartureConvert;
import cn.iocoder.yudao.module.order.dal.mysql.orderDeparture.OrderDepartureMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 订单始发仓库 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class OrderDepartureServiceImpl extends AbstractService<OrderDepartureMapper, OrderDepartureDO> implements OrderDepartureService {

    @Resource
    private OrderDepartureMapper orderDepartureMapper;

    @Override
    public Long createOrderDeparture(OrderDepartureCreateReqVO createReqVO) {
        // 插入
        OrderDepartureDO orderDeparture = OrderDepartureConvert.INSTANCE.convert(createReqVO);
        orderDepartureMapper.insert(orderDeparture);
        // 返回
        return orderDeparture.getId();
    }

    @Override
    public void updateOrderDeparture(OrderDepartureUpdateReqVO updateReqVO) {
        // 校验存在
//        this.validateOrderDepartureExists(updateReqVO.getId());
        // 更新
        OrderDepartureDO updateObj = OrderDepartureConvert.INSTANCE.convert(updateReqVO);
        orderDepartureMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderDeparture(Long id) {
        // 校验存在
//        this.validateOrderDepartureExists(id);
        // 删除
        orderDepartureMapper.deleteById(id);
    }

//    private void validateOrderDepartureExists(Long id) {
//        if (orderDepartureMapper.selectById(id) == null) {
//            throw exception(ORDER_DEPARTURE_NOT_EXISTS);
//        }
//    }

    @Override
    public OrderDepartureDO getOrderDeparture(Long id) {
        return orderDepartureMapper.selectById(id);
    }

    @Override
    public List<OrderDepartureDO> getOrderDepartureList(Collection<Long> ids) {
        return orderDepartureMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderDepartureDO> getOrderDeparturePage(OrderDepartureQueryVO query, PageVO page) {
        return orderDepartureMapper.selectPage(page, query);
    }

    @Override
    public List<OrderDepartureDO> getOrderDepartureList(OrderDepartureQueryVO query) {
        return orderDepartureMapper.selectList(query);
    }

    @Override
    public OrderDepartureDO getByOrderId(Long orderId) {
        return orderDepartureMapper.selectOne(new LambdaQueryWrapperX<OrderDepartureDO>()
            .eq(OrderDepartureDO::getOrderId, orderId)
                .last("limit 1")
        );
    }

    @Override
    public List<OrderDepartureDO> getByOrderIdList(List<Long> orderIdList) {
        return orderDepartureMapper.selectList(new LambdaQueryWrapperX<OrderDepartureDO>()
                .in(OrderDepartureDO::getOrderId, orderIdList)
        );
    }
}
