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

import com.docus.log.annotation.TrackRetryListener;
import com.docus.server.collect.ICollector;
import com.docus.server.collect.web.job.AbstractCollectJob;
import com.docus.server.entity.TaskConfig;
import com.docus.server.his.service.IHisService;
import com.docus.server.record.common.pojo.dto.TBasicDTO;
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;

/**
 * 主动view请求基础数据job
 *
 * @see AbstractCollectJob
 * @see com.docus.server.collect.IJob
 * @see ICollector
 */
@Component
public class TBasicViewCollectJob extends AbstractCollectJob<TBasicDTO> {
    @Resource
    private IHisService hisService;

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

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

    @Override
    public void doHandle(List<TBasicDTO> results, String param) {
        basicService.batchSaveBasics(results);
    }

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

}
