package com.yeejoin.amos.api.openapi.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.typroject.tyboot.component.event.RestEventTrigger;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.doc.TycloudResource;

import com.yeejoin.amos.api.common.restful.utils.ResponseHelper;
import com.yeejoin.amos.api.common.restful.utils.ResponseModel;
import com.yeejoin.amos.api.openapi.face.model.ElevatorAlarmModel;
import com.yeejoin.amos.api.openapi.face.model.ElevatorAlarmModelList;
import com.yeejoin.amos.api.openapi.face.model.ElevatorStatusModel;
import com.yeejoin.amos.api.openapi.face.model.ElevatorStatusModelList;
import com.yeejoin.amos.api.openapi.face.model.ElevatorVideoModel;
import com.yeejoin.amos.api.openapi.face.service.ElevatorAlarmService;
import com.yeejoin.amos.api.openapi.face.service.ElevatorStatusService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;

/**
 * 
 * <pre>
 * 电梯物联对接
 * </pre>
 *
 * @author gwb
 * @version $Id: ElevatorStatusController.java, v 0.1 2021年9月28日 上午9:11:38 gwb Exp $
 */
@RestController
@RequestMapping(value = "/lift")
@Api(tags = "lift-电梯物联对接")
public class LiftController
{

	@Autowired
	private ElevatorStatusService elevatorStatusService;
	
	@Autowired
	private ElevatorAlarmService elevatorAlarmService;

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(value = "电梯运行状态数据高频上传请求")
	@RequestMapping(value = "upload/{registerCode}", method = RequestMethod.GET)
	@RestEventTrigger(value = "openapiLogEventHandler")
	public ResponseModel<String> highUpload (
			@PathVariable(value = "registerCode") String registerCode) throws Exception 
	{
		elevatorStatusService.highUpload(registerCode);
		return ResponseHelper.buildResponse("");
	}
	
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(value = "获取最近一次电梯运行状态数据")
	@RequestMapping(value = "status/{registerCode}", method = RequestMethod.GET)
	@RestEventTrigger(value = "openapiLogEventHandler")
	public ResponseModel<ElevatorStatusModel>  getLatelyStatus (
			@PathVariable(value = "registerCode") String registerCode,
			@RequestParam(value = "highUpload", required = false) Boolean highUpload) throws Exception 
	{
		//默认状态数据不进行高频上传
		if (ValidationUtil.isEmpty(highUpload))
		{
			highUpload = false;
		}
		return ResponseHelper.buildResponse(elevatorStatusService.getLatelyStatus(registerCode, highUpload));
	}
	
	
	@SuppressWarnings("rawtypes")
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(value = "电梯运行状态数据上传")
	@RequestMapping(value = "run", method = RequestMethod.POST)
	@RestEventTrigger(value = "openapiLogEventHandler")
	public ResponseModel<String> multUploadRunData (
			@RequestParam(value = "access_token", required = false) String access_token,
			@RequestBody String runStatusData) throws Exception 
	{
		JSONObject jsonobject = JSONObject.fromObject(runStatusData);

		 Map<String, Class> classMap = new HashMap<String, Class>();
		 classMap.put("lift", ElevatorStatusModel.class);
		
		ElevatorStatusModelList elevatorStatusModelList =  (ElevatorStatusModelList)JSONObject.toBean(jsonobject,ElevatorStatusModelList.class,classMap);
		elevatorStatusService.multCreateElevatorStatus(elevatorStatusModelList.getLift());
		return ResponseHelper.buildResponse("");
	}


	@SuppressWarnings("rawtypes")
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(value = "电梯故障数据上传")
	@RequestMapping(value = "fault", method = RequestMethod.POST)
	@RestEventTrigger(value = "openapiLogEventHandler")
	public ResponseModel<String> multUploadAlarmData (
			@RequestParam(value = "access_token", required = false) String access_token,
			@RequestBody String faultData) throws Exception 
	{
		JSONObject jsonobject = JSONObject.fromObject(faultData);

		 Map<String, Class> classMap = new HashMap<String, Class>();
		 classMap.put("lift", ElevatorAlarmModel.class);
		
		ElevatorAlarmModelList elevatorAlarmModelList =  (ElevatorAlarmModelList)JSONObject.toBean(jsonobject,ElevatorAlarmModelList.class,classMap);
		elevatorAlarmService.multCreateElevatorAlarm(elevatorAlarmModelList.getLift());
		return ResponseHelper.buildResponse("");
	}
	
	
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(value = "获取电梯视频预览地址")
	@RequestMapping(value = "video/preview/{registerCode}", method = RequestMethod.GET)
	@RestEventTrigger(value = "openapiLogEventHandler")
	public ResponseModel<ElevatorVideoModel> videoPreview (
			@PathVariable(value = "registerCode") String registerCode) throws Exception 
	{
		
		ElevatorVideoModel elevatorVideoModel = elevatorStatusService.videoPreview(registerCode);
		return ResponseHelper.buildResponse(elevatorVideoModel);
	}
}
