package org.jeecg.modules.activiti.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.activiti.engine.repository.Model;
import org.jeecg.modules.activiti.model.entity.ReModelEntity;
import org.jeecg.modules.activiti.service.ReModelService;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 模型管理
 *
 * @author yao
 * @date 2019/06/13
 */
@RestController
@RequestMapping("/act/remodel")
public class ReModelController {

    @Autowired
    private ReModelService actReModelService;

    /**
     * 分页查询
     *
     * @param req
     *            查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    public Result list(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                       HttpServletRequest req) {
        IPage page = actReModelService.queryPage( pageNo, pageSize, req);

        return Result.OK(page);
    }

    /**
     * 新增
     *
     * @param actReModel
     *            actReModel
     * @return RestResponse
     */
    @PostMapping("/save")
    public Result save(@RequestBody  ReModelEntity actReModel,HttpServletRequest req) {
        String modelId = "";
        try {
            String tenantId = req.getHeader("tenant-id");
            actReModel.setTenantId(tenantId);
            Model model = actReModelService.add(actReModel);
            modelId = model.getId();
        } catch (Exception e) {
            Result.error(e.getMessage());
        }
        return Result.OK(modelId);
    }

    /**
     * 根据Model部署流程
     *
     * @param id
     *            标识
     * @return RestResponse
     */
    @RequestMapping("/deploy")
    public Result deploy(String id,HttpServletRequest request) {

        return actReModelService.deploy(id,request);
    }

    /**
     * 导出model的xml文件
     *
     * @param id
     *            model标识
     * @param response
     *            响应
     */
    @GetMapping(value = "export")
    public void export(String id, HttpServletResponse response) {
        actReModelService.export(id, response);
    }

    /**
     * 根据主键批量删除
     * @param ids
     * @return RestResponse
     */
    @RequestMapping("/deleteBatchByIds")
    public Result deleteBatchByIds(@RequestParam("ids[]") String[] ids) {
        actReModelService.deleteBatch(ids);
        return Result.OK();
    }

    /**
     * 根据主键删除
     * @param id
     * @return RestResponse
     */
    @RequestMapping("/deleteById")
    public Result deleteById(@RequestParam("id") String id) {
        actReModelService.delete(id);
        return Result.OK();
    }

}
