package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaTaskCountService;
import com.gxwebsoft.oa.entity.OaTaskCount;
import com.gxwebsoft.oa.param.OaTaskCountParam;
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-count")
public class OaTaskCountController extends BaseController {
    @Resource
    private OaTaskCountService oaTaskCountService;

    @Operation(summary = "分页查询数据统计")
    @GetMapping("/page")
    public ApiResult<PageResult<OaTaskCount>> page(OaTaskCountParam param) {
        // 使用关联查询
        return success(oaTaskCountService.pageRel(param));
    }

    @Operation(summary = "查询全部数据统计")
    @GetMapping()
    public ApiResult<List<OaTaskCount>> list(OaTaskCountParam param) {
        PageParam<OaTaskCount, OaTaskCountParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(oaTaskCountService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(oaTaskCountService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaTaskCount:list')")
    @OperationLog
    @Operation(summary = "根据id查询数据统计")
    @GetMapping("/{id}")
    public ApiResult<OaTaskCount> get(@PathVariable("id") Integer id) {
        return success(oaTaskCountService.getById(id));
        // 使用关联查询
        //return success(oaTaskCountService.getByIdRel(id));
    }

    @Operation(summary = "添加数据统计")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaTaskCount oaTaskCount) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaTaskCount.setUserId(loginUser.getUserId());
        }
        if (oaTaskCountService.save(oaTaskCount)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改数据统计")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaTaskCount oaTaskCount) {
        if (oaTaskCountService.updateById(oaTaskCount)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除数据统计")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaTaskCountService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加数据统计")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaTaskCount> list) {
        if (oaTaskCountService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改数据统计")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaTaskCount> batchParam) {
        if (batchParam.update(oaTaskCountService, "task_count_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除数据统计")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaTaskCountService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
