BorrowApp.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. namespace app\admin\controller\borrow;
  3. use app\admin\model\Borrow as BorrowModel;
  4. use app\admin\model\BorrowAccess;
  5. use app\admin\model\BorrowNumber;
  6. use app\common\controller\Backend;
  7. use ba\Random;
  8. use modules\notification\Notification;
  9. use think\facade\Db;
  10. use app\common\controller\SnowflakeId;
  11. use Throwable;
  12. use app\admin\controller\borrow\BorrowTools;
  13. use app\common\library\xmwechat\offiaccount\OaService;
  14. class BorrowApp extends Backend
  15. {
  16. protected array|string $quickSearchField = ['username', 'mobile'];
  17. public function initialize(): void
  18. {
  19. parent::initialize();
  20. $this->model = new BorrowModel;
  21. $this->BorrowAccess = new BorrowAccess();
  22. $this->BorrowNumber = new BorrowNumber();
  23. $this->adminModel = $this->auth->getAdmin();
  24. $this->BorrowTools = new BorrowTools($this->app);
  25. }
  26. public function apply(): void
  27. {
  28. if ($this->request->param('select')) {
  29. $this->select();
  30. }
  31. $type = $this->request->get()['type'];
  32. $user_id = $this->auth->id;
  33. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  34. list($where, $alias, $limit, $order) = $this->queryBuilder();
  35. if($type == 1){
  36. if ($user_group_id == 1 || $user_group_id == 2) {
  37. $res = $this->model
  38. ->alias($alias)
  39. ->where($where)
  40. ->where(function($query) {
  41. $query->where([
  42. ["status", "in", [0,2,6]]
  43. ]);
  44. })
  45. ->order($order)
  46. ->paginate($limit);
  47. } else if ($user_group_id == 3) {
  48. $res = $this->model
  49. ->alias($alias)
  50. ->where($where)
  51. ->where(function($query) {
  52. $query->where([
  53. ["status", "=", 6],
  54. ['purpose', '=', 0]
  55. ]);
  56. })
  57. ->whereOr(function($query) {
  58. $query->where([
  59. ["status", "=", 3],
  60. ['purpose', '=', 0],
  61. ['college_leader_id', '=', $this->auth->id]
  62. ]);
  63. })
  64. ->order($order)
  65. ->paginate($limit);
  66. } else if ($user_group_id == 4) {
  67. $res = $this->model
  68. ->alias($alias)
  69. ->where($where)
  70. ->where(function($query) {
  71. $query->where([
  72. ["status", "=", [0,2,6]],
  73. ['user_id', '=', $user_id]
  74. ]);
  75. })
  76. ->order($order)
  77. ->paginate($limit);
  78. }
  79. }else if($type == 2){
  80. if ($user_group_id == 1 || $user_group_id == 2) {
  81. $res = $this->model
  82. ->alias($alias)
  83. ->where($where)
  84. ->where(function($query) {
  85. $query->where([
  86. ["status", "in", [3,5]]
  87. ]);
  88. })
  89. ->order($order)
  90. ->paginate($limit);
  91. } else if ($user_group_id == 3) {
  92. $res = $this->model
  93. ->alias($alias)
  94. ->where($where)
  95. ->where(function($query) {
  96. $query->where([
  97. ["status", "in", [3,5]],
  98. ['purpose', '=', 0],
  99. ['college_leader_id', '=', $this->auth->id]
  100. ]);
  101. })
  102. ->order($order)
  103. ->paginate($limit);
  104. } else if ($user_group_id == 4) {
  105. $res = $this->model
  106. ->alias($alias)
  107. ->where($where)
  108. ->where(function($query) {
  109. $query->where([
  110. ["status", "=", [3,5]],
  111. ['user_id', '=', $user_id]
  112. ]);
  113. })
  114. ->order($order)
  115. ->paginate($limit);
  116. }
  117. }else if($type == 3){
  118. if ($user_group_id == 1 || $user_group_id == 2) {
  119. $res = $this->model
  120. ->alias($alias)
  121. ->where($where)
  122. ->where(function($query) {
  123. $query->where([
  124. ["status", "in", [1,4,7]]
  125. ]);
  126. })
  127. ->order($order)
  128. ->paginate($limit);
  129. } else if ($user_group_id == 3) {
  130. $res = $this->model
  131. ->alias($alias)
  132. ->where($where)
  133. ->where(function($query) {
  134. $query->where([
  135. ["status", "in", [1,4,7]],
  136. ['purpose', '=', 0],
  137. ['college_leader_id', '=', $this->auth->id]
  138. ]);
  139. })
  140. ->order($order)
  141. ->paginate($limit);
  142. } else if ($user_group_id == 4) {
  143. $res = $this->model
  144. ->alias($alias)
  145. ->where($where)
  146. ->where(function($query) {
  147. $query->where([
  148. ["status", "=", [1,4,7]],
  149. ['user_id', '=', $user_id]
  150. ]);
  151. })
  152. ->order($order)
  153. ->paginate($limit);
  154. }
  155. }
  156. $this->success('', [
  157. 'list' => $res,
  158. 'remark' => get_route_remark(),
  159. ]);
  160. }
  161. public function Useinng(): void
  162. {
  163. if ($this->request->param('select')) {
  164. $this->select();
  165. }
  166. $user_id = $this->auth->id;
  167. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  168. list($where, $alias, $limit, $order) = $this->queryBuilder();
  169. if ($user_group_id == 1 || $user_group_id == 2) {
  170. $res = $this->model
  171. ->where($where)
  172. ->where("status", 'in', [3, 5])
  173. ->order($order)
  174. ->paginate($limit);
  175. } else if ($user_group_id == 3) {
  176. $res = $this->model
  177. ->where($where)
  178. ->where("status", 'in', [3, 5])
  179. ->where('college_leader_id', $user_id)
  180. ->order($order)
  181. ->paginate($limit);
  182. } else if ($user_group_id == 4) {
  183. $res = $this->model
  184. ->where($where)
  185. ->where("status", 'in', [3, 5])
  186. ->where('user_id', $user_id)
  187. ->order($order)
  188. ->paginate($limit);
  189. }
  190. $this->success('', [
  191. 'list' => $res->items(),
  192. 'total' => $res->total(),
  193. 'remark' => get_route_remark(),
  194. ]);
  195. }
  196. public function toEnd(): void
  197. {
  198. if ($this->request->param('select')) {
  199. $this->select();
  200. }
  201. $user_id = $this->auth->id;
  202. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  203. list($where, $alias, $limit, $order) = $this->queryBuilder();
  204. if ($user_group_id == 1 || $user_group_id == 2) {
  205. $res = $this->model
  206. ->where($where)
  207. ->where("status", 'in', [1, 4])
  208. ->order($order)
  209. ->paginate($limit);
  210. } else if ($user_group_id == 3) {
  211. $res = $this->model
  212. ->where($where)
  213. ->where("status", 'in', [1, 4])
  214. ->where('college_leader_id', $user_id)
  215. ->order($order)
  216. ->paginate($limit);
  217. } else if ($user_group_id == 4) {
  218. $res = $this->model
  219. ->where($where)
  220. ->where("status", 'in', [1, 4])
  221. ->where('user_id', $user_id)
  222. ->order($order)
  223. ->paginate($limit);
  224. }
  225. $this->success('', [
  226. 'list' => $res->items(),
  227. 'total' => $res->total(),
  228. 'remark' => get_route_remark(),
  229. ]);
  230. }
  231. public function consoleInformation(): void
  232. {
  233. $user_id = $this->auth->id;
  234. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  235. $res = [
  236. 'damage_num' => Db::name('asset')->where("status", 2)->count(),
  237. 'asset_count' => Db::name('asset')->count(),
  238. 'asset_lent' => Db::name('asset')->where('status', 1)->count(),
  239. 'asset_value' => Db::name('asset')->sum('unit_price'),
  240. 'damage_people' => Db::name('asset')->where("status", 2)->count(),
  241. 'damage_nature' => Db::name('asset')->where("status", 2)->count(),
  242. 'apply' => $this->model->where("status", 0)->count(),
  243. 'useing' => $this->model->where("status", 2)->count(),
  244. 'overdue' => $this->model->where("status", 5)->count(),
  245. ];
  246. $this->success('', $res);
  247. }
  248. public function edit($id = null): void
  249. {
  250. if ($this->request->isPost()) {
  251. $data = $this->request->post()['data'];
  252. $this->BorrowTools->checkRules($data);
  253. $data = $this->BorrowTools->checkText($data);
  254. //获取用户类型
  255. $user_id = $this->auth->id;
  256. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  257. if ($data['status'] == 1 ) {
  258. if(!isset($data['annotation'])){
  259. $this->error("驳回时请填写批注!");
  260. }
  261. }
  262. if ( $data['status'] == 7 ) {
  263. if(!isset($data['college_annotation'])){
  264. $this->error("驳回时请填写批注!");
  265. }
  266. }
  267. if (!(array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '')) {
  268. $this->error("请检查仪器状态");
  269. }
  270. $data = $this->excludeFields($data);
  271. $this->model->startTrans();
  272. $result = false;
  273. //判断是否是科研类借取并判断是否需要领导审核
  274. try {
  275. // 调整仪器的变化
  276. $this->BorrowTools->checkAsset($data);
  277. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  278. if ($open_id !== null) {
  279. $text = [
  280. "openid" => $open_id,
  281. "pagepath" => "pages/login/index",
  282. "data" => [
  283. 'character_string1' => $data['id'],
  284. 'thing5' => $this->auth->nickname,
  285. ]
  286. ];
  287. }
  288. //判断是否是科研类借取并判断是否需要领导审核
  289. if ($data['purpose'] == 0) {
  290. $text['data']['const3'] = '科研借单';
  291. if ($user_group_id !== 3) {
  292. $data['approval_person'] = $this->auth->nickname;
  293. $data['approval_person_id'] = $this->auth->id;
  294. if ($data['status'] == 6) {
  295. $text['data']['const4'] = ['审批通过'];
  296. $data['status'] = 6;
  297. } else if ($data['status'] == 1) {
  298. $text['data']['const4'] = ['已驳回'];
  299. $data['status'] = 1;
  300. }
  301. try {
  302. $result = $this->model->update($data);
  303. $this->model->commit();
  304. } catch (Throwable $e) {
  305. $this->model->rollback();
  306. $this->error($e->getMessage());
  307. }
  308. } else {
  309. $data['college_leader_id'] = $user_id;
  310. $data['college_leader'] = $this->auth->nickname;
  311. if ($data['status'] == '1') {
  312. $text['data']['const4'] = ['已驳回'];
  313. $data['status'] = 7;
  314. } else {
  315. $text['data']['const4'] = ['审批通过'];
  316. }
  317. $result = false;
  318. $this->model->startTrans();
  319. try {
  320. $result = $this->model->update($data);
  321. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  322. // $num = count($data['asset']);
  323. foreach ($data['asset'] as $v) {
  324. for ($i = 0; $i < $v['num']; $i++) {
  325. $this->BorrowAccess->insert(
  326. [
  327. 'asset_name' => $v['model'],
  328. 'origin' => $v['origin'],
  329. 'borrow_id' => $data['id'],
  330. 'status' => -1,
  331. ]
  332. );
  333. }
  334. }
  335. }
  336. $this->model->commit();
  337. } catch (Throwable $e) {
  338. $this->model->rollback();
  339. $this->error($e->getMessage());
  340. }
  341. }
  342. } else {
  343. $data['approval_person'] = $this->auth->nickname;
  344. $data['approval_person_id'] = $this->auth->id;
  345. $text['data']['const3'] = ['教学借单'];
  346. if ($data['status'] == 2) {
  347. $text['data']['const4'] = ['审批通过'];
  348. } else if ($data['status'] == 1) {
  349. $text['data']['const4'] = ['已驳回'];
  350. }
  351. $result = $this->model->update($data);
  352. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  353. foreach ($data['asset'] as $v) {
  354. for ($i = 0; $i < $v['num']; $i++) {
  355. $this->BorrowAccess->insert(
  356. [
  357. 'asset_name' => $v['model'],
  358. 'origin' => $v['origin'],
  359. 'borrow_id' => $data['id'],
  360. 'status' => -1,
  361. ]
  362. );
  363. }
  364. }
  365. }
  366. }
  367. // 提交事务
  368. $this->model->commit();
  369. } catch (Throwable $e) {
  370. // 回滚事务
  371. $this->model->rollback();
  372. $this->error($e->getMessage());
  373. }
  374. if ($result !== false) {
  375. // $this->success(__('Update successful'));
  376. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  377. if ($open_id !== null) {
  378. // halt($text);
  379. $res = self::wxResultMessage($text);
  380. if ($res === true) {
  381. $this->success(__('Update successful'));
  382. } else {
  383. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  384. }
  385. }else{
  386. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  387. }
  388. } else {
  389. $this->error(__('No rows updated'));
  390. }
  391. }
  392. $row = $this->model->find($id);
  393. if (!$row) {
  394. $this->error(__('Record not found'));
  395. }
  396. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
  397. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  398. } else {
  399. $rows = $this->BorrowAccess->field('a.*,s.student_name,a.asset_name as model')->alias('a')->leftjoin('student s', 'a.student_id = s.id')->where('borrow_id', $id)->select();
  400. }
  401. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  402. $row['asset'] = $rows;
  403. $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select();
  404. $this->success('', [
  405. 'row' => $row
  406. ]);
  407. }
  408. public function add(): void
  409. {
  410. if ($this->request->isPost()) {
  411. $data = $this->request->post()['data'];
  412. $this->BorrowTools->checkRules($data);
  413. $data = $this->BorrowTools->checkText($data);
  414. $asset_userArr = [];
  415. $admin_idArr = [];
  416. $open_idArr = [];
  417. //获取消息推送目标人
  418. foreach ($data['asset'] as $k => $v){
  419. $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user');
  420. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  421. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  422. if(!in_array($asset_user,$asset_userArr)){
  423. $asset_userArr[] = $asset_user;
  424. }
  425. if(!in_array($admin_id,$admin_idArr)){
  426. $admin_idArr[] = $admin_id;
  427. }
  428. if(!in_array($open_id,$open_idArr)){
  429. $open_idArr[] = $open_id;
  430. }
  431. }
  432. $data['approval_person_id'] = implode(',',$admin_idArr);
  433. $data['approval_person'] = implode(',',$asset_userArr);
  434. //校验数据
  435. $uniID = new SnowflakeId;
  436. $data['user_id'] = $this->auth->id;
  437. $this->model->startTrans();
  438. //尝试存储
  439. try {
  440. $data['status'] = 0;
  441. $data['encoding'] = $uniID->generateParticle();
  442. if ($open_idArr !== [] && $open_idArr !== null) {
  443. $text = [
  444. "pagepath" => "pages/login/index",
  445. "data" => [
  446. 'character_string7' => $this->model->id,
  447. 'thing4' => $data['username']
  448. ]
  449. ];
  450. }
  451. if ($data['purpose'] == 0) {
  452. $text['data']['const6'] = ['科研借单'];
  453. $data['college_leader'] = Db::name('admin')->where('switch',1)->value('nickname');
  454. } else {
  455. $text['data']['const6'] = ['教学借单'];
  456. }
  457. $result = $this->model->save($data);
  458. $data['accessories']['borrow_id'] = $this->model->id;
  459. Db::name('accessories')->insert($data['accessories']);
  460. $groupAccess = [];
  461. foreach ($data['asset'] as $datum) {
  462. if ($datum['model'] !== '') {
  463. $groupAccess[] = [
  464. 'borrow_id' => $this->model->id,
  465. 'asset_name' => $datum['model'],
  466. 'origin' => $datum['origin'],
  467. 'num' => $datum['num']
  468. ];
  469. }
  470. }
  471. // halt($result);
  472. $this->BorrowNumber->saveAll($groupAccess);
  473. $this->model->commit();
  474. } catch (Throwable $e) {
  475. // 回滚事务
  476. $this->model->rollback();
  477. $this->error($e->getMessage());
  478. }
  479. if ($result !== false) {
  480. if ($open_idArr !== [] && $open_idArr !== null) {
  481. foreach ($open_idArr as $k => $v){
  482. $text['open_id'] = $open_idArr[$k];
  483. if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) {
  484. // halt($text);
  485. $res = self::wxAuditMessage($text);
  486. }
  487. }
  488. if (isset($res)) {
  489. $this->success(__('Update successful'));
  490. } else {
  491. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  492. }
  493. }else{
  494. // halt(2);
  495. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  496. }
  497. } else {
  498. // halt(3);
  499. $this->error(__('No rows were added'));
  500. }
  501. }
  502. else {
  503. $res = $this->model->where('user_id',$this->auth->id)->find();
  504. $this->success('',$res);
  505. }
  506. }
  507. public function editPassword(): void
  508. {
  509. if ($this->request->isPost()) {
  510. $data = $this->request->param()['data'];
  511. if (!$data) {
  512. $this->error(__('Parameter %s can not be empty', ['']));
  513. }
  514. $result = $this->adminModel->resetPassword($this->auth->id, $data['_value']);
  515. if ($result !== false) {
  516. $this->success(__('Update successful'));
  517. } else {
  518. $this->error(__('No rows updated'));
  519. }
  520. }
  521. }
  522. /**
  523. * 仪器预约待审核通知
  524. * @param
  525. * @return void
  526. * @throws \Exception
  527. */
  528. public function wxAuditMessage($res = null): bool
  529. {
  530. $openid = $res["open_id"];
  531. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  532. $data = [
  533. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  534. 'thing4' => ['value' => $res["data"]["thing4"]],
  535. 'const6' => ['value' => $res["data"]["const6"]],
  536. 'time9' => ['value' => date('Y-m-d H:i')],
  537. ];
  538. $url = 'http://weixin.qq.com/download';
  539. $miniprogram = [
  540. 'appid' => 'wx58633590ab59b7a1',
  541. 'pagepath' => ''
  542. ];
  543. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  544. return $result;
  545. }
  546. public function getProtocol(){
  547. $data = Db::name('protocol')->where("switch",1)->value('editor');
  548. if($data){
  549. $this->success('', [
  550. 'data' => $data,
  551. 'data1' => htmlspecialchars_decode($data)
  552. ]);
  553. }else{
  554. $this->error("失败!");
  555. }
  556. }
  557. /**
  558. * 仪器预约审核结果通知
  559. * @param
  560. * @return void
  561. * @throws \Exception
  562. */
  563. public function wxResultMessage($res): bool
  564. {
  565. $openid = $res["openid"];
  566. // halt($res);
  567. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  568. $data = [
  569. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  570. 'const3' => ['value' => [$res["data"]["const3"]]],
  571. 'const4' => ['value' => $res["data"]["const4"]],
  572. 'thing5' => ['value' => $res["data"]["thing5"]],
  573. // 'time9' => ['value' => date('Y-m-d H:i')],
  574. ];
  575. $url = 'http://weixin.qq.com/download';
  576. $miniprogram = [
  577. 'appid' => 'wx58633590ab59b7a1',
  578. 'pagepath' => "pages/login/index"
  579. ];
  580. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  581. return $result;
  582. }
  583. // public function (): void
  584. // {
  585. // if ($this->request->param('select')) {
  586. // $this->select();
  587. // }
  588. // $user_id = $this->auth->id;
  589. // $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id");
  590. // $res = [
  591. // 'damage_num' => Db::name('asset')->where("status",2)->count(),
  592. // 'damage_people' => Db::name('asset')->where("status",2)->where("damage_type",1)->count(),
  593. // 'damage_nature'=> Db::name('asset')->where("status",2)->where("damage_type",0)->count(),
  594. // 'apply'=> $this->model->where("status",0)->count(),
  595. // 'useing'=> $this->model->where("status",2)->count(),
  596. // 'overdue'=> $this->model->where("status",5)->count(),
  597. // ];
  598. // $this->success('', $res);
  599. // }
  600. }