package com.izouma.awesomeadmin.web.activiti;

import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.UserInfo;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/activiti/process")
@RequiresAuthentication
public class ProcessController {
    @Autowired
    private IdentityService                identityService;
    @Autowired
    private RepositoryService              repositoryService;
    @Autowired
    private RuntimeService                 runtimeService;
    @Autowired
    private TaskService                    taskService;
    @Autowired
    private ManagementService              managementService;
    @Autowired
    private FormService                    formService;
    @Autowired
    private HistoryService                 historyService;
    @Autowired
    private ProcessEngineConfigurationImpl processEngineConfiguration;

    @RequestMapping(value = "/start", method = RequestMethod.POST)
    @ResponseBody
    public Result startProcess(@RequestParam("processDefinitionId") String processDefinitionId, List<Map<String, String>> properties, String businessKey) {
        Map<String, String> formProperties = new HashMap<>();

        for (Map<String, String> map : properties) {
            formProperties.put(map.get("id"), map.get("value"));
        }
        try {
            identityService.setAuthenticatedUserId(((UserInfo) SecurityUtils.getSubject().getPrincipal()).getId().toString());
            ProcessInstance processInstance = formService.submitStartFormData(processDefinitionId, formProperties);
            return new Result(true, (Object) processInstance.getBusinessKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "失败");
    }


    @RequestMapping(value = "/history", method = RequestMethod.GET)
    @ResponseBody
    public Result list(Date start, Date end) {
        String userId = ((UserInfo) SecurityUtils.getSubject().getPrincipal()).getId().toString();
        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().startedBy(userId);
        if (start != null) {
            query.startedAfter(start);
        }
        if (end != null) {
            query.startedBefore(end);
        }
        return new Result(true, query.list());
    }

    @RequestMapping(value = "/deleteDeployment", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("deploymentId") String deploymentId) {
        try {
            repositoryService.deleteDeployment(deploymentId, true);
            return new Result(true, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "删除失败");
    }

    @RequestMapping(value = "/getStartForm")
    @ResponseBody
    public Result getStartForm(@RequestParam("processDefinitionId") String processDefinitionId) {

        // 根据流程定义ID读取外置表单
        Object startForm = formService.getRenderedStartForm(processDefinitionId);

        return new Result(true, startForm);
    }
}
