package com.prolog.rdc.tms.app.controller.web;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.rdc.tms.app.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.app.model.dto.VoiceBroadcastDTO;
import com.prolog.rdc.tms.app.model.vo.VoiceBroadcastQueryVO;
import com.prolog.rdc.tms.app.service.VoiceBroadcastService;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author hongbo.pan
 * @date 2023/1/12
 */
@Api(tags = "司机APP-语音播报")
@RestController
@RequestMapping("/voiceBroadcast")
public class VoiceBroadcastController {

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private VoiceBroadcastService voiceBroadcastService;

	@UserLog("司机开启关闭语音播报")
	@ApiOperation(value = "司机开启关闭语音播报", notes = "司机开启关闭语音播报")
	@PostMapping("/setDriverVoiceBroadcastSwitch")
	public RestMessage<Object> openOrCloseVoiceBroadcast(@RequestParam("driverId") String driverId, @RequestParam("switch") String openOrClose) {
		redisTemplate.opsForHash().put(DataDictionaryConstant.DRIVER_VOICE_BROADCAST_SWITCH_REDIS_KEY, driverId, openOrClose);
		return RestMessage.newInstance(true, "操作成功", null);
	}

	@UserLog("获取司机语音播报开关")
	@ApiOperation(value = "获取司机语音播报开关", notes = "获取司机语音播报开关")
	@GetMapping("/getDriverVoiceBroadcastSwitch")
	public RestMessage<String> getDriverVoiceBroadcastSwitch(@RequestParam("driverId") String driverId) {
		// 默认开启
		String openOrClose = "1";
		Object o = redisTemplate.opsForHash().get(DataDictionaryConstant.DRIVER_VOICE_BROADCAST_SWITCH_REDIS_KEY, driverId);
		if (ObjectUtil.isNotNull(o)) {
			openOrClose = o.toString();
		}
		return RestMessage.newInstance(true, "操作成功", openOrClose);
	}

	@UserLog("获取司机语音播报数据")
	@ApiOperation(value = "获取司机语音播报数据", notes = "获取司机语音播报数据")
	@PostMapping("/broadcast")
	public RestMessage<VoiceBroadcastDTO> broadcast(@RequestBody @Valid VoiceBroadcastQueryVO vo) {
		return RestMessage.newInstance(true, "操作成功", voiceBroadcastService.broadcast(vo));
	}

}
