package com.ywt.mg.web.controllers;


import com.ywt.mg.core.MGRight;
import com.ywt.mg.core.utils.Checker;
import com.ywt.mg.core.utils.serializers.JsonSerializer;
import com.ywt.mg.domain.models.ConstantDef;
import com.ywt.mg.domain.models.enums.PaymentStatusEnum;
import com.ywt.mg.params.offline.DownloadOfflineListNewRequest;
import com.ywt.mg.services.*;
import com.ywt.mg.web.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 线下就诊相关方法类(公司后台查询所有的，通过hospital_id来区分，公司后台hospital_id = -1)
 */
@RestController("/offline")
@RequestMapping({"/offline"})
@MGRight
public class OfflineController {

    private static Logger logger = LoggerFactory.getLogger(OfflineController.class);

    @Autowired
    private AuthService authService;

    @Autowired
    private OfflineNewService offlineNewService;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;

    @RequestMapping({"/downloadOfflineList"})
    public BaseResponse downloadOfflineList(@RequestBody DownloadOfflineListNewRequest request) {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
        request.setCurrentAdminId(currentAdminId);
        // 插入记录
        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "线下就诊";
        String fileName = "线下就诊";
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "/offline/downloadOfflineList";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecord(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                offlineNewService.downloadNewOfflineListCommon(downloadRecordId, fileName, request);
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }


}
