package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.UserExchangeIntegralRuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.UserExchangeIntegralRuleForm;
import com.zsElectric.boot.business.model.query.UserExchangeIntegralRuleQuery;
import com.zsElectric.boot.business.model.vo.UserExchangeIntegralRuleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 积分兑换规则前端控制层
 *
 * @author zsElectric
 * @since 2025-12-12 10:07
 */
@Tag(name = "积分兑换规则接口")
@RestController
@RequestMapping("/api/v1/user-exchange-integral-rule")
@RequiredArgsConstructor
public class UserExchangeIntegralRuleController  {

    private final UserExchangeIntegralRuleService userExchangeIntegralRuleService;

    @Operation(summary = "积分兑换规则分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:user-exchange-integral-rule:query')")
    public PageResult<UserExchangeIntegralRuleVO> getUserExchangeIntegralRulePage(UserExchangeIntegralRuleQuery queryParams ) {
        IPage<UserExchangeIntegralRuleVO> result = userExchangeIntegralRuleService.getUserExchangeIntegralRulePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增积分兑换规则")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:user-exchange-integral-rule:add')")
    public Result<Void> saveUserExchangeIntegralRule(@RequestBody @Valid UserExchangeIntegralRuleForm formData ) {
        boolean result = userExchangeIntegralRuleService.saveUserExchangeIntegralRule(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取积分兑换规则表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:user-exchange-integral-rule:edit')")
    public Result<UserExchangeIntegralRuleForm> getUserExchangeIntegralRuleForm(
        @Parameter(description = "积分兑换规则ID") @PathVariable Long id
    ) {
        UserExchangeIntegralRuleForm formData = userExchangeIntegralRuleService.getUserExchangeIntegralRuleFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改积分兑换规则")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:user-exchange-integral-rule:edit')")
    public Result<Void> updateUserExchangeIntegralRule(
            @Parameter(description = "积分兑换规则ID") @PathVariable Long id,
            @RequestBody @Validated UserExchangeIntegralRuleForm formData
    ) {
        boolean result = userExchangeIntegralRuleService.updateUserExchangeIntegralRule(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "积分兑换规则启用禁用")
    @PutMapping(value = "/editStatus/{id}")
    @PreAuthorize("@ss.hasPerm('business:user-exchange-integral-rule:editStatus')")
    public Result<Void> editStatus(
            @Parameter(description = "积分兑换规则ID") @PathVariable Long id
    ) {
        boolean result = userExchangeIntegralRuleService.editStatus(id);
        return Result.judge(result);
    }

    @Operation(summary = "删除积分兑换规则")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:user-exchange-integral-rule:delete')")
    public Result<Void> deleteUserExchangeIntegralRules(
        @Parameter(description = "积分兑换规则ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userExchangeIntegralRuleService.deleteUserExchangeIntegralRules(ids);
        return Result.judge(result);
    }
}
