package com.common.controller;

import com.common.entity.DeviceCollectTask;
import com.common.service.DeviceCollectTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/collect-tasks")
public class DeviceCollectTaskController {

    @Autowired
    private DeviceCollectTaskService deviceCollectTaskService;

    /**
     * 根据ID查询采集任务
     */
    @GetMapping("/{id}")
    public ResponseEntity<DeviceCollectTask> getById(@PathVariable Integer id) {
        DeviceCollectTask task = deviceCollectTaskService.getById(id);
        return ResponseEntity.ok(task);
    }

    /**
     * 查询所有采集任务
     */
    @GetMapping
    public ResponseEntity<List<DeviceCollectTask>> getAll() {
        List<DeviceCollectTask> tasks = deviceCollectTaskService.getAll();
        return ResponseEntity.ok(tasks);
    }

    /**
     * 根据设备ID查询采集任务
     */
    @GetMapping("/device/{deviceId}")
    public ResponseEntity<List<DeviceCollectTask>> getByDeviceId(@PathVariable Integer deviceId) {
        List<DeviceCollectTask> tasks = deviceCollectTaskService.getByDeviceId(deviceId);
        return ResponseEntity.ok(tasks);
    }

    /**
     * 多条件查询采集任务
     */
    @PostMapping("/search")
    public ResponseEntity<List<DeviceCollectTask>> search(@RequestBody DeviceCollectTask condition) {
        List<DeviceCollectTask> tasks = deviceCollectTaskService.getByCondition(condition);
        return ResponseEntity.ok(tasks);
    }

    /**
     * 新增采集任务
     */
    @PostMapping
    public ResponseEntity<DeviceCollectTask> create(@RequestBody DeviceCollectTask task) {
        deviceCollectTaskService.create(task);
        return ResponseEntity.ok(task);
    }

    /**
     * 更新采集任务
     */
    @PutMapping("/{id}")
    public ResponseEntity<DeviceCollectTask> update(@PathVariable Integer id,
                                                    @RequestBody DeviceCollectTask task) {
        task.setId(id);
        DeviceCollectTask updatedTask = deviceCollectTaskService.update(task);
        return ResponseEntity.ok(updatedTask);
    }

    /**
     * 删除采集任务
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        deviceCollectTaskService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 标记任务成功
     */
    @PostMapping("/{id}/success")
    public ResponseEntity<Void> markSuccess(@PathVariable Integer id) {
        deviceCollectTaskService.markSuccess(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 标记任务失败
     */
    @PostMapping("/{id}/failed")
    public ResponseEntity<Void> markFailed(@PathVariable Integer id) {
        deviceCollectTaskService.markFailed(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新EPM指标
     */
    @PutMapping("/{id}/epm")
    public ResponseEntity<Void> updateEpm(@PathVariable Integer id,
                                          @RequestParam Integer epm) {
        deviceCollectTaskService.updateEpm(id, epm);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询成功的任务
     */
    @GetMapping("/success")
    public ResponseEntity<List<DeviceCollectTask>> getSuccessTasks() {
        List<DeviceCollectTask> tasks = deviceCollectTaskService.getSuccessTasks();
        return ResponseEntity.ok(tasks);
    }

    /**
     * 查询失败的任务
     */
    @GetMapping("/failed")
    public ResponseEntity<List<DeviceCollectTask>> getFailedTasks() {
        List<DeviceCollectTask> tasks = deviceCollectTaskService.getFailedTasks();
        return ResponseEntity.ok(tasks);
    }

    /**
     * 获取设备最新任务
     */
    @GetMapping("/device/{deviceId}/latest")
    public ResponseEntity<DeviceCollectTask> getLatestByDeviceId(@PathVariable Integer deviceId) {
        DeviceCollectTask task = deviceCollectTaskService.getLatestByDeviceId(deviceId);
        return ResponseEntity.ok(task);
    }
}