package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.FirmInfoService;
import com.zsElectric.boot.business.converter.FirmInfoConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.FirmInfoForm;
import com.zsElectric.boot.business.model.query.FirmInfoQuery;
import com.zsElectric.boot.business.model.vo.FirmInfoVO;
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;

import java.util.List;

/**
 * 企业信息前端控制层
 *
 * @author zsElectric
 * @since 2025-12-11 10:10
 */
@Tag(name = "企业信息接口")
@RestController
@RequestMapping("/api/v1/firmInfo")
@RequiredArgsConstructor
public class FirmInfoController  {

    private final FirmInfoService firmInfoService;
    private final FirmInfoConverter firmInfoConverter;

    @Operation(summary = "企业信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:firmInfo:query')")
    public PageResult<FirmInfoVO> getFirmInfoPage(FirmInfoQuery queryParams ) {
        IPage<FirmInfoVO> result = firmInfoService.getFirmInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "企业信息列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPerm('business:firmInfo:query')")
    public Result<List<FirmInfoVO>> getFirmInfoList() {
        List<FirmInfoVO> result = firmInfoConverter.toVO(firmInfoService.list());
        return Result.success(result);
    }

    @Operation(summary = "新增企业信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:firmInfo:add')")
    public Result<Void> saveFirmInfo(@RequestBody @Valid FirmInfoForm formData ) {
        boolean result = firmInfoService.saveFirmInfo(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取企业信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:firmInfo:edit')")
    public Result<FirmInfoForm> getFirmInfoForm(
        @Parameter(description = "企业信息ID") @PathVariable Long id
    ) {
        FirmInfoForm formData = firmInfoService.getFirmInfoFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改企业信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:firmInfo:edit')")
    public Result<Void> updateFirmInfo(
            @Parameter(description = "企业信息ID") @PathVariable Long id,
            @RequestBody @Validated FirmInfoForm formData
    ) {
        boolean result = firmInfoService.updateFirmInfo(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除企业信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:firmInfo:delete')")
    public Result<Void> deleteFirmInfos(
        @Parameter(description = "企业信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = firmInfoService.deleteFirmInfos(ids);
        return Result.judge(result);
    }
}
