package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.Task;
import com.gxwebsoft.oa.service.TaskCountService;
import com.gxwebsoft.oa.entity.TaskCount;
import com.gxwebsoft.oa.param.TaskCountParam;
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.oa.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 数据统计控制器
 *
 * @author 科技小王子
 * @since 2023-10-30 14:25:30
 */
@Api(tags = "数据统计管理")
@RestController
@RequestMapping("/api/oa/task-count")
public class TaskCountController extends BaseController {
    @Resource
    private TaskCountService taskCountService;

    @ApiOperation("分页查询数据统计")
    @GetMapping("/page")
    public ApiResult<PageResult<TaskCount>> page(TaskCountParam param, HttpServletRequest request) {
        param.setToken(JwtUtil.getAccessToken(request));
        param.setTenantId(getTenantId());
        return success(taskCountService.pageRel(param));
    }

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

    @ApiOperation("根据id查询数据统计")
    @GetMapping("/{id}")
    public ApiResult<TaskCount> get(@PathVariable("id") Integer id) {
        return success(taskCountService.getById(id));
        // 使用关联查询
        //return success(taskCountService.getByIdRel(id));
    }

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

    @ApiOperation("修改数据统计")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TaskCount taskCount) {
        if (taskCountService.updateById(taskCount)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
