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.params.outpatientOrder.QueryOutPatientOrderListRequest;
import com.ywt.mg.services.AuthService;
import com.ywt.mg.services.DownloadRecordService;
import com.ywt.mg.services.IdGenerator;
import com.ywt.mg.services.OutpatientOrderService;
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;


@RestController("/outPatient")
@RequestMapping({"/outPatient"})
@MGRight
public class OutpatientOrderController {

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

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;

    @Autowired
    private AuthService authService;

    @Autowired
    private OutpatientOrderService outpatientOrderService;


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

    @RequestMapping({"/downloadOutPatientOrderBillList"})
    public BaseResponse downloadOutPatientOrderBillList(@RequestBody QueryOutPatientOrderListRequest request) {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录
//        int hospital = 0;
//        if(request.getHospitalId() > 0){
//            hospital = authService.getCurrentHospitalId();
//            request.setHospitalId(hospital);
//        }

        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "门诊缴费对账数据";
        String fileName = "门诊缴费对账数据";
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "/outpatient/downloadOutPatientOrderBillList";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecord(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                outpatientOrderService.downloadOutPatientOrderBillListNew(downloadRecordId, fileName, request, currentAdminId);
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }
}
