package org.dromara.wflow.workflow.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.wflow.workflow.bean.vo.ProcessStartParamsVo;
import org.dromara.wflow.workflow.bean.vo.RuansiMissionTaskVo;
import org.dromara.wflow.workflow.service.ProcessInstanceService;
import org.dromara.wflow.workflow.service.RuansiMissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 我的待办列表控制层
 */
@RestController
@RequestMapping("wflow/mission")
public class RuansiMissionCenterController {

    @Autowired
    private RuansiMissionService missionService;


    @Autowired
    private ProcessInstanceService processService;

    /**
     * 获取系统内发起的流程
     *
     * @param pageSize 每页数量
     * @param pageNo   页码
     * @param code     表单流程ID（流程定义KEY）
     * @param finished 流程是否已经结束
     * @return 列表数据
     */
    //默认任务流程ID 对应wlfow-models表的form_id
    //所有的流程内容存储在act_hi_varinst表
    public static final String default_code = "wf67fdbee6c717f47dcf804ac8";
    @GetMapping("mySubmitted")
    @SaIgnore
    public TableDataInfo<RuansiMissionTaskVo> getUserSubmittedList(@RequestParam(defaultValue = "20") Integer pageSize,
                                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(required = false ) String status,
                                                                   @RequestParam(required = false) String title,
                                                                   @RequestParam(required = false) String taskType,
                                                                   @RequestParam(required = false) String deptId,
                                                                   @RequestParam(required = false) String[] startTimes,
                                                                   @RequestParam(required = false) String level) {



        return missionService.getUserSubmittedList(pageSize, pageNum, LoginHelper.getUserId(), default_code,
                 startTimes, taskType, title,status,deptId, level);
    }

    /**
     * 查询流程表单数据及审批的进度步骤
     *
     * @param instanceId 流程实例ID
     * @param nodeId     当前获取流程人员关联的流程节点ID
     * @return 流程进度及表单详情
     */
    @GetMapping("{instanceId}/{nodeId}")
    public Object getProcessFormAndInstanceProgress(@PathVariable String instanceId,
                                                    @PathVariable(required = false) String nodeId) {
        return R.ok(missionService.getInstanceProgress(nodeId, instanceId));
    }

    /**
     * 保存或者编辑审批流程
     *
     * @param defId  流程定义ID
     * @param params 参数
     * @return 操作结果
     */
    @PostMapping("start/{defId}")
    public Object startTheProcess(@PathVariable String defId,
                                  @RequestBody ProcessStartParamsVo params) {
        Long userId = LoginHelper.getUserId();
        if(StringUtils.isNotBlank(params.getInstanceId())){
            //编辑
            Boolean aBoolean = missionService.editProcess(params);
            String msg = aBoolean ? "编辑成功" : "编辑失败";
            return org.dromara.wflow.utils.R.ok(msg);
        }else{
            String instanceId = processService.startProcess(defId, String.valueOf(userId), params);
            String msg = StringUtils.isNotBlank(instanceId) ? "保存成功":"保存失败";
            return org.dromara.wflow.utils.R.ok(msg);
        }

    }

    /**
     * 删除流程实例
     * @return
     */
    @DeleteMapping("{instanceId}")
    public Object deleteProcess(@PathVariable String instanceId) {
        processService.delProcessInstance(instanceId);
        return R.ok("删除成功");
    }



}
