浏览代码

feat: new first & toPointer & data.js

未来全栈 4 月之前
父节点
当前提交
82ff3cc8f8
共有 4 个文件被更改,包括 194 次插入29 次删除
  1. 10 4
      wisdom-prod/schema/schema.md
  2. 34 3
      wisdom-server/lib/ncloud.js
  3. 97 0
      wisdom-server/migration/data.js
  4. 53 22
      wisdom-server/migration/import-data.js

+ 10 - 4
wisdom-prod/schema/schema.md

@@ -18,16 +18,22 @@
 
 2. **Doctor(医生表)**
    - objectId: String
-   - createdAt: Date
-   - user: Pointer<User>
+   - name: String 医生名称
+   - title: String 医生职称
+   - desc: String 医生描述
+   - gender: String 性别
+   - age: String 年龄
    - specialty: String(专业领域)
    - qualifications: String(资格证书)
+   - depart: Pointer<Department>
+   - createdAt: Date
+   - user: Pointer<User>
 
 3. **Department(科室表)**
    - objectId: String
+   - name: String
+   - desc: String
    - createdAt: Date
-   - departmentName: String
-   - description: String
 
 4. **Appointment(预约表)**
    - objectId: String

+ 34 - 3
wisdom-server/lib/ncloud.js

@@ -7,7 +7,9 @@ class CloudObject{
     constructor(className){
         this.className = className
     }
-
+    toPointer(){
+        return {"__type":"Pointer","className":this.className,"objectId":this.id}
+    }
     set(json){
         Object.keys(json).forEach(key=>{
             if(["objectId","id","createdAt","updatedAt","ACL"].indexOf(key)>-1){
@@ -24,7 +26,7 @@ class CloudObject{
         let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className
         // 更新
         if(this.id){
-            url = "/"+this.id
+            url += "/"+this.id
             method = "PUT"
         } 
         let body = JSON.stringify(this.data)
@@ -39,6 +41,9 @@ class CloudObject{
             "credentials": "omit"
           });
           let result = await response?.json();
+          if(result?.error){
+            console.error(result?.error)
+          }
           if(result?.objectId){this.id = result?.objectId}
           return this
     }
@@ -125,8 +130,34 @@ class CloudQuery{
         let json = await response?.json();
         return json?.results || []
     }
-    first(){
+    async first(){
+        let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
+        
+        if(Object.keys(this.whereOptions)?.length){
+            let whereStr = JSON.stringify(this.whereOptions)
+            url += `where=${whereStr}`
+        }
 
+        let response = await fetch(url, {
+            "headers": {
+            "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
+            "x-parse-application-id": "dev"
+            },
+            "body": null,
+            "method": "GET",
+            "mode": "cors",
+            "credentials": "omit"
+        });
+        let json = await response?.json();
+        let exists = json?.results?.[0] || null
+        if(exists){
+            let existsObject = new CloudObject(this.className)
+            existsObject.set(exists)
+            existsObject.id = exists.objectId
+            existsObject.createdAt = exists.createdAt
+            existsObject.updatedAt = exists.updatedAt
+            return existsObject
+        }
     }
 }
 

+ 97 - 0
wisdom-server/migration/data.js

@@ -0,0 +1,97 @@
+module.exports.DoctorList = [
+    {
+      "objectId": "doc001",
+      "name": "张伟",
+      "title": "主任医师",
+      "desc": "拥有20年内科临床经验,擅长心血管疾病的治疗",
+      "gender": "男",
+      "age": 45,
+      "specialty": "内科",
+      "qualifications": ["医学博士,内科专科医生"],
+      "depart": {
+        "objectId": "dept001"
+      }
+    },
+    {
+      "objectId": "doc002",
+      "avatar":"https://nova-cloud.obs.cn-south-1.myhuaweicloud.com/storage/aigc/imagine/Q4Zif7fTbK-0.png",
+      "name": "李娜",
+      "title": "主任医师",
+      "desc": "外科领域专家,擅长微创手术",
+      "gender": "女",
+      "age": 50,
+      "specialty": "外科",
+      "qualifications": ["外科专科医生,硕士研究生"],
+      "depart": {
+        "objectId": "dept002"
+      }
+    },
+    {
+      "objectId": "doc003",
+      "name": "王芳",
+      "title": "主任医师",
+      "desc": "儿童健康专家,擅长儿童生长发育",
+      "gender": "女",
+      "age": 40,
+      "specialty": "儿科",
+      "qualifications": ["儿科专科医生,医学硕士"],
+      "depart": {
+        "objectId": "dept003"
+      }
+    },
+    {
+      "objectId": "doc004",
+      "name": "刘强",
+      "title": "主任医师",
+      "desc": "妇产科专家,专注于高危妊娠管理",
+      "gender": "男",
+      "age": 48,
+      "specialty": "妇产科",
+      "qualifications": ["妇产科专科医生,博士研究生"],
+      "depart": {
+        "objectId": "dept004"
+      }
+    },
+    {
+      "objectId": "doc005",
+      "name": "陈静",
+      "title": "主任医师",
+      "desc": "神经科专家,擅长癫痫和头痛的治疗",
+      "gender": "女",
+      "age": 42,
+      "specialty": "神经科",
+      "qualifications": ["神经科专科医生,医学博士"],
+      "depart": {
+        "objectId": "dept005"
+      }
+    }
+  ]
+
+module.exports.DepartList = [
+
+      {
+        "objectId": "dept001",
+        "name": "内科",
+        "desc": "负责内科疾病的诊断和治疗"
+      },
+      {
+        "objectId": "dept002",
+        "name": "外科",
+        "desc": "负责外科手术和相关疾病的治疗"
+      },
+      {
+        "objectId": "dept003",
+        "name": "儿科",
+        "desc": "专注于儿童疾病的预防和治疗"
+      },
+      {
+        "objectId": "dept004",
+        "name": "妇产科",
+        "desc": "负责女性生殖系统及相关疾病的治疗"
+      },
+      {
+        "objectId": "dept005",
+        "name": "神经科",
+        "desc": "专注于神经系统疾病的诊断和治疗"
+      }
+    ]

+ 53 - 22
wisdom-server/migration/import-data.js

@@ -1,27 +1,58 @@
 const { CloudQuery, CloudObject } = require("../lib/ncloud");
-// testCRUD()
-testQuery()
-async function testQuery(){
-    let query = new CloudQuery("Doctor")
-    // query.equalTo("gender","女")
-    query.greaterThanAndEqualTo("age",40)
-    query.lessThan("age",41)
-    let list = await query.find();
-    console.log(list)
+const { DepartList, DoctorList } = require("./data");
+inportDapartAndDoctor()
+
+DataMap = {
+    Doctor:{},
+    Department:{}
+}
+
+async function inportDapartAndDoctor(){
+    // 导入科室数据
+    let departList = DepartList
+    for (let index = 0; index < departList.length; index++) {
+        let depart = departList[index];
+        depart = await importObject("Department",depart)
+    }
+    // 导入医生数据
+    let doctorList = DoctorList
+    for (let index = 0; index < doctorList.length; index++) {
+        let doctor = doctorList[index];
+        doctor = await importObject("Doctor",doctor)
+    }
+    // console.log(DataMap["Doctor"])
 }
 
-async function testCRUD(){
-    // 基本的增删查改测试
-    let query = new CloudQuery("Doctor")
-    let doctorList = await query.find();
-    console.log("doctorList count",doctorList?.length)
+async function importObject(className,data){
 
-    let newDoctor = new CloudObject("Doctor")
-    newDoctor.set({"name":"123"})
-    
-    newDoctor = await newDoctor.save(newDoctor)
-    console.log("newDoctor",newDoctor)
+    // 查重 srcId 数据源列表中的objectId并非数据库生成的唯一ID,因此需要有一个srcId字段进行记录,并查重
+    let query = new CloudQuery(className)
+    let srcId = data.objectId
+    query.equalTo("srcId",srcId)
+    let importObj = await query.first()
+    console.log(importObj)
 
-    await newDoctor.destory()
-    console.log("newDoctor 已删除",newDoctor)
-}
+    // 导入
+    // 导入前批量处理Pointer类型数据,进行重定向
+    Object.keys(data)?.forEach(key=>{
+        let field = data[key]
+        let srcId = field?.objectId
+        if(srcId){ // 是数组字段
+            if(key=="depart"){
+                data[key] = DataMap?.["Department"]?.[srcId]?.toPointer();
+            }
+        }
+    })
+
+    // 若未添加,则创建新对象并保存
+    if(!importObj?.id){
+        importObj = new CloudObject(className)
+    }
+
+    // 保存或更新数据
+    data.srcId = srcId;
+    importObj.set(data);
+    importObj = await importObj.save();
+
+    DataMap[className][srcId] = importObj
+}