package org.jeecg.modules.sensor.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.device.entity.BuDevice;
import org.jeecg.modules.device.service.IBuDeviceOnlineService;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.message.websocket.WebSocket;
import org.jeecg.modules.sensor.entity.*;
import org.jeecg.modules.sensor.model.*;
import org.jeecg.modules.sensor.service.*;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 传感器信息表
 * @Author:
 * @Date:   2020-12-23
 * @Version: V1.0
 */
@Api(tags="传感器信息表")
@RestController
@RequestMapping("/sensor/buSensor")
@Slf4j
public class BuSensorController extends JeecgController<BuSensor, IBuSensorService> {

	@Autowired
	private IBuSensorService buSensorService;


	@Autowired
	private IBuSensorDataService buSensorDataService;

	@Autowired
	private ISysAnnouncementService sysAnnouncementService;

	@Autowired
	private IBuDeviceService buDeviceService;

	@Autowired
	private IBuDeviceOnlineService buDeviceOnlineService;


	@Autowired
	private IBuSensorDeviceDataService buSensorDeviceDataService;

	@Resource
	private WebSocket webSocket;
	@Autowired
	private IBuSensorTypeService buSensorTypeService;
	@Value("${jeecg.path.upload}")
	private String upLoadPath;

	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询
	 * @param buSensor
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "传感器信息表-分页列表查询")
	@ApiOperation(value="传感器信息表-分页列表查询", notes="传感器信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BuSensor buSensor,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<BuSensor> queryWrapper = QueryGenerator.initQueryWrapper(buSensor, req.getParameterMap());
//		queryWrapper.notLike("check_item","三相%");
		LambdaQueryWrapper<BuSensor> queryWrapper = new LambdaQueryWrapper<>();
		if(buSensor.getDeviceId()!=null){
			queryWrapper.eq(BuSensor::getDeviceId,buSensor.getDeviceId());
		}
//		if(buSensor.getAddress()!=null){
//			queryWrapper.eq(BuSensor::getAddress,buSensor.getAddress());
//		}
		queryWrapper.orderByAsc(BuSensor::getDataUnit);
		Page<BuSensor> page = new Page<BuSensor>(pageNo, pageSize);
		IPage<BuSensor> pageList = buSensorService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		BuSensor buSensor = buSensorService.getById(id);
		return Result.ok(buSensor);
	}
	/**
     *   添加
     * @param buSensor
     * @return
     */
    @AutoLog(value = "传感器信息表-添加")
    @ApiOperation(value="传感器信息表-添加", notes="传感器信息表-添加")
    @PostMapping(value = "/add")
	@Transactional
    public Result<?> add(@RequestBody BuSensor buSensor) {
    	BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
    	buSensor.setDeviceName(buDevice.getDeviceName());
//		switch (buSensor.getTypeId()) {
//			case "2":
//				buSensor.setCheckItem("环境温湿度");
//				break;
//			case "4":
//				buSensor.setCheckItem("烟雾浓度");
//				break;
//			case "5":
//				buSensor.setCheckItem("三相电压");
//				break;
//			case "6":
//				buSensor.setCheckItem("环境噪声");
//				break;
//			case "7":
//				buSensor.setCheckItem("功率因数");
//				break;
//		}
//		buSensor.setStatus(0);
		boolean save = buSensorService.save(buSensor);
//		if(save){
//
//		}
		return Result.OK("添加成功！");
    }


	@GetMapping(value = "/getItemData")
	public Result<?> getItemData(@RequestParam("sensorId") String sensorId) {
		List<BuSensor> list = new ArrayList<>();
		BuSensor buSensor = buSensorService.getById(sensorId);
		String[] result = buSensor.getCheckItem().split(",");
		for (String s : result) {
			BuSensor sensor =new BuSensor();
			sensor.setCheckItem(s);
			list.add(sensor);
		}
		return Result.OK(list);
	}

    /**
     *  编辑
     * @param buSensor
     * @return
     */
    @AutoLog(value = "传感器信息表-编辑")
    @ApiOperation(value="传感器信息表-编辑", notes="传感器信息表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BuSensor buSensor) {
        buSensorService.updateById(buSensor);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "传感器信息表-通过id删除")
    @ApiOperation(value="传感器信息表-通过id删除", notes="传感器信息表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        buSensorService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "传感器信息表-批量删除")
    @ApiOperation(value="传感器信息表-批量删除", notes="传感器信息表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.buSensorService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportSensorXls(HttpServletRequest request, BuSensor buSensor) {
        return this.exportSensorXls(request, buSensor, BuSensor.class, "传感器信息表");
    }
	protected ModelAndView exportSensorXls(HttpServletRequest request, BuSensor object, Class<BuSensor> clazz, String title) {
		// Step.1 组装查询条件
		QueryWrapper<BuSensor> queryWrapper = new QueryWrapper<>();
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// 过滤选中数据
		String selections = request.getParameter("selections");
		if (oConvertUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("id",selectionList);
		}
		// Step.2 获取导出数据
		List<BuSensor> exportList = service.list(queryWrapper);

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, clazz);
		//update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
		ExportParams  exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		exportParams.setImageBasePath(upLoadPath);
		//update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
		mv.addObject(NormalExcelConstants.PARAMS,exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;
	}
    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BuSensor.class);
    }
	/*---------------------------------主表处理-end-------------------------------------*/


    /*--------------------------------子表处理-传感器测量值表-begin----------------------------------------------*/





    /*--------------------------------子表处理-传感器测量值表-end----------------------------------------------*/

    /*--------------------------------子表处理-传感器数据表-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return ;
	 */
	@AutoLog(value = "传感器数据表-通过主表ID查询")
	@ApiOperation(value="传感器数据表-通过主表ID查询", notes="传感器数据表-通过主表ID查询")
	@GetMapping(value = "/listBuSensorDataByMainId")
    public Result<?> listBuSensorDataByMainId(BuSensorData buSensorData,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<BuSensorData> queryWrapper = QueryGenerator.initQueryWrapper(buSensorData, req.getParameterMap());
//		QueryWrapper<BuSensorData> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("sensor_id",buSensorData.getSensorId());
        Page<BuSensorData> page = new Page<BuSensorData>(pageNo, pageSize);
        IPage<BuSensorData> pageList = buSensorDataService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


	/**
	 * 添加
	 * @param buSensorData;
	 * @return ;
	 */
	@AutoLog(value = "传感器数据表-添加")
	@ApiOperation(value="传感器数据表-添加", notes="传感器数据表-添加")
	@PostMapping(value = "/addBuSensorData")
	public Result<?> addBuSensorData(@RequestBody BuSensorData buSensorData) {
		buSensorDataService.save(buSensorData);
		return Result.OK("添加成功！");
	}

	//通过选择的设备和传感器测量项目返回时域和频域所需的数据表名
//	@GetMapping(value = "getTableName")
//	public Integer[] getTableName(String deviceId,String checkItem) {
//		BuSensor busensor = buSensorService.getByCheckItem(deviceId,checkItem);
//		int value = Integer.parseInt(busensor.getAddress());
//		Integer[] result = new Integer[3];
//		result[0] = value;
//		result[1] = value+1;
//		result[2] = value+2;
//
//		return result;
//	}

	@GetMapping(value = "getSensorIp")
	public Result<?> getSensorIp(String deviceId) {
		List<TableNameStatistics> list = buSensorService.getItemByDeviceId(deviceId);
		return Result.OK(list);
	}

	// 查询不同传感器的预警报警上下限值
//	@GetMapping(value = "/getLimitValue")
//	public Result<?> getLimitValue(String deviceId,String checkItem,String sensorId) {
//		if (checkItem.contains("空气")){
//			BuSensor busensor = buSensorService.getByCheckItem(deviceId,"空气");
//			double[] result = new double[12];
//			String[] a = busensor.getAlarmLimit().split(",");
//			String[] b = busensor.getWarningLimit().split(",");
//			String[] c = busensor.getWarningMin().split(",");
//			String[] d = busensor.getAlarmMin().split(",");
//			result[0] = Double.parseDouble(a[0]);
//			result[1] = Double.parseDouble(b[0]);
//			result[2] = Double.parseDouble(c[0]);
//			result[3] = Double.parseDouble(d[0]);
//			result[4] = Double.parseDouble(a[1]);
//			result[5] = Double.parseDouble(b[1]);
//			result[6] = Double.parseDouble(c[1]);
//			result[7] = Double.parseDouble(d[1]);
//			result[8] = Double.parseDouble(a[2]);
//			result[9] = Double.parseDouble(b[2]);
//			result[10] = Double.parseDouble(c[2]);
//			result[11] = Double.parseDouble(d[2]);
//			return Result.OK(result);
//		}
//		else if (checkItem.contains("三相电压")){
//			BuSensor busensor = buSensorService.getByCheckItem(deviceId,"电能");
//			double[] result = new double[4];
//			String[] a = busensor.getAlarmLimit().split(",");
//			String[] b = busensor.getWarningLimit().split(",");
//			String[] c = busensor.getWarningMin().split(",");
//			String[] d = busensor.getAlarmMin().split(",");
//			result[0] = Double.parseDouble(a[0]);
//			result[1] = Double.parseDouble(b[0]);
//			result[2] = Double.parseDouble(c[0]);
//			result[3] = Double.parseDouble(d[0]);
//			return Result.OK(result);
//		}
//		else if (checkItem.contains("三相电流")){
//			BuSensor busensor = buSensorService.getByCheckItem(deviceId,"电能");
//			double[] result = new double[4];
//			String[] a = {"260","6"};
//			String[] b = {"260","6"};
//			String[] c = {"220","2"};
//			String[] d = {"220","2"};
//			result[0] = Double.parseDouble(a[1]);
//			result[1] = Double.parseDouble(b[1]);
//			result[2] = Double.parseDouble(c[1]);
//			result[3] = Double.parseDouble(d[1]);
//			return Result.OK(result);
//		}else {
//			BuSensor busensor = buSensorService.getByCode(deviceId,);
//			double[] result = new double[4];
//			result[0] = Double.parseDouble(busensor.getAlarmLimit());
//			result[1] = Double.parseDouble(busensor.getWarningLimit());
//			result[2] = Double.parseDouble(busensor.getWarningMin());
//			result[3] = Double.parseDouble(busensor.getAlarmMin());
//			return Result.OK(result);
//		}
//	}


 //查询不同传感器的预警报警上下限值
//@GetMapping(value = "/getLimitValue")
//public Result<?> getLimitValue(String deviceId,String checkItem,String sensorId) {
//	if (checkItem.contains("温湿度")){
//		BuSensor busensor = buSensorService.getBySensorId(deviceId,sensorId);
//		double[] result = new double[8];
//		String[] a = busensor.getAlarmLimit().split(",");
//		String[] b = busensor.getWarningLimit().split(",");
//		String[] c = busensor.getWarningMin().split(",");
//		String[] d = busensor.getAlarmMin().split(",");
//		result[0] = Double.parseDouble(a[0]);
//		result[1] = Double.parseDouble(b[0]);
//		result[2] = Double.parseDouble(c[0]);
//		result[3] = Double.parseDouble(d[0]);
//		result[4] = Double.parseDouble(a[1]);
//		result[5] = Double.parseDouble(b[1]);
//		result[6] = Double.parseDouble(c[1]);
//		result[7] = Double.parseDouble(d[1]);
//		return Result.OK(result);
//	}
//	else if (checkItem.contains("空气")){
//		BuSensor busensor = buSensorService.getBySensorId(deviceId,sensorId);
//		double[] result = new double[12];
//		String[] a = busensor.getAlarmLimit().split(",");
//		String[] b = busensor.getWarningLimit().split(",");
//		String[] c = busensor.getWarningMin().split(",");
//		String[] d = busensor.getAlarmMin().split(",");
//		result[0] = Double.parseDouble(a[0]);
//		result[1] = Double.parseDouble(b[0]);
//		result[2] = Double.parseDouble(c[0]);
//		result[3] = Double.parseDouble(d[0]);
//		result[4] = Double.parseDouble(a[1]);
//		result[5] = Double.parseDouble(b[1]);
//		result[6] = Double.parseDouble(c[1]);
//		result[7] = Double.parseDouble(d[1]);
//		result[8] = Double.parseDouble(a[2]);
//		result[9] = Double.parseDouble(b[2]);
//		result[10] = Double.parseDouble(c[2]);
//		result[11] = Double.parseDouble(d[2]);
//		return Result.OK(result);
//	}
//	else if (checkItem.contains("三相电压")){
//		BuSensor busensor = buSensorService.getBySensorId(deviceId,sensorId);
//		double[] result = new double[4];
//		String[] a = busensor.getAlarmLimit().split(",");
//		String[] b = busensor.getWarningLimit().split(",");
//		String[] c = busensor.getWarningMin().split(",");
//		String[] d = busensor.getAlarmMin().split(",");
//		result[0] = Double.parseDouble(a[0]);
//		result[1] = Double.parseDouble(b[0]);
//		result[2] = Double.parseDouble(c[0]);
//		result[3] = Double.parseDouble(d[0]);
//		return Result.OK(result);
//	}
//	else if (checkItem.contains("三相电流")){
//		BuSensor busensor = buSensorService.getBySensorId(deviceId,sensorId);
//		double[] result = new double[4];
//		String[] a = busensor.getAlarmLimit().split(",");
//		String[] b = busensor.getWarningLimit().split(",");
//		String[] c = busensor.getWarningMin().split(",");
//		String[] d = busensor.getAlarmMin().split(",");
//		result[0] = Double.parseDouble(a[1]);
//		result[1] = Double.parseDouble(b[1]);
//		result[2] = Double.parseDouble(c[1]);
//		result[3] = Double.parseDouble(d[1]);
//		return Result.OK(result);
//	}else {
////			String code = checkItem.substring(2);
//		BuSensor busensor = buSensorService.getBySensorId(deviceId,sensorId);
//		double[] result = new double[4];
//		result[0] = Double.parseDouble(busensor.getAlarmLimit());
//		result[1] = Double.parseDouble(busensor.getWarningLimit());
//		result[2] = Double.parseDouble(busensor.getWarningMin());
//		result[3] = Double.parseDouble(busensor.getAlarmMin());
//		return Result.OK(result);
//	}
//}

	public void getDataTableCount(){
		// 数据存储表名需要命名规则
//		int count = buSensorService.getDataCount();
	}


	//传感器数据图表展示查询
	@GetMapping(value = "/getSensorData")
	public Result<?> getSensorData(String beginTime,String endTime,String checkItems,String sensorId) throws ParseException {
		String substring = "";
		if(beginTime!=null){
			substring = beginTime.substring(0, beginTime
					.indexOf("-", beginTime.indexOf("-") + 1)).replaceAll("-","");
		}
		BuSensor buSensor = buSensorService.getById(sensorId);
		BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
		BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
		String tableName = "";
		if(buSensorType.getTableHead().equals("bu_vibration")||buSensorType.getTableHead().equals("bu_electric")){
			tableName = buSensorType.getTableHead() + "_history_" + buDevice.getTypeId() + "_" + buDevice.getDeviceCode() + "_" + substring;
		}else{
			tableName = buSensorType.getTableHead() + "_history_" + substring;
		}
		List<String> strings = Arrays.asList(checkItems.split(","));
		// 对查询的字段进行筛选
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("create_time, ");
		for (String checkItem : strings) {
			switch (checkItem) {
				case "U相电压":
					stringBuilder.append("udy,");
					break;
				case "V相电压":
					stringBuilder.append("vdy,");
					break;
				case "W相电压":
					stringBuilder.append("wdy,");
					break;
				case "U相电流":
					stringBuilder.append("udl,");
					break;
				case "V相电流":
					stringBuilder.append("vdl,");
					break;
				case "W相电流":
					stringBuilder.append("wdl,");
					break;
				case "总有功功率":
					stringBuilder.append("active_power,");
					break;
				case "频率":
					stringBuilder.append("frequency,");
					break;
				case "总电能":
					stringBuilder.append("eng,");
					break;
				case "总功率因数":
					stringBuilder.append("power_factor,");
					break;
				case "氧气":
					stringBuilder.append("o2,");
					break;
				case "一氧化碳":
					stringBuilder.append("co,");
					break;
				case "可燃气体":
					stringBuilder.append("ch4,");
					break;
				case "硫化氢":
					stringBuilder.append("h2s,");
					break;
				case "X速度":
					stringBuilder.append("speed_x,");
					break;
				case "Y速度":
					stringBuilder.append("speed_y,");
					break;
				case "Z速度":
					stringBuilder.append("speed_z,");
					break;
				case "X加速度":
					stringBuilder.append("acc_x,");
					break;
				case "Y加速度":
					stringBuilder.append("acc_y,");
					break;
				case "Z加速度":
					stringBuilder.append("acc_z,");
					break;
				case "X位移":
					stringBuilder.append("displacement_x,");
					break;
				case "Y位移":
					stringBuilder.append("displacement_y,");
					break;
				case "Z位移":
					stringBuilder.append("displacement_z,");
					break;
				case "温度":
					stringBuilder.append("temperature,");
					break;
				case "烟雾":
					stringBuilder.append("data,");
					break;
				case "流量":
					stringBuilder.append("discharge,");
					break;
				case "流速":
					stringBuilder.append("flowspeed,");
					break;
				case "累计流量":
					stringBuilder.append("flowrate,");
					break;
				case "甲烷":
					stringBuilder.append("ch4,");
					break;
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.setLength(stringBuilder.length() - 1);
		}
		List<BuSensorDeviceData> list = buSensorDeviceDataService.getCustomFieldData(tableName,stringBuilder.toString(), buSensor.getId(), beginTime, endTime);
//		List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(tableName,buSensor.getId(), beginTime, endTime);

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		List<JSONObject> result = new ArrayList<>();
		for (String checkItem : strings) {
			List<String> xList = new ArrayList<>();
			List<Double> yList = new ArrayList<>();
			JSONObject obj = new JSONObject();
			// 初始化时间范围 不变值
			Date time = sd.parse(beginTime);
			// 使用TreeMap来存储数据，键为时间
			TreeMap<Date, Double> dataMap = new TreeMap<>();
			switch (checkItem) {
				case "U相电压":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getUdy());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);

					/*for (int i=0; i<86400; i++){
						if (list.get(a).getCreateTime().compareTo(time) == 0){
							xList.add(sdf.format(time));
							yList.add(list.get(a).getUdy());
							if (a<list.size()-1){
								a++;
								if (list.get(a).getCreateTime().compareTo(time) == 0){
									a++;
								}
							}
							time.setTime(time.getTime() + 1000);
							continue;
						}
						xList.add(sdf.format(time));
						yList.add(null);
						time.setTime(time.getTime() + 1000);
					}
					obj.put("x", xList);
					obj.put("y", yList);*/
					break;
				case "V相电压":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getVdy());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "W相电压":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getWdy());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "U相电流":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getUdl());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "V相电流":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getVdl());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "W相电流":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getWdl());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "总有功功率":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getActivePower());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "频率":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getFrequency());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "总电能":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getWdl());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "总功率因数":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getPowerFactor());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "氧气":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getO2());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "一氧化碳":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getCo());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "可燃气体":
					for (BuSensorDeviceData data : list){
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getCh4());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "硫化氢":
					for (BuSensorDeviceData data : list){
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getH2s());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "X速度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getSpeedX());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "Y速度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getSpeedY());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "Z速度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getSpeedZ());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "X加速度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getAccX());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "Y加速度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getAccY());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "Z加速度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getAccZ());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "X位移":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getDisplacementX());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "Y位移":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getDisplacementY());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "Z位移":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getDisplacementZ());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "温度":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getTemperature());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "烟雾":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getData());
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "流量":
					for (BuSensorDeviceData data : list) {
//						xList.add(sdf.format(data.getCreateTime()));
//						yList.add(data.getData());

						xList.add(sdf.format(Math.abs(data.getCreateTime().getTime())));
						yList.add(Math.abs(data.getDischarge()));
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "流速":
					for (BuSensorDeviceData data : list) {
//						xList.add(sdf.format(data.getCreateTime()));
//						yList.add(data.getFlowspeed());

						xList.add(sdf.format(Math.abs(data.getCreateTime().getTime())));
						yList.add(Math.abs(data.getFlowspeed()));
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "累计流量":
					for (BuSensorDeviceData data : list) {
//						xList.add(sdf.format(data.getCreateTime()));
//						yList.add(data.getFlowrate());

						xList.add(sdf.format(Math.abs(data.getCreateTime().getTime())));
						yList.add(Math.abs(data.getFlowrate()));
					}
//					obj.put("x", xList);
//					obj.put("y", yList);
					break;
				case "甲烷":
					for (BuSensorDeviceData data : list) {
						xList.add(sdf.format(data.getCreateTime()));
						yList.add(data.getCh4());
					}
					break;
			}
			obj.put("x", xList);
			obj.put("y", yList);
			result.add(obj);
		}
		return Result.OK(result);


	}
	@GetMapping(value = "/getOnlineSensorData")
	public Result<?> getOnlineSensorData(String deviceId,String checkItems,String sensorId) {
		BuSensor buSensor = buSensorService.getById(sensorId);
		BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
		List<BuSensorDeviceData> list = buSensorDeviceDataService.getOnlineTestData(buSensorType.getTableHead() + "_online", buSensor.getId());
		List<String> strings = Arrays.asList(checkItems.split(","));
		JSONObject obj = new JSONObject();
		List<Object> yList = new ArrayList<>();
		for (String checkItem : strings) {
			switch (checkItem) {
				case "U相电压":
					Double[] dyU = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						dyU[n] = Double.valueOf(list.get(n).getUdy());
					}
					yList.add(dyU);
					break;
				case "V相电压":
					Double[] dyV = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						dyV[n] = Double.valueOf(list.get(n).getVdy());
					}
					yList.add(dyV);
					break;
				case "W相电压":
					Double[] dyW = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						dyW[n] = Double.valueOf(list.get(n).getWdy());
					}
					yList.add(dyW);
					break;
				case "U相电流":
					Double[] dlU = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						dlU[n] = Double.valueOf(list.get(n).getUdl());
					}
					yList.add(dlU);
					break;
				case "V相电流":
					Double[] dlV = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						dlV[n] = Double.valueOf(list.get(n).getVdl());
					}
					yList.add(dlV);
					break;
				case "W相电流":
					Double[] dlW = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						dlW[n] = Double.valueOf(list.get(n).getWdl());
					}
					yList.add(dlW);
					break;
				case "X速度":
					Double[] speedX = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						speedX[n] = list.get(n).getSpeedX();
					}
					yList.add(speedX);
					break;
				case "Y速度":
					Double[] speedY = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						speedY[n] = list.get(n).getSpeedY();
					}
					yList.add(speedY);
					break;
				case "Z速度":
					Double[] speedZ = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						speedZ[n] = list.get(n).getSpeedZ();
					}
					yList.add(speedZ);
					break;
				case "X加速度":
					Double[] accX = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						accX[n] = list.get(n).getAccX();
					}
					yList.add(accX);
					break;
				case "Y加速度":
					Double[] accY = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						accY[n] = list.get(n).getAccY();
					}
					yList.add(accY);
					break;
				case "Z加速度":
					Double[] accZ = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						accZ[n] = list.get(n).getAccZ();
					}
					yList.add(accZ);
					break;
				case "X位移":
					Double[] displacementX = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						displacementX[n] = list.get(n).getDisplacementX();
					}
					yList.add(displacementX);
					break;
				case "Y位移":
					Double[] displacementY = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						displacementY[n] = list.get(n).getDisplacementY();
					}
					yList.add(displacementY);
					break;
				case "Z位移":
					Double[] displacementZ = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						displacementZ[n] = list.get(n).getDisplacementZ();
					}
					yList.add(displacementZ);
					break;
				case "氧气":
					Double[] o2 = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						o2[n] = Double.valueOf(list.get(n).getO2());
					}
					yList.add(o2);
					break;
				case "一氧化碳":
					Double[] co = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						co[n] = Double.valueOf(list.get(n).getCo());
					}
					yList.add(co);
					break;
				case "可燃气体":
					Double[] ch4 = new Double[list.size()];
					for (int n = 0; n < list.size(); n++) {
						ch4[n] = Double.valueOf(list.get(n).getCh4());
					}
					yList.add(ch4);
					break;
				default:
			}

		}
		List<Date> x = new ArrayList<>();
		for (BuSensorDeviceData bu : list) {
			x.add(bu.getCreateTime());
		}
		obj.put("x", x);
		obj.put("y", yList);
		yList.clear();
		return Result.OK(obj);

	}

