package com.ywt.mg.web.controllers;

import com.ywt.mg.core.MGRight;
import com.ywt.mg.core.utils.serializers.JsonSerializer;
import com.ywt.mg.domain.models.ConstantDef;
import com.ywt.mg.params.registeredOrder.RegisteredOrderListRequest;
import com.ywt.mg.services.AuthService;
import com.ywt.mg.services.DownloadRecordService;
import com.ywt.mg.services.IdGenerator;
import com.ywt.mg.services.RegisteredOrderService;
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("/registeredOrder")
@RequestMapping({"/registeredOrder"})
@MGRight
public class RegisteredOrderController {

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

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;


    @Autowired
    private RegisteredOrderService registeredOrderService;

    @Autowired
    private AuthService authService;


    @RequestMapping({"/downloadRegisteredOrderList"})
    public BaseResponse downloadDepositList(@RequestBody RegisteredOrderListRequest request) {
//        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录
//        int hospital = 0;
//        if(request.getHospitalId() > 0){
//            hospital = authService.getCurrentHospitalId();
//        }
//        request.setHospitalId(hospital);
        request.setHosp(0);
        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "预约挂号";
        String fileName = "预约挂号";
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "/registeredOrder/downloadRegisteredOrderList";
        String paramJson = JsonSerializer.toJson(request);
        downloadRecordService.getOrInsertDownloadRecord(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
                registeredOrderService.downloadRegisteredOrderList(downloadRecordId, fileName, request);
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }

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