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

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
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.orderExceptionResult.OrderExceptionResultConvert;
import cn.iocoder.yudao.module.order.dal.dataobject.orderExceptionResult.OrderExceptionResultDO;
import cn.iocoder.yudao.module.order.dal.mysql.orderExceptionResult.OrderExceptionResultMapper;
import cn.iocoder.yudao.module.order.vo.orderExceptionResult.OrderExceptionResultCreateReqVO;
import cn.iocoder.yudao.module.order.vo.orderExceptionResult.OrderExceptionResultQueryVO;
import cn.iocoder.yudao.module.order.vo.orderExceptionResult.OrderExceptionResultUpdateReqVO;
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.ORDER_EXCEPTION_RESULT_NOT_EXISTS;

/**
 * 异常处理记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class OrderExceptionResultServiceImpl extends AbstractService<OrderExceptionResultMapper, OrderExceptionResultDO> implements OrderExceptionResultService {

    @Resource
    private OrderExceptionResultMapper orderExceptionResultMapper;

    @Override
    public Long createOrderExceptionResult(OrderExceptionResultCreateReqVO createReqVO) {
        // 插入
        OrderExceptionResultDO orderExceptionResult = OrderExceptionResultConvert.INSTANCE.convert(createReqVO);
        orderExceptionResultMapper.insert(orderExceptionResult);
        // 返回
        return orderExceptionResult.getId();
    }

    @Override
    public void updateOrderExceptionResult(OrderExceptionResultUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateOrderExceptionResultExists(updateReqVO.getId());
        // 更新
        OrderExceptionResultDO updateObj = OrderExceptionResultConvert.INSTANCE.convert(updateReqVO);
        orderExceptionResultMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderExceptionResult(Long id) {
        // 校验存在
        this.validateOrderExceptionResultExists(id);
        // 删除
        orderExceptionResultMapper.deleteById(id);
    }

    private void validateOrderExceptionResultExists(Long id) {
        if (orderExceptionResultMapper.selectById(id) == null) {
            throw exception(ORDER_EXCEPTION_RESULT_NOT_EXISTS);
        }
    }

    @Override
    public OrderExceptionResultDO getOrderExceptionResult(Long id) {
        return orderExceptionResultMapper.selectById(id);
    }

    @Override
    public OrderExceptionResultDO getOrderExceptionResultByOrderExceptionId(Long orderExceptionId) {
        List<OrderExceptionResultDO> orderExceptionResultDOList = orderExceptionResultMapper
                .selectList(new LambdaQueryWrapperX<OrderExceptionResultDO>().eq(OrderExceptionResultDO::getOrderExceptionId, orderExceptionId));
        if (CollectionUtil.isNotEmpty(orderExceptionResultDOList)) {
            return orderExceptionResultDOList.get(0);
        }
        return null;
    }

    @Override
    public List<OrderExceptionResultDO> getOrderExceptionResultList(Collection<Long> ids) {
        return orderExceptionResultMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderExceptionResultDO> getOrderExceptionResultPage(OrderExceptionResultQueryVO query, PageVO page) {
        return orderExceptionResultMapper.selectPage(page, query);
    }

    @Override
    public List<OrderExceptionResultDO> getOrderExceptionResultList(OrderExceptionResultQueryVO query) {
        return orderExceptionResultMapper.selectList(query);
    }
}
