BorrowUseing.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. if ($this->request->isPost()) {
  172. $data = $this->request->post();
  173. if (!$data) {
  174. $this->error(__('Parameter %s can not be empty', ['']));
  175. }
  176. if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) {
  177. $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8');
  178. }
  179. if (array_key_exists('remarks', $data) && $data['remarks'] !== null) {
  180. $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8');
  181. }
  182. if (!isset($data['asset']) || $data['asset'] == []) {
  183. $this->error("仪器选择不能为空!!");
  184. }
  185. $data = $this->excludeFields($data);
  186. //获取用户类型
  187. $user_id = $this->auth->id;
  188. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  189. $this->model->startTrans();
  190. $result = false;
  191. try {
  192. // 调整仪器的变化
  193. if (array_key_exists('asset', $data) && !empty($data['asset'])) {
  194. // 首先检索当前所有相关记录
  195. $existingRecords = Db::name('borrow_access')
  196. ->where('borrow_id', $data['id'])
  197. ->column('*', 'id');
  198. $toUpdate = [];
  199. $toInsert = [];
  200. foreach ($data['asset'] as $datum) {
  201. if (isset($datum['id'], $existingRecords[$datum['id']])) {
  202. // 对比现有数据与新数据
  203. $current = $existingRecords[$datum['id']];
  204. if ($current['asset_name'] !== $datum['model'] || $current['asset_id'] !== $datum['asset_id']
  205. || $current['student_id'] !== $datum['student_id'] || $current['origin'] !== $datum['origin']
  206. || $current['status'] !== $datum['status'] || $current['collection_time'] !== $datum['collection_time']
  207. || $current['lend_processor'] !== $datum['lend_processor']
  208. || $current['restitution_processor'] !== $datum['restitution_processor'] || $current['restitution_time'] !== $datum['restitution_time']
  209. ) {
  210. // 数据有变化,则准备更新
  211. $toUpdate[] = [
  212. 'id' => $datum['id'],
  213. 'borrow_id' => $data['id'],
  214. 'asset_name' => $datum['model'],
  215. 'asset_id' => $datum['asset_id'],
  216. 'student_id' => $datum['student_id'],
  217. 'origin' => $datum['origin'],
  218. 'status' => $data['status'],
  219. 'collection_time' => $datum['collection_time'],
  220. 'lend_processor' => $datum['lend_processor'],
  221. 'restitution_processor' => $datum['restitution_processor'],
  222. 'restitution_time' => $datum['restitution_time'],
  223. ];
  224. }
  225. // 从现有记录中移除已经处理过的项
  226. unset($existingRecords[$datum['id']]);
  227. } else {
  228. // 数据不存在,准备插入项
  229. $toInsert[] = [
  230. 'borrow_id' => $data['id'],
  231. 'asset_name' => $datum['model'],
  232. 'asset_id' => $datum['asset_id'],
  233. 'student_id' => $datum['student_id'],
  234. 'origin' => $datum['origin'],
  235. 'status' => $data['status'],
  236. 'collection_time' => $datum['collection_time'],
  237. 'lend_processor' => $datum['lend_processor'],
  238. 'restitution_processor' => $datum['restitution_processor'],
  239. 'restitution_time' => $datum['restitution_time'],
  240. ];
  241. }
  242. }
  243. // $existingRecords 中剩下的是需要软删除的
  244. $toDelete = array_keys($existingRecords);
  245. if (!empty($toDelete)) {
  246. Db::name('borrow_number')
  247. ->where('id', 'in', $toDelete)
  248. ->useSoftDelete('delete_time', date('Y-m-d H:i:s'))
  249. ->delete();
  250. }
  251. // 更新已变化的数据
  252. if (!empty($toUpdate)) {
  253. $this->BorrowNumber->saveAll($toUpdate);
  254. }
  255. // 插入新增的数据
  256. if (!empty($toInsert)) {
  257. $this->BorrowNumber->insertAll($toInsert);
  258. }
  259. }
  260. if (isset($data['fileList'])) {
  261. $result = Db::name('borrow_img')->where('borrow_id', $id)->delete();
  262. if ($result) {
  263. $groupImg = [];
  264. foreach ($data['fileList'] as $datum) {
  265. $groupImg[] = [
  266. 'borrow_id' => $data['id'],
  267. 'url' => $datum['sign'],
  268. ];
  269. }
  270. Db::name('borrow_img')->insertAll($groupImg);
  271. }
  272. }
  273. if (array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '') {
  274. if ($data['purpose'] == 0) {
  275. $this->model->startTrans();
  276. try {
  277. $result = $this->model->update($data);
  278. $this->model->commit();
  279. } catch (Throwable $e) {
  280. $this->model->rollback();
  281. $this->error($e->getMessage());
  282. }
  283. }
  284. }
  285. // 提交事务
  286. $this->model->commit();
  287. } catch (Throwable $e) {
  288. // 回滚事务
  289. $this->model->rollback();
  290. $this->error($e->getMessage());
  291. }
  292. if ($result !== false) {
  293. $this->success(__('Update successful'));
  294. } else {
  295. $this->error(__('No rows updated'));
  296. }
  297. }
  298. $this->success('', [
  299. 'row' => $row
  300. ]);
  301. }
  302. public function scientificBind(): void
  303. {
  304. $query = $this->request->param();
  305. $asset_id = $query['asset_id'];
  306. $borrow_id = $query['borrow_id'];
  307. $result = false;
  308. $is_leadAgree = $this->model->where('id', $borrow_id)->value('college_leader_id');
  309. if ($is_leadAgree == null) {
  310. $this->error("请等待学院领导审核");
  311. }
  312. $asset_name = Db::name('asset')->where('asset_id', $asset_id)->value('asset_name');
  313. $data = $this->BorrowTools->getDateForAssetName($asset_id,$asset_name,$borrow_id);
  314. if ($data !== null) {
  315. $data = $data->toArray();
  316. $data['asset_name'] = $asset_name;
  317. $data['asset_id'] = $asset_id;
  318. $data['status'] = 0;
  319. $data['collection_time'] = date('Y-m-d H:i:s', time());
  320. $data['lend_processor'] = $this->auth->nickname;
  321. } else {
  322. $this->error(__('Type of inspection instrument'));
  323. }
  324. $this->model->startTrans();
  325. try {
  326. //更改仪器状态
  327. $change = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 1])->update();
  328. //更新数据
  329. $result = $this->BorrowAccess->update($data);
  330. //判断是否应该更改借单状态
  331. $is_uesing = $this->BorrowAccess
  332. ->where(['borrow_id' => $borrow_id])
  333. ->where('asset_id', null)
  334. ->find();
  335. if ($is_uesing == null) {
  336. $this->model->where('id', $borrow_id)->update(['status' => 3]);
  337. }
  338. $this->model->commit();
  339. } catch (Throwable $e) {
  340. $this->model->rollback();
  341. $this->error($e->getMessage());
  342. }
  343. if ($result !== false) {
  344. $this->success(__('Update successful'));
  345. } else {
  346. $this->error(__('No rows updated'));
  347. }
  348. }
  349. public function assetChange(): void
  350. {
  351. $query = $this->request->param();
  352. $old_asset_id = $query['old_asset_id'];
  353. $new_asset_id = $query['new_asset_id'];
  354. $borrow_id = $query['borrow_id'];
  355. $isDamage = $query['isDamage'];
  356. $assetData = Db::name('borrow_access')
  357. ->where(['asset_id' => $old_asset_id, 'borrow_id' => $borrow_id, 'status' => 0])
  358. ->find();
  359. if ($assetData == null) {
  360. $this->error("请检查仪器编号!!");
  361. }
  362. $isOldAssset = Db::name('asset')->where('asset_id', $new_asset_id)->find();
  363. if ($isOldAssset == null) {
  364. $this->error("请检查仪器编号!!");
  365. }
  366. if ($isOldAssset['status'] == 1) {
  367. $this->error("该仪器已借出!!");
  368. }
  369. $result = false;
  370. $this->model->startTrans();
  371. try {
  372. $asset_name = Db::name('asset')->where('asset_id', $old_asset_id)->value('asset_name');
  373. $new_asset_name = Db::name('asset')->where('asset_id', $new_asset_id)->value('asset_name');
  374. if ($isDamage == 1) {
  375. $back = Db::name('asset')->where('asset_id', $old_asset_id)->data(['status' => 2])->update();
  376. $thisStatus = 1;
  377. } else {
  378. $back = Db::name('asset')->where('asset_id', $old_asset_id)->data(['status' => 0])->update();
  379. $thisStatus = 3;
  380. }
  381. $change = Db::name('borrow_access')
  382. ->where(['borrow_id' => $borrow_id, 'asset_id' => $old_asset_id, 'status' => 0])
  383. ->data([
  384. 'status' => $thisStatus,
  385. 'restitution_processor' => $this->auth->nickname,
  386. 'restitution_time' => date('Y-m-d H:i:s', time()),
  387. ])->update();
  388. if (isset($assetData['student_id'])) {
  389. $result = $this->BorrowAccess->insert(
  390. ['borrow_id' => $borrow_id,
  391. 'asset_name' => $new_asset_name,
  392. 'student_id' => $assetData['student_id'],
  393. 'asset_id' => $new_asset_id,
  394. 'origin' => $assetData['origin'],
  395. 'collection_time' => date('Y-m-d H:i:s', time()),
  396. 'lend_processor' => $this->auth->nickname,
  397. 'status' => 0]
  398. );
  399. } else {
  400. $result = $this->BorrowAccess->insert(
  401. ['borrow_id' => $borrow_id,
  402. 'asset_name' => $new_asset_name,
  403. 'asset_id' => $new_asset_id,
  404. 'origin' => $assetData['origin'],
  405. 'collection_time' => date('Y-m-d H:i:s', time()),
  406. 'lend_processor' => $this->auth->nickname,
  407. 'status' => 0]
  408. );
  409. }
  410. if (!($change && $result)) {
  411. $this->error("替换失败!请重试!!");
  412. }
  413. $this->model->commit();
  414. } catch (Throwable $e) {
  415. $this->model->rollback();
  416. $this->error($e->getMessage());
  417. }
  418. if ($result !== false) {
  419. $this->success(__('Update successful'));
  420. } else {
  421. $this->error(__('No rows updated'));
  422. }
  423. }
  424. public function assetDamage(): void
  425. {
  426. $query = $this->request->param();
  427. $asset_id = $query['asset_id'];
  428. $borrow_id = $query['borrow_id'];
  429. $assetData = Db::name('borrow_access')
  430. ->where(['asset_id' => $asset_id,'borrow_id' => $borrow_id, 'status' => 0])
  431. ->find();
  432. if ($assetData == null) {
  433. $this->error("请检查仪器编号!!");
  434. }
  435. $borrow_id = $assetData['borrow_id'];
  436. $result = false;
  437. $this->model->startTrans();
  438. try {
  439. $result = Db::name('borrow_access')
  440. ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  441. ->data([
  442. 'status' => 1,
  443. 'restitution_processor' => $this->auth->nickname,
  444. 'restitution_time' => date('Y-m-d H:i:s', time()),
  445. ])->update();
  446. $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 2])->update();
  447. $is_end = $this->BorrowAccess
  448. ->where(['borrow_id' => $borrow_id])
  449. ->where('status', "=", '0')
  450. ->find();
  451. if ($is_end == null) {
  452. $this->model->where('id', $borrow_id)->update(['status' => 4]);
  453. }
  454. $this->model->commit();
  455. } catch (Throwable $e) {
  456. $this->model->rollback();
  457. $this->error($e->getMessage());
  458. }
  459. if ($result !== false) {
  460. $this->success(__('Update successful'));
  461. } else {
  462. $this->error(__('No rows updated'));
  463. }
  464. }
  465. public function scientificComplete(): void
  466. {
  467. $query = $this->request->param();
  468. if (isset($query['asset_id']) && isset($query['borrow_id'])) {
  469. $asset_id = $query['asset_id'];
  470. $borrow_id = $query['borrow_id'];
  471. } else {
  472. $this->error('缺少信息,请重试!!');
  473. }
  474. $is_r = Db::name('borrow_access')
  475. ->where(['asset_id' => $asset_id,'borrow_id' => $borrow_id, 'status' => 0])
  476. ->find();
  477. if ($is_r == null) {
  478. $this->error("请检查仪器编号!!");
  479. }
  480. $result = false;
  481. $this->model->startTrans();
  482. try {
  483. $result = Db::name('borrow_access')
  484. ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  485. ->data([
  486. 'status' => 2,
  487. 'restitution_processor' => $this->auth->nickname,
  488. 'restitution_time' => date('Y-m-d H:i:s', time())
  489. ])
  490. ->update();
  491. $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 0])->update();
  492. $is_end = $this->BorrowAccess
  493. ->where(['borrow_id' => $borrow_id])
  494. ->where('status', "=", '0')
  495. ->find();
  496. // halt($is_end);
  497. if ($is_end == null) {
  498. $this->model->where('id', $borrow_id)->update(['status' => 4]);
  499. }
  500. $this->model->commit();
  501. } catch (Throwable $e) {
  502. $this->model->rollback();
  503. $this->error($e->getMessage());
  504. }
  505. if ($result !== false) {
  506. $this->success(__('Update successful'));
  507. } else {
  508. $this->error("归还失败!");
  509. }
  510. }
  511. //课程类借取绑定仪器
  512. public function courseBind(): void
  513. {
  514. $query = $this->request->param();
  515. $result = false;
  516. if (isset($query['asset_id']) && isset($query['student_phone']) && isset($query['borrow_id'])) {
  517. $asset_id = $query['asset_id'];
  518. $student_phone = $query['student_phone'];
  519. $borrow_id = $query['borrow_id'];
  520. } else {
  521. $this->error('缺少信息,请重试!!');
  522. }
  523. $student = DB::name('student')->where('student_phone', $student_phone)->find();
  524. if (!$student) {
  525. $this->error('该学生未注册!');
  526. }
  527. $asset_name = Db::name('asset')->where('asset_id', $asset_id)->value('asset_name');
  528. $data = $this->BorrowTools->getDateForAssetName($asset_id,$asset_name,$borrow_id);
  529. $this->model->startTrans();
  530. try {
  531. //判断扫描的时是身份码还是仪器码并作出相应操作
  532. if (array_key_exists('asset_id', $query) && isset($query['asset_id'])) {
  533. $data->status = 0;
  534. $data->asset_name = $asset_name;
  535. $data->asset_id = $asset_id;
  536. $data->student_id = $student['id'];
  537. $data->collection_time = date('Y-m-d H:i:s', time());
  538. $data->lend_processor = $this->auth->nickname;
  539. $result = $data->save();
  540. $change = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 1])->update();
  541. //检验是否完成绑定并调整借单状态值
  542. $is_allIn = $this->BorrowAccess
  543. ->where(['asset_id' => null])
  544. ->where(['borrow_id' => $borrow_id])
  545. ->find();
  546. if ($is_allIn === null) {
  547. $this->model->where('id', $borrow_id)->update(['status' => '3']);
  548. }
  549. }
  550. $this->model->commit();
  551. } catch (Throwable $e) {
  552. $this->model->rollback();
  553. $this->error($e->getMessage());
  554. }
  555. if ($result !== false) {
  556. $this->success(__('Update successful'));
  557. } else {
  558. $this->error(__('No rows updated'));
  559. }
  560. }
  561. // public function courseBindChange(): void
  562. // {
  563. // $query = $this->request->param();
  564. // $asset_id = $query['asset_id'];
  565. // $borrow_id = $query['asset_id'];
  566. // $row = Db::name('borrow_access')
  567. // ->where(['asset_id' => $asset_id, 'borrow_id' => $borrow_id, 'status' => 0])
  568. // ->find();
  569. // if ($row == null) {
  570. // $this->error("请勿重复扫码!");
  571. // }
  572. // $result = false;
  573. // $this->model->startTrans();
  574. // try {
  575. // $assetData = Db::name('asset')->where('asset_id', $asset_id)->find();
  576. // $change = Db::name('borrow_access')
  577. // ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  578. // ->data(['status' => 1])
  579. // ->update();
  580. // $result = $this->BorrowAccess->insert(
  581. // ['borrow_id' => $borrow_id, 'asset_name' => $assetData['asset_name'], 'origin' => $assetData['origin'], 'status' => -1]
  582. // );
  583. // $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 2])->update();
  584. // if (!($change && $result)) {
  585. // $this->error("报损失败!请重试!!");
  586. // }
  587. // $this->model->commit();
  588. // } catch (Throwable $e) {
  589. // $this->model->rollback();
  590. // $this->error($e->getMessage());
  591. // }
  592. // if ($result !== false) {
  593. // $this->success(__('Update successful'));
  594. // } else {
  595. // $this->error(__('No rows updated'));
  596. // }
  597. // }
  598. public function courseComplete(): void
  599. {
  600. $query = $this->request->param();
  601. if (isset($query['asset_id']) && isset($query['borrow_id'])) {
  602. $asset_id = $query['asset_id'];
  603. // $student_phone = $query['student_phone'];
  604. $borrow_id = $query['borrow_id'];
  605. } else {
  606. $this->error('缺少信息,请重试!!');
  607. }
  608. $row = Db::name('borrow_access')
  609. ->where(['asset_id' => $asset_id, 'borrow_id' => $borrow_id])
  610. ->find();
  611. if ($row == null || $row['status'] !== 0 ) {
  612. $this->error("请检查仪器编号!!");
  613. }
  614. $result = false;
  615. $this->model->startTrans();
  616. try {
  617. $result = Db::name('borrow_access')
  618. ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0])
  619. ->data([
  620. 'status' => 2,
  621. 'restitution_processor' => $this->auth->nickname,
  622. 'restitution_time' => date('Y-m-d H:i:s', time())
  623. ])->update();
  624. $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 0])->update();
  625. $is_end = $this->BorrowAccess
  626. ->where(['borrow_id' => $borrow_id])
  627. ->where('status', "=", '0')
  628. ->find();
  629. // halt($is_end);
  630. if ($is_end == null) {
  631. $this->model->where('id', $borrow_id)->update(['status' => '4']);
  632. }
  633. $this->model->commit();
  634. } catch (Throwable $e) {
  635. $this->model->rollback();
  636. $this->error($e->getMessage());
  637. }
  638. if ($result !== false) {
  639. $this->success(__('Update successful'));
  640. } else {
  641. $this->error("归还失败!");
  642. }
  643. }
  644. public function uniUpload(): void
  645. {
  646. $data = $this->request->post();
  647. $borrow_id = $data['borrow_id'];
  648. $url = $data['url'];
  649. $res = Db::name('borrow_img')->insert(
  650. [
  651. 'borrow_id' => $borrow_id,
  652. 'url' => $url
  653. ]
  654. );
  655. if ($res) {
  656. $this->success('上传成功!!');
  657. } else {
  658. $this->error('保存失败!!');
  659. }
  660. }
  661. public function delImg(): void
  662. {
  663. $data = $this->request->post();
  664. $borrow_id = $data['borrow_id'];
  665. $url = $data['url'];
  666. $res = Db::name('borrow_img')->where([
  667. 'borrow_id' => $borrow_id,
  668. 'url' => $url
  669. ])->delete();
  670. if ($res) {
  671. $this->success('删除成功!!');
  672. } else {
  673. $this->error('删除失败!!');
  674. }
  675. }
  676. }