/*
 * 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.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.CouponDto;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.coupon.comment.model.Coupon;
import com.yami.shop.coupon.comment.model.CouponUser;
import com.yami.shop.coupon.comment.service.CouponService;
import com.yami.shop.coupon.comment.service.CouponUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/p/myCoupon")
@Api(tags = "我的优惠券接口")
@AllArgsConstructor
public class MyCouponController {

    private CouponService couponService;
    private CouponUserService couponUserService;

    @GetMapping("/listCouponIds")
    @ApiOperation(value = "查看用户拥有的所有优惠券id", notes = "查看用户拥有的所有优惠券id")
    public ResponseEntity<List<Long>> list() {
        String userId = SecurityUtils.getUser().getUserId();
        List<Long> couponIds = couponService.listCouponIdsByUserId(userId);
        return ResponseEntity.ok(couponIds);
    }

    @PostMapping("/receive")
    @ApiOperation(value = "领取优惠券接口", notes = "领取优惠券接口")
    @ApiImplicitParam(name = "couponId", value = "优惠券ID", required = true, dataType = "Long")
    public ResponseEntity<String> receive(@RequestBody Long couponId) {
        String userId = SecurityUtils.getUser().getUserId();
        Coupon coupon = couponService.getById(couponId);
        couponService.receive(coupon, userId);
        return ResponseEntity.ok("领取优惠券成功");
    }

    @GetMapping("/getMyCouponsStatusCount")
    @ApiOperation(value = "获取优惠券个数", notes = "获取各个状态下的优惠券个数")
    public ResponseEntity<Map<String, Long>> getCouponCountByStatus() {
        String userId = SecurityUtils.getUser().getUserId();
        Map<String, Long> couponCount = couponService.getCouponCountByStatus(userId);
        return ResponseEntity.ok(couponCount);
    }

    @GetMapping("/getCouponList")
    @ApiOperation(value = "通过状态查看用户的优惠券列表信息", notes = "通过状态查看用户的优惠券列表信息,优惠券状态 0:已过期 1:未使用 2:使用过")
    @ApiImplicitParam(name = "status", value = "优惠券状态 0:失效 1:有效 2:使用过", required = true, dataType = "Integer")
    public ResponseEntity<IPage<CouponDto>> getCouponList(PageParam<CouponDto> page, @RequestParam("status") Integer status) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<CouponDto> couponDtoList = couponService.getCouponListByStatus(page, userId, status);
        return ResponseEntity.ok(couponDtoList);
    }

    @Deprecated
    @DeleteMapping("/deleteCoupon/{couponId}")
    @ApiOperation(value = "删除用户优惠券", notes = "通过优惠券id删除用户优惠券")
    @ApiImplicitParam(name = "couponId", value = "优惠券Id", required = true, dataType = "Long")
    public ResponseEntity<String> deleteCoupon(@PathVariable("couponId") Long couponId) {
        String userId = SecurityUtils.getUser().getUserId();
        couponService.deleteUserCouponByCouponId(userId, couponId);
        return ResponseEntity.ok("删除成功");
    }

    @DeleteMapping("/delCoupon/{couponUserId}")
    @ApiOperation(value = "删除用户优惠券", notes = "通过优惠券关联id删除用户优惠券")
    @ApiImplicitParam(name = "couponUserId", value = "优惠券关联id", required = true, dataType = "Long")
    public ResponseEntity<String> deleteCouponById(@PathVariable("couponUserId") Long couponUserId) {
        String userId = SecurityUtils.getUser().getUserId();
        CouponUser couponUser = new CouponUser();
        couponUser.setCouponUserId(couponUserId);
        couponUser.setIsDelete(1);
        couponUserService.update(couponUser,new LambdaUpdateWrapper<CouponUser>()
                .eq(CouponUser::getCouponUserId,couponUserId).eq(CouponUser::getUserId,userId));
        return ResponseEntity.ok("删除成功");
    }

    @GetMapping("/generalCouponList")
    @ApiOperation(value = "通用券列表", notes = "获取通用券列表")
    public ResponseEntity<List<CouponDto>> generalCouponList() {
        String userId = SecurityUtils.getUser().getUserId();
        List<CouponDto> couponList = couponService.generalCouponList(userId);
        return ResponseEntity.ok(couponList);
    }

//    @GetMapping("/prodCouponList")
//    @ApiOperation(value = "商品券列表", notes = "获取商品券列表(指定商品可用优惠券)")
//    public ResponseEntity<IPage<CouponDto>> prodCouponList(PageParam<CouponDto> page) {
//        String userId = SecurityUtils.getUser().getUserId();
//        return ResponseEntity.ok(couponService.pageProdCoupon(page, userId));
//    }

    @GetMapping("/getCouponPage")
    @ApiOperation(value = "商品券列表", notes = "获取商品券列表(指定商品可用优惠券)")
    public ResponseEntity<IPage<CouponDto>> getCouponList(PageParam<CouponDto> page) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<CouponDto> couponDto = couponService.getCouponList(page,userId);
        return ResponseEntity.ok(couponDto);
    }
}
