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

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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.DiscountDto;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.enums.DiscountRule;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.discount.comment.model.Discount;
import com.yami.shop.discount.comment.model.DiscountItem;
import com.yami.shop.discount.comment.service.DiscountService;
import com.yami.shop.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 促销活动接口
 */
@RestController
@RequestMapping("/marking/discount")
@Api(tags = "促销活动接口")
@AllArgsConstructor
public class DiscountController {

    private final DiscountService discountService;
    private final ProductService productService;

    @GetMapping("/getDiscountByProdId")
    @ApiOperation(value = "通过商品id获取商品所有促销活动", notes = "通过商品id获取商品所有促销活动")
    @ApiImplicitParam(name = "prodId", value = "商品Id", required = true, dataType = "Long")
    public ResponseEntity<List<DiscountDto>> getDiscountByProdId(@RequestParam(value = "prodId") Long prodId) {
        Product product = productService.getById(prodId);
        List<DiscountDto> discountDtoList = discountService.listByProdId(prodId,product.getShopId());
        return ResponseEntity.ok(discountDtoList);
    }


    @GetMapping("/prodList")
    @ApiOperation(value = "限时特惠", notes = "获取限时特惠商品列表")
    @ApiImplicitParam(name = "discountId", value = "活动ID",required = true,dataType = "Long")
    public ResponseEntity<IPage<ProductDto>> discountProdList(PageParam<ProductDto> page,@RequestParam(value = "discountId") Long discountId) {
        IPage<ProductDto> productDtoPage = discountService.discountProdList(page,discountId);
        List<ProductDto> records = productDtoPage.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            Discount discount = discountService.getDiscountAndItemAndProdById(discountId);

            // 最优惠的那个满减项
            DiscountItem discountItem = discount.getDiscountItems().get(0);

            // 折扣百分比
            double dicountProportion;

            // 打折
            if (Objects.equals(discount.getDiscountRule(), DiscountRule.M2D.value()) || Objects.equals(discount.getDiscountRule(), DiscountRule.P2D.value())) {
                dicountProportion = Arith.sub(1,Arith.div(discountItem.getDiscount(), 10L));
            } else {
                dicountProportion = Arith.div(discountItem.getDiscount(), discountItem.getNeedAmount());
            }
            for (ProductDto prod : records) {
                // 将要减去的价格
                double subPrice = Arith.mul(prod.getPrice(), dicountProportion);
                // 如果有活动价格上限
                if (discount.getMaxReduceAmount() != null && subPrice > discount.getMaxReduceAmount()) {
                    subPrice = discount.getMaxReduceAmount();
                }
                //价格最低只能为0.01
                double activityPrice = Arith.sub(prod.getPrice(), subPrice);
                if (activityPrice<0.01){
                    activityPrice = 0.01;
                }
                prod.setActivityPrice(activityPrice);
            }
        }
        return ResponseEntity.ok(productDtoPage);
    }

    @GetMapping("/getDiscountList")
    @ApiOperation(value = "获取促销活动列表", notes = "获取促销活动列表")
    public ResponseEntity<IPage<DiscountDto>> getDiscountList(PageParam<Discount> page) {
        IPage<DiscountDto> productDtoPage = discountService.getDiscountList(page);
        return ResponseEntity.ok(productDtoPage);
    }

    @GetMapping("/getDiscountByDiscountId/{discountId}")
    @ApiOperation(value = "获取促销活动详情", notes = "通过活动id获取促销活动详情")
    @ApiImplicitParam(name = "discountId", value = "活动ID", required = true, dataType = "Long")
    public ResponseEntity<DiscountDto> getDiscountByDiscountId(@PathVariable("discountId") Long discountId) {
        DiscountDto discountDto = discountService.getDiscountByDiscountId(discountId);
        if (Objects.isNull(discountDto)){
            throw new GlobalException("该活动不存在或者已结束");
        }
        return ResponseEntity.ok(discountDto);
    }
}
