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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.order.convert.orderItemPackLog.OrderItemPackLogConvert;
import cn.iocoder.yudao.module.order.dal.dataobject.orderItemPackLog.OrderItemPackLogDO;
import cn.iocoder.yudao.module.order.dal.mysql.orderItemPackLog.OrderItemPackLogMapper;
import cn.iocoder.yudao.module.order.vo.orderItemPackLog.OrderItemPackLogCreateReqVO;
import cn.iocoder.yudao.module.order.vo.orderItemPackLog.OrderItemPackLogQueryVO;
import cn.iocoder.yudao.module.order.vo.orderItemPackLog.OrderItemPackLogUpdateReqVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.order.enums.ErrorCodeConstants.ITEM_PACK_LOG_NOT_EXISTS;

/**
 * 入仓项打包纪录 Service 实现类
 *
 * @author yanghao
 */
@Service
@Validated
public class OrderItemPackLogServiceImpl extends AbstractService<OrderItemPackLogMapper, OrderItemPackLogDO> implements OrderItemPackLogService {

    @Resource
    private OrderItemPackLogMapper itemPackLogMapper;

    @Override
    public Long createItemPackLog(OrderItemPackLogCreateReqVO createReqVO) {
        // 插入
        OrderItemPackLogDO itemPackLog = OrderItemPackLogConvert.INSTANCE.convert(createReqVO);
        itemPackLogMapper.insert(itemPackLog);
        // 返回
        return itemPackLog.getId();
    }

    @Override
    public void updateItemPackLog(OrderItemPackLogUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateItemPackLogExists(updateReqVO.getId());
        // 更新
        OrderItemPackLogDO updateObj = OrderItemPackLogConvert.INSTANCE.convert(updateReqVO);
        itemPackLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteItemPackLog(Long id) {
        // 校验存在
        this.validateItemPackLogExists(id);
        // 删除
        itemPackLogMapper.deleteById(id);
    }

    private void validateItemPackLogExists(Long id) {
        if (itemPackLogMapper.selectById(id) == null) {
            throw exception(ITEM_PACK_LOG_NOT_EXISTS);
        }
    }

    @Override
    public OrderItemPackLogDO getItemPackLog(Long id) {
        return itemPackLogMapper.selectById(id);
    }

    @Override
    public List<OrderItemPackLogDO> getItemPackLogList(Collection<Long> ids) {
        return itemPackLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderItemPackLogDO> getItemPackLogPage(OrderItemPackLogQueryVO query, PageVO page) {
        return itemPackLogMapper.selectPage(page, query);
    }

    @Override
    public List<OrderItemPackLogDO> getItemPackLogList(OrderItemPackLogQueryVO query) {
        return itemPackLogMapper.selectList(query);
    }

    @Override
    public List<OrderItemPackLogDO> getOriginalPackLogList(Long orderId) {
        return itemPackLogMapper.getOriginalPackLogList(orderId);
    }
}
