package com.fdkankan.platform.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.goods.entity.Goods;
import com.fdkankan.platform.goods.entity.GoodsSku;
import com.fdkankan.platform.goods.mapper.IGoodsMapper;
import com.fdkankan.platform.goods.service.IGoodsService;
import com.fdkankan.platform.goods.service.IGoodsSkuService;
import com.fdkankan.platform.goods.vo.ResponseGoods;
import com.fdkankan.platform.goods.vo.ResponseGoodsSku;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 商品主表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<IGoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private IGoodsSkuService goodsSkuService;

    @Override
    public ResponseGoods getByGoodsId(Long goodsId) {
        Goods goods = this.getById(goodsId);
        ResponseGoods responseGoods = null;
        if (goods != null){
            responseGoods = new ResponseGoods();
            BeanUtils.copyProperties(goods, responseGoods);
            responseGoods.setGoodsStatus(goods.getGoodsStatus());
        }
        return responseGoods;
    }

    @Override
    public ResponseGoods getDetail(Long goodsId) {
        ResponseGoods responseGoods = this.getByGoodsId(goodsId);
        if(responseGoods == null){
            throw new BusinessException(ErrorCode.NOT_RECORD);
        }
        List<GoodsSku> skus = goodsSkuService.getByGoodsId(goodsId);
        List<ResponseGoodsSku> responseGoodsSkus = null;
        if (skus != null && skus.size() > 0){
            responseGoodsSkus = new ArrayList<>();
            for (GoodsSku sku : skus){
                ResponseGoodsSku responseGoodsSku = new ResponseGoodsSku();
                BeanUtils.copyProperties(sku, responseGoodsSku);
                responseGoodsSkus.add(responseGoodsSku);
            }
        }
        responseGoods.setSkuList(responseGoodsSkus);
        return responseGoods;
    }
}
