package com.pqs9200.controller.advanced;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.advanced.*;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.advanced.RelevanceService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UtilPackageData;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.DataInputStream;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping(value = "advanced")
public class RelevanceControl {
    @Autowired
    private RelevanceService relevanceService;
    @Autowired
    private UserLogDetailService userLog;
    private static final Logger logger = LoggerFactory.getLogger(RelevanceControl.class);

    @PostMapping(value = "querySagEvents")
    @ResponseBody
    //归一化暂降事件展示列表
    public EntityResultData<EntityRelevance> querySagEvents(String queryCondition,String startTime, String endTime,HttpServletRequest request) {
        try {
            userLog.getLog("查询暂降事件", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return UtilPackageData.success(relevanceService.querySagEvents(queryCondition,startTime,endTime));
        } catch (Exception e) {
            userLog.getLog("查询暂降事件", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            logger.error(e.toString(), RelevanceControl.class);
            return UtilPackageData.abnormal(EnumCode.ABNORMAL.getCode(), EnumCode.ABNORMAL.getDescription(), null);
        }
    }

    @PostMapping(value = "querySagEventss")
    @ResponseBody
    //根据监测点id获取暂降事件展示列表
    public EntityResultData<EntityRelevance> querySagEventss(String queryCondition, HttpServletRequest request) {
        try {
            userLog.getLog("查询暂降事件", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return UtilPackageData.success(relevanceService.querySagEventss(queryCondition));
        } catch (Exception e) {
            userLog.getLog("查询暂降事件", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            logger.error(e.toString(), RelevanceControl.class);
            return UtilPackageData.abnormal(EnumCode.ABNORMAL.getCode(), EnumCode.ABNORMAL.getDescription(), null);
        }
    }

    @PostMapping(value = "processEvents")
    @ResponseBody
    public int processEvents(String arrayList, HttpServletRequest request) {
        if (arrayList == null) {
            try {
                DataInputStream in = new DataInputStream(request.getInputStream());
                int totalbytes = request.getContentLength();
                byte[] dataOrigin = new byte[totalbytes];
                in.readFully(dataOrigin);
                in.close();
                in = null;
                arrayList = URLDecoder.decode(new String(dataOrigin), "UTF-8");
                arrayList = arrayList.substring(10);// 截取arrayList=
                dataOrigin = null;
            } catch (Exception e) {
                logger.error(e.toString(), EnumCode.PARAMETERABNORMAL.getDescription());
            }
        }

        try {
            relevanceService.processEvents(arrayList);
            userLog.getLog("事件关联分析", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            userLog.getLog("事件关联分析", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            logger.error(e.toString(), EnumCode.ABNORMAL.getDescription());
            return 1;
        }

        return 0;
    }

    @PostMapping(value = "queryNormalizeData")
    @ResponseBody
    //展示归一化结果
    public EntityResultData<List<EventAssObj>> queryNormalizeData() {
        try {
            return UtilPackageData.success(relevanceService.queryNormalizeData());
        } catch (Exception e) {
            logger.error(e.toString());
            return UtilPackageData.abnormal(EnumCode.ABNORMAL.getCode(), EnumCode.ABNORMAL.getDescription(), null);
        }
    }

    @PostMapping(value = "queryEvents")
    @ResponseBody
    public EntityResultData<List<EntityPqsRelevance>> queryEvents() {
        try {
            return UtilPackageData.success(relevanceService.queryEvents());
        } catch (Exception e) {
            logger.error(e.toString());
            return UtilPackageData.abnormal(EnumCode.ABNORMAL.getCode(), EnumCode.ABNORMAL.getDescription(), null);
        }
    }

    //归一化算法删除
    @PostMapping(value = "deleteEvent")
    @ResponseBody
    public HttpResult deleteEvent(String date, HttpServletRequest request) {
        HttpResult httpResult = null;
        try {
            relevanceService.deleteEvent(date);
            userLog.getLog("回退归一化操作", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            httpResult = PubUtils.assignmentResult(null,200,"删除成功");
        } catch (Exception e) {
            userLog.getLog("回退归一化操作", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            logger.error(e.toString());
            httpResult = PubUtils.assignmentResult(null,500,"删除失败");
        }
        return httpResult;
    }
}
