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.DeliveryMethodEnum;
import com.ywt.mg.params.natOrder.NatOrderListRequest;
import com.ywt.mg.params.prescription.QueryPrescriptionListRequest;
import com.ywt.mg.services.*;
import com.ywt.mg.web.BaseResponse;
import com.ywt.mg.web.controllers.hospital.HospOfflineController;
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;

@RestController("/prescription")
@RequestMapping({"/prescription"})
@MGRight
public class PrescriptionController {

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

    @Autowired
    private AuthService authService;

    @Autowired
    private PrescriptionServices prescriptionServices;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;


    @RequestMapping({"/downloadPrescriptionlist"})
    public BaseResponse downloadNatOrderList(@RequestBody QueryPrescriptionListRequest request) {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录
        int  hospital = authService.getCurrentHospitalId();
//        request.setHospitalId(hospital);
//        if(request.getHosp() > 0){
//            hospital = authService.getCurrentHospitalId();
//            request.setHospitalId(hospital);
//        }
        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "处方列表";
        String fileName = "处方列表";
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "/prescription/downloadPrescriptionlist";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecord(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                prescriptionServices.downloadPrescriptionlist(downloadRecordId, fileName, request);
//
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }


    @RequestMapping({"/downloadPrescriptionDrugList"})
    public BaseResponse downloadPrescriptionDrugList(@RequestBody QueryPrescriptionListRequest 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 = "/prescription/downloadPrescriptionDrugList";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecord(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                prescriptionServices.downloadPrescriptionDrugList(downloadRecordId, fileName, request);
//
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }

    @RequestMapping({"/downloadPrescriptionBillList"})
    public BaseResponse downloadPrescriptionBillList(@RequestBody QueryPrescriptionListRequest request) {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录
        int  hospital = authService.getCurrentHospitalId();
//        request.setHospitalId(hospital);
//        if(request.getHosp() > 0){
//            hospital = authService.getCurrentHospitalId();
//            request.setHospitalId(hospital);
//        }
        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "处方列表对账数据";
        String fileName = "处方列表对账数据";
        if (!Checker.isNone(request.getDeliveryMethod()) && Integer.parseInt(request.getDeliveryMethod()) == DeliveryMethodEnum.HomeDelivery.getValue()) {
            name = "处方运费订单对账数据";
            fileName = "处方运费订单对账数据";
        }
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "/prescription/downloadPrescriptionBillList";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecord(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        String finalFileName = fileName;
        Thread t = new Thread() {
            @Override
            public void run() {
                prescriptionServices.downloadPrescriptionBillList(downloadRecordId, finalFileName, request);
//
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }


}
