package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.UserAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.UserAccountForm;
import com.zsElectric.boot.business.model.query.UserAccountQuery;
import com.zsElectric.boot.business.model.vo.UserAccountVO;
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:20
 */
@Tag(name = "个人账户接口")
@RestController
@RequestMapping("/api/v1/user-account")
@RequiredArgsConstructor
public class UserAccountController  {

    private final UserAccountService userAccountService;

    @Operation(summary = "个人账户分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:user-account:query')")
    public PageResult<UserAccountVO> getUserAccountPage(UserAccountQuery queryParams ) {
        IPage<UserAccountVO> result = userAccountService.getUserAccountPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增个人账户")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:user-account:add')")
    public Result<Void> saveUserAccount(@RequestBody @Valid UserAccountForm formData ) {
        boolean result = userAccountService.saveUserAccount(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取个人账户表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:user-account:edit')")
    public Result<UserAccountForm> getUserAccountForm(
        @Parameter(description = "个人账户ID") @PathVariable Long id
    ) {
        UserAccountForm formData = userAccountService.getUserAccountFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改个人账户")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:user-account:edit')")
    public Result<Void> updateUserAccount(
            @Parameter(description = "个人账户ID") @PathVariable Long id,
            @RequestBody @Validated UserAccountForm formData
    ) {
        boolean result = userAccountService.updateUserAccount(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除个人账户")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:user-account:delete')")
    public Result<Void> deleteUserAccounts(
        @Parameter(description = "个人账户ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userAccountService.deleteUserAccounts(ids);
        return Result.judge(result);
    }
}