//	//查询衡简传感器采集数据
//	@GetMapping(value = "/getSensorData")
//	public Result<?> getSensorData(String deviceId,String beginTime,String endTime,String checkItems,String sensorId) {
//		List<String> strings = Arrays.asList(checkItems.split(","));
//		List<JSONObject> objList = new ArrayList<>();
//		for (String checkItem : strings) {
//			JSONObject obj = new JSONObject();
//
//				if(checkItem.equals("三相电压")){
//				BuSensor buSensor = buSensorService.getById(sensorId);
//				BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead()+"_history",buSensor.getId(),beginTime,endTime);
//				Double[] dyU = new Double[list.size()];
//				Double[] dyV = new Double[list.size()];
//				Double[] dyW = new Double[list.size()];
//				for (int n=0; n<list.size(); n++){
//					dyU[n] = Double.valueOf(list.get(n).getUdy());
//					dyV[n] = Double.valueOf(list.get(n).getVdy());
//					dyW[n] = Double.valueOf(list.get(n).getWdy());
//				}
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x",x);
//				obj.put("y",dyU);
//				obj.put("v",dyV);
//				obj.put("w",dyW);
//	//				obj.put("gl",gl);
//			}else if(checkItem.equals("三相电流")) {
//				BuSensor buSensor = buSensorService.getById(sensorId);
//				BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead()+"_history",buSensor.getId(), beginTime, endTime);
//				Double[] dlU = new Double[list.size()];
//				Double[] dlV = new Double[list.size()];
//				Double[] dlW = new Double[list.size()];
//				for (int n = 0; n < list.size(); n++) {
//					dlU[n] = Double.valueOf(list.get(n).getUdl());
//					dlV[n] = Double.valueOf(list.get(n).getVdl());
//					dlW[n] = Double.valueOf(list.get(n).getWdl());
//				}
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x", x);
//				obj.put("u",dlU);
//				obj.put("v",dlV);
//				obj.put("w",dlW);
//			}else if(checkItem.contains("速度")) {
//				BuSensor buSensor = buSensorService.getById(sensorId);
//				BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead()+"_history", buSensor.getId(), beginTime, endTime);
//				Double[] dlU = new Double[list.size()];
//				Double[] dlV = new Double[list.size()];
//				Double[] dlW = new Double[list.size()];
//				for (int n = 0; n < list.size(); n++) {
//					dlU[n] = Double.valueOf(list.get(n).getSpeedX());
//					dlV[n] = Double.valueOf(list.get(n).getSpeedY());
//					dlW[n] = Double.valueOf(list.get(n).getSpeedZ());
//				}
//
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x", x);
//				obj.put("u", dlU);
//				obj.put("v", dlV);
//				obj.put("w", dlW);
//			}else if(checkItem.contains("加速度")) {
//				BuSensor buSensor = buSensorService.getById(sensorId);
//				BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead()+"_history", buSensor.getId(), beginTime, endTime);
//				Double[] dlU = new Double[list.size()];
//				Double[] dlV = new Double[list.size()];
//				Double[] dlW = new Double[list.size()];
//				for (int n = 0; n < list.size(); n++) {
//					dlU[n] = Double.valueOf(list.get(n).getAccX());
//					dlV[n] = Double.valueOf(list.get(n).getAccY());
//					dlW[n] = Double.valueOf(list.get(n).getAccZ());
//				}
//
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x", x);
//				obj.put("u", dlU);
//				obj.put("v", dlV);
//				obj.put("w", dlW);
//			}else if(checkItem.contains("位移")) {
//				BuSensor buSensor = buSensorService.getById(sensorId);
//				BuSensorType buSensorType = buSensorTypeService.getById(buSensor.getTypeId());
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead()+"_history", buSensor.getId(), beginTime, endTime);
//				Double[] dlU = new Double[list.size()];
//				Double[] dlV = new Double[list.size()];
//				Double[] dlW = new Double[list.size()];
//				for (int n = 0; n < list.size(); n++) {
//					dlU[n] = Double.valueOf(list.get(n).getDisplacementX());
//					dlV[n] = Double.valueOf(list.get(n).getDisplacementY());
//					dlW[n] = Double.valueOf(list.get(n).getDisplacementZ());
//				}
//
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x", x);
//				obj.put("u", dlU);
//				obj.put("v", dlV);
//				obj.put("w", dlW);
//			}else if(checkItem.equals("空气")) {
//				BuSensor buSensor = buSensorService.getById(sensorId);
//				//传感器类型表
//				LambdaQueryWrapper<BuSensorType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//				lambdaQueryWrapper.eq(BuSensorType::getId,buSensor.getTypeId());
//				BuSensorType buSensorType = buSensorTypeService.list(lambdaQueryWrapper).get(0);
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead()+"_history",buSensor.getId(), beginTime, endTime);
//				Double[] dlU = new Double[list.size()];
//				Double[] dlV = new Double[list.size()];
//				Double[] dlW = new Double[list.size()];
//				for (int n = 0; n < list.size(); n++) {
//					dlU[n] = Double.valueOf(list.get(n).getO2());
//					dlV[n] = Double.valueOf(list.get(n).getCo());
//					dlW[n] = Double.valueOf(list.get(n).getCh4());
//				}
//
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x", x);
//				obj.put("u",dlU);
//				obj.put("v",dlV);
//				obj.put("w",dlW);
//
//			}else {
//				BuSensor buSensor = buSensorService.getByCheckItem(deviceId,checkItem);
//				//传感器类型表
//				LambdaQueryWrapper<BuSensorType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//				lambdaQueryWrapper.eq(BuSensorType::getId,buSensor.getTypeId());
//				BuSensorType buSensorType = buSensorTypeService.list(lambdaQueryWrapper).get(0);
//				List<BuSensorDeviceData> list = buSensorDeviceDataService.getTestData(buSensorType.getTableHead(),buSensor.getId(),beginTime,endTime);
//				Double[] y = new Double[list.size()];
//				if (buSensor.getCheckItem().contains("噪声")){
//					for (int n=0; n<list.size(); n++){
//						y[n] = Double.valueOf(list.get(n).getZs());
//					}
//				}else if (buSensor.getCheckItem().contains("烟雾")){
//					for (int n=0; n<list.size(); n++){
//						y[n] = Double.valueOf(list.get(n).getSmoke());
//					}
//				}else if (buSensor.getCheckItem().contains("功率")){
//					for (int n=0; n<list.size(); n++){
//						y[n] = list.get(n).getGl();
//					}
//				}
//				List<Date> x = new ArrayList<>();
//				for (BuSensorDeviceData bu : list) {
//					x.add(bu.getCreateTime());
//				}
//				obj.put("x",x);
//				obj.put("y",y);
//			}
//			objList.add(obj);
//		}
//
//		return Result.OK(objList);
//	}


	// 查询设备状态并记录
