package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.UserInfoForm;
import com.zsElectric.boot.business.model.query.UserInfoQuery;
import com.zsElectric.boot.business.model.vo.UserInfoVO;
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:27
 */
@Tag(name = "个人用户信息接口")
@RestController
@RequestMapping("/api/v1/user-info")
@RequiredArgsConstructor
public class UserInfoController  {

    private final UserInfoService userInfoService;

    @Operation(summary = "个人用户信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:user-info:query')")
    public PageResult<UserInfoVO> getUserInfoPage(UserInfoQuery queryParams ) {
        IPage<UserInfoVO> result = userInfoService.getUserInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增个人用户信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:user-info:add')")
    public Result<Void> saveUserInfo(@RequestBody @Valid UserInfoForm formData ) {
        boolean result = userInfoService.saveUserInfo(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取个人用户信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:user-info:edit')")
    public Result<UserInfoForm> getUserInfoForm(
        @Parameter(description = "个人用户信息ID") @PathVariable Long id
    ) {
        UserInfoForm formData = userInfoService.getUserInfoFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改个人用户信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:user-info:edit')")
    public Result<Void> updateUserInfo(
            @Parameter(description = "个人用户信息ID") @PathVariable Long id,
            @RequestBody @Validated UserInfoForm formData
    ) {
        boolean result = userInfoService.updateUserInfo(id, formData);
        return Result.judge(result);
    }

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