package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.UserFirmService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.UserFirmForm;
import com.zsElectric.boot.business.model.query.UserFirmQuery;
import com.zsElectric.boot.business.model.vo.UserFirmVO;
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:29
 */
@Tag(name = "企业与用户关系接口")
@RestController
@RequestMapping("/api/v1/user-firm")
@RequiredArgsConstructor
public class UserFirmController  {

    private final UserFirmService userFirmService;

    @Operation(summary = "企业与用户关系分页列表")
    @GetMapping("/page")
    // @PreAuthorize("@ss.hasPerm('business:user-firm:query')")
    public PageResult<UserFirmVO> getUserFirmPage(UserFirmQuery queryParams ) {
        IPage<UserFirmVO> result = userFirmService.getUserFirmPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增企业与用户关系")
    @PostMapping
    // @PreAuthorize("@ss.hasPerm('business:user-firm:add')")
    public Result<Void> saveUserFirm(@RequestBody @Valid UserFirmForm formData ) {
        boolean result = userFirmService.saveUserFirm(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取企业与用户关系表单数据")
    @GetMapping("/{id}/form")
    // @PreAuthorize("@ss.hasPerm('business:user-firm:edit')")
    public Result<UserFirmForm> getUserFirmForm(
        @Parameter(description = "企业与用户关系ID") @PathVariable Long id
    ) {
        UserFirmForm formData = userFirmService.getUserFirmFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改企业与用户关系")
    @PutMapping(value = "/{id}")
    // @PreAuthorize("@ss.hasPerm('business:user-firm:edit')")
    public Result<Void> updateUserFirm(
            @Parameter(description = "企业与用户关系ID") @PathVariable Long id,
            @RequestBody @Validated UserFirmForm formData
    ) {
        boolean result = userFirmService.updateUserFirm(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除企业与用户关系")
    @DeleteMapping("/{ids}")
    // @PreAuthorize("@ss.hasPerm('business:user-firm:delete')")
    public Result<Void> deleteUserFirms(
        @Parameter(description = "企业与用户关系ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userFirmService.deleteUserFirms(ids);
        return Result.judge(result);
    }
}
