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.util.PeriodTime;
import com.docus.server.zz.service.IZzService;
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 ZzViewCollectJob extends AbstractCollectJob<Map<String, Object>> {
    @Resource
    private IZzService zzService;
    @Resource(name = "zzViewJobResultImpl")
    private IJobResult jobResult;

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

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

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

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