package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ShopCouponApplyItemService;
import com.gxwebsoft.shop.entity.ShopCouponApplyItem;
import com.gxwebsoft.shop.param.ShopCouponApplyItemParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 优惠券可用分类控制器
 *
 * @author 科技小王子
 * @since 2025-08-11 12:47:49
 */
@Tag(name = "优惠券可用分类管理")
@RestController
@RequestMapping("/api/shop/shop-coupon-apply-item")
public class ShopCouponApplyItemController extends BaseController {
    @Resource
    private ShopCouponApplyItemService shopCouponApplyItemService;

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:list')")
    @Operation(summary = "分页查询优惠券可用分类")
    @GetMapping("/page")
    public ApiResult<PageResult<ShopCouponApplyItem>> page(ShopCouponApplyItemParam param) {
        // 使用关联查询
        return success(shopCouponApplyItemService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:list')")
    @Operation(summary = "查询全部优惠券可用分类")
    @GetMapping()
    public ApiResult<List<ShopCouponApplyItem>> list(ShopCouponApplyItemParam param) {
        // 使用关联查询
        return success(shopCouponApplyItemService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:list')")
    @Operation(summary = "根据id查询优惠券可用分类")
    @GetMapping("/{id}")
    public ApiResult<ShopCouponApplyItem> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(shopCouponApplyItemService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:save')")
    @OperationLog
    @Operation(summary = "添加优惠券可用分类")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ShopCouponApplyItem shopCouponApplyItem) {

        if (shopCouponApplyItemService.save(shopCouponApplyItem)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:update')")
    @OperationLog
    @Operation(summary = "修改优惠券可用分类")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ShopCouponApplyItem shopCouponApplyItem) {
        if (shopCouponApplyItemService.updateById(shopCouponApplyItem)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:remove')")
    @OperationLog
    @Operation(summary = "删除优惠券可用分类")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (shopCouponApplyItemService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:save')")
    @OperationLog
    @Operation(summary = "批量添加优惠券可用分类")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ShopCouponApplyItem> list) {
        if (shopCouponApplyItemService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:update')")
    @OperationLog
    @Operation(summary = "批量修改优惠券可用分类")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ShopCouponApplyItem> batchParam) {
        if (batchParam.update(shopCouponApplyItemService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:shopCouponApplyItem:remove')")
    @OperationLog
    @Operation(summary = "批量删除优惠券可用分类")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (shopCouponApplyItemService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