//	@PostMapping(value = "/normalStatus")
//	public Result<?> normalStatus() {
//		List<String> deviceId = buSensorService.getNormalStatus();
//		for (String s : deviceId) {
//			BuDeviceOnline buDeviceOnline = buDeviceOnlineService.getDataByDeviceId(s);
//			buDeviceOnline.setDeviceStatus("0");
//			buDeviceOnlineService.updateById(buDeviceOnline);
//		}
//		return Result.OK(deviceId);
//	}

	// 查询设备预警状态并记录
//	@PostMapping(value = "/earlyStatus")
//	public Result<?> earlyStatus() {
//		List<BuSensor> buSensor = buSensorService.getEarlyStatus();
//		List<String> deviceId = buSensor.stream().map(BuSensor::getDeviceId).collect(Collectors.toList());
//		for (int i=0;i<buSensor.size();i++) {
//			BuDeviceOnline buDeviceOnline = buDeviceOnlineService.getDataByDeviceId(deviceId.get(i));
//			buDeviceOnline.setEarlyTime(buDeviceOnline.getEarlyTime() + 1);
//			buDeviceOnline.setDeviceStatus("1");
//			buDeviceOnlineService.updateById(buDeviceOnline);
//		}
//		return Result.OK(buSensor);
//	}

	// 查询设备报警状态并记录
