package com.docus.server.controller;

import com.docus.core.util.ParamsUtils;
import com.docus.core.util.json.JSON;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.log.annotation.TrackGroup;
import com.docus.server.api.scheduling.management.SchCollectRecordApi;
import com.docus.server.common.SchCollectorTask;
import com.docus.server.common.process.ChannelProcessor;
import com.docus.server.dto.scheduling.management.schcollector.task.ReportDownTwoDTO;
import com.docus.server.dto.scheduling.management.schcollectrecord.AddSchCollectRecordDTO;
import com.docus.server.dto.scheduling.management.schcollectrecord.DeleteSchCollectRecordDTO;
import com.docus.server.dto.scheduling.management.schcollectrecord.EditSchCollectRecordDTO;
import com.docus.server.dto.scheduling.management.schcollectrecord.RetrySchCollectRecordDTO;
import com.docus.server.entity.scheduling.management.SchCollectRecord;
import com.docus.server.enums.RetryTaskEnum;
import com.docus.server.service.ISchCollectRecordService;
import com.docus.server.vo.scheduling.management.schcollectrecord.SchCollectRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 采集记录表 控制器类
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Api(value = "采集记录表管理接口", tags = "采集记录表管理接口")
@RestController
public class SchCollectRecordController implements SchCollectRecordApi {
    @Resource
    private ISchCollectRecordService iSchCollectRecordService;
    @Resource
    private SchCollectorTask schedulerTask;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @ApiOperation("按主键查询")
    @Override
    public SchCollectRecordVO findById(String id) {
        return iSchCollectRecordService.findById(id);
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @ApiOperation("关键字搜索")
    @Override
    public PageResult<SchCollectRecordVO> search(SearchDTO searchDTO) {
        return iSchCollectRecordService.search(searchDTO);
    }

    /**
     * 新增
     *
     * @param addSchCollectRecordDTO 编辑参数
     * @return 成功或失败
     */
    @ApiOperation("新增")
    @Override
    public SchCollectRecord add(AddSchCollectRecordDTO addSchCollectRecordDTO) {
        return iSchCollectRecordService.add(addSchCollectRecordDTO);
    }

    /**
     * 编辑
     *
     * @param editSchCollectRecordDTO 编辑参数
     * @return 成功或失败
     */
    @ApiOperation("编辑")
    @TrackGroup(group = "SchCollectRecordController-edit", processor = ChannelProcessor.class)
    @Override
    public void edit(EditSchCollectRecordDTO editSchCollectRecordDTO) {
        iSchCollectRecordService.edit(editSchCollectRecordDTO);
    }

    /**
     * 批量删除
     *
     * @param deleteSchCollectRecordDTO 删除参数
     * @return 成功或失败
     */
    @ApiOperation("批量删除")
    @Override
    public int delete(DeleteSchCollectRecordDTO deleteSchCollectRecordDTO) {
        return iSchCollectRecordService.delete(deleteSchCollectRecordDTO);
    }

    @ApiOperation("任务重试")
    @Override
    public void retryTask(RetrySchCollectRecordDTO retrySchCollectRecordDTO) {
        ReportDownTwoDTO report = JSON.fromJSON(retrySchCollectRecordDTO.getTaskOriginJson(), ReportDownTwoDTO.class);

        report.setParams(ParamsUtils
            .addParam("collectRecordId", retrySchCollectRecordDTO.getId())
            .addParam("isRetryTask", RetryTaskEnum.RETRY_TASK.getValue())
            .param());
        schedulerTask.addRetryTask(report);
    }
}
