package cn.palmte.work.controller.backend;


import cn.palmte.work.bean.ResponseMsg;
import cn.palmte.work.service.ActProcDefService;
import cn.palmte.work.utils.ActUtil;
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.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 流程定义
 */
@Controller
@RequestMapping("/actProcDef")
public class ActProcDefController extends BaseController {

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

    @Autowired
    private ActProcDefService actProcDefService;

    @Autowired
    private ActUtil actUtil;

    @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", actProcDefService.list(searchInfo, pageNumber, pageSize));
        return "/admin/act_proc_def_list";
    }


    /**
     * 查看默认的未标红的流程图片
     *
     * @param response
     * @param deploymentId
     * @throws Exception
     */
    @RequestMapping("/procDefPng/{deploymentId}")
    public void png(HttpServletResponse response, @PathVariable("deploymentId") String deploymentId) throws Exception {
        actProcDefService.createProcDefPng(response, deploymentId);
    }


    /**
     * 查看流程xml
     *
     * @param deploymentId
     * @return
     */
    @RequestMapping("/xml/{deploymentId}")
    public void xml(HttpServletResponse response, @PathVariable("deploymentId") String deploymentId) throws Exception {
        actUtil.responseXml(response, deploymentId);
    }


    /**
     * 删除流程
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/delete")
    public ResponseMsg delete(@RequestParam("ids") String ids) {
        if ("".equals(ids)) {
            return ResponseMsg.buildFailedMsg("删除失败，无选中项！");
        } else {
            String[] deleteIds = ids.split("#%#");
            for (String id : deleteIds) {
                actProcDefService.deleteDeployment(id);
            }
            return ResponseMsg.buildSuccessMsg("删除成功");
        }
    }


    /**
     * 挂起与激活
     *
     * @param id
     * @param status 1-激活 2-挂起
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/suspend")
    public String suspend(@RequestParam String id, @RequestParam int status) {
        actProcDefService.suspend(id, status);
        return "redirect:/actProcDef/list";
    }


}