//	@PostMapping(value = "/alarmStatus")
//	public Result<?> alarmStatus() {
//		List<BuSensor> buSensor = buSensorService.getAlarmStatus();
//		List<String> deviceId = buSensor.stream().map(BuSensor::getDeviceId).collect(Collectors.toList());
//		for (int i=0;i<buSensor.size();i++) {
//			BuDeviceOnline buDeviceOnline = buDeviceOnlineService.getDataByDeviceId(deviceId.get(i));
//			buDeviceOnline.setAlarmTime(buDeviceOnline.getAlarmTime() + 1);
//			buDeviceOnline.setDeviceStatus("2");
//			buDeviceOnlineService.updateById(buDeviceOnline);
//		}
//		return Result.OK(buSensor);
//	}

//	@PostMapping(value = "/repairStatus")
//	public Result<?> repairStatus() {
//		List<BuSensor> buSensor = buSensorService.getRepairStatus();
//		List<String> deviceId = buSensor.stream().map(BuSensor::getDeviceId).collect(Collectors.toList());
//		for (int i=0;i<buSensor.size();i++) {
//			BuDeviceOnline buDeviceOnline = buDeviceOnlineService.getDataByDeviceId(deviceId.get(i));
//			buDeviceOnline.setRepairTime(buDeviceOnline.getRepairTime() + 1);
//			buDeviceOnline.setDeviceStatus("3");
//			buDeviceOnlineService.updateById(buDeviceOnline);
//		}
//		return Result.OK(buSensor);
//	}

	/**
	 * 查询传感器电池电压，低于3V通知更换
	 *
	 */
