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

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

import cn.iocoder.yudao.module.order.service.order.OrderConsignorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.orderConsignor.*;
import cn.iocoder.yudao.module.order.dal.dataobject.orderConsignor.OrderConsignorDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.order.convert.orderConsignor.OrderConsignorConvert;
import cn.iocoder.yudao.module.order.dal.mysql.orderConsignor.OrderConsignorMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 订单发货人信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class OrderConsignorServiceImpl extends AbstractService<OrderConsignorMapper, OrderConsignorDO> implements OrderConsignorService {

    @Resource
    private OrderConsignorMapper orderConsignorMapper;

    @Override
    public Long createOrderConsignor(OrderConsignorCreateReqVO createReqVO) {
        // 插入
        OrderConsignorDO orderConsignor = OrderConsignorConvert.INSTANCE.convert(createReqVO);
        orderConsignorMapper.insert(orderConsignor);
        // 返回
        return orderConsignor.getId();
    }

    @Override
    public void updateOrderConsignor(OrderConsignorUpdateReqVO updateReqVO) {
        // 校验存在
//        this.validateOrderConsignorExists(updateReqVO.getId());
        // 更新
        OrderConsignorDO updateObj = OrderConsignorConvert.INSTANCE.convert(updateReqVO);
        orderConsignorMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderConsignor(Long id) {
        // 校验存在
//        this.validateOrderConsignorExists(id);
        // 删除
        orderConsignorMapper.deleteById(id);
    }

//    private void validateOrderConsignorExists(Long id) {
//        if (orderConsignorMapper.selectById(id) == null) {
//            throw exception(ORDER_CONSIGNOR_NOT_EXISTS);
//        }
//    }

    @Override
    public OrderConsignorDO getOrderConsignor(Long id) {
        return orderConsignorMapper.selectById(id);
    }

    @Override
    public OrderConsignorDO getOrderConsignorByOrderId(Long orderId) {
        return orderConsignorMapper.selectOne(new LambdaQueryWrapper<OrderConsignorDO>()
                .eq(OrderConsignorDO::getOrderId, orderId).orderByDesc(OrderConsignorDO::getId).last("limit 1"));
    }

    @Override
    public List<OrderConsignorDO> getOrderConsignorList(Collection<Long> ids) {
        return orderConsignorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderConsignorDO> getOrderConsignorPage(OrderConsignorQueryVO query, PageVO page) {
        return orderConsignorMapper.selectPage(page, query);
    }

    @Override
    public List<OrderConsignorDO> getOrderConsignorList(OrderConsignorQueryVO query) {
        return orderConsignorMapper.selectList(query);
    }
}
