package thyyxxk.webserver.scheduled;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.dao.his.medicalinsurance.SiManageDao;
import thyyxxk.webserver.entity.inpatient.patient.Overview;
import thyyxxk.webserver.service.medicalinsurance.SiManageService;

import java.util.List;

@Component
public class UploadFrontSheetTask {
    @Value("${execute-scheduled}")
    private Boolean executeScheduled;
    private final SiManageDao dao;
    private final SiManageService service;

    @Autowired
    public UploadFrontSheetTask(SiManageDao dao, SiManageService service) {
        this.dao = dao;
        this.service = service;
    }

    @Scheduled(cron = "0 20 2 * * ?")
    public void uploadStatistics() {
        if (executeScheduled) {
            startUploadStatistics();
        }
    }

    private void startUploadStatistics() {
        List<Overview> list = dao.selectNotUploadedSheet();
        for (Overview item : list) {
            service.uploadFrontSheet(item.getInpatientNo(), item.getAdmissTimes());
        }
    }
}
