/*
 * 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.ProdCommDataDto;
import com.yami.shop.bean.app.dto.ProdCommDto;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.dto.ProdByCategoryIdAndShopIdDTO;
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.common.util.PageParam;
import com.yami.shop.service.ProdCommService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/prod")
@Api(tags = "商品接口")
public class ProdController {

    @Autowired
    private ProductService prodService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProdCommService prodCommService;


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

        Product product = prodService.getProductByProdId(prodId, platform);
        if (product == null || product.getStatus() != 1) {
            throw new GlobalException("商品已下线");
        }

        List<Sku> skuList = skuService.listByProdId(prodId, platform, shopId);
        if (ObjectUtils.isNotEmpty(skuList)) {
            // 启用的sku列表
            List<Sku> useSkuList = skuList.stream().sorted(Comparator.comparing(
                    Sku::getPrice,
                    Comparator.nullsFirst(Comparator.naturalOrder())
            )).peek((sku) -> product.setTotalStocks(product.getTotalStocks() + sku.getActualStocks())).collect(Collectors.toList());
            product.setPrice(useSkuList.get(0).getPrice());
            product.setSkuList(useSkuList);
        }

//        ProductDto productDto = mapperFacade.map(product, ProductDto.class);
        ProductDto productDto = new ProductDto();
        BeanUtils.copyProperties(product, productDto);

        return ResponseEntity.ok(productDto);
    }

    @GetMapping("/isStatus")
    @ApiOperation(value = "校验商品是否下架", notes = "根据商品ID（prodId）校验商品是否下架")
    @ApiImplicitParam(name = "prodId", value = "商品ID", required = true)
    public ResponseEntity<Boolean> isStatus(Long prodId) {
        Product product = prodService.getProductByProdId(prodId);
        if (product == null || product.getStatus() != 1) {
            return ResponseEntity.ok(false);
        }
        return ResponseEntity.ok(true);
    }

    @GetMapping("/lastedProdPage")
    @ApiOperation(value = "新品推荐", notes = "获取新品推荐商品列表")
    @ApiImplicitParams({
    })
    public ResponseEntity<IPage<ProductDto>> lastedProdPage(PageParam<ProductDto> page,
                                                            @RequestParam(value = "lat", required = false) String lat,
                                                            @RequestParam(value = "lon", required = false) String lon,
                                                            @RequestParam(value = "distance", required = false) String distance) {
        IPage<ProductDto> productDtoIPage = prodService.pageByPutawayTime(page, lat, lon, distance);
        return ResponseEntity.ok(productDtoIPage);
    }


    @GetMapping("/moreBuyProdList")
    @ApiOperation(value = "每日疯抢", notes = "获取销量最多的商品列表")
    @ApiImplicitParams({})
    public ResponseEntity<IPage<ProductDto>> moreBuyProdList(PageParam<ProductDto> page) {
        IPage<ProductDto> productDtoIPage = prodService.moreBuyProdList(page);
        return ResponseEntity.ok(productDtoIPage);
    }

    @GetMapping("/discountProds")
    @ApiOperation(value = "根据活动id获取活动商品列表", notes = "根据活动id获取活动商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "discountId", value = "活动id", required = true, dataType = "Long"),
    })
    public ResponseEntity<IPage<ProductDto>> getDiscountProds(
            @RequestParam(value = "discountId", required = true) Long discountId,
            PageParam<ProductDto> page) {
        IPage<ProductDto> productDtoList = prodService.pageByDiscountId(page, discountId);
        return ResponseEntity.ok(productDtoList);
    }

    @GetMapping("/prodCommData")
    @ApiOperation(value = "返回商品评论数据(好评率 好评数量 中评数 差评数)", notes = "根据商品id获取")
    public ResponseEntity<ProdCommDataDto> getProdCommData(Long prodId) {
        return ResponseEntity.ok(prodCommService.getProdCommDataByProdId(prodId));
    }


    @GetMapping("/prodCommPageByProd")
    @ApiOperation(value = "根据商品返回评论分页数据", notes = "传入商品id和页码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prodId", value = "商品id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "evaluate", value = "-1或null 全部，0好评 1中评 2差评 3有图", required = true, dataType = "Long"),
    })
    public ResponseEntity<IPage<ProdCommDto>> getProdCommPageByProdId(PageParam page, Long prodId, Integer evaluate) {
        return ResponseEntity.ok(prodCommService.getProdCommDtoPageByProdId(page, prodId, evaluate));
    }

    @PostMapping("/listProdByCategoryIdAndShopId")
    @ApiOperation(value = "根据分类ID、商户ID查询归属的所有商品")
    public ResponseEntity<IPage<Product>> listProdByCategoryIdAndShopId(@RequestBody ProdByCategoryIdAndShopIdDTO prodByCategoryIdAndShopIdDTO) {
        return ResponseEntity.ok(prodService.listProdByCategoryIdAndShopId(prodByCategoryIdAndShopIdDTO));
    }

}
