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

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.CouponDto;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.enums.CouponProdType;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.coupon.comment.model.Coupon;
import com.yami.shop.coupon.comment.model.CouponProd;
import com.yami.shop.coupon.comment.service.CouponService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/coupon")
@Api(tags = "优惠券接口")
@AllArgsConstructor
public class CouponController {

    private CouponService couponService;

    private MapperFacade mapperFacade;

    @GetMapping("/listShopCoupon")
    @ApiOperation(value = "查看一店铺的所有优惠券", notes = "通过店铺id(shopId)获取该店优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID", required = true, dataType = "Long")
    })
    public ResponseEntity<List<CouponDto>> listShopCoupon(@RequestParam(value = "shopId") Long shopId) {
        List<Coupon> couponList = couponService.listPutonByShopId(shopId);
        List<CouponDto> couponDtos = mapperFacade.mapAsList(couponList, CouponDto.class);
        return ResponseEntity.ok(couponDtos);
    }

    @GetMapping("/listByProdId")
    @ApiOperation(value = "获取商品可用的优惠券列表", notes = "获取该商品可用的优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = true),
            @ApiImplicitParam(name = "prodId", value = "商品id", required = true)
    })
    public ResponseEntity<List<CouponDto>> listByProdId(Long shopId, Long prodId) {
        // 获取已投放优惠券
        List<Coupon> couponList = couponService.listPutonByShopId(shopId);
        // 过滤商品
        couponList = couponList.stream().filter(a -> {
            boolean flag = true;
            //过滤指定商品不参与
            if (Objects.equals(a.getSuitableProdType(), CouponProdType.PROD_NO_IN.value())) {
                for (CouponProd couponProd : a.getCouponProds()) {
                    if (Objects.equals(couponProd.getProdId(), prodId)) {
                        flag = false;
                        break;
                    }
                }
            }
            //过滤指定商品参与
            if (Objects.equals(a.getSuitableProdType(), CouponProdType.PROD_IN.value())) {
                flag = false;
                for (CouponProd couponProd : a.getCouponProds()) {
                    if (Objects.equals(couponProd.getProdId(), prodId)) {
                        flag = true;
                        break;
                    }
                }
            }
            return flag;
        }).collect(Collectors.toList());

        List<CouponDto> couponDtos = mapperFacade.mapAsList(couponList, CouponDto.class);

        return ResponseEntity.ok(couponDtos);
    }

    @GetMapping("/prodListByCouponId")
    @ApiOperation(value = "获取优惠券可用的商品列表", notes = "获取优惠券可用的商品列表")
    @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    public ResponseEntity<IPage<ProductDto>> prodListByCouponId(PageParam<ProductDto> page, @RequestParam("couponId") Long couponId) {
        if (couponId <=0) {
            page.setRecords(Collections.emptyList());
            return ResponseEntity.ok(page);
        }
        Coupon coupon = couponService.getById(couponId);
        if (Objects.isNull(coupon) || coupon.getOverdueStatus() != 1) {
            page.setRecords(Collections.emptyList());
            return ResponseEntity.ok(page);
        }

        IPage<ProductDto> productDtoIPage = couponService.prodListByCoupon(page, coupon);
        return ResponseEntity.ok(productDtoIPage);
    }

    @GetMapping("/couponScorePage")
    @ApiOperation(value = "积分优惠券列表", notes = "获取积分商城可换的优惠券列表")
    @ApiImplicitParams({
    })
    public ResponseEntity<IPage<Coupon>> couponScorePage(PageParam<Coupon> page) {
        IPage<Coupon> couponDtos = couponService.page(page,new LambdaQueryWrapper<Coupon>()
            .eq(Coupon::getOverdueStatus,1)
            .eq(Coupon::getPutonStatus, 1)
            .eq(Coupon::getIsScoreType,1));
        return ResponseEntity.ok(couponDtos);
    }

    @GetMapping("/couponById")
    @ApiOperation(value = "获取一条优惠券信息", notes = "通过id获取对应优惠券")
    @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    public ResponseEntity<Coupon> getCouponById(Long couponId) {
        Coupon coupon = couponService.getById(couponId);
        return ResponseEntity.ok(coupon);
    }
}
