ApproveService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constant\ApplyConstant;
  4. use app\common\constant\CommonConstant;
  5. use app\common\model\Approve;
  6. use app\common\model\ApproveApplyGoods;
  7. use app\common\model\ApproveEvectionPeerUser;
  8. use app\common\model\ApproveInfo;
  9. use app\common\model\ApproveInfoLog;
  10. use app\common\model\ApproveMaintain;
  11. use app\common\model\ApproveStockGoods;
  12. use app\common\model\ApproveUseGoods;
  13. use app\common\model\User;
  14. use think\Db;
  15. use think\Exception;
  16. /**
  17. * 审批服务类
  18. */
  19. class ApproveService
  20. {
  21. /**
  22. * 审批待处理统计
  23. *
  24. * @param mixed $user 用户信息
  25. **/
  26. public static function get_count(User $user)
  27. {
  28. $get_module_list = CommonConstant::get_module_list();
  29. $modules = array_keys($get_module_list);
  30. $userid = $user['userid'];
  31. $ids = Approve::where('status', CommonConstant::STATUS_2)
  32. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  33. ->where('approve_user', $userid)
  34. ->column('info_id');
  35. $object = [];
  36. if ($ids) {
  37. $list = ApproveInfo::field('module,count(id) as number')
  38. ->where('id', 'in', $ids)
  39. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  40. ->where('module', 'in', $modules)
  41. ->where('status', CommonConstant::STATUS_2)
  42. ->group('module')
  43. ->select()
  44. ->toArray();
  45. $object = $list ? array_column($list, null, 'module') : [];
  46. }
  47. $data = [];
  48. foreach ($get_module_list as $module => $value) {
  49. $module_text = $value;
  50. $number = 0;
  51. if (array_key_exists($module, $object)) {
  52. $number = $object[$module]['number'];
  53. }
  54. $params = compact('module', "module_text", 'number');
  55. $data[] = $params;
  56. }
  57. return $data;
  58. }
  59. /**
  60. * 审批列表
  61. *
  62. * @param integer $status 处理状态
  63. * @param integer $module 模块类型
  64. * @param string $start_time 申请开始时间
  65. * @param string $end_time 申请结束时间
  66. * @param string $search 搜索
  67. * @param integer $offset 起始位置
  68. * @param integer $length 查询数量
  69. * @param mixed $user 用户信息
  70. **/
  71. public static function get_list($status, $module, $start_time, $end_time, $search, $offset, $length, $user)
  72. {
  73. if (!in_array($status, [1, 2, 3])) {
  74. return [];
  75. }
  76. if ($module && !array_key_exists($module, CommonConstant::get_module_list())) {
  77. return [];
  78. }
  79. $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
  80. $userid = $user['userid'];
  81. $aliasName = 'approve.';
  82. $jobName = 'approveInfo.';
  83. $field = 'id as approve_id,info_id,status,create_at';
  84. $joinField = 'module,user_id,apply_user_id,order_no,apply_date,reason,type,desc,start_time,end_time';
  85. $field = CommonService::getAliasField($field, $aliasName);
  86. $joinField = CommonService::getAliasField($joinField, $jobName);
  87. $field = implode(',', array_merge($field, $joinField));
  88. $list = Approve::alias('approve')
  89. ->field($field)
  90. ->when($status, function ($query) use ($aliasName, $status) {
  91. if ($status == 1) {
  92. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_1, CommonConstant::STATUS_2]);
  93. }
  94. if ($status == 2) {
  95. $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
  96. }
  97. })
  98. ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
  99. ->where($aliasName . 'group', CommonConstant::IS_WHO_0)
  100. ->where($aliasName . 'state', CommonConstant::IS_WHO_0)
  101. ->where($aliasName . 'approve_user', $userid)
  102. ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
  103. ->when($module, function ($query) use ($jobName, $module) {
  104. $query->where($jobName . 'module', 'in', $module);
  105. })
  106. ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
  107. $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
  108. })
  109. ->when($userids, function ($query) use ($jobName, $userids) {
  110. $query->where($jobName . 'user_id', 'in', $userids);
  111. })
  112. ->when($search, function ($query) use ($search, $userids) {
  113. if (!$userids) {
  114. if (!is_numeric($search)) {
  115. $query->where('order_no|reason', 'like', '%' . $search . '%');
  116. } else {
  117. $query->where('order_no', 'like', '%' . $search . '%');
  118. }
  119. }
  120. })
  121. ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
  122. if ($status == 1) {
  123. // 待处理(待审批 审批中) 才关联 审批人信息
  124. $list->with([
  125. 'approveInfoUser' => function ($query) {
  126. $query->field('userid,name');
  127. },
  128. 'approveOne' => function ($query) {
  129. $query->field('id,info_id,status,approve_user')
  130. ->where('status', CommonConstant::STATUS_2)
  131. ->where('group', CommonConstant::IS_WHO_0)
  132. ->where('state', CommonConstant::IS_WHO_0)
  133. ->with([
  134. 'user' => function ($query) {
  135. $query->field('userid,name');
  136. }
  137. ]);
  138. }
  139. ]);
  140. } else {
  141. $list->with([
  142. 'approveInfoUser' => function ($query) {
  143. $query->field('userid,name');
  144. }
  145. ]);
  146. }
  147. $list = $list->order('approve_id desc')
  148. ->limit($offset, $length)
  149. ->select();
  150. return self::get_array($list);
  151. }
  152. public static function get_array($list)
  153. {
  154. $apply_goods_info_ids = [];
  155. $stock_goods_info_ids = [];
  156. $use_goods_info_ids = [];
  157. $peer_user_info_ids = [];
  158. $apply_goods_list = [];
  159. $stock_goods_list = [];
  160. $use_goods_list = [];
  161. $peer_user_list = [];
  162. foreach ($list as $value) {
  163. $module = $value['module'];
  164. switch ($module) {
  165. case CommonConstant::MODULE_1:
  166. if ($value['type'] == ApplyConstant::TYPE_1) {
  167. $apply_goods_info_ids[] = $value['info_id'];
  168. }
  169. break;
  170. case CommonConstant::MODULE_3:
  171. $stock_goods_info_ids[] = $value['info_id'];
  172. break;
  173. case CommonConstant::MODULE_4:
  174. $use_goods_info_ids[] = $value['info_id'];
  175. break;
  176. case CommonConstant::MODULE_5:
  177. $peer_user_info_ids[] = $value['info_id'];
  178. break;
  179. }
  180. }
  181. if ($apply_goods_info_ids) {
  182. $apply_goods_list = ApproveApplyGoods::field('id,info_id,goods_name')
  183. ->where('info_id', 'in', $apply_goods_info_ids)
  184. ->select()
  185. ->toArray();
  186. }
  187. if ($stock_goods_info_ids) {
  188. $stock_goods_list = ApproveStockGoods::field('id,info_id,goods_name')
  189. ->where('info_id', 'in', $stock_goods_info_ids)
  190. ->select()
  191. ->toArray();
  192. }
  193. if ($use_goods_info_ids) {
  194. $use_goods_list = ApproveUseGoods::field('id,info_id,goods_name')
  195. ->where('info_id', 'in', $use_goods_info_ids)
  196. ->select()
  197. ->toArray();
  198. }
  199. if ($peer_user_info_ids) {
  200. $peer_user_list = ApproveEvectionPeerUser::field('id,info_id,name')
  201. ->where('info_id', 'in', $peer_user_info_ids)
  202. ->select()
  203. ->toArray();
  204. }
  205. foreach ($list as $value) {
  206. $module = $value['module'];
  207. $apply_goods = [];
  208. $stock_goods = [];
  209. $use_goods = [];
  210. $peer_user = [];
  211. switch ($module) {
  212. case CommonConstant::MODULE_1:
  213. if ($value['type'] == ApplyConstant::TYPE_1) {
  214. foreach ($apply_goods_list as $val) {
  215. if ($value['info_id'] == $val['info_id']) {
  216. $apply_goods[] = $val;
  217. }
  218. }
  219. }
  220. break;
  221. case CommonConstant::MODULE_3:
  222. foreach ($stock_goods_list as $val) {
  223. if ($value['info_id'] == $val['info_id']) {
  224. $stock_goods[] = $val;
  225. }
  226. }
  227. break;
  228. case CommonConstant::MODULE_4:
  229. foreach ($use_goods_list as $val) {
  230. if ($value['info_id'] == $val['info_id']) {
  231. $use_goods[] = $val;
  232. }
  233. }
  234. break;
  235. case CommonConstant::MODULE_5:
  236. foreach ($peer_user_list as $val) {
  237. if ($value['info_id'] == $val['info_id']) {
  238. $peer_user[] = $val;
  239. }
  240. }
  241. break;
  242. }
  243. $value['apply_goods'] = $apply_goods;
  244. $value['stock_goods'] = $stock_goods;
  245. $value['use_goods'] = $use_goods;
  246. $value['peer_user'] = $peer_user;
  247. }
  248. return $list;
  249. // $info_ids = [];
  250. // foreach ($list as $value){
  251. // if($value['status'] == CommonConstant::STATUS_1) {
  252. // $info_ids[] = $value['info_id'];
  253. // }
  254. // }
  255. // if($info_ids){
  256. // $approve_one_list = Approve::field('id,info_id,status,approve_user')
  257. // ->where('info_id','in',$info_ids)
  258. // ->where('status',CommonConstant::STATUS_2)
  259. // ->where('is_deleted', CommonConstant::IS_DELETED_0)
  260. // ->with(['user' => function ($query) {
  261. // $query->field('userid,name');
  262. // }])
  263. // ->select();
  264. // $approve_one_data = array_column($approve_one_list, null, 'info_id');
  265. // foreach ($list as $val){
  266. // if(array_key_exists($val['info_id'],$approve_one_data)){
  267. // $val['approve_one'] = $approve_one_data[$val['info_id']];
  268. // }
  269. // }
  270. // }
  271. }
  272. /**
  273. * 详情
  274. *
  275. * @param integer $id 审批ID
  276. * @param mixed $user 用户信息
  277. * @param string $type 类型:detail=详情,info=信息
  278. **/
  279. public static function get_detail($id, $user, $type)
  280. {
  281. $userid = $user['userid'];
  282. $info = Approve::field('id,info_id,status')
  283. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  284. ->where('group', CommonConstant::IS_WHO_0)
  285. ->where('state', CommonConstant::IS_WHO_0)
  286. ->where('approve_user', $userid)
  287. ->find($id);
  288. $data = [];
  289. if ($info) {
  290. $data = ApproveInfoService::get_detail($info['info_id'], $user, CommonConstant::IS_WHO_0, $type);
  291. if ($data) {
  292. $data['approve_id'] = $id;
  293. $data['approve_status'] = $info['status'];
  294. // 条件:维修模块 审批状态是审批中 用户是物业主管或信息负责人 是否显示维修人员:false=不显示,true=显示
  295. // 条件:维修模块 审批状态是审批同意 用户是物业主管或信息负责人 是否已反馈 是否显示上传反馈结果:false=不显示,true=显示
  296. $is_maintain = false;
  297. $is_feedback = false;
  298. if ($data['module'] == CommonConstant::MODULE_8) {
  299. if ($data['approve_status'] == CommonConstant::STATUS_2) {
  300. $is_maintain = CommonService::isMaintain($user);
  301. }
  302. if ($data['approve_status'] == CommonConstant::STATUS_3) {
  303. $is_maintain = CommonService::isMaintain($user);
  304. if ($is_maintain && $data['module_info']['maintain_user_id'] > 0) {
  305. $is_feedback = true;
  306. }
  307. }
  308. }
  309. $data['is_maintain'] = $is_maintain;
  310. $data['is_feedback'] = $is_feedback;
  311. }
  312. }
  313. return $data;
  314. }
  315. /**
  316. * 操作方法
  317. *
  318. * @param integer $id 审批ID
  319. * @param array $params 数组
  320. * @param mixed $user 用户信息
  321. * @param string $type 类型:audit=审批,edit=修改,feedback=上传反馈结果
  322. **/
  323. public static function make($id, $params, $user, $type)
  324. {
  325. $userid = $user['userid'];
  326. $approve = Approve::field('id,info_id,status,approve_user,approve_flow,approve_time')
  327. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  328. ->where('group', CommonConstant::IS_WHO_0)
  329. ->where('state', CommonConstant::IS_WHO_0)
  330. ->where('approve_user', $userid)
  331. ->with([
  332. 'approveInfo' => function ($query) {
  333. $query->field('is_deleted', true);
  334. }
  335. ])
  336. ->find($id);
  337. switch ($type) {
  338. case 'audit':
  339. if (!$approve) {
  340. except('审批记录不存在或已删除');
  341. }
  342. if ($approve->status != CommonConstant::STATUS_2) {
  343. except('非待处理状态无法操作');
  344. }
  345. $info = $approve->approve_info;
  346. if (!$info) {
  347. except('申请记录不存在或已删除');
  348. }
  349. if ($info->status != CommonConstant::STATUS_2) {
  350. except('非待处理状态无法操作!');
  351. }
  352. self::audit($approve, $info, $params, $user);
  353. break;
  354. case 'edit':
  355. if (!$approve) {
  356. except('审批记录不存在或已删除');
  357. }
  358. if ($approve->status != CommonConstant::STATUS_2) {
  359. except('非待处理状态无法操作');
  360. }
  361. $info = $approve->approve_info;
  362. if (!$info) {
  363. except('申请记录不存在或已删除');
  364. }
  365. if ($info->status != CommonConstant::STATUS_2) {
  366. except('非待处理状态无法操作!');
  367. }
  368. $info->module_info;
  369. if (!$info->module_info) {
  370. except(CommonConstant::get_module_list()[$params['module']] . '记录不存在或已删除');
  371. }
  372. self::edit($approve, $info, $params);
  373. break;
  374. case 'feedback':
  375. if (!$approve) {
  376. except('审批记录不存在或已删除');
  377. }
  378. if ($approve->status != CommonConstant::STATUS_3) {
  379. except('非审批同意状态无法操作');
  380. }
  381. $info = $approve->approve_info;
  382. if (!$info) {
  383. except('申请记录不存在或已删除');
  384. }
  385. if ($info->status != CommonConstant::STATUS_3) {
  386. except('非审批同意状态无法操作!');
  387. }
  388. if ($info->module != CommonConstant::MODULE_8) {
  389. except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
  390. }
  391. $is_maintain = CommonService::isMaintain($user);
  392. if(!$is_maintain){
  393. except('您没有权限操作');
  394. }
  395. self::feedback($approve, $info, $params);
  396. break;
  397. }
  398. return true;
  399. }
  400. /**
  401. * 审批
  402. *
  403. * @param mixed $approve 审批信息
  404. * @param mixed $info 申请信息
  405. * @param array $params 数组
  406. * @param mixed $user 用户信息
  407. **/
  408. public static function audit($approve, $info, $params, $user)
  409. {
  410. $status = $params['status'];
  411. $remark = $params['remark'];
  412. $maintain_user_id = $params['maintain_user_id'];
  413. $approve_time = date('Y-m-d H:i:s');
  414. $duration = time() - strtotime($approve['approve_time']);
  415. if ($status == CommonConstant::STATUS_3) {
  416. if ($info->module == CommonConstant::MODULE_3) {
  417. // 入库商品列表 TODO
  418. $stock_goods = $info->stock_goods;
  419. if (!$stock_goods->toArray()) {
  420. except('入库商品不存在');
  421. }
  422. }
  423. if ($info->module == CommonConstant::MODULE_4) {
  424. // 领用商品列表 TODO
  425. $use_goods = $info->use_goods;
  426. if (!$use_goods->toArray()) {
  427. except('领用商品不存在');
  428. }
  429. }
  430. if ($info->module == CommonConstant::MODULE_8) {
  431. // 维修
  432. $is_maintain = CommonService::isMaintain($user);
  433. if ($is_maintain && !$maintain_user_id) {
  434. except('请选择维修人员');
  435. }
  436. }
  437. }
  438. Db::startTrans();
  439. try {
  440. // 更新审批申请
  441. if ($status == CommonConstant::STATUS_3) {
  442. // 审批通过
  443. // 审批流程批次小于审批次数 更新下一级审批状态
  444. if ($approve['approve_flow'] < $info['approve_num']) {
  445. Approve::where(['info_id' => $approve['info_id'], 'approve_flow' => $approve['approve_flow'] + 1])->update(['status' => CommonConstant::STATUS_2, 'approve_time' => $approve_time]);
  446. } else {
  447. $info->status = $status;
  448. }
  449. }
  450. if ($status == CommonConstant::STATUS_4) {
  451. // 审批驳回
  452. // 全部改为历史记录
  453. Approve::where('info_id', $approve['info_id'])->update(['state' => CommonConstant::IS_WHO_1]);
  454. $info->status = $status;
  455. }
  456. if ($maintain_user_id > 0) {
  457. $info->maintain_user_id = $maintain_user_id;
  458. }
  459. $info->cur_num = $info['cur_num'] + 1;
  460. $info->save();
  461. // 更新审批
  462. $approve->status = $status;
  463. $approve->approve_time = $approve_time;
  464. $approve->remark = $remark;
  465. $approve->time = $duration;
  466. $approve->time_text = get_stay_time($duration);
  467. $approve->save();
  468. Db::commit();
  469. } catch (Exception $e) {
  470. except('出现错误:' . $e->getMessage(), 2, $e);
  471. }
  472. return true;
  473. }
  474. /**
  475. * 修改
  476. *
  477. * @param mixed $approve 审批信息
  478. * @param mixed $info 申请信息
  479. * @param array $params 数组
  480. **/
  481. public static function edit($approve, $info, $params)
  482. {
  483. Db::startTrans();
  484. try {
  485. // 编辑对应模块
  486. $result = ApproveInfoService::create_module($params['module'], $params, $info->id, $info, 'update');
  487. $data = [
  488. 'reason' => $params['reason'],
  489. 'type' => $params['type'],
  490. 'desc' => $params['desc'],
  491. 'start_time' => $params['start_time'],
  492. 'end_time' => $params['end_time'],
  493. 'apply_id' => $params['apply_id'],
  494. ];
  495. // 编辑审批申请
  496. $info->save($data);
  497. // 保存修改记录
  498. self::create_approve_info_log($approve, $info, $result);
  499. Db::commit();
  500. } catch (Exception $e) {
  501. except('出现错误:' . $e->getMessage(), 2, $e);
  502. }
  503. return true;
  504. }
  505. /**
  506. * 保存修改记录
  507. *
  508. * @param mixed $approve 审批信息
  509. * @param mixed $info 申请信息
  510. * @param array $params 数组
  511. **/
  512. public static function create_approve_info_log($approve, $info, $params)
  513. {
  514. $info['module_info'] = $params['module_data'];
  515. $info['other'] = $params['other'] ? $params['other']['data'] : [];
  516. $data = [
  517. 'info_id' => $info->id,
  518. 'user_id' => $approve->approve_user,
  519. 'data' => json_encode($info, JSON_UNESCAPED_UNICODE),
  520. ];
  521. ApproveInfoLog::create($data);
  522. return true;
  523. }
  524. /**
  525. * 上传反馈结果
  526. *
  527. * @param mixed $approve 审批信息
  528. * @param mixed $info 申请信息
  529. * @param array $params 数组
  530. **/
  531. public static function feedback($approve, $info, $params)
  532. {
  533. try {
  534. $data = [
  535. 'maintain_user_id' => $info->maintain_user_id,
  536. 'feedback' => $params['feedback'],
  537. 'feedback_images' => $params['feedback_images'],
  538. ];
  539. // 更新维修信息
  540. ApproveMaintain::where('info_id', $info->id)->update($data);
  541. } catch (Exception $e) {
  542. except('出现错误:' . $e->getMessage());
  543. }
  544. return true;
  545. }
  546. }