
package com.tmerclub.cloud.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.leaf.manager.SegmentManager;
import com.tmerclub.cloud.constant.DistributedIdKey;
import com.tmerclub.cloud.order.mapper.OrderInvoiceMapper;
import com.tmerclub.cloud.order.mapper.OrderMapper;
import com.tmerclub.cloud.order.model.OrderInvoice;
import com.tmerclub.cloud.order.service.OrderInvoiceService;
import com.tmerclub.cloud.order.vo.OrderInvoiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Pineapple
 * @date 2021/8/2 8:58
 */
@Service
public class OrderInvoiceServiceImpl implements OrderInvoiceService {

    @Autowired
    private OrderInvoiceMapper orderInvoiceMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SegmentManager segmentManager;

    @Override
    public OrderInvoice getByOrderInvoiceId(Long orderInvoiceId) {
        return orderInvoiceMapper.getByOrderInvoiceId(orderInvoiceId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long userId, OrderInvoice orderInvoice) {
        Long orderInvoiceId = segmentManager.getSegmentIdWithDateTime(DistributedIdKey.tmerclub_ORDER_INVOICE, userId);
        orderInvoice.setOrderInvoiceId(orderInvoiceId);
        orderInvoiceMapper.save(orderInvoice);
        orderMapper.updateOrderTime(orderInvoice.getOrderId());
    }

    @Override
    public void saveBatch(List<OrderInvoice> orderInvoices) {
        orderInvoiceMapper.saveBatch(orderInvoices);
    }

    @Override
    public void update(OrderInvoice orderInvoice) {
        orderInvoiceMapper.update(orderInvoice);
    }

    @Override
    public void deleteById(Long orderInvoiceId) {
        orderInvoiceMapper.deleteById(orderInvoiceId);
    }

    @Override
    public OrderInvoiceVO getById(Long orderInvoiceId) {
        return orderInvoiceMapper.getById(orderInvoiceId, I18nMessage.getLang());
    }

    @Override
    public Long getByOrderId(Long orderId, Long shopId, Long supplierId) {
        return orderInvoiceMapper.getByOrderId(orderId, shopId, supplierId);
    }

    @Override
    public boolean isUpload(Long orderId, Long shopId, Long supplierId) {
        return orderInvoiceMapper.isUpload(orderId, shopId, supplierId) >= 1;
    }

    @Override
    public void deleteBatch(List<Long> orderIds) {
        List<Long> orderDeleteIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (Long orderId : orderIds) {
            if (Objects.nonNull(orderInvoiceMapper.getByOrderId(orderId, null, null))) {
                //如果该订单有发票信息
                orderDeleteIds.add(orderId);
            }
        }
        if (CollectionUtil.isNotEmpty(orderDeleteIds)) {
            orderInvoiceMapper.deleteBatch(orderDeleteIds);
        }
    }
}
