package com.pqs9900.controller.business;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.data.EventDetail;
import com.njcn.pojo.data.VdListQueryVO;
import com.njcn.service.data.EventDetailService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
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 java.math.BigDecimal;
import java.util.List;


/**
 * 区域业务的controller
 */
@Controller
@RequestMapping("/area")
public class AreaController {

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

	@Autowired
	private EventDetailService eventDetailService;

	@Autowired
	private UserLogDetailService userLog;


	/**
	*暂降列表查询
	 */
	@RequestMapping(value = "getVoltageDetail", method = RequestMethod.POST)
	@ResponseBody
	public HttpResult getVoltageDetail(@RequestBody VdListQueryVO vdListQueryVO, HttpServletRequest request) {
		HttpResult result;
		DatePojo datePojo;
		datePojo = PubUtils.validateDate(vdListQueryVO.getStartTime(), vdListQueryVO.getEndTime(), "getVoltageDetail");
		if (!datePojo.getValidity()) {
			result = PubUtils.assignmentResult(null, 500, "解析前台传递的时间有误");
			return result;
		}
		String area = vdListQueryVO.getArea();
		String type = vdListQueryVO.getType();
		BigDecimal minfuzhi = vdListQueryVO.getMinfuzhi();
		BigDecimal maxfuzhi = vdListQueryVO.getMaxfuzhi();
		BigDecimal mintime = vdListQueryVO.getMintime();
		BigDecimal maxtime = vdListQueryVO.getMaxtime();
		BigDecimal minseverity = vdListQueryVO.getMinseverity();
		BigDecimal maxseverity = vdListQueryVO.getMaxseverity();
		List eventType =vdListQueryVO.getEventType();
		List eventReason =vdListQueryVO.getEventReason();
		List triggerType =vdListQueryVO.getTriggerType();
		Integer fileflag = vdListQueryVO.getFileflag();
		// 统计时间段内的暂降原因次数、暂降类型次数
		List<EventDetail> voltageList;
		try {
			voltageList = eventDetailService.getVolList(datePojo.getStartTime(), datePojo.getEndTime(), area,type,minfuzhi,maxfuzhi,mintime,maxtime,minseverity,maxseverity,eventType,eventReason,triggerType,fileflag);
			if (null == voltageList) {
				result = PubUtils.assignmentResult(null, 500, "没有暂降列表的数据");
			} else {
				result = PubUtils.assignmentResult(voltageList, 200, "获取暂降数据成功");
			}
			userLog.getLog("查询电压暂降列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
		} catch (Exception e) {
			result = PubUtils.assignmentResult(null, 500, "获取暂降数据异常");
			userLog.getLog("查询电压暂降列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
			logger.error("后台获取数据出错，方法名为：getVoltageDetail，异常为：" + e);
		}
		return result;
	}


}
