package com.sqx.modules.taskCenter.controller;


import cn.hutool.core.date.DateUtil;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.taskCenter.entity.TaskCenter;
import com.sqx.modules.taskCenter.service.TaskCenterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@Slf4j
@RestController
@Api(value = "任务中心", tags = {"任务中心"})
@RequestMapping
public class TaskCenterController {
    /**
     * 服务对象
     */
    @Autowired
    private TaskCenterService taskCenterService;

    @PostMapping("/taskCenter/insertTaskCenter")
    @ApiOperation("添加任务中心")
    public Result insertTaskCenter(@RequestBody TaskCenter taskCenter) {
        taskCenter.setCreateTime(DateUtil.now());
        taskCenterService.save(taskCenter);
        return Result.success();
    }

    @GetMapping("/taskCenter/{id}")
    @ApiOperation("通过Id查询详情")
    public Result selectOne(@PathVariable Integer id) {
        return Result.success().put("data", taskCenterService.getById(id));
    }

    @PostMapping("/taskCenter/updateTaskCenter")
    @ApiOperation("修改任务中心")
    public Result updateTaskCenter(@RequestBody TaskCenter taskCenter) {
        taskCenter.setUpdateTime(DateUtil.now());
        taskCenterService.updateById(taskCenter);
        return Result.success();
    }

    @PostMapping("/taskCenter/deleteTaskCenter")
    @ApiOperation("删除任务中心")
    public Result deleteTaskCenter(Long id) {
        taskCenterService.removeById(id);
        return Result.success();
    }

    @GetMapping("/taskCenter/selectTaskCenter")
    @ApiOperation("查询任务中心")
    public Result selectTaskCenter(Integer page, Integer limit) {
        return Result.success().put("data", taskCenterService.page(new Page<>(page, limit), new QueryWrapper<TaskCenter>().orderByDesc("id")));
    }


    @Login
    @GetMapping("/app/taskCenter/selectTaskCenter")
    @ApiOperation("App查询任务中心")
    public Result selectAppTaskCenter(Integer page, Integer limit, @ApiIgnore @RequestAttribute("userId") Long userId) {
        return taskCenterService.queryTaskCenter(page, limit, userId);
    }

    @Login
    @GetMapping("/app/taskCenter/taskReceive")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "任务id", dataTypeClass = Long.class),
    })
    @ApiOperation("App 任务中心 领取")
    public Result taskReceive(@ApiIgnore @RequestAttribute("userId") Long userId,Long id) {
        return taskCenterService.taskReceive(userId,id);
    }
}

