package co.yixiang.yshop.module.order.service.storeordercartinfo;

import cn.hutool.core.util.IdUtil;
import co.yixiang.yshop.module.order.dal.dataobject.storeordercartinfo.StoreOrderCartInfoDO;
import co.yixiang.yshop.module.order.dal.mysql.storeordercartinfo.StoreOrderCartInfoMapper;
import co.yixiang.yshop.module.product.controller.app.cart.vo.AppStoreCartQueryVo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

/**
 * 订单购物详情 Service 实现类
 *
 * @author yshop
 */
@Slf4j
@Service
@Validated
public class StoreOrderCartInfoServiceImpl extends ServiceImpl<StoreOrderCartInfoMapper, StoreOrderCartInfoDO> implements StoreOrderCartInfoService {

    /**
     * 添加购物车商品信息
     * @param oid 订单id
     * @param orderId
     * @param cartInfo 购物车信息
     */
    @Async
    @Override
    //@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCartInfo(Long oid, String orderId, List<AppStoreCartQueryVo> cartInfo) {
        log.info("==========添加购物车商品信息start===========");
        List<StoreOrderCartInfoDO> list = new ArrayList<>();
        for (AppStoreCartQueryVo cart : cartInfo) {
            StoreOrderCartInfoDO info = new StoreOrderCartInfoDO();
            info.setOid(oid);
            info.setOrderId(orderId);
            info.setCartId(cart.getId());
            info.setProductId(cart.getProductId());
            info.setCartInfo(JSONObject.toJSON(cart).toString());
            info.setUnique(IdUtil.simpleUUID());
            info.setIsAfterSales(1);
            list.add(info);
        }

        this.saveBatch(list);
    }

}
