package com.yunzhupaas.integrate.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.controller.SuperController;
import com.yunzhupaas.base.vo.PageListVO;
import com.yunzhupaas.base.vo.PaginationVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.integrate.entity.IntegrateNodeEntity;
import com.yunzhupaas.integrate.entity.IntegrateQueueEntity;
import com.yunzhupaas.integrate.entity.IntegrateTaskEntity;
import com.yunzhupaas.integrate.model.integratetask.IntegrateQueueListVO;
import com.yunzhupaas.integrate.model.integratetask.IntegrateTaskInfo;
import com.yunzhupaas.integrate.model.integratetask.IntegrateTaskListVO;
import com.yunzhupaas.integrate.model.integrate.IntegratePageModel;
import com.yunzhupaas.integrate.model.integratetask.IntegrateTaskModel;
import com.yunzhupaas.integrate.service.IntegrateNodeService;
import com.yunzhupaas.integrate.service.IntegrateQueueService;
import com.yunzhupaas.integrate.service.IntegrateTaskService;
import com.yunzhupaas.integrate.util.IntegrateUtil;
import com.yunzhupaas.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "集成助手日志", description = "IntegrateTask")
@RestController
@RequestMapping("/api/visualdev/IntegrateTask")
public class IntegrateTaskController extends SuperController<IntegrateTaskService, IntegrateTaskEntity> {

    @Autowired
    private IntegrateTaskService integrateTaskService;
    @Autowired
    private IntegrateNodeService integrateNodeService;
    @Autowired
    private IntegrateUtil integrateUtil;
    @Autowired
    private IntegrateQueueService integrateQueueService;

    /**
     * 列表
     *
     * @return
     */
    @Operation(summary = "日志列表")
    @GetMapping
    public ActionResult<PageListVO<IntegrateTaskListVO>> list(IntegratePageModel pagination) {
        List<IntegrateTaskEntity> data = integrateTaskService.getList(pagination);
        List<IntegrateTaskListVO> list = JsonUtil.getJsonToList(data, IntegrateTaskListVO.class);
        for (IntegrateTaskListVO taskListVO : list) {
            taskListVO.setIsRetry("0".equals(taskListVO.getParentId()) ? 0 : 1);
        }
        PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(list, paginationVO);
    }

    /**
     * 删除
     *
     * @param id 主键值
     * @return
     */
    @Operation(summary = "删除")
    @Parameters({
            @Parameter(name = "id", description = "主键值", required = true)
    })
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        integrateTaskService.delete(id);
        return ActionResult.success(MsgCode.SU003.get());
    }

    /**
     * 日志列表
     *
     * @return
     */
    @Operation(summary = "执行列表")
    @GetMapping("/queueList")
    public ActionResult<List<IntegrateQueueListVO>> queueList() {
        List<IntegrateQueueEntity> list = integrateQueueService.getList();
        List<IntegrateQueueListVO> listVO = JsonUtil.getJsonToList(list, IntegrateQueueListVO.class);
        return ActionResult.success(listVO);
    }

    /**
     * 日志列表
     *
     * @return
     */
    @Operation(summary = "日志详情")
    @GetMapping("/{id}")
    public ActionResult<IntegrateTaskInfo> list(@PathVariable("id") String id) {
        IntegrateTaskEntity taskEntity = integrateTaskService.getInfo(id);
        List<IntegrateNodeEntity> nodeList = integrateNodeService.getList(new ArrayList() {{
            add(id);
        }}, null);
        List<IntegrateTaskModel> list = JsonUtil.getJsonToList(nodeList, IntegrateTaskModel.class);
        for (IntegrateTaskModel taskModel : list) {
            boolean isType = "0".equals(taskModel.getParentId());
            taskModel.setType(isType ? 1 : 0);
        }
        IntegrateTaskInfo info = new IntegrateTaskInfo();
        info.setList(list);
        info.setData(taskEntity.getData());
        return ActionResult.success(info);
    }

    /**
     * 节点重试
     *
     * @return
     */
    @Operation(summary = "节点重试")
    @GetMapping(value = "/{id}/nodeRetry")
    public ActionResult taskNode(@PathVariable("id") String id, String nodeId) {
        IntegrateTaskEntity taskEntity = integrateTaskService.getInfo(id);
        if (taskEntity != null) {
            integrateUtil.integrate(id, taskEntity.getParentId(), nodeId);
            return ActionResult.success(MsgCode.SU005.get());
        }
        return ActionResult.fail(MsgCode.FA007.get());
    }

    /**
     * 重试
     *
     * @return
     */
    @Operation(summary = "重试")
    @PutMapping(value = "/{id}/retry")
    public ActionResult ImportData(@PathVariable("id") String id) {
        IntegrateTaskEntity taskEntity = integrateTaskService.getInfo(id);
        if (taskEntity != null) {
            integrateUtil.integrate(id, id, "0");
            return ActionResult.success(MsgCode.SU005.get());
        }
        return ActionResult.fail(MsgCode.FA007.get());
    }

}

