package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaTaskService;
import com.gxwebsoft.oa.entity.OaTask;
import com.gxwebsoft.oa.param.OaTaskParam;
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.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 2024-09-10 20:57:42
 */
@Tag(name = "任务记录表管理")
@RestController
@RequestMapping("/api/oa/oa-task")
public class OaTaskController extends BaseController {
    @Resource
    private OaTaskService oaTaskService;

    @Operation(summary = "分页查询任务记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OaTask>> page(OaTaskParam param) {
        // 使用关联查询
        return success(oaTaskService.pageRel(param));
    }

    @Operation(summary = "查询全部任务记录表")
    @GetMapping()
    public ApiResult<List<OaTask>> list(OaTaskParam param) {
        PageParam<OaTask, OaTaskParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaTaskService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaTaskService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaTask:list')")
    @OperationLog
    @Operation(summary = "根据id查询任务记录表")
    @GetMapping("/{id}")
    public ApiResult<OaTask> get(@PathVariable("id") Integer id) {
        return success(oaTaskService.getById(id));
        // 使用关联查询
        //return success(oaTaskService.getByIdRel(id));
    }

    @Operation(summary = "添加任务记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaTask oaTask) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaTask.setUserId(loginUser.getUserId());
        }
        if (oaTaskService.save(oaTask)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改任务记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaTask oaTask) {
        if (oaTaskService.updateById(oaTask)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除任务记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaTaskService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加任务记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaTask> list) {
        if (oaTaskService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改任务记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaTask> batchParam) {
        if (batchParam.update(oaTaskService, "task_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除任务记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaTaskService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
