ActionProcess.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ProcessWait;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. use app\admin\model\ActionProcess as APModel;
  8. use app\admin\model\Process as PModel;
  9. //流程节点
  10. class ActionProcess extends BaseController
  11. {
  12. //获取流程节点列表
  13. public function getList()
  14. {
  15. $param = $this->request->filter('trim')->only(['process_id', 'action_type' => '', 'operation_type' => '', 'page' => 1, 'size' => 15], 'post');
  16. $val = Validate::rule([
  17. 'process_id|流程id' => 'require|number|gt:0',
  18. 'page|页码' => 'require|number|gt:0',
  19. 'size|每页显示的页码' => 'require|number|gt:0',
  20. ]);
  21. if (!$val->check($param)) return error_show(1005, $val->getError());
  22. $where = [['is_del', '=', APModel::$is_del_normal]];
  23. if ($param['process_id'] != '') $where[] = ['process_id', '=', $param['process_id']];
  24. if ($param['action_type'] != '') $where[] = ['action_type', '=', $param['action_type']];
  25. if ($param['operation_type'] != '') $where[] = ['operation_type', '=', $param['operation_type']];
  26. $count = Db::name('action_process')
  27. ->where($where)
  28. ->count('id');
  29. $list = APModel::field('id,status_name,order_process,status,action_type,operation_type,next_action_ids,creater,addtime,remark')
  30. ->where($where)
  31. ->order('id', 'desc')
  32. ->page($param['page'], $param['size'])
  33. ->select()
  34. ->toArray();
  35. $all_next_ids = implode(',', array_column($list, 'next_action_ids'));
  36. //处理下一个节点的名称
  37. $child = Db::name('action_process')
  38. ->whereIn('id', $all_next_ids)
  39. ->column('status_name', 'id');
  40. foreach ($list as &$item) {
  41. if ($item['next_action_ids']) {
  42. $next_action_ids = explode(',', $item['next_action_ids']);
  43. foreach ($next_action_ids as $next_action_id) {
  44. if (isset($child[$next_action_id])) $item['next_actions'][] = $child[$next_action_id];
  45. }
  46. } else $item['next_actions'] = [];
  47. }
  48. return app_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  49. }
  50. //新增
  51. public function add()
  52. {
  53. $param = $this->request->filter('trim')->only(['token', 'process_id', 'action_type', 'operation_type', 'status_name', 'order_process', 'next_action_ids' => '', 'remark' => ''], 'post');
  54. $val = Validate::rule([
  55. 'token' => 'require',
  56. 'process_id|流程id' => 'require|number|gt:0',
  57. 'action_type|节点类型' => 'require|number|between:' . APModel::$action_type_start . ',' . APModel::$action_type_end,
  58. 'operation_type|操作类型' => 'require|number|in:' . APModel::$operation_type_approval . ',' . APModel::$operation_type_system,
  59. 'status_name|节点名称' => 'require|max:255',
  60. 'order_process|节点值' => 'require|number|egt:0|checkOrderProcess:',
  61. 'next_action_ids|下一节点' => 'array|requireIf:action_type,' . APModel::$action_type_start . '|requireIf:action_type,' . APModel::$action_type_process,
  62. ]);
  63. $val->extend('checkOrderProcess', function ($val, $rule, $data) {
  64. return APModel::where([
  65. 'process_id' => $data['process_id'],
  66. 'order_process' => $val,
  67. 'is_del' => APModel::$is_del_normal
  68. ])->field('id')->findOrEmpty()->isEmpty() ? true : '同一个流程编码下该节点值已存在';
  69. });
  70. if (!$val->check($param)) return error_show(1005, $val->getError());
  71. $user = GetUserInfo($param['token']);
  72. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  73. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  74. $date = date('Y-m-d H:i:s');
  75. return APModel::create(array_merge($param, [
  76. 'status' => APModel::$status_disable,
  77. 'is_del' => APModel::$is_del_normal,
  78. 'createrid' => $uid,
  79. 'creater' => $uname,
  80. 'addtime' => $date,
  81. 'updaterid' => $uid,
  82. 'updater' => $uname,
  83. 'updatetime' => $date,
  84. ]))->save() ? app_show(0, '新增流程节点成功') : error_show(1005, '新增流程节点失败');
  85. }
  86. //读取
  87. public function read()
  88. {
  89. $id = $this->request->filter('trim')->post('id/d', 0);
  90. $res = APModel::field(true)
  91. ->where(['id' => $id, 'is_del' => APModel::$is_del_normal])
  92. ->withAttr('next_action_ids', function ($val) {
  93. return explode(',', $val);
  94. })
  95. ->findOrEmpty()
  96. ->toArray();
  97. return app_show(0, '请求成功', $res);
  98. }
  99. //修改
  100. public function update()
  101. {
  102. $param = $this->request->filter('trim')->only(['token', 'id', 'process_id', 'action_type', 'operation_type', 'status_name', 'order_process', 'next_action_ids', 'remark' => '', 'status', 'is_del'], 'post');
  103. $val = Validate::rule([
  104. 'token' => 'require',
  105. 'id|ID' => 'require|number|gt:0',
  106. 'process_id' => 'number|gt:0',
  107. 'action_type|节点类型' => 'number|between:' . APModel::$action_type_start . ',' . APModel::$action_type_end,
  108. 'operation_type|操作类型' => 'number|in:' . APModel::$operation_type_approval . ',' . APModel::$operation_type_system,
  109. 'status_name|节点名称' => 'max:255',
  110. 'order_process|节点值' => 'number|egt:0|checkOrderProcess:',
  111. 'next_action_ids|下一节点' => 'array|requireIf:action_type,' . APModel::$action_type_start . '|requireIf:action_type,' . APModel::$action_type_process,
  112. 'is_del|是否删除' => 'eq:' . APModel::$is_deleted,
  113. 'status|状态' => 'in:' . APModel::$status_disable . ',' . APModel::$status_normal,
  114. ]);
  115. $val->extend('checkOrderProcess', function ($val, $rule, $data) {
  116. if (isset($val) && isset($data['process_id'])) return APModel::where(['process_id' => $data['process_id'], 'order_process' => $val, 'is_del' => APModel::$is_del_normal])->where('id', '<>', $data['id'])->field('id')->findOrEmpty()->isEmpty() ? true : '同一个流程编码下该节点值已存在';
  117. else return true;
  118. });
  119. if (!$val->check($param)) return error_show(1005, $val->getError());
  120. $info = APModel::where(['id' => $param['id'], 'is_del' => APModel::$is_del_normal])
  121. ->field('id,process_id')
  122. ->findOrEmpty();
  123. if ($info->isEmpty()) return error_show(1005, '该节点记录不存在');
  124. if (isset($param['status']) && $param['status'] == APModel::$status_disable) {
  125. //禁用节点时,要校验其所属流程是否被禁用
  126. $p_info = PModel::field('id')
  127. ->where(['id' => $info->process_id, 'is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  128. ->findOrEmpty()
  129. ->isEmpty();
  130. if (!$p_info) return error_show(1005, '该节点所属流程尚未禁用');
  131. }
  132. $user = GetUserInfo($param['token']);
  133. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  134. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  135. $date = date('Y-m-d H:i:s');
  136. if (isset($param['next_action_ids']) && is_array($param['next_action_ids'])) $param['next_action_ids'] = implode(',', $param['next_action_ids']);
  137. return APModel::where(['id' => $param['id'], 'is_del' => APModel::$is_del_normal])->strict(false)->save(array_merge($param, ['updaterid' => $uid, 'updater' => $uname, 'updatetime' => $date])) ? app_show(0, '修改流程节点成功') : error_show(1005, '修改流程节点失败');
  138. }
  139. //获取所有流程的所有节点
  140. public function getAll()
  141. {
  142. $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  143. ->order(['weight' => 'desc', 'id' => 'desc'])
  144. ->column('id,process_name,process_type', 'id');
  145. $action = APModel::where(['is_del' => APModel::$is_del_normal, 'status' => APModel::$status_normal, 'operation_type' => APModel::$operation_type_approval])
  146. ->field('id,process_id,order_process,status_name')
  147. ->cursor();
  148. foreach ($action as $item) {
  149. $data[$item->process_id]['child'][] = $item->toArray();
  150. }
  151. return app_show(0, '请求成功', array_column($data, null, null));
  152. }
  153. }