package com.xunmei.core.retrieval.controller;

import com.xunmei.common.core.web.controller.BaseController;
import com.xunmei.common.core.web.domain.AjaxResult;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.common.log.annotation.Log;
import com.xunmei.common.log.enums.BusinessType;
import com.xunmei.common.security.annotation.RequiresPermissions;
import com.xunmei.core.access.dto.CoreMonitoringTaskRegistrationDTO;
import com.xunmei.common.core.domain.retrieval.domain.CoreMonitoringRetrievalTask;
import com.xunmei.common.core.domain.retrieval.dto.RetrievalTaskPageDto;
import com.xunmei.core.retrieval.service.ICoreMonitoringRetrievalTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 监控调阅任务Controller
 *
 * @author xunmei
 * @date 2023-08-25
 */
@Api(tags = {"CoreMonitoringRetrievalTask"})
@RestController
@RequestMapping("/task")
public class CoreMonitoringRetrievalTaskController extends BaseController {
    @Autowired
    private ICoreMonitoringRetrievalTaskService coreMonitoringRetrievalTaskService;

    /**
     * 查询监控调阅任务列表
     */
    @ApiOperation(value = "查询CoreMonitoringRetrievalTask列表")
    @GetMapping("/list")
    public TableDataInfo list(RetrievalTaskPageDto request) {

        return coreMonitoringRetrievalTaskService.selectPage(request);
    }

    /**
     * 查询监控调阅任务列表app
     */
    @ApiOperation(value = "app查询调阅任务列表")
    @GetMapping("/selectListApp")
    public AjaxResult selectListApp(CoreMonitoringTaskRegistrationDTO coreMonitoringTaskRegistrationDTO) {
        return success(coreMonitoringRetrievalTaskService.selectListApp(coreMonitoringTaskRegistrationDTO));
    }

    /**
     * 导出excel
     */
    @ApiOperation(value = "导出监控调阅任务")
    @RequiresPermissions("core:task:export")
    @Log(title = "导出监控调阅任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RetrievalTaskPageDto request, HttpServletResponse response) throws IOException {
        coreMonitoringRetrievalTaskService.export(request, response);
    }


    /**
     * 获取监控调阅任务详细信息
     */
    @ApiOperation(value = "获取CoreMonitoringRetrievalTask详细信息")
    @RequiresPermissions("core:task:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(coreMonitoringRetrievalTaskService.selectCoreMonitoringRetrievalTaskById(id));
    }

    /**
     * 新增监控调阅任务
     */
    @ApiOperation(value = "新增CoreMonitoringRetrievalTask")
    @RequiresPermissions("core:task:add")
    @Log(title = "监控调阅任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CoreMonitoringRetrievalTask CoreMonitoringRetrievalTask) {
        return toAjax(coreMonitoringRetrievalTaskService.insertCoreMonitoringRetrievalTask(CoreMonitoringRetrievalTask));
    }

    /**
     * 修改监控调阅任务
     */
    @ApiOperation(value = "修改CoreMonitoringRetrievalTask")
    @RequiresPermissions("core:task:edit")
    @Log(title = "监控调阅任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CoreMonitoringRetrievalTask CoreMonitoringRetrievalTask) {
        return toAjax(coreMonitoringRetrievalTaskService.updateCoreMonitoringRetrievalTask(CoreMonitoringRetrievalTask));
    }

    /**
     * 删除监控调阅任务
     */
    @ApiOperation(value = "删除CoreMonitoringRetrievalTask")
    @RequiresPermissions("core:task:remove")
    @Log(title = "监控调阅任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(coreMonitoringRetrievalTaskService.deleteCoreMonitoringRetrievalTaskByIds(ids));
    }

    /**
     * 每天生成日任务
     * 如果当天是周一还需生成周任务
     * @return
     */
    @GetMapping("/dayTask/{taskTime}")
    public AjaxResult dayTask(@PathVariable("taskTime") LocalDate taskTime) {
        try {
            coreMonitoringRetrievalTaskService.dayTask(taskTime);
            return AjaxResult.success("任务生成成功");
        } catch (Exception e) {
            return AjaxResult.error("任务生成失败" + e.getMessage());
        }

    }

    /**
     * 1.1生成周期为每月，每季，每半年，每年的任务
     * 4.1||10.1日生成周期为每月，每季
     * 7.1日获取执行周期为每月，每季,每半年的
     * 其他日期生成月任务
     * @return
     */
    @GetMapping("/monthTask/{taskTime}")
    public AjaxResult monthTask(@PathVariable("taskTime") LocalDate taskTime) {
        try {
            coreMonitoringRetrievalTaskService.monthTask(taskTime);
            return AjaxResult.success("任务生成成功");
        } catch (Exception e) {
            return AjaxResult.error("任务生成失败" + e.getMessage());
        }
    }

}
