ActionProcess.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(['order_type', 'page' => 1, 'size' => 15], 'post');
  14. $val = Validate::rule([
  15. 'order_type|流程编码' => 'require',
  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', 'order_type', 'order_name', 'action_type', 'operation_type', 'status_name', 'order_process', 'next_action_ids' => '', 'remark' => ''], 'post');
  50. $val = Validate::rule([
  51. 'token' => 'require',
  52. 'order_type|流程编码' => 'require|max:255',
  53. 'order_name|流程名称' => 'require|max:255|checkProcess:',
  54. 'action_type|节点类型' => 'require|number|between:' . APModel::$action_type_start . ',' . APModel::$action_type_end,
  55. 'operation_type|操作类型' => 'require|number|in:' . APModel::$operation_type_approval . ',' . APModel::$operation_type_system,
  56. 'status_name|节点名称' => 'require|max:255',
  57. 'order_process|节点值' => 'require|number|egt:0|checkOrderProcess:',
  58. 'next_action_ids|下一节点' => 'array|requireIf:action_type,' . APModel::$action_type_start . '|requireIf:action_type,' . APModel::$action_type_process,
  59. ]);
  60. $val->extend('checkProcess', function ($val, $rule, $data) {
  61. return PModel::where([
  62. 'process_name' => $val,
  63. 'process_type' => $data['order_type'],
  64. 'is_del' => PModel::$is_del_normal
  65. ])->field('id')->findOrEmpty()->isEmpty() ? '流程编码和流程名称不存在' : true;
  66. });
  67. $val->extend('checkOrderProcess', function ($val, $rule, $data) {
  68. return APModel::where([
  69. 'order_type' => $data['order_type'],
  70. 'order_process' => $val,
  71. 'is_del' => APModel::$is_del_normal
  72. ])->field('id')->findOrEmpty()->isEmpty() ? true : '同一个流程编码下该节点值已存在';
  73. });
  74. if (!$val->check($param)) return error_show(1005, $val->getError());
  75. $user = GetUserInfo($param['token']);
  76. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  77. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  78. $date = date('Y-m-d H:i:s');
  79. return APModel::create(array_merge($param, [
  80. 'status' => APModel::$status_normal,
  81. 'is_del' => APModel::$is_del_normal,
  82. 'createrid' => $uid,
  83. 'creater' => $uname,
  84. 'addtime' => $date,
  85. 'updaterid' => $uid,
  86. 'updater' => $uname,
  87. 'updatetime' => $date,
  88. ]))->save() ? app_show(0, '新增流程节点成功') : error_show(1005, '新增流程节点失败');
  89. }
  90. //读取
  91. public function read()
  92. {
  93. $id = $this->request->filter('trim')->post('id/d', 0);
  94. $res = APModel::field(true)
  95. ->where(['id' => $id, 'is_del' => APModel::$is_del_normal])
  96. ->withAttr('next_action_ids', function ($val) {
  97. return explode(',', $val);
  98. })
  99. ->findOrEmpty()
  100. ->toArray();
  101. return app_show(0, '请求成功', $res);
  102. }
  103. //修改
  104. public function update()
  105. {
  106. $param = $this->request->filter('trim')->only(['token', 'id', 'order_type', 'order_name', 'action_type', 'operation_type', 'status_name', 'order_process', 'next_action_ids' => '', 'remark' => '', 'status' => '', 'is_del' => ''], 'post');
  107. $val = Validate::rule([
  108. 'token' => 'require',
  109. 'id|ID' => 'require|number|gt:0',
  110. 'order_type|流程编码' => 'max:255',
  111. 'order_name|流程名称' => 'max:255|checkProcess:',
  112. 'action_type|节点类型' => 'number|between:' . APModel::$action_type_start . ',' . APModel::$action_type_end,
  113. 'operation_type|操作类型' => 'number|in:' . APModel::$operation_type_approval . ',' . APModel::$operation_type_system,
  114. 'status_name|节点名称' => 'max:255',
  115. 'order_process|节点值' => 'number|egt:0|checkOrderProcess:',
  116. 'next_action_ids|下一节点' => 'array|requireIf:action_type,' . APModel::$action_type_start . '|requireIf:action_type,' . APModel::$action_type_process,
  117. 'is_del|是否删除' => 'eq:' . APModel::$is_deleted,
  118. 'status|状态' => 'in:' . APModel::$status_disable . ',' . APModel::$status_normal,
  119. ]);
  120. $val->extend('checkProcess', function ($val, $rule, $data) {
  121. if (isset($val) && isset($data['order_type'])) return PModel::where(['process_name' => $val, 'process_type' => $data['order_type'], 'is_del' => PModel::$is_del_normal])->field('id')->findOrEmpty()->isEmpty() ? '流程编码和流程名称不存在' : true;
  122. else return true;
  123. });
  124. $val->extend('checkOrderProcess', function ($val, $rule, $data) {
  125. 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 : '同一个流程编码下该节点值已存在';
  126. else return true;
  127. });
  128. if (!$val->check($param)) return error_show(1005, $val->getError());
  129. $user = GetUserInfo($param['token']);
  130. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  131. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  132. $date = date('Y-m-d H:i:s');
  133. 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, '修改流程节点失败');
  134. }
  135. //获取所有流程的所有节点
  136. public function getAll()
  137. {
  138. $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  139. ->column('id,process_name,process_type', 'process_type');
  140. $action = APModel::where(['is_del' => APModel::$is_del_normal, 'status' => APModel::$status_normal])
  141. ->field('id,order_type,order_process,status_name')
  142. ->cursor();
  143. foreach ($action as $item) {
  144. $data[$item->order_type]['child'][] = $item->toArray();
  145. }
  146. return app_show(0, '请求成功', array_column($data,null,null));
  147. }
  148. }