Process.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. use app\admin\model\Process as ProcessModel;
  8. use app\admin\model\ActionProcess as APModel;
  9. //流程单
  10. class Process extends Base
  11. {
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. }
  16. /**
  17. * @return \think\response\Json|void
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public function list()
  23. {
  24. $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
  25. $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
  26. $count = Db::name("process")->where('is_del', 0)->count();
  27. $total = ceil($count / $size);
  28. $page = $page > $total ? $total : $page;
  29. $list = Db::name("process")->where('is_del', 0)->page($page, $size)->select();
  30. return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
  31. }
  32. /**
  33. * @return \think\response\Json|void
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function info()
  39. {
  40. $id = isset($this->post['id']) && $this->post['id'] != "" ? intval($this->post['id']) : "";
  41. if ($id == "") {
  42. return error_show(1004, "参数id 不能为空");
  43. }
  44. $info = Db::name("process")->where(['id' => $id, "is_del" => 0])->find();
  45. if (empty($info)) {
  46. return error_show(1004, "流程信息未找到");
  47. }
  48. $list = Db::name("action_process")->where(["pid" => $id, "is_del" => 0])->order("weight,id")->select();
  49. $info['item'] = empty($list) ? [] : $list;
  50. return app_show(0, "获取成功", $info);
  51. }
  52. //审核记录
  53. public function process()
  54. {
  55. $param = $this->request->filter('trim')->only(['type', 'orderCode'], 'post');
  56. $val = Validate::rule([
  57. 'type'=>'require',
  58. 'orderCode'=>'require',
  59. ]);
  60. if (!$val->check($param)) return error_show(1004, $val->getError());
  61. $rs = Db::name("process_order")
  62. ->alias('a')
  63. ->leftJoin('action_process b', 'b.order_type=a.order_type AND b.order_process=a.action_process')
  64. ->where(["a.order_type" => $param['type']])
  65. ->order('a.id', 'asc')
  66. ->field('a.id,a.action_uid,a.action_name,a.addtime,a.action_process,a.order_type,a.source,a.level,a.action_process,b.status_name');
  67. if (is_numeric($param['orderCode'])) $rs->where('a.order_id', $param['orderCode']);
  68. else $rs->where('a.order_code', $param['orderCode']);
  69. $node = $rs
  70. ->select()
  71. ->toArray();
  72. $i = 0;
  73. $data = [['status_name' => '创建', 'order_process' => -1]];
  74. foreach ($node as $value) {
  75. $data[$i] = array_merge($data[$i], [
  76. 'action_uid' => $value['action_uid'],
  77. 'action_name' => $value['action_name'],
  78. 'addtime' => $value['addtime'],
  79. 'source' => $value['source'],
  80. 'level' => $value['level'],
  81. ]);
  82. $data[$i + 1] = [
  83. 'status_name' => $value['status_name'],
  84. 'order_process' => $value['action_process']
  85. ];
  86. $i++;
  87. }
  88. return app_show(0, "获取成功", $data);
  89. }
  90. //获取流程列表
  91. public function getList()
  92. {
  93. $param = $this->request->filter('trim')->only(['status' => '', 'process_name' => '', 'creater' => '', 'addtime_start' => '', 'addtime_end' => '', 'page' => 1, 'size' => 15], 'post');
  94. $where = [['is_del', '=', ProcessModel::$is_del_normal]];
  95. if ($param['status'] != '') $where[] = ['status', '=', $param['status']];
  96. if ($param['process_name'] != '') $where[] = ['process_name', 'like', '%' . $param['process_name'] . '%'];
  97. if ($param['creater'] != '') $where[] = ['creater', 'like', '%' . $param['creater'] . '%'];
  98. if ($param['addtime_start'] != '' && $param['addtime_end'] != '') $where[] = ['addtime', 'between', [$param['addtime_start'], $param['addtime_end'] . ' 23:59:59']];
  99. $count = Db::name('process')
  100. ->where($where)
  101. ->count('id');
  102. $list = ProcessModel::field('id,process_name,process_type,status,creater,addtime')
  103. ->where($where)
  104. ->order(['id'=>'desc'])
  105. ->page($param['page'], $param['size'])
  106. ->select()
  107. ->toArray();
  108. return app_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  109. }
  110. //增加流程
  111. public function add()
  112. {
  113. $param = $this->request->filter('trim')->only(['token',
  114. 'process_name',
  115. 'process_type',
  116. 'remark' => ''
  117. ], 'post');
  118. $val = Validate::rule([
  119. 'token' => 'require',
  120. 'process_name|流程名称' => 'require|max:255',
  121. 'process_type|流程值' => 'require|max:255|checkProcessType:',
  122. ]);
  123. $val->extend('checkProcessType', function ($val) {
  124. return ProcessModel::where(['process_type' => $val, 'is_del' => ProcessModel::$is_del_normal])->field('id')->findOrEmpty()->isEmpty() ? true : '该流程值已存在';
  125. });
  126. if (!$val->check($param)) return error_show(1005, $val->getError());
  127. $user = GetUserInfo($param['token']);
  128. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  129. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  130. $date = date('Y-m-d H:i:s');
  131. return ProcessModel::create(array_merge($param, [
  132. 'is_del' => ProcessModel::$is_del_normal,
  133. 'status' => ProcessModel::$status_disable,
  134. 'createrid' => $uid,
  135. 'creater' => $uname,
  136. 'addtime' => $date,
  137. 'updaterid' => $uid,
  138. 'updater' => $uname,
  139. 'updatetime' => $date,
  140. ]))->save() ? app_show(0, '新增流程成功') : error_show(1005, '新增流程失败');
  141. }
  142. //读取
  143. public function read()
  144. {
  145. $id = $this->request->filter('trim')->post('id/d', 0);
  146. $res = ProcessModel::field('id,process_name,process_type')
  147. ->where(['id' => $id, 'is_del' => ProcessModel::$is_del_normal])
  148. ->findOrEmpty()
  149. ->toArray();
  150. return app_show(0, '获取详情成功', $res);
  151. }
  152. //修改流程
  153. public function update()
  154. {
  155. $param = $this->request->filter('trim')->only(['token', 'id', 'process_name', 'process_type', 'status', 'is_del', 'remark'], 'post');
  156. $val = Validate::rule([
  157. 'token' => 'require',
  158. 'id|ID' => 'require|number|gt:0',
  159. 'process_name|流程名称' => 'max:255',
  160. 'process_type|流程值' => 'max:255|checkProcessType:',
  161. 'status|状态' => 'number|in:' . ProcessModel::$status_normal . ',' . ProcessModel::$status_disable,
  162. 'is_del|是否删除' => 'number|eq:' . ProcessModel::$is_deleted,
  163. ]);
  164. $val->extend('checkProcessType', function ($val, $rule, $data) {
  165. return ProcessModel::where(['process_type' => $val, 'is_del' => ProcessModel::$is_del_normal])->where('id', '<>', $data['id'])->field('id')->findOrEmpty()->isEmpty() ? true : '该流程值已存在';
  166. });
  167. if (!$val->check($param)) return error_show(1005, $val->getError());
  168. if (isset($param['status']) && $param['status'] == ProcessModel::$status_normal) {
  169. //启用流程时候,校验该流程下有没有开始节点和结束节点
  170. $ap_info = APModel::where([
  171. 'process_id' => $param['id'],
  172. 'is_del' => APModel::$is_del_normal,
  173. 'status' => APModel::$status_normal
  174. ])->whereIn('action_type', [
  175. APModel::$action_type_start,
  176. APModel::$action_type_end
  177. ])->column('id', 'action_type');
  178. if (count($ap_info) != 2) return error_show(1005, '该流程下缺少开始节点和结束节点');
  179. }
  180. $user = GetUserInfo($param['token']);
  181. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  182. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  183. $date = date('Y-m-d H:i:s');
  184. return ProcessModel::where(['id' => $param['id'], 'is_del' => ProcessModel::$is_del_normal])->strict(false)->save(array_merge($param, [
  185. 'updaterid' => $uid,
  186. 'updater' => $uname,
  187. 'updatetime' => $date,
  188. ])) ? app_show(0, '修改流程成功') : error_show(1005, '修改流程失败');
  189. }
  190. }