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

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

import cn.iocoder.yudao.module.order.service.order.OrderConsigneeService;
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.orderConsignee.*;
import cn.iocoder.yudao.module.order.dal.dataobject.orderConsignee.OrderConsigneeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.order.convert.orderConsignee.OrderConsigneeConvert;
import cn.iocoder.yudao.module.order.dal.mysql.orderConsignee.OrderConsigneeMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 订单收货人信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class OrderConsigneeServiceImpl extends AbstractService<OrderConsigneeMapper, OrderConsigneeDO> implements OrderConsigneeService {

    @Resource
    private OrderConsigneeMapper orderConsigneeMapper;

    @Override
    public Long createOrderConsignee(OrderConsigneeCreateReqVO createReqVO) {
        // 插入
        OrderConsigneeDO orderConsignee = OrderConsigneeConvert.INSTANCE.convert(createReqVO);
        orderConsigneeMapper.insert(orderConsignee);
        // 返回
        return orderConsignee.getId();
    }

    @Override
    public void updateOrderConsignee(OrderConsigneeUpdateReqVO updateReqVO) {
        // 校验存在
//        this.validateOrderConsigneeExists(updateReqVO.getId());
        // 更新
        OrderConsigneeDO updateObj = OrderConsigneeConvert.INSTANCE.convert(updateReqVO);
        orderConsigneeMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderConsignee(Long id) {
        // 校验存在
//        this.validateOrderConsigneeExists(id);
        // 删除
        orderConsigneeMapper.deleteById(id);
    }

//    private void validateOrderConsigneeExists(Long id) {
//        if (orderConsigneeMapper.selectById(id) == null) {
//            throw exception(ORDER_CONSIGNEE_NOT_EXISTS);
//        }
//    }

    @Override
    public OrderConsigneeDO getOrderConsignee(Long id) {
        return orderConsigneeMapper.selectById(id);
    }

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

    @Override
    public List<OrderConsigneeDO> getOrderConsigneeList(Collection<Long> ids) {
        return orderConsigneeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderConsigneeDO> getOrderConsigneePage(OrderConsigneeQueryVO query, PageVO page) {
        return orderConsigneeMapper.selectPage(page, query);
    }

    @Override
    public List<OrderConsigneeDO> getOrderConsigneeList(OrderConsigneeQueryVO query) {
        return orderConsigneeMapper.selectList(query);
    }
}
