package com.playlet.web.controller.system;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.playlet.common.core.domain.Result;
import com.playlet.system.domain.PlayletItem;
import com.playlet.system.service.IPlayletItemService;
import com.playlet.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.playlet.common.annotation.Log;
import com.playlet.common.enums.BusinessType;
import com.playlet.system.domain.PlayletTask;
import com.playlet.system.service.IPlayletTaskService;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.AjaxResult;
import com.playlet.common.utils.poi.ExcelUtil;
import com.playlet.common.core.page.TableDataInfo;

/**
 * @Date: 2024-03-21 23:51
 * @Author: 但星霖
 * @Version: v1.0
 * @Description: 任务Controller
 */
@Controller
@RequestMapping("/system/playlet/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "外部系统*任务接口")
public class PlayletTaskController extends BaseController {
    private String prefix = "system/playlet/task";

    private final IPlayletTaskService playletTaskService;

    private final IPlayletItemService playletItemService;

    private final ISysDictTypeService sysDictTypeService;

    @RequiresPermissions("playlet:task:view")
    @GetMapping()
    public String task() {
        return prefix + "/task";
    }

    /**
     * 查询任务列表
     */
    @RequiresPermissions("playlet:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlayletTask playletTask) {
        startPage();
        List<PlayletTask> list = playletTaskService.selectPlayletTaskList(playletTask);
        list.forEach(model->{
            PlayletItem playletItem = playletItemService.getById(model.getItemId());
            if(playletItem != null){
                model.setItemName(playletItem.getVideoName());
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @RequiresPermissions("playlet:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlayletTask playletTask) {
        List<PlayletTask> list = playletTaskService.selectPlayletTaskList(playletTask);
        ExcelUtil<PlayletTask> util = new ExcelUtil<PlayletTask>(PlayletTask.class);
        return util.exportExcel(list, "任务数据");
    }

    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        modelMap.put("sourceTypes", sysDictTypeService.selectDictDataByType("playlet_source_type"));
        modelMap.put("playletItems", playletItemService.lambdaQuery().list());
        return prefix + "/add";
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("playlet:task:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PlayletTask playletTask) {
        return toAjax(playletTaskService.insertPlayletTask(playletTask));
    }

    @PostMapping("/batchAdd")
    @ResponseBody
    @ApiOperation("外部系统批量创建任务")
    public AjaxResult batchAdd(@RequestBody List<PlayletTask> playletTasks) {
        playletTaskService.saveBatch(playletTasks);
        return AjaxResult.success();
    }

    /**
     * 修改任务
     */
    @RequiresPermissions("playlet:task:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PlayletTask playletTask = playletTaskService.selectPlayletTaskById(id);
        mmap.put("playletTask", playletTask);
        mmap.put("playletItems", playletItemService.lambdaQuery().list());
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("playlet:task:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlayletTask playletTask) {
        return toAjax(playletTaskService.updatePlayletTask(playletTask));
    }

    @PostMapping("/editTask")
    @ResponseBody
    @ApiOperation("外部系统更新任务状态")
    public AjaxResult editTask(@RequestBody PlayletTask playletTask) {
        return toAjax(playletTaskService.updatePlayletTask(playletTask));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("playlet:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(playletTaskService.deletePlayletTaskByIds(ids));
    }

    @PostMapping("/removeTask")
    @ResponseBody
    @ApiOperation("外部系统删除任务")
    public AjaxResult removeTask(@RequestParam(value = "ids") String ids) {
        return toAjax(playletTaskService.deletePlayletTaskByIds(ids));
    }

    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("playlet:task:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(PlayletTask playletTask)
    {
        return toAjax(playletTaskService.updatePlayletTask(playletTask));
    }

}
