package com.pj.tb_goods_cart;

import com.pj.tb_goods_cart.vo.GoodsCartVo;
import com.pj.tb_goods_transit.TbGoodsTransit;
import com.pj.tb_goods_transit.TbGoodsTransitMapper;
import com.pj.tb_shop.TbShopMapper;
import org.springframework.beans.BeanUtils;
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;

/**
 * @Author Mechrevo
 * @Date 2023 08 04 16 30
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodGoodsCartService {

    @Autowired
    private TbGoodsCartMapper tbGoodsCartMapper;
    @Autowired
    private TbShopMapper tbShopMapper;
    @Autowired
    private TbGoodsTransitMapper tbGoodsTransitMapper;


    /**
     * 购物车转成VO
     * @param cartList
     * @return
     */
    public List<GoodsCartVo> changeTbGoodsCartToGoodsCartVoList(List<TbGoodsCart> cartList){
        //校验大小
        if(cartList.size() == 0)return null;
        //创建返回值集合
        List<GoodsCartVo> resultList = new ArrayList<>();
        //遍历封装
        for (TbGoodsCart item : cartList){
            //获取店铺 todo:届时放开店铺信息
//            TbShop tbShop = tbShopMapper.selectById(item.getShopId());
//            if(tbShop == null)throw new ServiceException("店铺信息有误~");
//            //店铺名称
//            String shopName = tbShop.getName();
            //设置重量
            Long goodsTransitId = item.getPublishGoodsId();
            TbGoodsTransit tbGoodsTransit = tbGoodsTransitMapper.selectById(goodsTransitId);
            if(tbGoodsTransit == null){
                System.out.println("\n过审商品信息有误!\n");
                continue;
            }
            Double grossWeight = tbGoodsTransit.getGrossWeight(); //毛重
            Double netWeight = tbGoodsTransit.getNetWeight(); //净重
            String goodsUnits = tbGoodsTransit.getGoodsUnits();
            //创建对象
            GoodsCartVo goodsCartVo = new GoodsCartVo();
            //关联的商品是否已被下单
            goodsCartVo.setGoodsIsorderStatus(item.getGoodsIsorderStatus());
            //数据拷贝
            BeanUtils.copyProperties(item,goodsCartVo);
            //设置店铺名称
            goodsCartVo.setShopName("shopName");
            //设置净重
            goodsCartVo.setNetWeight(netWeight + goodsUnits);
            //设置毛重
            goodsCartVo.setGrossWeight(grossWeight + goodsUnits);
            //装进集合
            resultList.add(goodsCartVo);
        }

        return resultList;
    }


}
