package cn.palmte.work.controller.backend;


import cn.palmte.work.bean.ResponseMsg;
import cn.palmte.work.config.activiti.ActConstant;
import cn.palmte.work.service.ActProcInsService;
import cn.palmte.work.utils.InterfaceUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 流程实列相关
 */
@Controller
@RequestMapping("/actProcIns")
public class ActProcInsController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ActProcInsController.class);

    @Autowired
    private ActProcInsService actProcInsService;

    /**
     * 列表
     *
     * @param keywords
     * @param pageNumber
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "keywords", required = false) String keywords,
                       @RequestParam(value = PAGE_NUMBER, defaultValue = DEFAULT_PAGE_NUMBER) int pageNumber,
                       @RequestParam(value = PAGE_SIZE, defaultValue = DEFAULT_PAGE_SIZE) int pageSize,
                       Map<String, Object> model) {
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo(keywords, model);
        model.put("pager", actProcInsService.list(searchInfo, pageNumber, pageSize));
        return "/admin/act_proc_ins_list";
    }


    /**
     * 删除流程实例
     *
     * @param procInsId
     * @param reason
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/deleteProcessInstance")
    public ResponseMsg deleteProcessInstance(@RequestParam String procInsId, @RequestParam String reason) {
        actProcInsService.deleteProcessInstance(procInsId, reason);
        return ResponseMsg.buildSuccessMsg("撤销成功");
    }


    /**
     * 流程实列图片
     *
     * @param response
     * @param procInstId
     * @throws Exception
     */
    @RequestMapping("/procInsPng/{procInstId}")
    public void png(HttpServletResponse response, @PathVariable("procInstId") String procInstId) throws Exception {
        actProcInsService.createProcInsPng(response, procInstId);
    }

    /**
     * 启动流程
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/startProcIns/{procDefKey}")
    public ResponseMsg startProcessInstance(@PathVariable String procDefKey, @RequestBody String body) {
        JSONObject json = JSON.parseObject(body);
        Map<String, Object> variables = new HashMap<>();
        for (Object key : json.keySet()) {
            variables.put((String)key, json.get(key));
        }

        variables.put(ActConstant.START_PROCESS_USERID, InterfaceUtil.getAdminId());
        String procInsId = null;
        try {
            procInsId = actProcInsService.startProcessInstance(procDefKey, "0", variables);
        } catch (Exception e) {
            logger.error("", e);
            return ResponseMsg.buildFailedMsg("流程启动失败：" + e.getMessage());
        }
        return ResponseMsg.buildSuccessMsg("流程启动成功", procInsId);
    }


}
