package com.pqs9200.controller.business;

import cn.hutool.json.JSONArray;
import com.njcn.pojo.advanced.MessageObj;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.njcn.utils.WebSocketUtil;
import com.pqs9200.service.data.IEventPzService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年02月17日 15:06
 */
@Controller
@RequestMapping(value = "event")
public class EventController {

    /*** 日志记录 */
    private static final Logger logger = LoggerFactory.getLogger(EventController.class);

    // 建立socket对象，推送数据
    private WebSocketUtil webSocketUtil = new WebSocketUtil();

    @Autowired
    private IEventPzService iEventPzService;

    /**
     * 需要展示的新事件列表
     *
     * @param eventInfo 暂降事件数据
     */
    @GetMapping("/tips")
    public void showEvents(String eventInfo) {
        //System.out.println(eventInfo);
        JSONObject jsonObject = JSONObject.fromObject(eventInfo);

        //语言测试代码
       /* JSONObject jsonObject = JSONObject.fromObject("{\"eventdetail_index\":\"d4963f8f-201f-443f-b293-dda8ef870ee6\",\"lineid\":\"71\",\"timeid\":\"2022-07-15 15:00:14\",\"ms\":\"509\",\"describe\":\"\",\"wavetype\":\"1\",\"persisttime\":\"23.000\",\"eventvalue\":\"0.35\",\"eventreason\":\"未明\",\"eventtype\":\"其他\",\"gdname\":\"上海集成电路\",\"bdname\":\"220kV总降变\",\"pointname\":\"测试监测点\"}");

        if(eventInfo.equals("1")){
            jsonObject = JSONObject.fromObject("{\"eventdetail_index\":\"d4963f8f-201f-443f-b293-dda8ef870ee6\",\"lineid\":\"71\",\"timeid\":\"2022-07-15 15:00:14\",\"ms\":\"509\",\"describe\":\"\",\"wavetype\":\"1\",\"persisttime\":\"23.000\",\"eventvalue\":\"0.35\",\"eventreason\":\"未明\",\"eventtype\":\"其他\",\"gdname\":\"上海集成电路\",\"bdname\":\"220kV总降变\",\"pointname\":\"测试监测点\"}");

        }else if(eventInfo.equals("2")){
            jsonObject = JSONObject.fromObject("{\"eventdetail_index\":\"d4963f8f-201f-443f-b293-dda8ef870ee6\",\"lineid\":\"71\",\"timeid\":\"2022-07-15 15:00:14\",\"ms\":\"509\",\"describe\":\"\",\"wavetype\":\"1\",\"persisttime\":\"23.000\",\"eventvalue\":\"0.35\",\"eventreason\":\"未明\",\"eventtype\":\"其他\",\"gdname\":\"上海集成电路\",\"bdname\":\"南京总降变\",\"pointname\":\"南京监测点\"}");

        }else{
            jsonObject = JSONObject.fromObject("{\"eventdetail_index\":\"d4963f8f-201f-443f-b293-dda8ef870ee6\",\"lineid\":\"71\",\"timeid\":\"2022-07-15 15:00:14\",\"ms\":\"509\",\"describe\":\"\",\"wavetype\":\"1\",\"persisttime\":\"23.000\",\"eventvalue\":\"0.35\",\"eventreason\":\"未明\",\"eventtype\":\"其他\",\"gdname\":\"上海集成电路\",\"bdname\":\"江苏总降变\",\"pointname\":\"江苏监测点\"}");

        }*/
        jsonObject.replace("eventvalue", PubUtils.doubleRound(2,jsonObject.getDouble("eventvalue") * 100));
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        MessageObj messageObj = new MessageObj();
        messageObj.setMsgId(messageObj.getRelevance() + 1);
        messageObj.setMsgBody("insert");
        messageObj.setMsgbody(jsonArray);
        try {
            webSocketUtil.sendAll(messageObj);
        } catch (IOException | EncodeException e1) {
            logger.error(e1.getMessage());
        }

    }



    /**
     * 暂降消息处理
     * @author cdf
     * @date 2022/7/15
     */
    @PostMapping("dealEventWarn")
    @ResponseBody
    public HttpResult dealEventWarn(@RequestBody List<String> eventIds){
       boolean res = iEventPzService.dealEventWarn(eventIds);
       if(res){
           return PubUtils.assignmentResult(null,200,"成功");
       }
        return PubUtils.assignmentResult(null,500,"失败");
    }


}
