package com.pig4cloud.pig.marketing.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.marketing.api.dto.mongo.PageDeviceInfoDTO;
import com.pig4cloud.pig.marketing.api.dto.mongo.PageMessageDTO;
import com.pig4cloud.pig.marketing.api.dto.mongo.SaveDeviceInfoDTO;
import com.pig4cloud.pig.marketing.api.dto.mongo.SaveTcpMessageDTO;
import com.pig4cloud.pig.marketing.api.entity.mongo.Device;
import com.pig4cloud.pig.marketing.api.entity.mongo.Message;
import com.pig4cloud.pig.marketing.api.vo.mongo.PageDeviceInfoVO;
import com.pig4cloud.pig.marketing.api.vo.mongo.UserStatisticsVO;
import com.pig4cloud.pig.marketing.service.TcpDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * @author: lwh
 * @date: 2025-08-27
 * @description: TCP数据控制器
 */

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/tcp")
@Tag(description = "TcpData", name = "TCP数据上报服务")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TcpDataController {

	private final TcpDataService tcpDataService;

	/******************************************* 设备 *******************************************/
	@PostMapping("/device/save")
	@Operation(summary = "保存或更新设备信息")
	public R<String> saveOrUpdateDevice(@Valid @RequestBody SaveDeviceInfoDTO reqDto) {
		return R.ok(tcpDataService.saveOrUpdateDevice(reqDto));
	}

	@GetMapping("/device/get/{clientID}")
	@Operation(summary = "根据客户端ID查询设备信息")
	public R<Device> getDevice(@PathVariable String clientID) {
		return R.ok(tcpDataService.getDeviceByClientID(clientID));
	}

	@PostMapping("/device/page")
	@Operation(summary = "分页查询设备信息")
	public R<Page<PageDeviceInfoVO>> PageDeviceInfo(@RequestBody PageDeviceInfoDTO reqDto) {
		return R.ok(tcpDataService.pageDeviceInfo(reqDto));
	}

	/******************************************* 消息 *******************************************/
	@PostMapping("/msg/save")
	@Operation(summary = "保存上报数据")
	public R<String> saveMessage(@Valid @RequestBody SaveTcpMessageDTO reqDto) {
		return R.ok(tcpDataService.saveMessage(reqDto));
	}

	@GetMapping("/msg/get")
	@Operation(summary = "根据ID查询数据详情")
	public R<Message> getMessagesByClient(
			@NotBlank(message = "id不能为空")
			@ParameterObject String id) {
		return R.ok(tcpDataService.getMessagesById(id));
	}

	@PostMapping("/msg/page")
	@Operation(summary = "分页查询数据列表")
	public R<Page<Message>> PageMsgInfo(@Valid @RequestBody PageMessageDTO reqDto) {
		return R.ok(tcpDataService.pageMsgInfo(reqDto));
	}

	/******************************************* 统计 *******************************************/
	@GetMapping("/statistics/users")
	@Operation(summary = "统计用户数", description = "统计累计用户数和活跃用户数")
	public R<UserStatisticsVO> getUserStatistics() {
		log.info("开始统计用户数");
		try {
			UserStatisticsVO statistics = tcpDataService.getUserStatistics();
			return R.ok(statistics);
		} catch (Exception e) {
			log.error("统计用户数异常", e);
			return R.failed("统计失败：" + e.getMessage());
		}
	}
}
