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.entity.TaskConfig;
import com.docus.server.pacs.service.IPacsService;
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;

/**
 * Pacs系统数据视图
 *
 * @author linruifeng
 * @see AbstractCollectJob
 * @see IJob
 * @see ICollector
 */
@Component
public class PacsViewCollectJob extends AbstractCollectJob<Map<String, Object>> {
    @Resource
    private IPacsService pacsService;
    @Resource(name = "pacsViewJobResultImpl")
    private IJobResult jobResult;

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

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

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

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