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.dal.dataobject.orderDeparture.OrderDepartureDO;
import cn.iocoder.yudao.module.order.service.order.OrderObjectiveService;
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.orderObjective.*;
import cn.iocoder.yudao.module.order.dal.dataobject.orderObjective.OrderObjectiveDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.order.convert.orderObjective.OrderObjectiveConvert;
import cn.iocoder.yudao.module.order.dal.mysql.orderObjective.OrderObjectiveMapper;

/**
 * 订单目的仓库 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class OrderObjectiveServiceImpl extends AbstractService<OrderObjectiveMapper, OrderObjectiveDO> implements OrderObjectiveService {

    @Resource
    private OrderObjectiveMapper orderObjectiveMapper;

    @Override
    public Long createOrderObjective(OrderObjectiveCreateReqVO createReqVO) {
        // 插入
        OrderObjectiveDO orderObjective = OrderObjectiveConvert.INSTANCE.convert(createReqVO);
        orderObjectiveMapper.insert(orderObjective);
        // 返回
        return orderObjective.getId();
    }

    @Override
    public void updateOrderObjective(OrderObjectiveUpdateReqVO updateReqVO) {
        // 校验存在
//        this.validateOrderObjectiveExists(updateReqVO.getId());
        // 更新
        OrderObjectiveDO updateObj = OrderObjectiveConvert.INSTANCE.convert(updateReqVO);
        orderObjectiveMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderObjective(Long id) {
        // 校验存在
//        this.validateOrderObjectiveExists(id);
        // 删除
        orderObjectiveMapper.deleteById(id);
    }

//    private void validateOrderObjectiveExists(Long id) {
//        if (orderObjectiveMapper.selectById(id) == null) {
//            throw exception(ORDER_OBJECTIVE_NOT_EXISTS);
//        }
//    }

    @Override
    public OrderObjectiveDO getOrderObjective(Long id) {
        return orderObjectiveMapper.selectById(id);
    }

    @Override
    public List<OrderObjectiveDO> getOrderObjectiveList(Collection<Long> ids) {
        return orderObjectiveMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderObjectiveDO> getOrderObjectivePage(OrderObjectiveQueryVO query, PageVO page) {
        return orderObjectiveMapper.selectPage(page, query);
    }

    @Override
    public List<OrderObjectiveDO> getOrderObjectiveList(OrderObjectiveQueryVO query) {
        return orderObjectiveMapper.selectList(query);
    }

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

    @Override
    public List<OrderObjectiveDO> getByOrderIdList(List<Long> orderIdList) {
        return orderObjectiveMapper.selectList(new LambdaQueryWrapperX<OrderObjectiveDO>()
                .inIfPresent(OrderObjectiveDO::getOrderId, orderIdList)
        );
    }

}
