package com.gxwebsoft.project.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.project.service.ProjectFieldService;
import com.gxwebsoft.project.entity.ProjectField;
import com.gxwebsoft.project.param.ProjectFieldParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用参数控制器
 *
 * @author 科技小王子
 * @since 2025-03-14 16:21:11
 */
@Tag(name = "应用参数管理")
@RestController
@RequestMapping("/api/project/project-field")
public class ProjectFieldController extends BaseController {
    @Resource
    private ProjectFieldService projectFieldService;

    @PreAuthorize("hasAuthority('project:project:list')")
    @Operation(summary = "分页查询应用参数")
    @GetMapping("/page")
    public ApiResult<PageResult<ProjectField>> page(ProjectFieldParam param) {
        // 使用关联查询
        return success(projectFieldService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('project:project:list')")
    @Operation(summary = "查询全部应用参数")
    @GetMapping()
    public ApiResult<List<ProjectField>> list(ProjectFieldParam param) {
        // 使用关联查询
        return success(projectFieldService.listRel(param));
    }

    @PreAuthorize("hasAuthority('project:project:list')")
    @Operation(summary = "根据id查询应用参数")
    @GetMapping("/{id}")
    public ApiResult<ProjectField> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(projectFieldService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('project:project:save')")
    @OperationLog
    @Operation(summary = "添加应用参数")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ProjectField projectField) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         projectField.setUserId(loginUser.getUserId());
        }
        if (projectFieldService.save(projectField)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('project:project:update')")
    @OperationLog
    @Operation(summary = "修改应用参数")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ProjectField projectField) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          projectField.setUpdateUserId(loginUser.getUserId());
        }
        if (projectFieldService.updateById(projectField)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('project:project:remove')")
    @OperationLog
    @Operation(summary = "删除应用参数")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (projectFieldService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('project:project:save')")
    @OperationLog
    @Operation(summary = "批量添加应用参数")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ProjectField> list) {
        if (projectFieldService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('project:project:update')")
    @OperationLog
    @Operation(summary = "批量修改应用参数")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ProjectField> batchParam) {
        if (batchParam.update(projectFieldService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('project:project:remove')")
    @OperationLog
    @Operation(summary = "批量删除应用参数")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (projectFieldService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
