package ink.wgink.module.activiti.controller.route;

import ink.wgink.interfaces.consts.ISystemConstant;
import io.swagger.annotations.Api;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName: ActivitiRouteController
 * @Description: activiti路由
 * @Author: wanggeng
 * @Date: 2021/9/1 11:09 上午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.ROUTE_TAGS_PREFIX + "activiti路由")
@Controller
@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/activiti")
public class ActivitiRouteController {

    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("activiti/list");
        mv.addObject("modelCategories", listCategory());
        return mv;
    }

    @GetMapping("save")
    public ModelAndView save() {
        ModelAndView mv = new ModelAndView("activiti/save");
        mv.addObject("modelCategories", listCategory());
        return mv;
    }

    @GetMapping("update")
    public ModelAndView update() {
        ModelAndView mv = new ModelAndView("activiti/update");
        return mv;
    }

    /**
     * 模型列表
     *
     * @return
     */
    private List<String> listCategory() {
        Set<String> modelCategorySet = repositoryService.createModelQuery().list().stream().map(Model::getCategory).collect(Collectors.toSet());
        modelCategorySet.removeIf(modelCategory -> StringUtils.isBlank(modelCategory));
        ArrayList<String> modelCategories = new ArrayList<>(modelCategorySet);
        Collections.sort(modelCategories);
        return modelCategories;
    }

}
