package com.gxwebsoft.project.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.project.service.ProjectUserService;
import com.gxwebsoft.project.entity.ProjectUser;
import com.gxwebsoft.project.param.ProjectUserParam;
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-user")
public class ProjectUserController extends BaseController {
    @Resource
    private ProjectUserService projectUserService;

    @PreAuthorize("hasAuthority('project:projectUser:list')")
    @Operation(summary = "分页查询应用成员")
    @GetMapping("/page")
    public ApiResult<PageResult<ProjectUser>> page(ProjectUserParam param) {
        // 使用关联查询
        return success(projectUserService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('project:projectUser:list')")
    @Operation(summary = "查询全部应用成员")
    @GetMapping()
    public ApiResult<List<ProjectUser>> list(ProjectUserParam param) {
        // 使用关联查询
        return success(projectUserService.listRel(param));
    }

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

    @PreAuthorize("hasAuthority('project:projectUser:save')")
    @OperationLog
    @Operation(summary = "添加应用成员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ProjectUser projectUser) {
        if (projectUserService.save(projectUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('project:projectUser:update')")
    @OperationLog
    @Operation(summary = "修改应用成员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ProjectUser projectUser) {
        if (projectUserService.updateById(projectUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
