package com.ruoyi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.IDGenerator;
import com.ruoyi.mall.domain.dto.OrderProductListDTO;
import com.ruoyi.mall.domain.order.OrderItem;
import com.ruoyi.mall.mapper.OrderItemMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单中所包含的商品Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class OrderItemService extends ServiceImpl<OrderItemMapper, OrderItem> {




    @Transactional
    public void saveOrderItem(AppUser member, LocalDateTime optTime,
                              Long orderId, List<OrderProductListDTO> list){
        List<OrderItem> addOrderItemList = new ArrayList<>();
        list.forEach(item -> {
            OrderItem orderItem = new OrderItem();
            orderItem.setId(IDGenerator.generateId());
            orderItem.setOrderId(orderId);
            orderItem.setProductId(item.getProduct().getId());
            orderItem.setOutProductId(item.getProduct().getOutProductId());
            orderItem.setSkuId(item.getSku().getId());
            orderItem.setOutSkuId(item.getSku().getOutSkuId());
            orderItem.setPic(item.getSku().getPic());
            orderItem.setProductName(item.getProduct().getName());
            orderItem.setSalePrice(item.getSku().getPrice());
            orderItem.setQuantity(item.getQuantity());
            orderItem.setProductCategoryId(item.getProduct().getCategoryId());
            orderItem.setSpData(item.getSku().getSpData());
            orderItem.setCreateBy(member.getId());
            orderItem.setCreateTime(optTime);
            orderItem.setStoreId(item.getProduct().getStoreId());
            addOrderItemList.add(orderItem);
        });
        boolean flag = saveBatch(addOrderItemList);
        if (!flag){
            throw new RuntimeException("新增订单item失败");
        }
    }
}
