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.domain.models.pojo.ConstantRightDisplayDef;
import com.ywt.mg.params.checkupBooking.QueryCheckUpOrderListRequest;
import com.ywt.mg.services.AuthService;
import com.ywt.mg.services.CommonWxPushService;
import com.ywt.mg.services.DownloadRecordService;
import com.ywt.mg.services.IdGenerator;
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("/hosp/commonWxPush")
@RequestMapping({"/hosp/commonWxPush"})
@MGRight
public class HospCommonWxPushController {

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

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private DownloadRecordService downloadRecordService;

    @Autowired
    private AuthService authService;

    @Autowired
    private CommonWxPushService commonWxPushService;

    @RequestMapping({"/downloadHisPatientId"})
    public BaseResponse downloadHisPatientId() {
        int currentAdminId = Checker.getIntegerValue(authService.getCurrentAdminId());
        String downloadHisPatientidRightCode = ConstantRightDisplayDef.COMMON_WX_PUSH_DISPLAY_DOWNLOAD_HISPATIENTID;
        boolean downloadHisPatientidRight = authService.checkCacheRight(downloadHisPatientidRightCode);
        if (!downloadHisPatientidRight) {
            logger.error("HospCommonWxPushController#downloadHisPatientId>>没有权限，请先设置权限，userId:{}", authService.getCurrentAdmin());
        }
//        request.setCurrentAdminId(currentAdminId);
        // 插入记录

        String hosp = "";
//        if(request.getHosp()> 0){
        Integer hospital = authService.getCurrentHospitalId();
        hosp = hospital.toString();

//        }
        int downloadRecordId = idGenerator.genDownloadRecordId();
        String name = "下载患者ID";
        String fileName = "下载患者ID";
        String excelSuffixFormat = ConstantDef.EXCEL_SUFFIX_FORMAT;
        String paramUrl = "/hosp/commonWxPush/downloadHisPatientId";
        String paramJson = JsonSerializer.toJson(hospital);
        downloadRecordService.getOrInsertDownloadRecordByHosp(downloadRecordId, name, fileName + excelSuffixFormat, paramUrl, paramJson);
        Thread t = new Thread() {
            @Override
            public void run() {
             commonWxPushService.downloadHisPatientId(downloadRecordId, fileName, hospital);
            }
        };
        t.start();
        return new BaseResponse().succeed("后台下载中...");
    }
}