//	@GetMapping(value = "/powerRemind")
//	public List<String> powerRemind() {
//		List<BuSensor> buSensor = buSensorService.getPowerData();
//		List<String> deviceName = buSensor.stream().map(BuSensor::getDeviceName).collect(Collectors.toList());
//		List<String> name = buSensor.stream().map(BuSensor::getName).collect(Collectors.toList());
//		List<String> power = buSensor.stream().map(BuSensor::getPower).collect(Collectors.toList());
//		List<String> result = new ArrayList<>();
//		for (int i=0; i<buSensor.size(); i++){
//			result.set(i, deviceName.get(i) + "的" + name.get(i) + "电池电压低，请及时更换电池。当前电池电压为" + power.get(i));
//			result.add(deviceName.get(i) + "的" + name.get(i) + "电池电压低，请及时更换电池。当前电池电压为" + power.get(i));
//		}
//		Result<SysAnnouncement> result = new Result<SysAnnouncement>();
//		for (int i=0; i<t ; i++) {
//			SysAnnouncement sysAnnouncement = new SysAnnouncement();
//			sysAnnouncement.setTitile("请及时更换传感器电池");
//			sysAnnouncement.setMsgContent(deviceName.get(i) + "的" + name.get(i) + "电池电压低，请及时更换电池。当前电池电压为" + power.get(i));
//			sysAnnouncement.setSender("管理员");
//			sysAnnouncement.setPriority("L");
//			sysAnnouncement.setMsgCategory("2");
//			sysAnnouncement.setMsgType("USER");
//			sysAnnouncement.setSendStatus("0");
//			sysAnnouncement.setSendTime(new Date());
//			sysAnnouncement.setDelFlag("0");
//			sysAnnouncement.setStartTime(new Date());
//			sysAnnouncement.setEndTime(new Date());
//			sysAnnouncement.setUserIds("e9ca23d68d884d4ebb19d07889727dae,");
//			sysAnnouncement.setMsgAbstract("电池电压低");
//			sysAnnouncementService.saveAnnouncement(sysAnnouncement);
////			sysAnnouncementController.doReleaseData(sysAnnouncement.getId(),);
//			sysAnnouncement.setSendStatus(CommonSendStatus.PUBLISHED_STATUS_1);//发布中
//			sysAnnouncement.setSendTime(new Date());
////			String currentUserName = JwtUtil.getUserNameByToken(request);
////			sysAnnouncement.setSender(currentUserName);
//			boolean ok = sysAnnouncementService.updateById(sysAnnouncement);
//			if(ok) {
//				result.success("有新的系统通知注意查看");
//				if(sysAnnouncement.getMsgType().equals(CommonConstant.MSG_TYPE_ALL)) {
//					JSONObject obj = new JSONObject();
//					obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
//					obj.put(WebsocketConst.MSG_ID, sysAnnouncement.getId());
//					obj.put(WebsocketConst.MSG_TXT, sysAnnouncement.getTitile());
//					webSocket.sendAllMessage(obj.toJSONString());
//				}else {
//					// 2.插入用户通告阅读标记表记录
//					String userId = sysAnnouncement.getUserIds();
//					String[] userIds = userId.substring(0, (userId.length()-1)).split(",");
//					String anntId = sysAnnouncement.getId();
//					Date refDate = new Date();
//					JSONObject obj = new JSONObject();
//					obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_USER);
//					obj.put(WebsocketConst.MSG_ID, sysAnnouncement.getId());
//					obj.put(WebsocketConst.MSG_TXT, sysAnnouncement.getTitile());
//					webSocket.sendMoreMessage(userIds, obj.toJSONString());
//				}
//			}
//		}
//		return result;
//	}

    /*
	 * 编辑
	 */
	@AutoLog(value = "传感器数据表-编辑")
	@ApiOperation(value="传感器数据表-编辑", notes="传感器数据表-编辑")
	@PutMapping(value = "/editBuSensorData")
	public Result<?> editBuSensorData(@RequestBody BuSensorData buSensorData) {

//		Map<String, String> collectDate = buSensorData.collectDate();
//		AlarmEnum alarmLevel = AlarmEnum.Normal;
//		BuSensor buSensor = buSensorService.getDataById(buSensorData.getSensorId());
//		BuDeviceOnline buDeviceOnline = buDeviceOnlineService.getDataByDeviceId(buSensor.getDeviceId());
//		for (Map.Entry<String, String> stringStringEntry : collectDate.entrySet())
//		{
//			String warningLimit = buSensor.getWarningLimit();
//			String alarmLimit = buSensor.getAlarmLimit();
//			AlarmEnum alarmEnum = AlarmEnum.Normal;
//			if (Integer.parseInt(stringStringEntry.getValue())>0 && Integer.parseInt(stringStringEntry.getValue()) < Integer.parseInt(warningLimit))
//			{
//				alarmEnum = AlarmEnum.Normal;
//			}else if (Integer.parseInt(stringStringEntry.getValue())>=Integer.parseInt(warningLimit)&&Integer.parseInt(stringStringEntry.getValue())<Integer.parseInt(alarmLimit))
//			{
//				alarmEnum = AlarmEnum.Warning;
//			}else if (Integer.parseInt(stringStringEntry.getValue())>=Integer.parseInt(alarmLimit))
//			{
//				alarmEnum = AlarmEnum.Alarm;
//			}
//			if (alarmLevel.getCode() < alarmEnum.getCode())
//			{
//				alarmLevel = alarmEnum;
//			}
//		}
//		buDeviceOnline.setDeviceFunctionStatus(alarmLevel.getDes());
		buSensorDataService.updateById(buSensorData);
//		buDeviceOnlineService.updateById(buDeviceOnline);

		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id;
	 *
	 */
	@AutoLog(value = "传感器数据表-通过id删除")
	@ApiOperation(value="传感器数据表-通过id删除", notes="传感器数据表-通过id删除")
	@DeleteMapping(value = "/deleteBuSensorData")
	public Result<?> deleteBuSensorData(@RequestParam(name="id",required=true) String id) {
		buSensorDataService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids;
	 *
	 */
	@AutoLog(value = "传感器数据表-批量删除")
	@ApiOperation(value="传感器数据表-批量删除", notes="传感器数据表-批量删除")
	@DeleteMapping(value = "/deleteBatchBuSensorData")
	public Result<?> deleteBatchBuSensorData(@RequestParam(name="ids",required=true) String ids) {
	    this.buSensorDataService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     *
     */
    @RequestMapping(value = "/exportBuSensorData")
    public ModelAndView exportBuSensorData(HttpServletRequest request, BuSensorData buSensorData) {
		 // Step.1 组装查询条件
		 QueryWrapper<BuSensorData> queryWrapper = QueryGenerator.initQueryWrapper(buSensorData, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<BuSensorData> pageList = buSensorDataService.list(queryWrapper);
		 List<BuSensorData> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "传感器数据表"); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS, BuSensorData.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("传感器数据表报表", "导出人:" + sysUser.getRealname(), "传感器数据表"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /*
     * 导入
     */
//    @RequestMapping(value = "/importBuSensorData/{mainId}")
//    public Result<?> importBuSensorData(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
//		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
//			 MultipartFile file = entity.getValue();// 获取上传文件对象
//			 ImportParams params = new ImportParams();
//			 params.setTitleRows(2);
//			 params.setHeadRows(1);
//			 params.setNeedSave(true);
//			 try {
//				 List<BuSensorData> list = ExcelImportUtil.importExcel(file.getInputStream(), BuSensorData.class, params);
//				 for (BuSensorData temp : list) {
//                    temp.setSensorId(mainId);
//				 }
//				 long start = System.currentTimeMillis();
//				 buSensorDataService.saveBatch(list);
//				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
//				 return Result.OK("文件导入成功！数据行数：" + list.size());
//			 } catch (Exception e) {
//				 log.error(e.getMessage(), e);
//				 return Result.error("文件导入失败:" + e.getMessage());
//			 } finally {
//				 try {
//					 file.getInputStream().close();
//				 } catch (IOException e) {
//					 e.printStackTrace();
//				 }
//			 }
//		 }
//		 return Result.error("文件导入失败！");
//    }

    /*--------------------------------子表处理-传感器数据表-end----------------------------------------------*/


	//设备数据查询
	@GetMapping(value = "/getDeviceList")
	public Result<?> getDeviceList(String deviceId,String ip,String beginTime,String endTime){
		//传感器配置表
		LambdaQueryWrapper<BuSensor> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BuSensor::getDeviceId,deviceId);
		BuSensor buSensor = buSensorService.list(queryWrapper).get(0);
		//传感器类型
		LambdaQueryWrapper<BuSensorType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BuSensorType::getId,buSensor.getTypeId());
		BuSensorType buSensorType = buSensorTypeService.list(lambdaQueryWrapper).get(0);
		List<DeviceDataRequest> list = buSensorService.getDeviceData(buSensorType.getTableHead(),ip,beginTime,endTime);
		return Result.OK(list);
	}

}
