package org.springblade.modules.api.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.base.BaseEntity;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.BusinessSceneEnum;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.api.vo.CouponQuantityVO;
import org.springblade.modules.mall.entity.CouponUser;
import org.springblade.modules.mall.service.ICouponService;
import org.springblade.modules.mall.service.ICouponUserService;
import org.springblade.modules.mall.vo.CouponUserVO;
import org.springblade.modules.mall.vo.CouponVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Author pangqijun
 * Date 2023/5/4
 * Description 优惠券接口
 */
@RestController
@RequestMapping(CommonConstant.API_URL)
@Api(value = "优惠券接口", tags = "11.优惠券接口")
public class AppCouponController extends BladeController {

    @Resource
    private ICouponUserService couponUserService;
    @Resource
    private ICouponService couponService;

    @GetMapping("/user-coupon/quantity")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取用户优惠券数量")
    public R<CouponQuantityVO> quantity() {
        return data(couponUserService.quantity(AuthUtil.getUserId()));
    }

    @GetMapping("/user-coupon/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "用户优惠券列表")
    public R<List<CouponUserVO>> list(@ApiParam(value = "优惠券状态 1-未使用 2-已使用 3-已过期")
                                        @RequestParam(value = "state") Integer state, Query query) {
        IPage<CouponUserVO> page = couponUserService.userCouponList(Condition.getPage(query), state, AuthUtil.getUserId());
        if (state == 3 && CollUtil.isNotEmpty(page.getRecords())) {
            for (CouponUserVO record : page.getRecords()) {
                record.setState(3);
            }
        }
        return data(page.getRecords());
    }

    @GetMapping("/user-coupon/receive")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "用户领取优惠券")
    public R<List<CouponUserVO>> receive(@ApiParam(value = "优惠券ID")
                                      @RequestParam(value = "couponId") Long couponId) {
        couponUserService.receive(couponId, AuthUtil.getUserId());
        return success("ok");
    }

    @GetMapping("/coupon/getByScene")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "根据业务场景查询优惠券")
    public R<List<CouponVO>> getByScene(@ApiParam(value = "业务场景 1-新人福利 2-福利中心 3-其他（查询多个使用逗号分隔）")
                                      @RequestParam(value = "scene") String scene) {
        List<CouponVO> list = couponService.getByScene(Func.toLongList(scene));
        if (CollUtil.isNotEmpty(list)) {
            List<Long> collect = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
            List<CouponUser> couponUsers = couponUserService.lambdaQuery().eq(CouponUser::getUserId, AuthUtil.getUserId()).in(CouponUser::getCouponId, collect).list();
            if (CollUtil.isNotEmpty(couponUsers)) {
                Map<Long, CouponUser> map = couponUsers.stream().collect(Collectors.toMap(CouponUser::getCouponId, Function.identity()));
                for (CouponVO couponVO : list) {
                    CouponUser couponUser = map.get(couponVO.getId());
                    couponVO.setIsReceive(couponUser != null);
                }
            }
        }
        return data(list);
    }
}
