
package com.tmerclub.cloud.coupon.controller.app;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.coupon.service.CouponService;
import com.tmerclub.cloud.coupon.service.CouponUserService;
import com.tmerclub.cloud.coupon.vo.CouponAppVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * 优惠券
 *
 * @author YXF
 * @date 2020-12-08 17:22:56
 */
@RestController("appMyCouponController")
@RequestMapping("/myCoupon")
@Tag(name = "app-我的优惠券")
public class MyCouponController {

    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponUserService couponUserService;

    @GetMapping("/getUserCouponPage")
    @Operation(summary = "查看用户的优惠券(所有/指定状态)分页列表", description = "查看用户的优惠券(所有/指定状态)分页列表")
    @Parameters({
            @Parameter(name = "status", description = "优惠券状态 0:已过期 1:未使用 2:已使用"),
            @Parameter(name = "type", description = "0：所有 1：平台优惠券 2：店铺优惠券")
    })
    public ServerResponseEntity<PageVO<CouponAppVO>> getUserCouponPage(PageDTO page, @RequestParam("type") Long type, @RequestParam("status") Integer status) {
        return ServerResponseEntity.success(couponService.getUserCouponPage(page, type, status));
    }

    @GetMapping("/getCouponCountByStatus")
    @Operation(summary = "获取用户优惠券的数量", description = "获取各个状态下的优惠券个数")
    public ServerResponseEntity<Map<String, Long>> getCouponCountByStatus() {
        Map<String, Long> couponCount = couponUserService.getCouponCountByStatus(AuthUserContext.get().getUserId());
        return ServerResponseEntity.success(couponCount);
    }

    @PostMapping("/receive")
    @Operation(summary = "领取优惠券接口", description = "领取优惠券接口")
    public ServerResponseEntity<String> receive(@RequestBody Long couponId) {
        if (Objects.isNull(couponId)) {
            throw new LuckException("优惠券Id不能为空");
        }
        couponService.receive(couponId);
        return ServerResponseEntity.success("领取优惠券成功");
    }

    @DeleteMapping("/deleteCoupon/{couponUserId}")
    @Operation(summary = "删除用户优惠券", description = "通过优惠券id删除用户优惠券")
    @Parameter(name = "couponUserId", description = "用户优惠券Id")
    public ServerResponseEntity<String> deleteCoupon(@PathVariable("couponUserId") Long couponUserId) {
        couponUserService.deleteUserCouponByCouponId(couponUserId);
        return ServerResponseEntity.success("删除成功");
    }
}
