ActionProcess.php 7.2 KB

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