ActionProcess.php 8.5 KB

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