package com.hotent.uc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.controller.BaseController;
import com.hotent.base.feign.BpmModelFeignService;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.MatrixProcRelationManager;
import com.hotent.uc.model.MatrixProcRelation;
import com.hotent.uc.model.MatrixProcRelationWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * <pre>
 * 描述：矩阵流程关系表 控制器类
 * 构建组：x7
 * 作者:zjq
 * 邮箱:zjq@jee-soft.cn
 * 日期:2021-01-16 17:10:08
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@RestController
@RequestMapping(value = "/uc/matrixProcRelation/v1/")
@Api(tags = "matrixProcRelationController")
public class MatrixProcRelationController extends BaseController<MatrixProcRelationManager, MatrixProcRelation> {
    @Resource
    MatrixProcRelationManager matrixProcRelationManager;
    @Resource
    BpmModelFeignService bpmModelFeignService;

    /**
     * 矩阵流程关系表列表(分页条件查询)数据
     *
     * @param queryFilter
     * @return
     * @throws Exception PageJson
     * @throws
     */
    @PostMapping("listJson")
    @ApiOperation(value = "矩阵流程关系表数据列表", httpMethod = "POST", notes = "获取矩阵流程关系表列表")
    public PageList<MatrixProcRelation> list(@ApiParam(name = "queryFilter", value = "查询对象") @RequestBody QueryFilter queryFilter) throws Exception {
        PageList<MatrixProcRelation> res=matrixProcRelationManager.list(queryFilter);
        return res;
    }

    /**
     * 矩阵流程关系表明细页面
     *
     * @param id
     * @return
     * @throws Exception ModelAndView
     */
    @GetMapping(value = "getJson")
    @ApiOperation(value = "矩阵流程关系表数据详情", httpMethod = "GET", notes = "矩阵流程关系表数据详情")
    public MatrixProcRelation get(@ApiParam(name = "id", value = "业务对象主键", required = true) @RequestParam String id) throws Exception {
        return matrixProcRelationManager.get(id);
    }

    /**
     * 新增矩阵流程关系表
     *
     * @param matrixProcRelation
     * @return
     * @throws Exception
     * @throws
     */
    @PostMapping(value = "save")
    @ApiOperation(value = "新增,更新矩阵流程关系表数据", httpMethod = "POST", notes = "新增,更新矩阵流程关系表数据")
    public CommonResult<String> save(@ApiParam(name = "matrixProcRelation", value = "矩阵流程关系表业务对象", required = true) @RequestBody MatrixProcRelation matrixProcRelation) throws Exception {
        String msg = "添加矩阵流程关系表成功";
        if (StringUtil.isEmpty(matrixProcRelation.getId())) {
            matrixProcRelationManager.create(matrixProcRelation);
        } else {
            matrixProcRelationManager.update(matrixProcRelation);
            msg = "更新矩阵流程关系表成功";
        }
        return new CommonResult<String>(msg);
    }

    /**
     * 批量删除矩阵流程关系表记录
     *
     * @param ids
     * @return
     * @throws Exception
     * @throws
     */
    @DeleteMapping(value = "remove")
    @ApiOperation(value = "批量删除矩阵流程关系表记录", httpMethod = "DELETE", notes = "批量删除矩阵流程关系表记录")
    public CommonResult<String> removes(@ApiParam(name = "ids", value = "业务主键数组,多个业务主键之间用逗号分隔", required = true) @RequestParam String... ids) throws Exception {
        matrixProcRelationManager.removeByIds(ids);
        return new CommonResult<String>(true, "批量删除成功");
    }

    @PostMapping(value = "batchHandle")
    @ApiOperation(value = "批量增删矩阵流程关系表数据", httpMethod = "POST", notes = "批量增删矩阵流程关系表数据")
    public CommonResult<String> batchHandle(@ApiParam(name = "relationObjNode", value = "矩阵流程关系新旧数据", required = true) @RequestBody MatrixProcRelationWrapper relationObjNode) throws Exception {
        List<MatrixProcRelation> oldMatrixProcRelations = relationObjNode.getOldMatrixProcRelations();
        List<MatrixProcRelation> newMatrixProcRelations = relationObjNode.getNewMatrixProcRelations();
        matrixProcRelationManager.batchHandle(oldMatrixProcRelations, newMatrixProcRelations);
        String msg = "批量增删矩阵流程关系成功";
        return new CommonResult<String>(msg);
    }

    @PostMapping(value = "removeByProcDefKey")
    @ApiOperation(value = "通过流程定义Key删除矩阵流程关系表记录", httpMethod = "POST", notes = "通过流程定义Key删除矩阵流程关系表记录")
    public CommonResult<String> removeByProcDefKey(@ApiParam(name = "procDefKey", value = "流程定义Key", required = true) @RequestParam String procDefKey) throws Exception {
        QueryWrapper<MatrixProcRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("proc_def_key_", procDefKey);
        matrixProcRelationManager.remove(wrapper);
        return new CommonResult<String>(true, "批量删除成功");
    }
}
