package com.yunzhupaas.engine.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.UserInfo;
import com.yunzhupaas.base.controller.SuperController;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.engine.entity.FlowTaskEntity;
import com.yunzhupaas.engine.enums.FlowStatusEnum;
import com.yunzhupaas.engine.model.flowengine.FlowModel;
import com.yunzhupaas.engine.service.FlowDynamicService;
import com.yunzhupaas.engine.service.FlowTaskService;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.util.UserProvider;
import com.yunzhupaas.util.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 流程引擎
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2023/09/27
 */
@Tag(name = "流程引擎", description = "FlowTask")
@RestController
@RequestMapping("/api/workflow1/Engine/FlowTask")
public class FlowTaskController extends SuperController<FlowTaskService, FlowTaskEntity> {

    
    @Autowired
    private FlowTaskService flowTaskService;
    @Autowired
    private FlowDynamicService flowDynamicService;

    /**
     * 保存
     *
     * @param flowModel 流程模型
     * @return
     */
    @Operation(summary = "保存")
    @PostMapping
    @Parameters({
            @Parameter(name = "flowModel", description = "流程模型", required = true),
    })
    public ActionResult save(@RequestBody FlowModel flowModel) throws WorkFlowException {
        boolean isApp = !RequestContext.isOrignPc();
        UserInfo userInfo = UserProvider.getUser();
        flowModel.setUserInfo(userInfo);
        flowModel.setSystemId(isApp ? userInfo.getSystemId() : userInfo.getAppSystemId());
        flowDynamicService.batchCreateOrUpdate(flowModel);
        String msg = FlowStatusEnum.save.getMessage().equals(flowModel.getStatus()) ? MsgCode.SU002.get() : MsgCode.SU006.get();
        return ActionResult.success(msg);
    }

    /**
     * 提交
     *
     * @param id        主键
     * @param flowModel 流程模型
     * @return
     */
    @Operation(summary = "提交")
    @PutMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "flowModel", description = "流程模型", required = true),
    })
    public ActionResult submit(@RequestBody FlowModel flowModel, @PathVariable("id") String id) throws WorkFlowException {
        boolean isApp = !RequestContext.isOrignPc();
        UserInfo userInfo = UserProvider.getUser();
        flowModel.setId(id);
        flowModel.setUserInfo(userInfo);
        flowModel.setSystemId(isApp ? userInfo.getSystemId() : userInfo.getAppSystemId());
        FlowTaskEntity infoSubmit = flowTaskService.getInfoSubmit(id,FlowTaskEntity::getCreatorUserId);
        if (infoSubmit != null && !Objects.equals(infoSubmit.getCreatorUserId(), userInfo.getUserId())) {
            return ActionResult.fail(MsgCode.FA021.get());
        }
        flowDynamicService.batchCreateOrUpdate(flowModel);
        String msg = FlowStatusEnum.save.getMessage().equals(flowModel.getStatus()) ? MsgCode.SU002.get() : MsgCode.SU006.get();
        return ActionResult.success(msg);
    }

}

