package com.mosty.common.task.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.task.entity.SysTask;
import com.mosty.common.task.entity.vo.*;
import com.mosty.common.task.service.SysTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@SuppressWarnings("unused")
@RestController
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/sysTask")
@Api(tags = "任务接口")
public class SysTaskController {
    @Resource
    private SysTaskService sysTaskService;

    /**
     * 添加任务并下发
     * @param addSysTaskVO 添加任务并下发
     * @return 添加结果
     */
    @PostMapping("addSysTask")
    @ApiOperation(value = "添加任务并下发", httpMethod = "POST", response = Boolean.class)
    @Log(title = "添加任务并下发",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> addSysTask(@RequestBody @Valid AddSysTaskVO addSysTaskVO, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysTaskService.saveInfo(addSysTaskVO,ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 查看回调
     * @return 添加结果
     */
    @PostMapping("detail")
    @ApiOperation(value = "查看回调", httpMethod = "POST", response = SysTask.class)
    @Log(title = "查看回调",businessType = BusinessType.UPDATE)
    public ResponseResult<SysTask> detail(@RequestBody  DetailSysTaskVO detailSysTaskVO, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        SysTask task= sysTaskService.detail(detailSysTaskVO,ipAddress);
        return ResponseResult.success(task);
    }

    /**
     * 完成回调
     * @return 添加结果
     */
    @PostMapping("finish")
    @ApiOperation(value = "完成回调", httpMethod = "POST", response = Boolean.class)
    @Log(title = "完成回调",businessType = BusinessType.UPDATE)
    public ResponseResult<Boolean> finish(@RequestBody  FinishSysTaskVO finishSysTaskVO, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysTaskService.finish(finishSysTaskVO,ipAddress);
        return ResponseResult.success(true);
    }

    /**
     * 查询当前登陆用户的任务
     * @return 当前登陆用户的任务
     */
    @PostMapping("findByList")
    @ApiOperation(value = "查询当前登陆用户的任务", httpMethod = "POST", response = SysTaskItemVO.class)
    public ResponseResult<IPage<SysTaskItemVO>> findByList(@RequestBody QueryTaskVO queryTaskVO, HttpServletRequest servletRequest) {
        IPage<SysTaskItemVO>  page = sysTaskService.findByList(queryTaskVO);
        return ResponseResult.success(page);
    }



    /**
     * 查询当前登陆用户的任务日志
     * @return 当前登陆用户的任务
     */
    @PostMapping("findLogByList")
    @ApiOperation(value = "查询当前登陆用户的任务日志", httpMethod = "POST", response = SysTaskLogVO.class)
    public ResponseResult<List<SysTaskLogVO>> findLogByList(@RequestBody QueryTaskLogVO queryTaskLogVO, HttpServletRequest servletRequest) {
        List<SysTaskLogVO>  page = sysTaskService.findLogByList(queryTaskLogVO);
        return ResponseResult.success(page);
    }
    /**
     * 查询任务数量
     * @return 查询任务数量
     */
    @PostMapping("countByQuery")
    @ApiOperation(value = "查询任务数量", httpMethod = "POST", response = Long.class)
    public ResponseResult<Long> countByQuery(@RequestBody QueryTaskVO queryTaskVO, HttpServletRequest servletRequest) {
        Long count = sysTaskService.countByQuery(queryTaskVO);
        return ResponseResult.success(count);
    }




}
