package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.ProjectService;
import com.gxwebsoft.oa.entity.Project;
import com.gxwebsoft.oa.param.ProjectParam;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 项目管理表控制器
 *
 * @author WebSoft
 * @since 2022-11-16 11:00:43
 */
@Api(tags = "项目管理表管理")
@RestController
@RequestMapping("/api/oa/project")
public class ProjectController extends BaseController {
    @Resource
    private ProjectService projectService;

    @PreAuthorize("hasAuthority('oa:project:list')")
    @OperationLog
    @ApiOperation("分页查询项目管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<Project>> page(ProjectParam param) {
        PageParam<Project, ProjectParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(projectService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(projectService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:project:list')")
    @OperationLog
    @ApiOperation("查询全部项目管理表")
    @GetMapping()
    public ApiResult<List<Project>> list(ProjectParam param) {
        PageParam<Project, ProjectParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(projectService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(projectService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:project:list')")
    @OperationLog
    @ApiOperation("根据id查询项目管理表")
    @GetMapping("/{id}")
    public ApiResult<Project> get(@PathVariable("id") Integer id) {
        return success(projectService.getById(id));
        // 使用关联查询
        //return success(projectService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:project:save')")
    @OperationLog
    @ApiOperation("添加项目管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Project project) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          project.setUserId(loginUser.getUserId());
        }
        if (projectService.save(project)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:project:update')")
    @OperationLog
    @ApiOperation("修改项目管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Project project) {
        if (projectService.updateById(project)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:project:remove')")
    @OperationLog
    @ApiOperation("删除项目管理表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (projectService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:project:save')")
    @OperationLog
    @ApiOperation("批量添加项目管理表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Project> list) {
        if (projectService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:project:update')")
    @OperationLog
    @ApiOperation("批量修改项目管理表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Project> batchParam) {
        if (batchParam.update(projectService, "project_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:project:remove')")
    @OperationLog
    @ApiOperation("批量删除项目管理表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (projectService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
