package com.docus.server.collect.report.view;

import com.docus.log.annotation.TrackRetryListener;
import com.docus.server.collect.ICollector;
import com.docus.server.collect.IJob;
import com.docus.server.collect.IJobResult;
import com.docus.server.collect.web.job.AbstractCollectJob;
import com.docus.server.constants.DSKeyConstants;
import com.docus.server.ecg.service.IEcgService;
import com.docus.server.entity.TaskConfig;
import com.docus.server.util.PeriodTime;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 电生理系统数据
 *
 * @author linruifeng
 * @see AbstractCollectJob
 * @see IJob
 * @see ICollector
 */
@Component
public class EcgViewCollectJob extends AbstractCollectJob<Map<String, Object>> {
    @Resource
    private IEcgService ecgService;
    @Resource(name = "ecgViewJobResultImpl")
    private IJobResult jobResult;

    /**
     * xxl jobhandler 手工执行，全量执行
     */
    @XxlJob("startAllEcgViewCollect")
    @TrackRetryListener("startAllEcgViewCollect")
    public void startAllEcgViewCollect(String param) {
        super.startCollectAll(getJobParam(param));
    }

    /**
     * xxl jobhandler 配置定时启动，如果未完成任务跳过。增量收集
     */
    @XxlJob("startIncEcgViewCollect")
    @TrackRetryListener("startIncEcgViewCollect")
    public void startIncEcgViewCollect(String param) {
        super.startCollectIncrement(getJobParam(param));
    }

    @SuppressWarnings("unchecked")
    @Override
    public void doHandle(List<Map<String, Object>> results, String param) {
        jobResult.handle(results, DSKeyConstants.ECG_DS_KEY);
    }

    @Override
    public List<Map<String, Object>> execute(PeriodTime periodTime, TaskConfig taskConfig) {
        //每家医院按照固定的格式写sql，不同医院需要替换不同的sql即可。
        return ecgService.page(periodTime.getPeriodStartDate(), periodTime.getPeriodEndDate(),
            taskConfig.getPageNum(), taskConfig.getPageSize());
    }
}
