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.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.pqs9000.pojo.common.PowerQualityData;
import com.pqs9000.service.data.AssesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * @author hongawen
 * @date 2018/6/21 22:41
 */
@Controller
@RequestMapping("area")
public class PowerQualityController {

    @Autowired
    private AssesService assesService;


    @Autowired
    UserLogDetailService userLog;


    /**
     * 跳转区域统计的电能质量
     */
    @GetMapping("quality")
    public String quality(){
        return "business/area/powerQuality";
    }

    /**
     * 区域统计--》电能质量指标
     * @param startTime 起始时间
     * @param endTime 截止时间
     */
    @PostMapping("powerQualityVol")
    @ResponseBody
    public HttpResult getPowerQualityVol(String startTime, String endTime, String area, HttpServletRequest request){
        HttpResult result=PubUtils.initResult(TokenManager.getToken().getLoginName(),request,LogTypeEnum.BUSSINESS.toString(),0);
        DatePojo datePojo;
        datePojo=PubUtils.validateDate(startTime,endTime,"getPowerQualityVol");
        if(!StringUtils.isEmpty(datePojo.getMsg())){
            result=PubUtils.assignmentResult(null,500,datePojo.getMsg());
            return result;
        }
        PowerQualityData data;
        try{
            data=assesService.getPowerQualityVolArea(datePojo.getStartTime(),datePojo.getEndTime(),area);
            if(null==data){
                result=PubUtils.assignmentResultLog(null,500,"区域统计获取电压偏差数据为空","查询电能质量评估","成功",result);
            }else{
                 result=PubUtils.assignmentResultLog(data,200,"区域统计获取电压偏差数据成功","查询电能质量评估","成功",result);
            }
            userLog.getLog("查询电能质量指标", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        }catch (Exception e){
            userLog.getLog("查询电能质量指标", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result=PubUtils.assignmentResultLog(null,200,"区域统计获取电压偏差数据失败","查询电能质量评估","失败",result);
        }
        return result;
    }

}
