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

import com.tmerclub.cloud.api.delivery.dto.DeliveryOrderItemDTO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.order.constant.DeliveryType;
import com.tmerclub.cloud.order.mapper.PurchaseOrderItemMapper;
import com.tmerclub.cloud.order.model.PurchaseOrderItem;
import com.tmerclub.cloud.order.service.PurchaseOrderItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 采购商品
 *
 * @author lhd
 * @date 2022-06-22 17:12:02
 */
@Service
public class PurchaseOrderItemServiceImpl implements PurchaseOrderItemService {
    private static final Logger LOGGER = LoggerFactory.getLogger(PurchaseOrderItemServiceImpl.class);

    @Autowired
    private PurchaseOrderItemMapper purchaseOrderItemMapper;

    @Override
    public PageVO<PurchaseOrderItem> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> purchaseOrderItemMapper.list());
    }

    @Override
    public PurchaseOrderItem getByPurchaseSpuId(Long purchaseSpuId) {
        return purchaseOrderItemMapper.getByPurchaseSpuId(purchaseSpuId);
    }

    @Override
    public void save(PurchaseOrderItem purchaseOrderItem) {
        purchaseOrderItemMapper.save(purchaseOrderItem);
    }


    @Override
    public void updateByDeliveries(List<DeliveryOrderItemDTO> deliveryOrderItems, Integer deliveryType) {
        Integer initDeliveryType = deliveryType;
        int devType;
        for (DeliveryOrderItemDTO deliveryOrderItem : deliveryOrderItems) {
            devType = purchaseOrderItemMapper.getDevTypeByOrderItemId(deliveryOrderItem.getOrderItemId());
            LOGGER.info("物流配送方式为:" + devType + "传参为：" + deliveryType);
            if (Objects.equals(devType, DeliveryType.DELIVERY.value()) || Objects.equals(devType, DeliveryType.ONLINE.value())) {
                //如果快递项已有快递类型，则set进去应当是快递类型
                deliveryType = devType;
            }
            if (Objects.equals(devType, DeliveryType.ONLINE.value()) && Objects.equals(initDeliveryType, DeliveryType.DELIVERY.value())) {
                // 如果是线上发货，现改为线下的话就更新为线下
                deliveryType = DeliveryType.DELIVERY.value();
            }
            purchaseOrderItemMapper.updateByDelivery(deliveryOrderItem, deliveryType);
        }
    }

    @Override
    public int countUnDeliveryNumByOrderId(Long orderId) {
        return purchaseOrderItemMapper.countUnDeliveryNumByOrderId(orderId);
    }

    @Override
    public boolean getDevTypeByOrderId(Long orderId) {
        List<Integer> devTypes = purchaseOrderItemMapper.getDevTypeByOrderId(orderId);
        return devTypes.contains(DeliveryType.DELIVERY.value()) || devTypes.contains(DeliveryType.ONLINE.value());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatchById(List<PurchaseOrderItem> purchaseOrderItemListDb) {
        for (PurchaseOrderItem purchaseOrderItem : purchaseOrderItemListDb) {
            purchaseOrderItemMapper.update(purchaseOrderItem);
        }
    }

    @Override
    public void updateStatusByPurchaseOrderId(Integer purchaseOrderStatus, Long purchaseOrderId) {
        purchaseOrderItemMapper.updateStatusByPurchaseOrderId(Collections.singletonList(purchaseOrderId), purchaseOrderStatus);
    }
}
