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.natOrder.NatOrderListRequest;
import com.ywt.mg.services.AuthService;
import com.ywt.mg.services.DownloadRecordService;
import com.ywt.mg.services.IdGenerator;
import com.ywt.mg.services.NatOrderService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController("/natOrder")
@RequestMapping({"/natOrder"})
@MGRight
public class NatOrderController {

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


    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;

    @Autowired
    private AuthService authService;

    @Autowired
    private NatOrderService natOrderService;


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

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

}
