model = new BorrowModel; $this->BorrowAccess = new BorrowAccess(); $this->BorrowNumber = new BorrowNumber(); $this->BorrowTools = new BorrowTools($this->app); } public function index(): void { if ($this->request->param('select')) { $this->select(); } $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); list($where, $alias, $limit, $order) = $this->queryBuilder(); if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->alias($alias) ->where($where) ->where("status", 'in', [2, 3, 5]) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->alias($alias) ->where($where) ->where("status", 'in', [2, 3, 5]) ->where('college_leader_id', $user_id) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->alias($alias) ->where($where) ->where("status", 'in', [2, 3, 5]) ->where('user_id', $user_id) ->order($order) ->paginate($limit); } $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } public function add(): void { if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) { $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8'); } if (array_key_exists('remarks', $data) && $data['remarks'] !== null) { $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8'); } $data = $this->excludeFields($data); //获取用户类型 $this->model->startTrans(); $result = false; try { $uniID = new SnowflakeId; $data['user_id'] = $this->auth->id; $data['encoding'] = $uniID->generateParticle(); $result = $this->model->save($data); // 调整仪器 if (array_key_exists('asset', $data) && !empty($data['asset'])) { $toInsert = []; foreach ($data['asset'] as $datum) { // 数据不存在,准备插入项 $toInsert[] = [ 'borrow_id' => $this->model->id, 'asset_name' => $datum['model'], 'asset_id' => $datum['asset_id'], 'origin' => $datum['origin'], 'status' => $data['status'], 'collection_time' => $datum['collection_time'], 'lend_processor' => $datum['lend_processor'], 'restitution_processor' => $datum['restitution_processor'], 'restitution_time' => $datum['restitution_time'], ]; } // 插入新增的数据 if (!empty($toInsert)) { $this->BorrowAccess->insertAll($toInsert); } } if (isset($data['fileList'])) { $groupImg = []; foreach ($data['fileList'] as $datum) { $groupImg[] = [ 'borrow_id' => $this->model->id, 'url' => $datum['sign'], ]; } Db::name('borrow_img')->insertAll($groupImg); } // 提交事务 $this->model->commit(); } catch (Throwable $e) { // 回滚事务 $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } $this->error(__('Parameter error')); } public function del($ids = null): void { if (!$this->request->isDelete() || !$ids) { $this->error(__('Parameter error')); } $pk = $this->model->getPk(); $data = $this->model->where($pk, 'in', $ids)->select(); $count = 0; $count = 0; $this->model->startTrans(); try { foreach ($data as $v) { $count += $v->delete(); $this->BorrowAccess->where('borrow_id', 'in', $ids)->select(); Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete(); Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete(); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(__('Deleted successfully')); } else { $this->error(__('No rows were deleted')); } } public function edit($id = null): void { $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) { $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select(); } else { $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id', $id)->select(); } $row['asset'] = $rows; $row['fileList'] = Db::name('borrow_img')->where('borrow_id', $id)->field('*,url as sign')->select(); $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find(); if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } if (array_key_exists('borrow_reason', $data) && $data['borrow_reason'] !== null) { $data['borrow_reason'] = html_entity_decode($data['borrow_reason'], ENT_QUOTES, 'UTF-8'); } if (array_key_exists('remarks', $data) && $data['remarks'] !== null) { $data['remarks'] = html_entity_decode($data['remarks'], ENT_QUOTES, 'UTF-8'); } if (!isset($data['asset']) || $data['asset'] == []) { $this->error("仪器选择不能为空!!"); } $data = $this->excludeFields($data); //获取用户类型 $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); $this->model->startTrans(); $result = false; try { // 调整仪器的变化 if (array_key_exists('asset', $data) && !empty($data['asset'])) { // 首先检索当前所有相关记录 $existingRecords = Db::name('borrow_access') ->where('borrow_id', $data['id']) ->column('*', 'id'); $toUpdate = []; $toInsert = []; foreach ($data['asset'] as $datum) { if (isset($datum['id'], $existingRecords[$datum['id']])) { // 对比现有数据与新数据 $current = $existingRecords[$datum['id']]; if ($current['asset_name'] !== $datum['model'] || $current['asset_id'] !== $datum['asset_id'] || $current['student_id'] !== $datum['student_id'] || $current['origin'] !== $datum['origin'] || $current['status'] !== $datum['status'] || $current['collection_time'] !== $datum['collection_time'] || $current['lend_processor'] !== $datum['lend_processor'] || $current['restitution_processor'] !== $datum['restitution_processor'] || $current['restitution_time'] !== $datum['restitution_time'] ) { // 数据有变化,则准备更新 $toUpdate[] = [ 'id' => $datum['id'], 'borrow_id' => $data['id'], 'asset_name' => $datum['model'], 'asset_id' => $datum['asset_id'], 'student_id' => $datum['student_id'], 'origin' => $datum['origin'], 'status' => $data['status'], 'collection_time' => $datum['collection_time'], 'lend_processor' => $datum['lend_processor'], 'restitution_processor' => $datum['restitution_processor'], 'restitution_time' => $datum['restitution_time'], ]; } // 从现有记录中移除已经处理过的项 unset($existingRecords[$datum['id']]); } else { // 数据不存在,准备插入项 $toInsert[] = [ 'borrow_id' => $data['id'], 'asset_name' => $datum['model'], 'asset_id' => $datum['asset_id'], 'student_id' => $datum['student_id'], 'origin' => $datum['origin'], 'status' => $data['status'], 'collection_time' => $datum['collection_time'], 'lend_processor' => $datum['lend_processor'], 'restitution_processor' => $datum['restitution_processor'], 'restitution_time' => $datum['restitution_time'], ]; } } // $existingRecords 中剩下的是需要软删除的 $toDelete = array_keys($existingRecords); if (!empty($toDelete)) { Db::name('borrow_number') ->where('id', 'in', $toDelete) ->useSoftDelete('delete_time', date('Y-m-d H:i:s')) ->delete(); } // 更新已变化的数据 if (!empty($toUpdate)) { $this->BorrowNumber->saveAll($toUpdate); } // 插入新增的数据 if (!empty($toInsert)) { $this->BorrowNumber->insertAll($toInsert); } } if (isset($data['fileList'])) { $result = Db::name('borrow_img')->where('borrow_id', $id)->delete(); if ($result) { $groupImg = []; foreach ($data['fileList'] as $datum) { $groupImg[] = [ 'borrow_id' => $data['id'], 'url' => $datum['sign'], ]; } Db::name('borrow_img')->insertAll($groupImg); } } if (array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '') { if ($data['purpose'] == 0) { $this->model->startTrans(); try { $result = $this->model->update($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } } // 提交事务 $this->model->commit(); } catch (Throwable $e) { // 回滚事务 $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } $this->success('', [ 'row' => $row ]); } public function scientificBind(): void { $query = $this->request->param(); $asset_id = $query['asset_id']; $borrow_id = $query['borrow_id']; $result = false; $is_leadAgree = $this->model->where('id', $borrow_id)->value('college_leader_id'); if ($is_leadAgree == null) { $this->error("请等待学院领导审核"); } $asset_name = Db::name('asset')->where('asset_id', $asset_id)->value('asset_name'); $data = $this->BorrowTools->getDateForAssetName($asset_id,$asset_name,$borrow_id); if ($data !== null) { $data = $data->toArray(); $data['asset_name'] = $asset_name; $data['asset_id'] = $asset_id; $data['status'] = 0; $data['collection_time'] = date('Y-m-d H:i:s', time()); $data['lend_processor'] = $this->auth->nickname; } else { $this->error(__('Type of inspection instrument')); } $this->model->startTrans(); try { //更改仪器状态 $change = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 1])->update(); //更新数据 $result = $this->BorrowAccess->update($data); //判断是否应该更改借单状态 $is_uesing = $this->BorrowAccess ->where(['borrow_id' => $borrow_id]) ->where('asset_id', null) ->find(); if ($is_uesing == null) { $this->model->where('id', $borrow_id)->update(['status' => 3]); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } public function assetChange(): void { $query = $this->request->param(); $old_asset_id = $query['old_asset_id']; $new_asset_id = $query['new_asset_id']; $borrow_id = $query['borrow_id']; $isDamage = $query['isDamage']; $assetData = Db::name('borrow_access') ->where(['asset_id' => $old_asset_id, 'borrow_id' => $borrow_id, 'status' => 0]) ->find(); if ($assetData == null) { $this->error("请检查仪器编号!!"); } $isOldAssset = Db::name('asset')->where('asset_id', $new_asset_id)->find(); if ($isOldAssset == null) { $this->error("请检查仪器编号!!"); } if ($isOldAssset['status'] == 1) { $this->error("该仪器已借出!!"); } $result = false; $this->model->startTrans(); try { $asset_name = Db::name('asset')->where('asset_id', $old_asset_id)->value('asset_name'); $new_asset_name = Db::name('asset')->where('asset_id', $new_asset_id)->value('asset_name'); if ($isDamage == 1) { $back = Db::name('asset')->where('asset_id', $old_asset_id)->data(['status' => 2])->update(); $thisStatus = 1; } else { $back = Db::name('asset')->where('asset_id', $old_asset_id)->data(['status' => 0])->update(); $toUse = Db::name('asset')->where('asset_id', $new_asset_id)->data(['status' => 1])->update(); $thisStatus = 3; } $change = Db::name('borrow_access') ->where(['borrow_id' => $borrow_id, 'asset_id' => $old_asset_id, 'status' => 0]) ->data([ 'status' => $thisStatus, 'restitution_processor' => $this->auth->nickname, 'restitution_time' => date('Y-m-d H:i:s', time()), ])->update(); if (isset($assetData['student_id'])) { $result = $this->BorrowAccess->insert( ['borrow_id' => $borrow_id, 'asset_name' => $new_asset_name, 'student_id' => $assetData['student_id'], 'asset_id' => $new_asset_id, 'origin' => $assetData['origin'], 'collection_time' => date('Y-m-d H:i:s', time()), 'lend_processor' => $this->auth->nickname, 'status' => 0] ); } else { $result = $this->BorrowAccess->insert( ['borrow_id' => $borrow_id, 'asset_name' => $new_asset_name, 'asset_id' => $new_asset_id, 'origin' => $assetData['origin'], 'collection_time' => date('Y-m-d H:i:s', time()), 'lend_processor' => $this->auth->nickname, 'status' => 0] ); } if (!($change && $result)) { $this->error("替换失败!请重试!!"); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } public function assetDamage(): void { $query = $this->request->param(); $asset_id = $query['asset_id']; $borrow_id = $query['borrow_id']; $assetData = Db::name('borrow_access') ->where(['asset_id' => $asset_id,'borrow_id' => $borrow_id, 'status' => 0]) ->find(); if ($assetData == null) { $this->error("请检查仪器编号!!"); } $borrow_id = $assetData['borrow_id']; $result = false; $this->model->startTrans(); try { $result = Db::name('borrow_access') ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0]) ->data([ 'status' => 1, 'restitution_processor' => $this->auth->nickname, 'restitution_time' => date('Y-m-d H:i:s', time()), ])->update(); $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 2])->update(); $is_end = $this->BorrowAccess ->where(['borrow_id' => $borrow_id]) ->where('status', "=", '0') ->find(); if ($is_end == null) { $this->model->where('id', $borrow_id)->update(['status' => 4]); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } public function scientificComplete(): void { $query = $this->request->param(); if (isset($query['asset_id']) && isset($query['borrow_id'])) { $asset_id = $query['asset_id']; $borrow_id = $query['borrow_id']; } else { $this->error('缺少信息,请重试!!'); } $is_r = Db::name('borrow_access') ->where(['asset_id' => $asset_id,'borrow_id' => $borrow_id, 'status' => 0]) ->find(); if ($is_r == null) { $this->error("请检查仪器编号!!"); } $result = false; $this->model->startTrans(); try { $result = Db::name('borrow_access') ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0]) ->data([ 'status' => 2, 'restitution_processor' => $this->auth->nickname, 'restitution_time' => date('Y-m-d H:i:s', time()) ]) ->update(); $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 0])->update(); $is_end = $this->BorrowAccess ->where(['borrow_id' => $borrow_id]) ->where('status', "=", '0') ->find(); // halt($is_end); if ($is_end == null) { $this->model->where('id', $borrow_id)->update(['status' => 4]); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error("归还失败!"); } } //课程类借取绑定仪器 public function courseBind(): void { $query = $this->request->param(); $result = false; if (isset($query['asset_id']) && isset($query['student_phone']) && isset($query['borrow_id'])) { $asset_id = $query['asset_id']; $student_phone = $query['student_phone']; $borrow_id = $query['borrow_id']; } else { $this->error('缺少信息,请重试!!'); } $student = DB::name('student')->where('student_phone', $student_phone)->find(); if (!$student) { $this->error('该学生未注册!'); } $asset_name = Db::name('asset')->where('asset_id', $asset_id)->value('asset_name'); $data = $this->BorrowTools->getDateForAssetName($asset_id,$asset_name,$borrow_id); $this->model->startTrans(); try { //判断扫描的时是身份码还是仪器码并作出相应操作 if (array_key_exists('asset_id', $query) && isset($query['asset_id'])) { $data->status = 0; $data->asset_name = $asset_name; $data->asset_id = $asset_id; $data->student_id = $student['id']; $data->collection_time = date('Y-m-d H:i:s', time()); $data->lend_processor = $this->auth->nickname; $result = $data->save(); $change = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 1])->update(); //检验是否完成绑定并调整借单状态值 $is_allIn = $this->BorrowAccess ->where(['asset_id' => null]) ->where(['borrow_id' => $borrow_id]) ->find(); if ($is_allIn === null) { $this->model->where('id', $borrow_id)->update(['status' => '3']); } } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } // public function courseBindChange(): void // { // $query = $this->request->param(); // $asset_id = $query['asset_id']; // $borrow_id = $query['asset_id']; // $row = Db::name('borrow_access') // ->where(['asset_id' => $asset_id, 'borrow_id' => $borrow_id, 'status' => 0]) // ->find(); // if ($row == null) { // $this->error("请勿重复扫码!"); // } // $result = false; // $this->model->startTrans(); // try { // $assetData = Db::name('asset')->where('asset_id', $asset_id)->find(); // $change = Db::name('borrow_access') // ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0]) // ->data(['status' => 1]) // ->update(); // $result = $this->BorrowAccess->insert( // ['borrow_id' => $borrow_id, 'asset_name' => $assetData['asset_name'], 'origin' => $assetData['origin'], 'status' => -1] // ); // $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 2])->update(); // if (!($change && $result)) { // $this->error("报损失败!请重试!!"); // } // $this->model->commit(); // } catch (Throwable $e) { // $this->model->rollback(); // $this->error($e->getMessage()); // } // if ($result !== false) { // $this->success(__('Update successful')); // } else { // $this->error(__('No rows updated')); // } // } public function courseComplete(): void { $query = $this->request->param(); if (isset($query['asset_id']) && isset($query['borrow_id'])) { $asset_id = $query['asset_id']; // $student_phone = $query['student_phone']; $borrow_id = $query['borrow_id']; } else { $this->error('缺少信息,请重试!!'); } $row = Db::name('borrow_access') ->where(['asset_id' => $asset_id, 'borrow_id' => $borrow_id]) ->find(); if ($row == null || $row['status'] !== 0 ) { $this->error("请检查仪器编号!!"); } $result = false; $this->model->startTrans(); try { $result = Db::name('borrow_access') ->where(['borrow_id' => $borrow_id, 'asset_id' => $asset_id, 'status' => 0]) ->data([ 'status' => 2, 'restitution_processor' => $this->auth->nickname, 'restitution_time' => date('Y-m-d H:i:s', time()) ])->update(); $back = Db::name('asset')->where('asset_id', $asset_id)->data(['status' => 0])->update(); $is_end = $this->BorrowAccess ->where(['borrow_id' => $borrow_id]) ->where('status', "=", '0') ->find(); // halt($is_end); if ($is_end == null) { $this->model->where('id', $borrow_id)->update(['status' => '4']); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error("归还失败!"); } } public function uniUpload(): void { $data = $this->request->post(); $borrow_id = $data['borrow_id']; $url = $data['url']; $res = Db::name('borrow_img')->insert( [ 'borrow_id' => $borrow_id, 'url' => $url ] ); if ($res) { $this->success('上传成功!!'); } else { $this->error('保存失败!!'); } } public function delImg(): void { $data = $this->request->post(); $borrow_id = $data['borrow_id']; $url = $data['url']; $res = Db::name('borrow_img')->where([ 'borrow_id' => $borrow_id, 'url' => $url ])->delete(); if ($res) { $this->success('删除成功!!'); } else { $this->error('删除失败!!'); } } }