package com.czg.controller.user;

import com.czg.account.vo.ShopInfoCouponVO;
import com.czg.account.vo.UserCouponVo;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.ShopCouponService;
import com.czg.market.vo.ShopCouponPopUp;
import com.czg.market.vo.UserCouponVO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ww
 * @description
 */
@RestController
@RequestMapping("/user/coupon")
public class UShopCouponController {
    @Resource
    private ShopCouponService couponService;

    /**
     * 优惠券弹窗
     *
     * @param getMode 首页home/用餐eat/订单order
     * @return
     */
    @GetMapping("/popUp")
    public CzgResult<List<ShopCouponPopUp>> getPopUp(String getMode) {
        Long shopId = StpKit.USER.getShopId();
        return CzgResult.success(couponService.getPopUp(shopId, StpKit.USER.getLoginIdAsLong(), getMode));
    }

    /**
     * 优惠券弹窗领取
     *
     * @param getMode 首页home/用餐eat/订单order
     */
    @GetMapping("/receivePopUp")
    public CzgResult<Void> receivePopUp(String getMode) {
        Long shopId = StpKit.USER.getShopId();
        couponService.receivePopUp(shopId, StpKit.USER.getLoginIdAsLong(), getMode);
        return CzgResult.success();
    }


    /**
     * 获取当前店铺会员信息
     * 请求头必须携带shopId, 不携带则会失败
     *
     * @return 店铺会员信息
     */
    @GetMapping("/shops")
    public CzgResult<List<ShopInfoCouponVO>> shops() {
        Long shopId = StpKit.USER.getShopId(0L);
        return CzgResult.success(couponService.getShopByCouponRecord(shopId == 0L ? null : shopId, StpKit.USER.getLoginIdAsLong()));
    }

    /**
     * 通过用户Id 查找优惠券
     *
     * @param status 0 未使用 1已使用 2已过期
     */
    @GetMapping("/findByUserId")
    public CzgResult<Page<UserCouponVO>> findByUserId(
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long shopId) {
        return CzgResult.success(couponService.find(StpKit.USER.getLoginIdAsLong(), name, shopId, status));
    }

    /**
     * 生成订单后使用
     * 通过用户Id 查找优惠券
     *
     * @param shopUserId 店铺用户Id
     */
    @GetMapping("/findCoupon")
    public CzgResult<List<UserCouponVo>> findCoupon(@RequestHeader String shopId, @RequestParam Long shopUserId,
                                                    @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer isFood) {
        AssertUtil.isBlank(shopId, "店铺Id不能为空");
        return CzgResult.success(couponService.findCoupon(Long.parseLong(shopId), shopUserId, type, isFood));
    }
}
