model = new BorrowModel; $this->BorrowAccess = new BorrowAccess(); $this->BorrowNumber = new BorrowNumber(); } 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(); // halt($where, $alias, $limit, $order); if($user_group_id == 1 || $user_group_id == 2){ $res = $this->model ->alias($alias) ->where($where) ->where("status",'in',[1,4]) ->where("approval_person_id",$this->auth->id) ->order($order) ->paginate($limit); }else if($user_group_id == 3){ $res = $this->model ->alias($alias) ->where($where) ->where("status",'in',[4,7]) ->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',[4]) ->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'); } if(!isset($data['asset']) || $data['asset'] == []){ $this->error("仪器选择不能为空!!"); } $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); } } $groupImg = []; if(isset($data['fileList'])){ 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; $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 ]); } }