package com.ruoyi.model.controller;

import java.util.List;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.AppletTask;
import com.ruoyi.model.service.IAppletTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务中心Controller
 *
 * @author daixiande
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/model/AppletTask")
public class AppletTaskController extends BaseController {
    @Autowired
    private IAppletTaskService appletTaskService;

    /**
     * 查询任务中心列表
     */
    @PreAuthorize("@ss.hasPermi('model:AppletTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppletTask appletTask) {
        startPage();
        List<AppletTask> list = appletTaskService.selectAppletTaskList(appletTask);
        return getDataTable(list);
    }

    /**
     * 导出任务中心列表
     */
    @PreAuthorize("@ss.hasPermi('model:AppletTask:export')")
    @Log(title = "任务中心", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppletTask appletTask) {
        List<AppletTask> list = appletTaskService.selectAppletTaskList(appletTask);
        ExcelUtil<AppletTask> util = new ExcelUtil<AppletTask>(AppletTask.class);
        return util.exportExcel(list, "任务中心数据");
    }

    /**
     * 获取任务中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (id == null || id <= 0) {
            return AjaxResult.error("任务ID不能为空或无效");
        }
        AppletTask task = appletTaskService.selectAppletTaskById(id);
        if (task == null) {
            return AjaxResult.error("任务不存在");
        }
        return AjaxResult.success(task);
    }

    /**
     * 新增任务中心
     */
    @PreAuthorize("@ss.hasPermi('model:AppletTask:add')")
    @Log(title = "任务中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody AppletTask appletTask) {
        // 参数验证
        if (appletTask.getStartTime() != null && appletTask.getEndTime() != null 
            && appletTask.getStartTime().isAfter(appletTask.getEndTime())) {
            return AjaxResult.error("任务开始时间不能晚于结束时间");
        }
        return toAjax(appletTaskService.insertAppletTask(appletTask));
    }

    /**
     * 修改任务中心
     */
    @PreAuthorize("@ss.hasPermi('model:AppletTask:edit')")
    @Log(title = "任务中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody AppletTask appletTask) {
        // 参数验证
        if (appletTask.getId() == null || appletTask.getId() <= 0) {
            return AjaxResult.error("任务ID不能为空或无效");
        }
        if (appletTask.getStartTime() != null && appletTask.getEndTime() != null 
            && appletTask.getStartTime().isAfter(appletTask.getEndTime())) {
            return AjaxResult.error("任务开始时间不能晚于结束时间");
        }
        // 检查任务是否存在
        AppletTask existTask = appletTaskService.selectAppletTaskById(appletTask.getId());
        if (existTask == null) {
            return AjaxResult.error("任务不存在");
        }
        return toAjax(appletTaskService.updateAppletTask(appletTask));
    }

    /**
     * 删除任务中心
     */
    @PreAuthorize("@ss.hasPermi('model:AppletTask:remove')")
    @Log(title = "任务中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请选择要删除的任务");
        }
        // 验证所有ID都有效
        for (Long id : ids) {
            if (id == null || id <= 0) {
                return AjaxResult.error("任务ID不能为空或无效");
            }
        }
        return toAjax(appletTaskService.deleteAppletTaskByIds(ids));
    }
}