BorrowUseing.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. namespace app\admin\controller\borrow;
  3. use app\admin\controller\borrow\BorrowTools;
  4. use app\admin\model\Borrow as BorrowModel;
  5. use app\admin\model\BorrowAccess;
  6. use app\admin\model\BorrowNumber;
  7. use app\common\controller\Backend;
  8. use ba\Random;
  9. use think\facade\Db;
  10. use app\common\controller\SnowflakeId;
  11. use Throwable;
  12. class BorrowUseing extends Backend
  13. {
  14. protected array|string $quickSearchField = ['username', 'mobile'];
  15. public function initialize(): void
  16. {
  17. parent::initialize();
  18. $this->model = new BorrowModel;
  19. $this->BorrowAccess = new BorrowAccess();
  20. $this->BorrowNumber = new BorrowNumber();
  21. $this->BorrowTools = new BorrowTools($this->app);
  22. }
  23. public function index(): void
  24. {
  25. if ($this->request->param('select')) {
  26. $this->select();
  27. }
  28. $user_id = $this->auth->id;
  29. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  30. list($where, $alias, $limit, $order) = $this->queryBuilder();
  31. if ($user_group_id == 1 || $user_group_id == 2) {
  32. $res = $this->model
  33. ->alias($alias)
  34. ->where($where)
  35. ->where("status", 'in', [2, 3, 5])
  36. ->order($order)
  37. ->paginate($limit);
  38. } else if ($user_group_id == 3) {
  39. $res = $this->model
  40. ->alias($alias)
  41. ->where($where)
  42. ->where("status", 'in', [2, 3, 5])
  43. ->where('college_leader_id', $user_id)
  44. ->order($order)
  45. ->paginate($limit);
  46. } else if ($user_group_id == 4) {
  47. $res = $this->model
  48. ->alias($alias)
  49. ->where($where)
  50. ->where("status", 'in', [2, 3, 5])
  51. ->where('user_id', $user_id)
  52. ->order($order)
  53. ->paginate($limit);
  54. }
  55. $this->success('', [
  56. 'list' => $res->items(),
  57. 'total' => $res->total(),
  58. 'remark' => get_route_remark(),
  59. ]);
  60. }
  61. public function add(): void
  62. {
  63. if ($this->request->isPost()) {
  64. $data = $this->request->post();
  65. if (!$data) {
  66. $this->error(__('Parameter %s can not be empty', ['']));
  67. }
  68. if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) {
  69. $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  70. }
  71. if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
  72. $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  73. }
  74. $data = $this->excludeFields($data);
  75. //获取用户类型
  76. $this->model->startTrans();
  77. $result = false;
  78. try {
  79. $uniID = new SnowflakeId;
  80. $data['user_id'] = $this->auth->id;
  81. $data['encoding'] = $uniID->generateParticle();
  82. $result = $this->model->save($data);
  83. // 调整仪器
  84. if (array_key_exists('asset', $data) && !empty($data['asset'])) {
  85. $toInsert = [];
  86. foreach ($data['asset'] as $datum) {
  87. // 数据不存在,准备插入项
  88. $toInsert[] = [
  89. 'borrow_id' => $this->model->id,
  90. 'asset_name' => $datum['model'],
  91. 'asset_id' => $datum['asset_id'],
  92. 'origin' => $datum['origin'],
  93. 'status' => $data['status'],
  94. 'collection_time' => $datum['collection_time'],
  95. 'lend_processor' => $datum['lend_processor'],
  96. 'restitution_processor' => $datum['restitution_processor'],
  97. 'restitution_time' => $datum['restitution_time'],
  98. ];
  99. }
  100. // 插入新增的数据
  101. if (!empty($toInsert)) {
  102. $this->BorrowAccess->insertAll($toInsert);
  103. }
  104. }
  105. if (isset($data['fileList'])) {
  106. $groupImg = [];
  107. foreach ($data['fileList'] as $datum) {
  108. $groupImg[] = [
  109. 'borrow_id' => $this->model->id,
  110. 'url' => $datum['sign'],
  111. ];
  112. }
  113. Db::name('borrow_img')->insertAll($groupImg);
  114. }
  115. // 提交事务
  116. $this->model->commit();
  117. } catch (Throwable $e) {
  118. // 回滚事务
  119. $this->model->rollback();
  120. $this->error($e->getMessage());
  121. }
  122. if ($result !== false) {
  123. $this->success(__('Update successful'));
  124. } else {
  125. $this->error(__('No rows updated'));
  126. }
  127. }
  128. $this->error(__('Parameter error'));
  129. }
  130. public function del($ids = null): void
  131. {
  132. if (!$this->request->isDelete() || !$ids) {
  133. $this->error(__('Parameter error'));
  134. }
  135. $pk = $this->model->getPk();
  136. $data = $this->model->where($pk, 'in', $ids)->select();
  137. $count = 0;
  138. $count = 0;
  139. $this->model->startTrans();
  140. try {
  141. foreach ($data as $v) {
  142. $count += $v->delete();
  143. $this->BorrowAccess->where('borrow_id', 'in', $ids)->select();
  144. Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  145. Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  146. }
  147. $this->model->commit();
  148. } catch (Throwable $e) {
  149. $this->model->rollback();
  150. $this->error($e->getMessage());
  151. }
  152. if ($count) {
  153. $this->success(__('Deleted successfully'));
  154. } else {
  155. $this->error(__('No rows were deleted'));
  156. }
  157. }
  158. public function edit($id = null): void
  159. {
  160. $row = $this->model->find($id);
  161. if (!$row) {
  162. $this->error(__('Record not found'));
  163. }
  164. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
  165. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  166. } else {
  167. $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id', $id)->select();
  168. }
  169. $row['asset'] = $rows;
  170. $row['fileList'] = Db::name('borrow_img')->where('borrow_id', $id)->field('*,url as sign')->select();
  171. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  172. if ($this->request->isPost()) {
  173. $data = $this->request->post();
  174. if (!$data) {
  175. $this->error(__('Parameter %s can not be empty', ['']));
  176. }
  177. if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) {
  178. $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  179. }
  180. if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
  181. $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  182. }
  183. if (!isset($data['asset']) || $data['asset'] == []) {
  184. $this->error("仪器选择不能为空!!");
  185. }
  186. $data = $this->excludeFields($data);
  187. //获取用户类型
  188. $user_id = $this->auth->id;
  189. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  190. $this->model->startTrans();
  191. $result = false;
  192. try {
  193. // 调整仪器的变化
  194. if (array_key_exists('asset', $data) && !empty($data['asset'])) {
  195. // 首先检索当前所有相关记录
  196. $existingRecords = Db::name('borrow_access')
  197. ->where('borrow_id', $data['id'])
  198. ->column('*', 'id');
  199. $toUpdate = [];
  200. $toInsert = [];
  201. foreach ($data['asset'] as $datum) {
  202. if (isset($datum['id'], $existingRecords[$datum['id']])) {
  203. // 对比现有数据与新数据
  204. $current = $existingRecords[$datum['id']];
  205. if ($current['asset_name'] !== $datum['model'] || $current['asset_id'] !== $datum['asset_id']
  206. || $current['student_id'] !== $datum['student_id'] || $current['origin'] !== $datum['origin']
  207. || $current['status'] !== $datum['status'] || $current['collection_time'] !== $datum['collection_time']
  208. || $current['lend_processor'] !== $datum['lend_processor']
  209. || $current['restitution_processor'] !== $datum['restitution_processor'] || $current['restitution_time'] !== $datum['restitution_time']
  210. ) {
  211. // 数据有变化,则准备更新
  212. $toUpdate[] = [
  213. 'id' => $datum['id'],
  214. 'borrow_id' => $data['id'],
  215. 'asset_name' => $datum['model'],
  216. 'asset_id' => $datum['asset_id'],
  217. 'student_id' => $datum['student_id'],
  218. 'origin' => $datum['origin'],
  219. 'status' => $data['status'],
  220. 'collection_time' => $datum['collection_time'],
  221. 'lend_processor' => $datum['lend_processor'],
  222. 'restitution_processor' => $datum['restitution_processor'],
  223. 'restitution_time' => $datum['restitution_time'],
  224. ];
  225. }
  226. // 从现有记录中移除已经处理过的项
  227. unset($existingRecords[$datum['id']]);
  228. } else {
  229. // 数据不存在,准备插入项
  230. $toInsert[] = [
  231. 'borrow_id' => $data['id'],
  232. 'asset_name' => $datum['model'],
  233. 'asset_id' => $datum['asset_id'],
  234. 'student_id' => $datum['student_id'],
  235. 'origin' => $datum['origin'],
  236. 'status' => $data['status'],
  237. 'collection_time' => $datum['collection_time'],
  238. 'lend_processor' => $datum['lend_processor'],
  239. 'restitution_processor' => $datum['restitution_processor'],
  240. 'restitution_time' => $datum['restitution_time'],
  241. ];
  242. }
  243. }
  244. // $existingRecords 中剩下的是需要软删除的
  245. $toDelete = array_keys($existingRecords);
  246. if (!empty($toDelete)) {
  247. Db::name('borrow_number')
  248. ->where('id', 'in', $toDelete)
  249. ->useSoftDelete('delete_time', date('Y-m-d H:i:s'))
  250. ->delete();
  251. }
  252. // 更新已变化的数据
  253. if (!empty($toUpdate)) {
  254. $this->BorrowNumber->saveAll($toUpdate);
  255. }
  256. // 插入新增的数据
  257. if (!empty($toInsert)) {
  258. $this->BorrowNumber->insertAll($toInsert);
  259. }
  260. }
  261. if (isset($data['fileList'])) {
  262. $result = Db::name('borrow_img')->where('borrow_id', $id)->delete();
  263. if ($result) {
  264. $groupImg = [];
  265. foreach ($data['fileList'] as $datum) {
  266. $groupImg[] = [
  267. 'borrow_id' => $data['id'],
  268. 'url' => $datum['sign'],
  269. ];
  270. }
  271. Db::name('borrow_img')->insertAll($groupImg);
  272. }
  273. }
  274. if (array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '') {
  275. if ($data['purpose'] == 0) {
  276. $this->model->startTrans();
  277. try {
  278. $result = $this->model->update($data);
  279. $this->model->commit();
  280. } catch (Throwable $e) {
  281. $this->model->rollback();
  282. $this->error($e->getMessage());
  283. }
  284. }
  285. }
  286. // 提交事务
  287. $this->model->commit();
  288. } catch (Throwable $e) {
  289. // 回滚事务
  290. $this->model->rollback();
  291. $this->error($e->getMessage());
  292. }
  293. if ($result !== false) {
  294. $this->success(__('Update successful'));
  295. } else {
  296. $this->error(__('No rows updated'));
  297. }
  298. }
  299. $this->success('', [
  300. 'row' => $row
  301. ]);
  302. }
  303. public function scientificBind(): void
  304. {
  305. $query = $this->request->param();
  306. $asset_id = $query['asset_id'];
  307. $borrow_id = $query['borrow_id'];
  308. $result = false;
  309. $is_leadAgree = $this->model->where('id', $borrow_id)->value('college_leader_id');
  310. if ($is_leadAgree == null) {
  311. $this->error("请等待学院领导审核");
  312. }
  313. $asset_name = Db::name('asset')->where('asset_id', $asset_id)->value('asset_name');
  314. $data = $this->BorrowTools->getDateForAssetName($asset_id,$asset_name,$borrow_id);
  315. if ($data !== null) {
  316. $data = $data->toArray();
  317. $data['asset_name'] = $asset_name;
  318. $data['asset_id'] = $asset_id;
  319. $data['status'] = 0;
  320. $data['collection_time'] = date('Y-m-d H:i:s', time());
  321. $data['lend_processor'] = $this->auth->nickname;
  322. } else {
  323. $this->error(__('Type of inspection instrument'));
  324. }
  325. $this->model->startTrans();
  326. try {
  327. //更改仪器状态
  328. $change = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 1])->update();
  329. //更新数据
  330. $result = $this->BorrowAccess->update($data);
  331. //判断是否应该更改借单状态
  332. $is_uesing = $this->BorrowAccess
  333. ->where(['borrow_id' => $borrow_id])
  334. ->where('asset_id', null)
  335. ->find();
  336. if ($is_uesing == null) {
  337. $this->model->where('id', $borrow_id)->update(['status' => 3]);
  338. }
  339. $this->model->commit();
  340. } catch (Throwable $e) {
  341. $this->model->rollback();
  342. $this->error($e->getMessage());
  343. }
  344. if ($result !== false) {
  345. $this->success(__('Update successful'));
  346. } else {
  347. $this->error(__('No rows updated'));
  348. }
  349. }
  350. public function assetChange(): void
  351. {
  352. $query = $this->request->param();
  353. $old_asset_id = $query['old_asset_id'];
  354. $new_asset_id = $query['new_asset_id'];
  355. $borrow_id = $query['borrow_id'];
  356. $isDamage = $query['isDamage'];
  357. $assetData = Db::name('borrow_access')
  358. ->where(['asset_id' => $old_asset_id, 'borrow_id' => $borrow_id, 'status' => 0])
  359. ->find();
  360. if ($assetData == null) {
  361. $this->error("请检查仪器编号!!");
  362. }
  363. $isOldAssset = Db::name('asset')->where('asset_id', $new_asset_id)->find();
  364. if ($isOldAssset == null) {
  365. $this->error("请检查仪器编号!!");
  366. }
  367. if ($isOldAssset['status'] == 1) {
  368. $this->error("该仪器已借出!!");
  369. }
  370. $result = false;
  371. $this->model->startTrans();
  372. try {
  373. $asset_name = Db::name('asset')->where('asset_id', $old_asset_id)->value('asset_name');
  374. $new_asset_name = Db::name('asset')->where('asset_id', $new_asset_id)->value('asset_name');
  375. if ($isDamage == 1) {
  376. $back = Db::name('asset')->where('asset_id', $old_asset_id)->data(['status' => 2])->update();
  377. $thisStatus = 1;
  378. } else {
  379. $back = Db::name('asset')->where('asset_id', $old_asset_id)->data(['status' => 0])->update();
  380. $toUse = Db::name('asset')->where('asset_id', $new_asset_id)->data(['status' => 1])->update();
  381. $thisStatus = 3;
  382. }
  383. $change = Db::name('borrow_access')
  384. ->where(['borrow_id' => $borrow_id, 'asset_id' => $old_asset_id, 'status' => 0])
  385. ->data([
  386. 'status' => $thisStatus,
  387. 'restitution_processor' => $this->auth->nickname,
  388. 'restitution_time' => date('Y-m-d H:i:s', time()),
  389. ])->update();
  390. if (isset($assetData['student_id'])) {
  391. $result = $this->BorrowAccess->insert(
  392. ['borrow_id' => $borrow_id,
  393. 'asset_name' => $new_asset_name,
  394. 'student_id' => $assetData['student_id'],
  395. 'asset_id' => $new_asset_id,
  396. 'origin' => $assetData['origin'],
  397. 'collection_time' => date('Y-m-d H:i:s', time()),
  398. 'lend_processor' => $this->auth->nickname,
  399. 'status' => 0]
  400. );
  401. } else {
  402. $result = $this->BorrowAccess->insert(
  403. ['borrow_id' => $borrow_id,
  404. 'asset_name' => $new_asset_name,
  405. 'asset_id' => $new_asset_id,
  406. 'origin' => $assetData['origin'],
  407. 'collection_time' => date('Y-m-d H:i:s', time()),
  408. 'lend_processor' => $this->auth->nickname,
  409. 'status' => 0]
  410. );
  411. }
  412. if (!($change && $result)) {
  413. $this->error("替换失败!请重试!!");
  414. }
  415. $this->model->commit();
  416. } catch (Throwable $e) {
  417. $this->model->rollback();
  418. $this->error($e->getMessage());
  419. }
  420. if ($result !== false) {
  421. $this->success(__('Update successful'));
  422. } else {
  423. $this->error(__('No rows updated'));
  424. }
  425. }
  426. public function assetDamage(): void
  427. {
  428. $query = $this->request->param();
  429. $asset_id = $query['asset_id'];
  430. $borrow_id = $query['borrow_id'];
  431. $assetData = Db::name('borrow_access')
  432. ->where(['asset_id' => $asset_id,'borrow_id' => $borrow_id, 'status' => 0])
  433. ->find();
  434. if ($assetData == null) {
  435. $this->error("请检查仪器编号!!");
  436. }
  437. $borrow_id = $assetData['borrow_id'];
  438. $result = false;
  439. $this->model->startTrans();
  440. try {
  441. $result = Db::name('borrow_access')
  442. ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  443. ->data([
  444. 'status' => 1,
  445. 'restitution_processor' => $this->auth->nickname,
  446. 'restitution_time' => date('Y-m-d H:i:s', time()),
  447. ])->update();
  448. $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 2])->update();
  449. $is_end = $this->BorrowAccess
  450. ->where(['borrow_id' => $borrow_id])
  451. ->where('status', "=", '0')
  452. ->find();
  453. if ($is_end == null) {
  454. $this->model->where('id', $borrow_id)->update(['status' => 4]);
  455. }
  456. $this->model->commit();
  457. } catch (Throwable $e) {
  458. $this->model->rollback();
  459. $this->error($e->getMessage());
  460. }
  461. if ($result !== false) {
  462. $this->success(__('Update successful'));
  463. } else {
  464. $this->error(__('No rows updated'));
  465. }
  466. }
  467. public function scientificComplete(): void
  468. {
  469. $query = $this->request->param();
  470. if (isset($query['asset_id']) && isset($query['borrow_id'])) {
  471. $asset_id = $query['asset_id'];
  472. $borrow_id = $query['borrow_id'];
  473. } else {
  474. $this->error('缺少信息,请重试!!');
  475. }
  476. $is_r = Db::name('borrow_access')
  477. ->where(['asset_id' => $asset_id,'borrow_id' => $borrow_id, 'status' => 0])
  478. ->find();
  479. if ($is_r == null) {
  480. $this->error("请检查仪器编号!!");
  481. }
  482. $result = false;
  483. $this->model->startTrans();
  484. try {
  485. $result = Db::name('borrow_access')
  486. ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  487. ->data([
  488. 'status' => 2,
  489. 'restitution_processor' => $this->auth->nickname,
  490. 'restitution_time' => date('Y-m-d H:i:s', time())
  491. ])
  492. ->update();
  493. $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 0])->update();
  494. $is_end = $this->BorrowAccess
  495. ->where(['borrow_id' => $borrow_id])
  496. ->where('status', "=", '0')
  497. ->find();
  498. // halt($is_end);
  499. if ($is_end == null) {
  500. $this->model->where('id', $borrow_id)->update(['status' => 4]);
  501. }
  502. $this->model->commit();
  503. } catch (Throwable $e) {
  504. $this->model->rollback();
  505. $this->error($e->getMessage());
  506. }
  507. if ($result !== false) {
  508. $this->success(__('Update successful'));
  509. } else {
  510. $this->error("归还失败!");
  511. }
  512. }
  513. //课程类借取绑定仪器
  514. public function courseBind(): void
  515. {
  516. $query = $this->request->param();
  517. $result = false;
  518. if (isset($query['asset_id']) && isset($query['student_phone']) && isset($query['borrow_id'])) {
  519. $asset_id = $query['asset_id'];
  520. $student_phone = $query['student_phone'];
  521. $borrow_id = $query['borrow_id'];
  522. } else {
  523. $this->error('缺少信息,请重试!!');
  524. }
  525. $student = DB::name('student')->where('student_phone', $student_phone)->find();
  526. if (!$student) {
  527. $this->error('该学生未注册!');
  528. }
  529. $asset_name = Db::name('asset')->where('asset_id', $asset_id)->value('asset_name');
  530. $data = $this->BorrowTools->getDateForAssetName($asset_id,$asset_name,$borrow_id);
  531. $this->model->startTrans();
  532. try {
  533. //判断扫描的时是身份码还是仪器码并作出相应操作
  534. if (array_key_exists('asset_id', $query) && isset($query['asset_id'])) {
  535. $data->status = 0;
  536. $data->asset_name = $asset_name;
  537. $data->asset_id = $asset_id;
  538. $data->student_id = $student['id'];
  539. $data->collection_time = date('Y-m-d H:i:s', time());
  540. $data->lend_processor = $this->auth->nickname;
  541. $result = $data->save();
  542. $change = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 1])->update();
  543. //检验是否完成绑定并调整借单状态值
  544. $is_allIn = $this->BorrowAccess
  545. ->where(['asset_id' => null])
  546. ->where(['borrow_id' => $borrow_id])
  547. ->find();
  548. if ($is_allIn === null) {
  549. $this->model->where('id', $borrow_id)->update(['status' => '3']);
  550. }
  551. }
  552. $this->model->commit();
  553. } catch (Throwable $e) {
  554. $this->model->rollback();
  555. $this->error($e->getMessage());
  556. }
  557. if ($result !== false) {
  558. $this->success(__('Update successful'));
  559. } else {
  560. $this->error(__('No rows updated'));
  561. }
  562. }
  563. // public function courseBindChange(): void
  564. // {
  565. // $query = $this->request->param();
  566. // $asset_id = $query['asset_id'];
  567. // $borrow_id = $query['asset_id'];
  568. // $row = Db::name('borrow_access')
  569. // ->where(['asset_id' => $asset_id, 'borrow_id' => $borrow_id, 'status' => 0])
  570. // ->find();
  571. // if ($row == null) {
  572. // $this->error("请勿重复扫码!");
  573. // }
  574. // $result = false;
  575. // $this->model->startTrans();
  576. // try {
  577. // $assetData = Db::name('asset')->where('asset_id', $asset_id)->find();
  578. // $change = Db::name('borrow_access')
  579. // ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  580. // ->data(['status' => 1])
  581. // ->update();
  582. // $result = $this->BorrowAccess->insert(
  583. // ['borrow_id' => $borrow_id, 'asset_name' => $assetData['asset_name'], 'origin' => $assetData['origin'], 'status' => -1]
  584. // );
  585. // $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 2])->update();
  586. // if (!($change && $result)) {
  587. // $this->error("报损失败!请重试!!");
  588. // }
  589. // $this->model->commit();
  590. // } catch (Throwable $e) {
  591. // $this->model->rollback();
  592. // $this->error($e->getMessage());
  593. // }
  594. // if ($result !== false) {
  595. // $this->success(__('Update successful'));
  596. // } else {
  597. // $this->error(__('No rows updated'));
  598. // }
  599. // }
  600. public function courseComplete(): void
  601. {
  602. $query = $this->request->param();
  603. if (isset($query['asset_id']) && isset($query['borrow_id'])) {
  604. $asset_id = $query['asset_id'];
  605. // $student_phone = $query['student_phone'];
  606. $borrow_id = $query['borrow_id'];
  607. } else {
  608. $this->error('缺少信息,请重试!!');
  609. }
  610. $row = Db::name('borrow_access')
  611. ->where(['asset_id' => $asset_id, 'borrow_id' => $borrow_id])
  612. ->find();
  613. if ($row == null || $row['status'] !== 0 ) {
  614. $this->error("请检查仪器编号!!");
  615. }
  616. $result = false;
  617. $this->model->startTrans();
  618. try {
  619. $result = Db::name('borrow_access')
  620. ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  621. ->data([
  622. 'status' => 2,
  623. 'restitution_processor' => $this->auth->nickname,
  624. 'restitution_time' => date('Y-m-d H:i:s', time())
  625. ])->update();
  626. $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 0])->update();
  627. $is_end = $this->BorrowAccess
  628. ->where(['borrow_id' => $borrow_id])
  629. ->where('status', "=", '0')
  630. ->find();
  631. // halt($is_end);
  632. if ($is_end == null) {
  633. $this->model->where('id', $borrow_id)->update(['status' => '4']);
  634. }
  635. $this->model->commit();
  636. } catch (Throwable $e) {
  637. $this->model->rollback();
  638. $this->error($e->getMessage());
  639. }
  640. if ($result !== false) {
  641. $this->success(__('Update successful'));
  642. } else {
  643. $this->error("归还失败!");
  644. }
  645. }
  646. public function uniUpload(): void
  647. {
  648. $data = $this->request->post();
  649. $borrow_id = $data['borrow_id'];
  650. $url = $data['url'];
  651. $res = Db::name('borrow_img')->insert(
  652. [
  653. 'borrow_id' => $borrow_id,
  654. 'url' => $url
  655. ]
  656. );
  657. if ($res) {
  658. $this->success('上传成功!!');
  659. } else {
  660. $this->error('保存失败!!');
  661. }
  662. }
  663. public function delImg(): void
  664. {
  665. $data = $this->request->post();
  666. $borrow_id = $data['borrow_id'];
  667. $url = $data['url'];
  668. $res = Db::name('borrow_img')->where([
  669. 'borrow_id' => $borrow_id,
  670. 'url' => $url
  671. ])->delete();
  672. if ($res) {
  673. $this->success('删除成功!!');
  674. } else {
  675. $this->error('删除失败!!');
  676. }
  677. }
  678. }