/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.SkuDto;
import com.yami.shop.bean.model.Sku;
import com.yami.shop.bean.vo.SkuBuyTogetherOftenVo;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sku")
@Api(tags = "sku规格接口")
@AllArgsConstructor
public class SkuController {

    private final SkuService skuService;

    private final MapperFacade mapperFacade;

    @GetMapping("/getSkuList")
    @ApiOperation(value = "通过prodId获取上架的规格列表", notes = "通过prodId获取上架的规格列表")
    @ApiImplicitParam(name = "prodId", value = "商品id", dataType = "Long")
    public ResponseEntity<List<SkuDto>> getSkuListByProdId(Long prodId) {
        List<Sku> skus = skuService.listByProdId(prodId);

        List<SkuDto> skuDtoList = mapperFacade.mapAsList(skus.stream().filter(sku -> sku.getStatus() == 1).collect(Collectors.toList()), SkuDto.class);
        return ResponseEntity.ok(skuDtoList);
    }

    @GetMapping("/buyTogetherOften")
    @ApiOperation(value = "商品详情-经常一起买")
    public ResponseEntity<IPage<SkuBuyTogetherOftenVo>> buyTogetherOften(PageParam<SkuBuyTogetherOftenVo> page, Long prodId,Long shopId,Long channelId,String userId) {
        shopId = shopId == null ? 1 : shopId;
        channelId = channelId == null ? 1 : channelId;
        return ResponseEntity.ok(skuService.buyTogetherOften(page,prodId,shopId,channelId,userId));
    }

}
