package com.yunzhupaas.engine.controller;

import com.google.common.collect.ImmutableList;
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.UserInfo;
import com.yunzhupaas.base.vo.PageListVO;
import com.yunzhupaas.base.vo.PaginationVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.engine.entity.FlowTaskEntity;
import com.yunzhupaas.engine.model.flowengine.FlowModel;
import com.yunzhupaas.engine.model.flowlaunch.FlowLaunchListVO;
import com.yunzhupaas.engine.model.flowtask.PaginationFlowTask;
import com.yunzhupaas.engine.service.FlowTaskNewService;
import com.yunzhupaas.engine.service.FlowTaskService;
import com.yunzhupaas.engine.util.FlowNature;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.StringUtil;
import com.yunzhupaas.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 流程发起
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2023/09/27
 */
@Tag(name = "流程发起", description = "FlowLaunch")
@RestController
@RequestMapping("/api/workflow1/Engine/FlowLaunch")
public class FlowLaunchController {

    @Autowired
    private FlowTaskService flowTaskService;
    
    @Autowired
    private FlowTaskNewService flowTaskNewService;

    /**
     * 获取流程发起列表
     *
     * @param paginationFlowTask 分页模型
     * @return
     */
    @Operation(summary = "获取流程发起列表(带分页)")
    @GetMapping
    public ActionResult<PageListVO<FlowLaunchListVO>> list(PaginationFlowTask paginationFlowTask) {
        List<FlowTaskEntity> list = flowTaskService.getLaunchList(paginationFlowTask);
        List<FlowLaunchListVO> listVO = JsonUtil.getJsonToList(list, FlowLaunchListVO.class);
        PaginationVO paginationVO = JsonUtil.getJsonToBean(paginationFlowTask, PaginationVO.class);
        return ActionResult.page(listVO, paginationVO);
    }

    /**
     * 删除流程发起
     *
     * @param id 主键
     * @return
     */
    @Operation(summary = "删除流程发起")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult delete(@PathVariable("id") String id) throws WorkFlowException {
        FlowTaskEntity entity = flowTaskService.getInfo(id, FlowTaskEntity::getId,
                FlowTaskEntity::getParentId, FlowTaskEntity::getFlowType, FlowTaskEntity::getFullName,
                FlowTaskEntity::getStatus
        );
        if (entity != null) {
            if (Objects.equals(entity.getFlowType(), 1)) {
                return ActionResult.fail(MsgCode.WF020.get());
            }
            if (!FlowNature.ParentId.equals(entity.getParentId()) && StringUtil.isNotEmpty(entity.getParentId())) {
                return ActionResult.fail(entity.getFullName() + MsgCode.WF021.get());
            }
            if(!Objects.equals(entity.getCreatorUserId(), UserProvider.getLoginUserId())){
                return ActionResult.fail(MsgCode.FA021.get());
            }
            flowTaskService.delete(entity);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }

    /**
     * 待我审核催办
     *
     * @param id 主键
     * @return
     */
    @Operation(summary = "发起催办")
    @PostMapping("/Press/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult press(@PathVariable("id") String id) throws WorkFlowException {
        FlowModel flowModel = new FlowModel();
        UserInfo userInfo = UserProvider.getUser();
        flowModel.setUserInfo(userInfo);
        boolean flag = flowTaskNewService.press(id, flowModel);
        if (flag) {
            return ActionResult.success(MsgCode.WF022.get());
        }
        return ActionResult.fail(MsgCode.WF023.get());
    }

    /**
     * 撤回流程发起
     * 注意：在撤销流程时要保证你的下一节点没有处理这条记录；如已处理则无法撤销流程。
     *
     * @param id        主键
     * @param flowModel 流程模型
     * @return
     */
    @Operation(summary = "撤回流程发起")
    @PutMapping("/{id}/Actions/Withdraw")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "flowModel", description = "流程模型", required = true),
    })
    public ActionResult revoke(@PathVariable("id") String id, @RequestBody FlowModel flowModel) throws WorkFlowException {
        UserInfo userInfo = UserProvider.getUser();
        flowModel.setUserInfo(userInfo);
        flowTaskNewService.revoke(ImmutableList.of(id), flowModel, true);
        return ActionResult.success(MsgCode.WF008.get());
    }
}

