package com.xhpc.charging.station.controller;

import com.xhpc.charging.station.service.ITimingTerminalDeactivateService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.poi.ExcelUtil;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.domain.TimingTerminalDeactivate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * @author yuyang
 * @Date 2026-05-08 13:50
 */
@RestController
@RequestMapping("/timingTerminalDeactivate")
public class TimingTerminalDeactivateController extends BaseController {

    @Autowired
    private ITimingTerminalDeactivateService deactivateService;

    /**
     * 查询桩停用调整列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TimingTerminalDeactivate deactivate) {
        startPage();
        List<TimingTerminalDeactivate> list = deactivateService.selectTimingTerminalDeactivateList(deactivate);
        return getDataTable(list);
    }

    /**
     * 导出桩停用调整列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, TimingTerminalDeactivate deactivate) throws IOException {
        List<TimingTerminalDeactivate> list = deactivateService.selectTimingTerminalDeactivateList(deactivate);
        ExcelUtil<TimingTerminalDeactivate> util = new ExcelUtil<>(TimingTerminalDeactivate.class);
        util.exportExcel(response, list, "桩停用调整数据");
    }

    /**
     * 获取桩停用调整详细信息
     */
    @GetMapping("/details")
    public AjaxResult details(Long timingTerminalDeactivateId) {
        return AjaxResult.success(deactivateService.selectTimingTerminalDeactivateById(timingTerminalDeactivateId));
    }

    /**
     * 新增桩停用调整
     */
    @PostMapping("/add")
    public R add(@RequestBody TimingTerminalDeactivate deactivate) {
        return deactivateService.insertTimingTerminalDeactivate(deactivate);
    }

    /**
     * 修改桩停用调整
     */
    @PostMapping("/edit")
    public R edit(@RequestBody TimingTerminalDeactivate deactivate) {
        return deactivateService.updateTimingTerminalDeactivate(deactivate);
    }

    /**
     * 删除桩停用调整
     */
    @GetMapping("/remove")
    public AjaxResult remove(Long timingTerminalDeactivateId) {
        return toAjax(deactivateService.deleteTimingTerminalDeactivateById(timingTerminalDeactivateId));
    }


    /**
     * 每分钟执行一次，检查需要开始调整的任务
     */
    @Scheduled(cron = "0 0/3 * * * ? ")
    public void executeStartAdjustment() {
        try {
            //log.info("开始执行桩停用开始调整定时任务");
            deactivateService.executeStartAdjustment();
            //log.info("桩停用开始调整定时任务执行完成");
        } catch (Exception e) {
           // log.error("桩停用开始调整定时任务执行失败", e);
        }
    }

    /**
     * 每分钟执行一次，检查需要结束调整的任务
     */
    @Scheduled(cron = "0 0/3 * * * ? ")
    public void executeEndAdjustment() {
        try {
            //log.info("开始执行桩停用结束调整定时任务");
            deactivateService.executeEndAdjustment();
           // log.info("桩停用结束调整定时任务执行完成");
        } catch (Exception e) {
           // log.error("桩停用结束调整定时任务执行失败", e);
        }
    }


}
