package com.yami.shop.user.api.controller;

import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.Sku;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.SkuService;
import com.yami.shop.user.comment.service.UserLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
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.Objects;
import java.util.stream.Collectors;

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


/**
 * 积分商品信息管理实现
 *
 * @author LGH
 * @version 1.0.0
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/score/prod")
@Api(tags = "积分商品接口")
public class ScoreProdController {

    @Autowired
    private ProductService prodService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private SkuService skuService;

    @Autowired
    private UserLevelService userLevelService;





    @GetMapping("/prodInfo")
    @ApiOperation(value = "积分商品详情信息", notes = "根据商品ID（prodId）获取商品信息")
    @ApiImplicitParam(name = "prodId", value = "商品ID", required = true, dataType = "Long")
    public ResponseEntity<ProductDto> prodInfo(Long prodId) {

        Product product = prodService.getProductByProdId(prodId);
        if (product == null || product.getStatus() != 1) {
            throw new GlobalException("商品已下线");
        }
        if (!Objects.equals(product.getProdType(), ProdType.PROD_TYPE_SCORE.value()) || product.getScorePrice() <= 0) {
            throw new GlobalException("不是积分商品或者积分价格异常！");
        }

        List<Sku> skuList = skuService.listByProdId(prodId);
        // 启用的sku列表
        List<Sku> useSkuList = skuList.stream().filter(sku -> sku.getStatus() == 1).collect(Collectors.toList());
        product.setSkuList(useSkuList);
        ProductDto productDto = mapperFacade.map(product, ProductDto.class);
        return ResponseEntity.ok(productDto);
    }




}
