package com.inspect.analysis.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspect.analysis.service.IResultAnalysisService;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@RequestMapping({"/algorithmUpdateResult"})
public class AlgorithmController extends BaseController {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    private final IResultAnalysisService resultAnalysisService;

    public AlgorithmController(IResultAnalysisService resultAnalysisService) {
        this.resultAnalysisService = resultAnalysisService;
    }

    @PostMapping
    public AjaxResult add(@RequestBody final String alarmDefect) {
        log.info("alarmDefect: {}", alarmDefect);

        try {
            JSONObject.parseObject(alarmDefect);
        } catch (Exception e) {
            return AjaxResult.error(400, "格式错误");
        }

        (new Thread(() -> {
            log.info("add task start");
            try {
                Thread.sleep(2000L);
                AlgorithmController.this.analysis(alarmDefect);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            log.info("add task finish");
        })).start();
        return AjaxResult.success();
    }

    public void analysis(String str) {
        JSONObject parseObject = JSONObject.parseObject(str);
        String requestId = parseObject.get("requestId").toString();
        String result = parseObject.get("result").toString();
        log.info("requestId: {}, result: {}", requestId, result);
    }

    public static void main(String[] args) {
        String text = "tx_yzwpy";
        int hashCode = text.hashCode();
        System.out.println("hashCode: " + hashCode);
    }
}
