package com.uavps.web.controller.system;

import com.uavps.common.annotation.Log;
import com.uavps.common.core.controller.BaseController;
import com.uavps.common.core.domain.AjaxResult;
import com.uavps.common.core.page.TableDataInfo;
import com.uavps.common.enums.BusinessType;
import com.uavps.common.utils.StringUtils;
import com.uavps.common.utils.poi.ExcelUtil;
import com.uavps.system.domain.UavpsAlgorithmTask;
import com.uavps.system.service.IUavpsAlgorithmTaskFormationService;
import com.uavps.system.service.IUavpsAlgorithmTaskService;
import com.uavps.system.service.IUavpsMultiTargetFormationService;
import com.uavps.system.service.IUavpsUnmannedAerialVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 算法任务Controller
 *
 * @author ruoyi
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/uavps/task")
public class UavpsAlgorithmTaskController extends BaseController {
    @Autowired
    private IUavpsAlgorithmTaskService uavpsAlgorithmTaskService;

    @Autowired
    private IUavpsUnmannedAerialVehicleService uavpsUnmannedAerialVehicleService;

    @Autowired
    private IUavpsMultiTargetFormationService uavpsMultiTargetFormationService;

    @Autowired
    private IUavpsAlgorithmTaskFormationService uavpsAlgorithmTaskFormationService;

    /**
     * 查询算法任务列表
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(UavpsAlgorithmTask uavpsAlgorithmTask) {
        startPage();
        List<UavpsAlgorithmTask> list = uavpsAlgorithmTaskService.selectUavpsAlgorithmTaskList(uavpsAlgorithmTask);
        return getDataTable(list);
    }

    /**
     * 导出算法任务列表
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:export')")
    @Log(title = "算法任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UavpsAlgorithmTask uavpsAlgorithmTask) {
        List<UavpsAlgorithmTask> list = uavpsAlgorithmTaskService.selectUavpsAlgorithmTaskList(uavpsAlgorithmTask);
        ExcelUtil<UavpsAlgorithmTask> util = new ExcelUtil<UavpsAlgorithmTask>(UavpsAlgorithmTask.class);
        util.exportExcel(response, list, "算法任务数据");
    }

    /**
     * 获取算法任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:query')")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id)) {
            UavpsAlgorithmTask task = uavpsAlgorithmTaskService.selectUavpsAlgorithmTaskById(id);
            ajax.put(AjaxResult.DATA_TAG, task);
            ajax.put("formationIds", uavpsAlgorithmTaskFormationService.selectFormationByTaskId(id));
        }
        ajax.put("formations", uavpsMultiTargetFormationService.selectUavpsMultiTargetFormationAll());
        return ajax;
    }

    /**
     * 新增算法任务
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:add')")
    @Log(title = "算法任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UavpsAlgorithmTask uavpsAlgorithmTask) {
        uavpsAlgorithmTask.setCreateBy(getUsername());
        return toAjax(uavpsAlgorithmTaskService.insertUavpsAlgorithmTask(uavpsAlgorithmTask));
    }

    /**
     * 修改算法任务
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:edit')")
    @Log(title = "算法任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UavpsAlgorithmTask uavpsAlgorithmTask) {
        uavpsAlgorithmTask.setUpdateBy(getUsername());
        return toAjax(uavpsAlgorithmTaskService.updateUavpsAlgorithmTask(uavpsAlgorithmTask));
    }

    /**
     * 删除算法任务
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:remove')")
    @Log(title = "算法任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(uavpsAlgorithmTaskService.deleteUavpsAlgorithmTaskByIds(ids));
    }

    @GetMapping("/getUavList")
    public AjaxResult getUavList(HttpServletResponse response) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, uavpsUnmannedAerialVehicleService.selectUavpsUavAll());
        return ajax;
    }

    /**
     * 执行算法任务
     */
    @PreAuthorize("@ss.hasPermi('uavps:task:run')")
    @Log(title = "算法任务", businessType = BusinessType.OTHER)
    @GetMapping("/run/{id}")
    public AjaxResult execute(@PathVariable("id") Long id) {
        uavpsAlgorithmTaskFormationService.selectFormationListByTaskId(id);
        return null;
    }
}
