package com.fizz.business.controller;

import com.fizz.business.domain.BizSendJob;
import com.fizz.business.domain.dto.SendJobCreateDTO;
import com.fizz.business.domain.dto.SendJobQueryDTO;
import com.fizz.business.domain.vo.SendJobDetailVO;
import com.fizz.business.service.ISendJobService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.fizz.business.domain.vo.FurnaceSendCoilInfoVO;
import com.fizz.business.domain.vo.SendJobLastSuccessVO;
import com.fizz.business.service.ISendJobQueryService;
import com.fizz.business.utils.RedisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发送任务 Controller
 */
@RestController
@RequestMapping("/business/sendJob")
public class SendJobController extends BaseController {

    @Autowired
    private ISendJobService sendJobService;

    @Autowired
    private ISendJobQueryService sendJobQueryService;

    /**
     * 创建发送任务
     */
    @Log(title = "发送任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult create(@Validated @RequestBody SendJobCreateDTO dto) {
        Integer jobId = sendJobService.createSendJob(dto);
        return success(jobId);
    }

    /**
     * 查询发送任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SendJobQueryDTO query) {
        startPage();  // 使用BaseController的分页方法
        List<BizSendJob> list = sendJobService.selectSendJobList(query);
        return getDataTable(list);
    }

    /**
     * 获取发送任务详情
     */
    @GetMapping("/{jobId}")
    public AjaxResult getDetail(@PathVariable Integer jobId) {
        SendJobDetailVO detail = sendJobService.selectSendJobDetail(jobId);
        return success(detail);
    }

    /**
     * 删除发送任务（逻辑删除）
     */
    @Log(title = "发送任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Integer[] jobIds) {
        return toAjax(sendJobService.deleteSendJobByJobIds(jobIds));
    }

    /**
     * 执行发送任务
     */
    @Log(title = "发送任务", businessType = BusinessType.UPDATE)
    @PostMapping("/{jobId}/execute")
    public AjaxResult execute(@PathVariable Integer jobId) {
        return toAjax(sendJobService.executeSendJob(jobId));
    }

    /**
     * 查询最近一次成功发送（用于界面显示上次发送时间 + 推荐上次值）
     * @param groupType DRIVE / FURNACE
     */
    @GetMapping("/lastSuccess")
    public AjaxResult lastSuccess(@RequestParam String groupType) {
        SendJobLastSuccessVO vo = sendJobQueryService.getLastSuccess(groupType);
        return AjaxResult.success(vo);
    }

    /**
     * 获取上次炉火写入的钢卷信息
     * @return 上次写入的钢卷信息（coilId、planId、sendTime）
     */
    @GetMapping("/furnace/lastCoilInfo")
    public AjaxResult getLastFurnaceSendCoilInfo() {
        try {
            String jsonValue = RedisUtil.getValue("furnace:send:coil:info");
            if (jsonValue == null || jsonValue.isEmpty()) {
                return AjaxResult.success(null);
            }

            ObjectMapper objectMapper = new ObjectMapper();
            FurnaceSendCoilInfoVO coilInfo = objectMapper.readValue(jsonValue, FurnaceSendCoilInfoVO.class);
            return AjaxResult.success(coilInfo);
        } catch (Exception e) {
            return AjaxResult.error("获取上次炉火写入钢卷信息失败: " + e.getMessage());
        }
    }
}
