package com.czg.controller.admin;

import com.czg.account.dto.QueryReceiveDto;
import com.czg.account.dto.ShopCouponDTO;
import com.czg.account.service.ShopCouponService;
import com.czg.account.vo.CouponReceiveVo;
import com.czg.account.vo.UserCouponVo;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺优惠券
 *
 * @author ww
 * @description
 */

@RestController
@RequestMapping("/admin/coupon")
public class ShopCouponController {
    @Resource
    private ShopCouponService couponService;

    /**
     * 店铺优惠券列表
     * 权限标识: coupon:list
     * 状态 0 未使用 1已使用 2已过期
     */
    @SaAdminCheckPermission(value = "coupon:list", name = "优惠券列表")
    @GetMapping
    public CzgResult<List<ShopCouponDTO>> detail(@RequestParam(required = false) Integer type,
                                                 @RequestParam(required = false) Integer status) {
        return CzgResult.success(couponService.getList(StpKit.USER.getShopId(), type, status));
    }

    /**
     * 店铺优惠券新增
     * 权限标识: coupon:add
     */
    @SaAdminCheckPermission(value = "coupon:add", name = "优惠券添加")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated ShopCouponDTO couponDTO) {
        couponDTO.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(couponService.add(couponDTO));
    }

    /**
     * 店铺优惠券修改
     * 权限标识: coupon:edit
     */
    @SaAdminCheckPermission(value = "coupon:edit", name = "优惠券修改")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopCouponDTO couponDTO) {
        couponDTO.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(couponService.edit(couponDTO));
    }

    /**
     * 店铺优惠券获取记录
     * 权限标识: coupon:delete
     */
    @SaAdminCheckPermission(value = "coupon:queryReceive", name = "优惠券领取记录")
    @GetMapping("/queryReceive")
    public CzgResult<Page<CouponReceiveVo>> queryReceive(@Validated QueryReceiveDto param) {
        return CzgResult.success(couponService.queryReceive(param));
    }

    /**
     * 生成订单后使用
     * 通过用户Id 查找优惠券
     *
     * @param shopUserId 店铺用户Id
     */
    @GetMapping("/findCoupon")
    public CzgResult<List<UserCouponVo>> findCoupon(@RequestParam Long shopUserId, @RequestParam(required = false) Integer type) {
        return CzgResult.success(couponService.findCoupon(StpKit.USER.getShopId(), shopUserId, type));
    }

}
