package com.pqs9900.controller.linedata;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.device.DeviceGeneral;
import com.njcn.service.configuration.DeviceService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.pqs9900.pojo.linedata.HalfReportRVO;
import com.pqs9900.pojo.linedata.OnlineRateCldRVO;
import com.pqs9900.service.linedata.AreaIntegralityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * system
 *
 * @author cdf
 * @date 2021/12/6
 */
@RestController
@RequestMapping("areaData")
public class AreaIntegralityController {

    @Resource
    private AreaIntegralityService areaIntegralityService;
    /**
     * 区域统计--》数据完整性
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     */
    @PostMapping("dataIntegrality")
    @ResponseBody
    public HttpResult dataIntegrality(String startTime, String endTime, Integer overview,String area,
                                      String scale, String manc, String loadtype,Integer lineGrade, HttpServletRequest request) {
        HttpResult result = PubUtils.initResult(TokenManager.getToken().getLoginName(), request, LogTypeEnum.BUSSINESS.toString(), 0);
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "dataIntegrality");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        DeviceGeneral data;
        try {
            data = areaIntegralityService.getDataIntegrality(datePojo.getStartTime(), datePojo.getEndTime(), overview,area,scale, manc, loadtype,lineGrade);
            if (null == data) {
                result = PubUtils.assignmentResultLog(null, 500, "获取数据完整性数据为空", "查询数据完整性", "成功", result);
            } else {
                result = PubUtils.assignmentResultLog(data, 200, "获取数据完整性数据成功", "查询数据完整性", "成功", result);
            }

        } catch (Exception e) {
            result = PubUtils.assignmentResultLog(null, 500, "获取数据完整性数据异常", "查询数据完整性", "失败", result);
        }
        return result;
    }

    /**
     *区域统计--》终端状态
     */
    @PostMapping("deviceStatus")
    @ResponseBody
    public HttpResult getDeviceStatus(Integer overview, String area,String scale,String manc,String loadtype,
                                      HttpServletRequest request,String startTime,String endTime,Integer lineGrade) {
        HttpResult result=PubUtils.initResult(TokenManager.getToken().getLoginName(),request,LogTypeEnum.BUSSINESS.toString(),0);
        DeviceGeneral data ;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "deviceStatus");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        try {
            data=areaIntegralityService.getDeviceSiftData(overview,area,scale,manc,loadtype,datePojo.getStartTime(),datePojo.getEndTime(),lineGrade);
            if(null==data){
                result = PubUtils.assignmentResultLog(null, 500, "获取终端运行数据为空","查询稳态终端状态","成功",result);
            }else{
                result = PubUtils.assignmentResultLog(data, 200, "获取终端运行数据成功","查询稳态终端状态","成功",result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = PubUtils.assignmentResultLog(null, 500, "获取终端运行数据异常","查询稳态终端状态","失败",result);
        }
        return result;
    }


    /**
     * 获取半月报监测点详情
     * @author cdf
     * @date 2022/5/7
     */
    @GetMapping("getHalfReportList")
    public HttpResult getHalfReportList(String startTime,String endTime,String lineGrade,Integer status,Integer devFlag) {

        DatePojo datePojo = PubUtils.validateDate(startTime, endTime, "getHalfReportList");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            return PubUtils.assignmentResult(null, 500, datePojo.getMsg());
        }
        List<HalfReportRVO> list = areaIntegralityService.getHalfReportList(datePojo.getStartTime(),datePojo.getEndTime(),lineGrade,status,devFlag);
        return PubUtils.assignmentResult(list, 200, "获取成功");
    }
}
