package com.pqs9200.controller.advanced;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.advanced.EntityResultData;
import com.njcn.pojo.advanced.EnumCode;
import com.njcn.pojo.advanced.SagEvent;
import com.njcn.service.advanced.WaveformService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.serviceimpl.log.UserLogDetailServiceImpl;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "advanced")
public class WaveformControl {
    @Autowired
    private WaveformService waveformService;
    @Autowired
    private UserLogDetailService userLog;
    private static final Logger logger = LoggerFactory.getLogger(WaveformControl.class);

    @PostMapping(value = "queryEventsByGUID")
    public EntityResultData<List<SagEvent>> queryEventsByGUID(String queryCondition) {
        try {
            return UtilPackageData.success(waveformService.queryEventsByGUID(queryCondition));
        } catch (Exception e) {
            logger.error(e.toString());
            return UtilPackageData.abnormal(EnumCode.ABNORMAL.getCode(), EnumCode.ABNORMAL.getDescription(), null);
        }
    }

    @PostMapping(value = "addNormalization")
    public String addNormalization(String condition, HttpServletRequest request) {
        try {
            waveformService.addNormalization(condition);
            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 "business/advanced/addwavecompare";
    }

    @PostMapping(value = "deleteWaveform")
    public String deleteWaveform(String condition, HttpServletRequest request) {
        try {
            waveformService.deleteWaveform(condition);
            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 "business/advanced/deletewaveform";
    }
}
