package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StorePlatformBenefits;
import shop.alien.entity.store.vo.StorePlatformBenefitsVo;
import shop.alien.store.service.StorePlatformBenefitsService;

/**
 * 福利管理 前端控制器
 *
 * @author ssk
 * @since 2025-12-18
 */
@Slf4j
@Api(tags = {"福利管理"})
@ApiSort(10)
@CrossOrigin
@RestController
@RequestMapping("/benefits")
@RequiredArgsConstructor
public class StorePlatformBenefitsController {

    private final StorePlatformBenefitsService benefitsService;

    @ApiOperation("分页查询福利列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "type", value = "类型：1=优惠券，2=U币", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0待审核/1审核拒绝/2审核通过", dataType = "int", paramType = "query")
    })
    public R<IPage<StorePlatformBenefitsVo>> getPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String name) {
        log.info("StorePlatformBenefitsController.getPage - pageNum={}, pageSize={}, type={}, name={}", pageNum, pageSize, type, name);
        return R.data(benefitsService.getPage(pageNum, pageSize, type, name));
    }

    @ApiOperation("根据ID查询详情")
    @ApiOperationSupport(order = 2)
    @GetMapping("/detail/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "path", required = true)
    })
    public R<StorePlatformBenefits> getDetail(@PathVariable Integer id) {
        log.info("StorePlatformBenefitsController.getDetail - id={}", id);
        return R.data(benefitsService.getById(id));
    }

    @ApiOperation("新增福利")
    @ApiOperationSupport(order = 3)
    @PostMapping("/add")
    public R<String> add(@RequestBody StorePlatformBenefits benefits) {
        log.info("StorePlatformBenefitsController.add - benefits={}", benefits);
        int result = benefitsService.add(benefits);
        if (result > 0) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("修改福利")
    @ApiOperationSupport(order = 4)
    @PostMapping("/update")
    public R<String> update(@RequestBody StorePlatformBenefits benefits) {
        log.info("StorePlatformBenefitsController.update - benefits={}", benefits);
        int result = benefitsService.update(benefits);
        if (result > 0) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("删除福利")
    @ApiOperationSupport(order = 5)
    @PostMapping("/delete/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "path", required = true)
    })
    public R<String> delete(@PathVariable Integer id) {
        log.info("StorePlatformBenefitsController.delete - id={}", id);
        int result = benefitsService.delete(id);
        if (result > 0) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("审核福利")
    @ApiOperationSupport(order = 6)
    @PostMapping("/audit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "审核状态：1审核拒绝/2审核通过", dataType = "int", paramType = "query", required = true)
    })
    public R<String> audit(@RequestParam Integer id, @RequestParam Integer status) {
        log.info("StorePlatformBenefitsController.audit - id={}, status={}", id, status);
        int result = benefitsService.audit(id, status);
        if (result > 0) {
            return R.success("审核成功");
        }
        return R.fail("审核失败");
    }


}

