Process.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // $process_type = isset($this->post['type']) && $this->post['type'] != "" ? $this->post['type'] : "";
  62. // if ($process_type == "") {
  63. // return error_show(1004, "参数type 不能为空");
  64. // }
  65. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? $this->post['orderCode'] : "";
  66. // if ($orderCode == "") {
  67. // return error_show(1004, "参数orderCode不能为空");
  68. // }
  69. // $list = Db::name("action_process")
  70. // ->where(['order_type' => $param['type'], "is_del" => 0, "status" => 1])
  71. // ->order("weight desc,id desc")
  72. // ->column('*', 'order_process');
  73. //根据所有的状态,查询对应的操作
  74. // $order_process_s = array_column($list, 'order_process');
  75. $rs = Db::name("process_order")
  76. ->alias('a')
  77. ->leftJoin('action_process b', 'b.order_type=a.order_type AND b.order_process=a.action_process')
  78. ->where(["a.order_type" => $param['type']])
  79. ->order('a.id', 'asc')
  80. ->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');
  81. if (is_numeric($param['orderCode'])) $rs->where('a.order_id', $param['orderCode']);
  82. else $rs->where('a.order_code', $param['orderCode']);
  83. $node = $rs
  84. ->select()
  85. ->toArray();
  86. $i = 0;
  87. $data = [['status_name' => '创建', 'order_process' => -1]];
  88. foreach ($node as $value) {
  89. $data[$i] = array_merge($data[$i], [
  90. 'action_uid' => $value['action_uid'],
  91. 'action_name' => $value['action_name'],
  92. 'addtime' => $value['addtime'],
  93. 'source' => $value['source'],
  94. 'level' => $value['level'],
  95. ]);
  96. $data[$i + 1] = [
  97. 'status_name' => $value['status_name'],
  98. 'order_process' => $value['action_process']
  99. ];
  100. // $value['order_process'] = isset($list[$value['action_process']]['order_process']) ? $list[$value['action_process']]['order_process'] : 0;
  101. // $value['order_name'] = isset($list[$value['action_process']]['order_name']) ? $list[$value['action_process']]['order_name'] : '';
  102. // $value['pid'] = isset($list[$value['action_process']]['pid']) ? $list[$value['action_process']]['pid'] : '';
  103. // $value['status_name'] = isset($list[$value['action_process']]['status_name']) ? $list[$value['action_process']]['status_name'] : '';
  104. // $value['roleid'] = isset($list[$value['action_process']]['roleid']) ? $list[$value['action_process']]['roleid'] : '';
  105. // $value['status'] = isset($list[$value['action_process']]['status']) ? $list[$value['action_process']]['status'] : '';
  106. // $value['weight'] = isset($list[$value['action_process']]['weight']) ? $list[$value['action_process']]['weight'] : '';
  107. // $value['is_del'] = isset($list[$value['action_process']]['is_del']) ? $list[$value['action_process']]['is_del'] : '';
  108. $i++;
  109. }
  110. // if ($node) array_unshift($node, ['status_name' => '创建', 'order_process' => -1]);
  111. return app_show(0, "获取成功", $data);
  112. }
  113. //获取流程列表
  114. public function getList()
  115. {
  116. $param = $this->request->filter('trim')->only(['status' => '', 'process_name' => '', 'creater' => '', 'addtime_start' => '', 'addtime_end' => '', 'page' => 1, 'size' => 15], 'post');
  117. $where = [['is_del', '=', ProcessModel::$is_del_normal]];
  118. if ($param['status'] != '') $where[] = ['status', '=', $param['status']];
  119. if ($param['process_name'] != '') $where[] = ['process_name', 'like', '%' . $param['process_name'] . '%'];
  120. if ($param['creater'] != '') $where[] = ['creater', 'like', '%' . $param['creater'] . '%'];
  121. if ($param['addtime_start'] != '' && $param['addtime_end'] != '') $where[] = ['addtime', 'between', [$param['addtime_start'], $param['addtime_end'] . ' 23:59:59']];
  122. $count = Db::name('process')
  123. ->where($where)
  124. ->count('id');
  125. $list = ProcessModel::field('id,process_name,process_type,status,creater,addtime')
  126. ->where($where)
  127. ->order(['weight'=>'desc','id'=>'desc'])
  128. ->page($param['page'], $param['size'])
  129. ->select()
  130. ->toArray();
  131. return app_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  132. }
  133. //增加流程
  134. public function add()
  135. {
  136. $param = $this->request->filter('trim')->only(['token', 'process_name', 'process_type', 'remark' => ''], 'post');
  137. $val = Validate::rule([
  138. 'token' => 'require',
  139. 'process_name|流程名称' => 'require|max:255',
  140. 'process_type|流程值' => 'require|max:255|checkProcessType:',
  141. ]);
  142. $val->extend('checkProcessType', function ($val) {
  143. return ProcessModel::where(['process_type' => $val, 'is_del' => ProcessModel::$is_del_normal])->field('id')->findOrEmpty()->isEmpty() ? true : '该流程值已存在';
  144. });
  145. if (!$val->check($param)) return error_show(1005, $val->getError());
  146. $user = GetUserInfo($param['token']);
  147. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  148. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  149. $date = date('Y-m-d H:i:s');
  150. return ProcessModel::create(array_merge($param, [
  151. 'is_del' => ProcessModel::$is_del_normal,
  152. 'status' => ProcessModel::$status_disable,
  153. 'createrid' => $uid,
  154. 'creater' => $uname,
  155. 'addtime' => $date,
  156. 'updaterid' => $uid,
  157. 'updater' => $uname,
  158. 'updatetime' => $date,
  159. ]))->save() ? app_show(0, '新增流程成功') : error_show(1005, '新增流程失败');
  160. }
  161. //读取
  162. public function read()
  163. {
  164. $id = $this->request->filter('trim')->post('id/d', 0);
  165. $res = ProcessModel::field('id,process_name,process_type')
  166. ->where(['id' => $id, 'is_del' => ProcessModel::$is_del_normal])
  167. ->findOrEmpty()
  168. ->toArray();
  169. return app_show(0, '获取详情成功', $res);
  170. }
  171. //修改流程
  172. public function update()
  173. {
  174. $param = $this->request->filter('trim')->only(['token', 'id', 'process_name', 'process_type', 'status', 'is_del', 'remark'], 'post');
  175. $val = Validate::rule([
  176. 'token' => 'require',
  177. 'id|ID' => 'require|number|gt:0',
  178. 'process_name|流程名称' => 'max:255',
  179. 'process_type|流程值' => 'max:255|checkProcessType:',
  180. 'status|状态' => 'number|in:' . ProcessModel::$status_normal . ',' . ProcessModel::$status_disable,
  181. 'is_del|是否删除' => 'number|eq:' . ProcessModel::$is_deleted,
  182. ]);
  183. $val->extend('checkProcessType', function ($val, $rule, $data) {
  184. return ProcessModel::where(['process_type' => $val, 'is_del' => ProcessModel::$is_del_normal])->where('id', '<>', $data['id'])->field('id')->findOrEmpty()->isEmpty() ? true : '该流程值已存在';
  185. });
  186. if (!$val->check($param)) return error_show(1005, $val->getError());
  187. if (isset($param['status']) && $param['status'] == ProcessModel::$status_normal) {
  188. //启用流程时候,校验该流程下有没有开始节点和结束节点
  189. $ap_info = APModel::where([
  190. 'process_id' => $param['id'],
  191. 'is_del' => APModel::$is_del_normal,
  192. 'status' => APModel::$status_normal
  193. ])->whereIn('action_type', [
  194. APModel::$action_type_start,
  195. APModel::$action_type_end
  196. ])->column('id', 'action_type');
  197. if (count($ap_info) != 2) return error_show(1005, '该流程下缺少开始节点和结束节点');
  198. }
  199. $user = GetUserInfo($param['token']);
  200. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  201. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  202. $date = date('Y-m-d H:i:s');
  203. return ProcessModel::where(['id' => $param['id'], 'is_del' => ProcessModel::$is_del_normal])->strict(false)->save(array_merge($param, [
  204. 'updaterid' => $uid,
  205. 'updater' => $uname,
  206. 'updatetime' => $date,
  207. ])) ? app_show(0, '修改流程成功') : error_show(1005, '修改流程失败');
  208. }
  209. }