package com.rj.controller;

import com.rj.entity.YhyAudioUploadLog;
import com.rj.service.IYhyAudioUploadLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 音频上传日志表 前端控制器
 * </p>
 *
 * @author Auto Generated
 * @since 2025-01-29
 */
@RestController
@RequestMapping("/api/yhyAudioUploadLog")
@Tag(name = "音频上传日志", description = "音频上传日志相关接口")
@Slf4j
public class YhyAudioUploadLogController {

    @Autowired
    private IYhyAudioUploadLogService yhyAudioUploadLogService;

    /**
     * 分页查询音频上传日志列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询音频上传日志列表", description = "分页查询音频上传日志信息列表，支持按设备号和开始时间、结束时间查询")
    public ResponseEntity<Map<String, Object>> getYhyAudioUploadLogList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "设备号（精确查询）")
            @RequestParam(required = false) String deviceNo,
            @Parameter(description = "开始时间（格式：251129185131）")
            @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间（格式：251129190131）")
            @RequestParam(required = false) String endTime,
            @Parameter(description = "创建开始时间", example = "2025-01-01 00:00:00")
            @RequestParam(required = false) String createStartTime,
            @Parameter(description = "创建结束时间", example = "2025-12-31 23:59:59")
            @RequestParam(required = false) String createEndTime) {
        Map<String, Object> result = yhyAudioUploadLogService.getYhyAudioUploadLogList(
                current, size, deviceNo, startTime, endTime, createStartTime, createEndTime);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            String message = (String) result.get("message");
            if (message != null && message.contains("格式错误")) {
                return ResponseEntity.badRequest().body(result);
            }
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据ID查询音频上传日志
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询音频上传日志", description = "根据音频上传日志ID获取详细信息")
    public ResponseEntity<Map<String, Object>> getYhyAudioUploadLogById(
            @Parameter(description = "音频上传日志ID", required = true)
            @PathVariable String id) {
        Map<String, Object> result = yhyAudioUploadLogService.getYhyAudioUploadLogById(id);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据设备号查询音频上传日志列表
     */
    @GetMapping("/getByDeviceNo")
    @Operation(summary = "根据设备号查询音频上传日志", description = "根据设备号查询音频上传日志列表")
    public ResponseEntity<Map<String, Object>> getYhyAudioUploadLogByDeviceNo(
            @Parameter(description = "设备号", required = true)
            @RequestParam String deviceNo) {
        Map<String, Object> result = yhyAudioUploadLogService.getYhyAudioUploadLogByDeviceNo(deviceNo);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 新增音频上传日志
     */
    @PostMapping("/add")
    @Operation(summary = "新增音频上传日志", description = "添加新的音频上传日志信息")
    public ResponseEntity<Map<String, Object>> addYhyAudioUploadLog(
            @Parameter(description = "音频上传日志信息", required = true)
            @RequestBody YhyAudioUploadLog yhyAudioUploadLog) {
        Map<String, Object> result = yhyAudioUploadLogService.addYhyAudioUploadLog(yhyAudioUploadLog);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 更新音频上传日志信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新音频上传日志信息", description = "更新音频上传日志详细信息")
    public ResponseEntity<Map<String, Object>> updateYhyAudioUploadLog(
            @Parameter(description = "音频上传日志信息", required = true)
            @RequestBody YhyAudioUploadLog yhyAudioUploadLog) {
        Map<String, Object> result = yhyAudioUploadLogService.updateYhyAudioUploadLog(yhyAudioUploadLog);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            String message = (String) result.get("message");
            if (message != null && message.contains("不能为空")) {
                return ResponseEntity.badRequest().body(result);
            }
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 根据ID删除音频上传日志
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除音频上传日志", description = "根据音频上传日志ID删除信息")
    public ResponseEntity<Map<String, Object>> deleteYhyAudioUploadLog(
            @Parameter(description = "音频上传日志ID", required = true)
            @PathVariable String id) {
        Map<String, Object> result = yhyAudioUploadLogService.deleteYhyAudioUploadLog(id);
        
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }
}

