package com.persagy.dmp.rwd.iot.controller;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ParamCheckUtil;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.rwd.iot.model.CurrentDataModel;
import com.persagy.dmp.rwd.iot.model.SettingDataModel;
import com.persagy.dmp.rwd.iot.model.history.InfoCodeModel;
import com.persagy.dmp.rwd.iot.model.history.RequestModel;
import com.persagy.dmp.rwd.iot.service.CurrentIotService;
import com.persagy.dmp.rwd.iot.service.HistoryIotService;
import com.persagy.dmp.rwd.iot.service.SettingIotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * iot-查询实时数据
 * @author:linhuili
 * @date:2021/8/11
 */
@RestController
@RequestMapping("/rwd/iot")
public class IotController {

	@Autowired
	private CurrentIotService currentDataService;

	@Autowired
	private HistoryIotService historyDataService;

	@Autowired
	private SettingIotService settingDataService;

	/**
	 * 查询对象绑点信息点的实时数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/data/current")
	@Deprecated
	public CommonResult<List<CurrentDataModel>> current(@RequestBody List<CurrentDataModel> param) throws Exception {
		//基础参数校验
		ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
		//查询实时数据
		List<CurrentDataModel> currentData = currentDataService.getCurrentData(param);
		if(currentData == null || currentData.size()<=0){
			return ResultHelper.multi(new ArrayList<>(),0);
		}
		return ResultHelper.multi(currentData,currentData.size());
	}


	/**
	 * 查询历史数据/分精度数据
	 * @param request
	 * @return
	 */
	@PostMapping("/data/history")
	@Deprecated
	public CommonResult<List<InfoCodeModel>> history(@RequestBody RequestModel request) throws Exception{
		//基础参数校验
		ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
		checkHistoryRequest(request);
        //查询历史数据/分精度数据
		List<InfoCodeModel> historyData = historyDataService.getHistoryData(request);

		if(historyData == null || historyData.size()<=0){
			return ResultHelper.multi(new ArrayList<>(),0);
		}
		return ResultHelper.multi(historyData,historyData.size());
	}

	/**
	 * 发送控制指令/设定动态参数
	 * @param param
	 * @return
	 */
	@PostMapping("/funcid/setting")
	@Deprecated
	public CommonResult<List<SettingDataModel>> setting(@RequestBody List<SettingDataModel> param) {
		//基础参数校验
		ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
		//发送控制指令/设定动态参数
		List<SettingDataModel> settingData = settingDataService.settingDataService(param);

		if(settingData == null || settingData.size()<=0){
			return ResultHelper.multi(new ArrayList<>(),0);
		}
		return ResultHelper.multi(settingData,settingData.size());
	}


	/**
	 * 查询指令执行结果
	 * @param param
	 * @return
	 */
	@PostMapping("/funcid/settingQuery")
	@Deprecated
	public CommonResult<List<SettingDataModel>> settingQuery(@RequestBody List<String> param) {
		//基础参数校验
		ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
		//发送控制指令/设定动态参数
		List<SettingDataModel> settingData = settingDataService.settingQuery(param);

		if(settingData == null || settingData.size()<=0){
			return ResultHelper.multi(new ArrayList<>(),0);
		}
		return ResultHelper.multi(settingData,settingData.size());
	}

	/**
	 * 查询历史数据请求参数校验
	 * @param request
	 */
	private void checkHistoryRequest(RequestModel request) {
		String startTime = request.getStartTime();
		if (startTime == null) {
			throw new BusinessException(ResponseCode.A0400.getCode(), "startTime is required");
		}
		String endTime = request.getEndTime();
		if (endTime == null) {
			throw new BusinessException(ResponseCode.A0400.getCode(), "endTime is required");
		}
		String period = request.getPeriod();
		if (period == null) {
			throw new BusinessException(ResponseCode.A0400.getCode(), "period is required");
		}
		List<InfoCodeModel> params = request.getParams();
		if (params == null || params.size() == 0) {
			throw new BusinessException(ResponseCode.A0400.getCode(), "params is required");
		}
	}



}
