package com.pqs9000.controller.area;

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.configuration.DeviceService;
import com.njcn.service.data.EventDetailService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author hongawen
 * @Date: 2018/7/2 10:18
 */
@Controller
@RequestMapping("area")
public class VdListController {
    private static final Logger logger= LoggerFactory.getLogger(VdListController.class);

    @Autowired
    private EventDetailService eventDetailService;

    @Autowired
    UserLogDetailService userLog;

    /**
     * 跳转区域统计的暂降事件列表
     */
    @GetMapping("vdlist")
    public String status(){
        return "business/area/vdList";
    }

    /**
     * 电压暂降列表，获取列表数据
     *
     */
    @PostMapping( "getVoltageDetail")
    @ResponseBody
    public HttpResult getVoltageDetail(@RequestBody VdListQueryVO vdListQueryVO, HttpServletRequest request) {
        HttpResult result=PubUtils.initResult(TokenManager.getToken().getLoginName(),request,LogTypeEnum.BUSSINESS.toString(),0);
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(vdListQueryVO.getStartTime(), vdListQueryVO.getEndTime(), "getVoltageDetail");
        if(!StringUtils.isEmpty(datePojo.getMsg())){
            result=PubUtils.assignmentResult(null,500,datePojo.getMsg());
            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.assignmentResultLog(null, 500, "暂降列表的数据为空","查询暂降事件列表","成功",result);
            } else {
                result = PubUtils.assignmentResultLog(voltageList, 200, "获取暂降数据成功","查询暂降事件列表","成功",result);
            }
            userLog.getLog("查询暂降事件列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("后台获取数据出错，方法名为：getVoltageDetail，异常为：" + e);
            userLog.getLog("查询暂降事件列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResultLog(null, 500, "获取暂降数据失败","查询暂降事件列表","失败",result);
        }
        return result;
    }


}
