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.EventAssObj;
import com.njcn.service.advanced.RangeService;
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.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("advanced")
public class RangeControl {
    @Autowired
    private RangeService rangeService;

    @Autowired
    private UserLogDetailService userLog;

    private static final Logger logger = LoggerFactory.getLogger(RangeControl.class);

    @PostMapping(value = "queryEventsByTime")
    public EntityResultData<List<EventAssObj>> queryEventsByTime(String startTime, String endTime, HttpServletRequest request) {
        try {
            userLog.getLog("查询影响范围分析", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return UtilPackageData.success(rangeService.queryEventsByTime(startTime, endTime));
        } catch (Exception e) {
            logger.error(e.toString(), this.getClass());
            userLog.getLog("查询影响范围分析", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return UtilPackageData.abnormal(EnumCode.ABNORMAL.getCode(), EnumCode.ABNORMAL.getDescription(), null);
        }
    }

    @PostMapping(value = "rangeAnalysis")
    public String updatebRangeByGUID(String indexEventAss, HttpServletRequest request) {
        try {
            rangeService.updatebRangeByGUID(indexEventAss);
            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(), this.getClass());
        }

        return "business/advanced/range";
    }
}
