浏览代码

数据迁移第一版备份

fuyu 5 年之前
父节点
当前提交
a4a7736506
共有 2 个文件被更改,包括 137 次插入9 次删除
  1. 133 9
      server/src/modules/import/controller.js
  2. 4 0
      server/src/modules/import/router.js

+ 133 - 9
server/src/modules/import/controller.js

@@ -42,7 +42,7 @@ import {
   Wechat_bind as OldWechat_bind,
   Wechat_pay_log as OldWechat_pay_log,
   Wechat_refund_log as OldWechat_refund_log
-} from '../../old_dao2'
+} from '../../old_dao'
 
 
 import {
@@ -124,6 +124,7 @@ export async function message(ctx) {
       await message.save();
     }
   }
+  console.log('同步完成')
   ctx.body = true;
 }
 
@@ -152,6 +153,7 @@ export async function reportCallback(report, { doctorMap }) {
 
 export async function exam(ctx) {
   let res = await limitMultipleCallBack(TestExams, 'exams', examCallbck);
+  console.log('同步完成')
   ctx.body = res;
 
   // name card_num studyid sex age birthday  exam_sub_class body_part device_name
@@ -321,11 +323,13 @@ export async function normalLimit(ctx) {
   // await copyLimit(OldImages, TestImages, 'id');
 
   // res.patient_infos = await copyLimit(OldPatient_infos, TestPatient_infos, 'id', 'patient_infos');
-  // res.studies = await copyLimit(OldStudies, TestStudies, 'id', 'studies');
+  res.studies = await copyLimit(OldStudies, TestStudies, 'id', 'studies');
   // res.exams = await copyLimit(OldExams, TestExams, 'id', 'exams');
   // res.report = await copyLimit(OldReport, TestReport, 'id', 'report');
+  // res.exams = await copyLimit(OldExams, TestExams, 'id', 'exams');
+
   // res.series = await copyLimit(OldSeries, TestSeries, 'id', 'series');
-  res.images = await copyLimit(OldImages, TestImages, 'id', 'images');
+  // res.images = await copyLimit(OldImages, TestImages, 'id', 'images');
 
   const fileUri = path.join(__dirname, '../../logs', 'normalLimit.log');
   fs.writeFileSync(fileUri, JSON.stringify(res));
@@ -455,18 +459,19 @@ async function copyLimit(oldDb, newDb, key, tableName) {
   let olds = await oldDb.findAll({
     limit,
     order,
-    // where: {
+    where: {
     //   id: {
     //     [Sequelize.Op.gt]: '116cc1a93d0ad836'
     //   }
-    // }
+      createdAt: {[Sequelize.Op.gte]: new Date('2019-11-28 12:00:00')}
+    }
   });
   while (olds && olds.length) {
     for (let i = 0; i < olds.length; i++) {
       const where = {};
       where[key] = olds[i][key];
       console.log('old', where)
-      const old = await newDb.findAll({ where });
+      const old = await newDb.findOne({ where });
       console.log('old', old)
       if (!old) {
         let obj = {...olds[i].dataValues };
@@ -494,8 +499,10 @@ async function copyLimit(oldDb, newDb, key, tableName) {
     olds = await oldDb.findAll({
       where: {
         id: {
-          [Sequelize.Op.gt]: id
+          [Sequelize.Op.gt]: id,
+          
         }
+        ,createdAt: {[Sequelize.Op.gte]: new Date('2019-11-28 12:00:00')}
       },
       limit,
       order
@@ -540,7 +547,8 @@ export async function limitCallBack(DbController, tableName, callback, param) {
       where: {
         id: {
           [Sequelize.Op.gt]: id
-        }
+        },
+        createdAt: {[Sequelize.Op.gte]: new Date('2019-11-28 12:00:00')}
       },
       limit,
       order
@@ -587,7 +595,8 @@ export async function limitMultipleCallBackSingle(DbController, tableName, callb
       study_id: {
         [Op.ne]: null
       },
-      studyuid: null
+      studyuid: null,
+      createdAt: {[Sequelize.Op.gte]: new Date('2019-11-28 12:00:00')}
     },
   });
   while (olds && olds.length) {
@@ -622,8 +631,123 @@ export async function limitMultipleCallBackSingle(DbController, tableName, callb
   }
 }
 
+let patientCounter  = 0;
+let studyCounter  = 0;
+let seriesCounter  = 0;
+let imageCounter  = 0;
+
+export async function importSinlgeHospital(ctx) {
+  const hospitalId = ctx.request.body.hospitalId;
+  // console.log('hospitalId', hospitalId)
+  // return
+  // const hospitalId = '24100002';
+  const patients = await OldPatient_infos.findAll({where:{institution_id: hospitalId, 
+    createdAt: {[Sequelize.Op.gte]: new Date('2020-01-14 12:00:00')}}})
+  for(let i = 0; i < patients.length; i++) {
+    await synchPatient(patients[i]);
+  }
+  ctx.body = {hospitalId, length: patients.length,patientCounter, studyCounter, seriesCounter, imageCounter}
+  console.log(patients.length)
+  console.log(patientCounter, studyCounter, seriesCounter, imageCounter)
+  // const limit = 1000;
+  // const order = [
+  //   ['id', 'asc']
+  // ];
+  // let where = {
+  // }
+  // let patients = await OldPatient_infos.findAll({limit, order})
+  
+  // while(true) {
+  //   let i = 0;
+  //   while(i < patients.length) {
+  //     await synchPatients(patients[i++]);
+  //   }
+  //   if(patients.length > 0) {
+  //     patients = await OldPatient_infos.findAll({limit, order, where: {
+  //       [Sequelize.Op.gt]: patients[patients.length - 1].id
+  //     }})
+  //   } else {
+      console.log('同步完成');
+      console.log('hospitalId', hospitalId)
+  //     return;
+  //   }
+  // }
+  
+}
+
+async function synchPatient(patient) {
+  patientCounter++
+  let old = await TestPatient_infos.findOne({where:{
+    id: patient.id
+  }});
+  console.log('old', old)
+  if(!old) {
+    await TestPatient_infos.create({...patient.dataValues})
+  }
+  await synchStudies(patient.id);
+// dataValues
+}
+
 
+async function synchStudies(patient_id) {
+  const studies = await OldStudies.findAll({where: {patient_id}})
+  for(let i = 0; i < studies.length; i++) {
+    await synchStudy(studies[i]);
+  }
+}
+
+async function synchStudy(study) {
+  let old = await TestStudies.findOne({where:{id: study.id}});
+  studyCounter++
+  if(!old) {
+    await TestStudies.create({...study.dataValues})
+  }
+  await synchSeries(study.id);
+}
+
+async function synchSeries(study_id) {
+  const series = await OldSeries.findAll({where: {study_id}})
+  for(let i = 0; i < series.length; i++) {
+    await synchSerie(series[i]);
+  }
+}
 
+async function synchSerie(serie) {
+  seriesCounter++
+  let old = await TestSeries.findOne({where:{id: serie.id}});
+  if(!old) {
+    await TestSeries.create({...serie.dataValues})
+  }
+  await synchImages(serie.id);
+}
+
+async function synchImages(series_id) {
+  const images = await OldImages.findAll({where: {series_id}})
+  for(let i = 0; i < images.length; i++) {
+    await synchImage(images[i]);
+  }
+}
+
+async function synchImage(image) {
+  imageCounter++;
+  let old = await TestImages.findOne({where:{id: image.id}});
+ 
+  if(!old) {
+    // console.log(image)
+    // throw new Error('xxx')
+    let i = {...image.dataValues}
+    i.imageId = image.image_id;
+    i.sopUid = image.sop_uid;
+    i.imageNumber = image.image_number;
+    i.windowWidth = image.window_width;
+    i.windoCenter = image.windo_center;
+    i.pixeSpacing = image.pixe_spacing;
+    i.imagePosition = image.image_position;
+    i.imageOrientation = image.image_orientation;
+    i.hospitalId = image.institution_id;
+    await TestImages.create(i)
+  }
+}
 
 // 2.0      
 // bbs  表          (对话表)

+ 4 - 0
server/src/modules/import/router.js

@@ -26,4 +26,8 @@ export default [{
   method: 'POST',
   route: '/remote',
   handlers: [_import.remoteApplication]
+}, {
+  method: 'POST',
+  route: '/importSinlgeHospital',
+  handlers: [_import.importSinlgeHospital]
 }]