ActionProcess.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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,is_master')
  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' => '','is_master'=>APModel::$is_master_no], '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. 'is_master|是否主节点' => 'require|number|in:' . APModel::$is_master_yes . ',' . APModel::$is_master_no
  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. //获取流程
  72. $process = PModel::field('id,process_name,process_type')
  73. ->where(['id' => $param['process_id'], 'is_del' => PModel::$is_del_normal])
  74. ->findOrEmpty()
  75. ->toArray();
  76. if (empty($process)) return error_show(1005, '您所选的流程不存在');
  77. $uid = $this->uid;
  78. $uname = $this->uname;
  79. $date = date('Y-m-d H:i:s');
  80. return APModel::create(array_merge($param, [
  81. 'order_type' => $process['process_type'],
  82. 'order_name' => $process['process_name'],
  83. 'status' => APModel::$status_disable,
  84. 'is_del' => APModel::$is_del_normal,
  85. 'createrid' => $uid,
  86. 'creater' => $uname,
  87. 'addtime' => $date,
  88. 'updaterid' => $uid,
  89. 'updater' => $uname,
  90. 'updatetime' => $date,
  91. ]))->save() ? app_show(0, '新增流程节点成功') : error_show(1005, '新增流程节点失败');
  92. }
  93. //读取
  94. public function read()
  95. {
  96. $id = $this->request->filter('trim')->post('id/d', 0);
  97. $res = APModel::field(true)
  98. ->where(['id' => $id, 'is_del' => APModel::$is_del_normal])
  99. ->withAttr('next_action_ids', function ($val) {
  100. return explode(',', $val);
  101. })
  102. ->findOrEmpty()
  103. ->toArray();
  104. return app_show(0, '请求成功', $res);
  105. }
  106. //修改
  107. public function update()
  108. {
  109. $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','is_master'], 'post');
  110. $val = Validate::rule([
  111. 'token' => 'require',
  112. 'id|ID' => 'require|number|gt:0',
  113. 'process_id' => 'number|gt:0',
  114. 'action_type|节点类型' => 'number|between:' . APModel::$action_type_start . ',' . APModel::$action_type_end,
  115. 'operation_type|操作类型' => 'number|in:' . APModel::$operation_type_approval . ',' . APModel::$operation_type_system,
  116. 'status_name|节点名称' => 'max:255',
  117. 'order_process|节点值' => 'number|egt:0|checkOrderProcess:',
  118. 'next_action_ids|下一节点' => 'array|requireIf:action_type,' . APModel::$action_type_start . '|requireIf:action_type,' . APModel::$action_type_process,
  119. 'is_del|是否删除' => 'eq:' . APModel::$is_deleted,
  120. 'status|状态' => 'in:' . APModel::$status_disable . ',' . APModel::$status_normal,
  121. 'is_master|是否主节点' => 'number|in:' . APModel::$is_master_yes . ',' . APModel::$is_master_no
  122. ]);
  123. $val->extend('checkOrderProcess', function ($val, $rule, $data) {
  124. 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 : '同一个流程编码下该节点值已存在';
  125. else return true;
  126. });
  127. if (!$val->check($param)) return error_show(1005, $val->getError());
  128. $info = APModel::where(['id' => $param['id'], 'is_del' => APModel::$is_del_normal])
  129. ->field('id,process_id')
  130. ->findOrEmpty();
  131. if ($info->isEmpty()) return error_show(1005, '该节点记录不存在');
  132. if (isset($param['status']) && $param['status'] == APModel::$status_disable) {
  133. //禁用节点时,要校验其所属流程是否被禁用
  134. $p_info = PModel::field('id')
  135. ->where(['id' => $info->process_id, 'is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  136. ->findOrEmpty()
  137. ->isEmpty();
  138. if (!$p_info) return error_show(1005, '该节点所属流程尚未禁用');
  139. }
  140. $uid = $this->uid;
  141. $uname = $this->uname;
  142. $date = date('Y-m-d H:i:s');
  143. if (isset($param['next_action_ids']) && is_array($param['next_action_ids'])) $param['next_action_ids'] = implode(',', $param['next_action_ids']);
  144. 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, '修改流程节点失败');
  145. }
  146. //获取所有流程的所有节点
  147. public function getAll()
  148. {
  149. $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  150. ->order(['weight' => 'desc', 'id' => 'desc'])
  151. ->column('id,process_name,process_type', 'id');
  152. $action = APModel::where(['is_del' => APModel::$is_del_normal, 'status' => APModel::$status_normal, 'operation_type' => APModel::$operation_type_approval])
  153. ->field('id,process_id,order_process,status_name')
  154. ->cursor();
  155. foreach ($action as $item) {
  156. $data[$item->process_id]['child'][] = $item->toArray();
  157. }
  158. return app_show(0, '请求成功', array_column($data, null, null));
  159. }
  160. }