package com.ywt.mg.web.controllers.hospital;

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.params.prescription.QueryPrescriptionListRequest;
import com.ywt.mg.services.AuthService;
import com.ywt.mg.services.DownloadRecordService;
import com.ywt.mg.services.IdGenerator;
import com.ywt.mg.services.PrescriptionServices;
import com.ywt.mg.web.BaseResponse;
import com.ywt.mg.web.controllers.PrescriptionController;
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("/hosp/adverPrescription")
@RequestMapping({"/hosp/adverPrescription"})
@MGRight
public class HospAdverPrescriptionController {
    private static Logger logger = LoggerFactory.getLogger(HospAdverPrescriptionController.class);

    @Autowired
    private AuthService authService;

    @Autowired
    private PrescriptionServices prescriptionServices;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;




    @RequestMapping({"/downloadAdverPrescriptionlist"})
    public BaseResponse downloadAdverPrescriptionlist(@RequestBody QueryPrescriptionListRequest request) {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录
        int hospital = authService.getCurrentHospitalId();
        request.setHospitalId(hospital);
//        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 = "/hosp/adverPrescription/downloadAdverPrescriptionlist";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecordByHosp(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                prescriptionServices.downloadAdverPrescriptionlist(downloadRecordId, fileName, request);
//
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }


    @RequestMapping({"/downloadAdverPrescriptionDrugList"})
    public BaseResponse downloadPrescriptionDrugList(@RequestBody QueryPrescriptionListRequest request) {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录
        int hospital = authService.getCurrentHospitalId();
        request.setHospitalId(hospital);
        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "处方外转(药品)";
        String fileName = "处方外转(药品)";
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "hosp/adverPrescription/downloadAdverPrescriptionDrugList";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecordByHosp(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                prescriptionServices.downloadAdverPrescriptionDrugList(downloadRecordId, fileName, request);
//
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }
}
