package com.youchain.modules.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.inputJson.MoInfo;
import com.youchain.businessdata.inputJson.MoParams;
import com.youchain.businessdata.inputJson.OrderInfo;
import com.youchain.businessdata.service.MlsService;
import com.youchain.exception.handler.ApiResult;
import com.youchain.service.LogService;
import com.youchain.utils.UrlApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 获取mo定时任务
 */


@Slf4j
@Service
@RestController
@RequestMapping("/api/moTask")
public class moTask {
    @Autowired
    public MlsService mlsService;

    @Autowired
    public LogService logService;

    public void moTask() {
        long startTime = System.currentTimeMillis();
        synchronized ("mlsOrder") {
            MoInfo moInfo = mlsService.getMoInfo(new Date());
            String resultJson = mlsService.getMoJson(moInfo);
            mlsService.getMoInfo(resultJson);
            long time = System.currentTimeMillis() - startTime;
            // 保存日志
            logService.saveLogInfo(moInfo, UrlApi.publicApi, null, "获取MO票信息", time, "INFO");
        }
    }
    @Log("获取MLS Mo票数据")
    @PostMapping(value = "/mlsMo")
    @AnonymousAccess
    public ResponseEntity<Object> mlsMo(@RequestBody List<Date> dateList){
        for (Date date:dateList){
            synchronized ("mlsOrder"){
                MoInfo moInfo = mlsService.getMoInfo(date);
                String resultJson = mlsService.getMoJson(moInfo);
                mlsService.getMoInfo(resultJson);
            }
        }
        return new ResponseEntity<>(ApiResult.fail(200, "获取成功", null), HttpStatus.OK);
    }
}
