123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /*
- * @Author: fuyu
- * @Date: 2021-03-23 12:00:09
- * @LastEditors: fuyu
- * @LastEditTime: 2021-03-26 10:44:40
- * @FilePath: /202103/code/sql_merge/src/service/move.js
- */
- import config from '../../config'
- const limit = config['limit']
- const FLAG_DELAULT = 0
- const FLAG_REPORT = 1
- const FLAG_PACS = 2
- const order = [['createdAt', 'DESC']]
- import {logSum, logSingle} from './log'
- import {RemoteApplication as OldRemoteApplication, ApplicationProgressLog as OldApplicationProgressLog, Bbs as OldBbs, RemoteOrder as OldRemoteOrder, RemoteWater as OldRemoteWater, Register as OldRegister, Exams as OldExams, PatientInfos as OldPatientInfos, Studies as OldStudies, Series as OldSeries, Images as OldImages, Report as OldReport, ReportRecord as OldReportRecord, QualityControl as OldQualityControl, QualityCause as OldQualityCause} from '../dao_old'
- import {Institution, RemoteApplication as NewRemoteApplication, ApplicationProgressLog as NewApplicationProgressLog, Bbs as NewBbs, RemoteOrder as NewRemoteOrder, RemoteWater as NewRemoteWater, Register as NewRegister, Exams as NewExams, PatientInfos as NewPatientInfos, Studies as NewStudies, Series as NewSeries, Images as NewImages, Report as NewReport, ReportRecord as NewReportRecord, QualityControl as NewQualityControl, QualityCause as NewQualityCause} from '../dao_new'
- export async function moveAll(institution_id) {
- if(institution_id) {
- await moveSingle(institution_id)
- return
- }
- const ids = await Institution.findAll({attributes: ['id']})
- for(let i = 0; i < ids.length; i++) {
- await moveSingle(ids[i]['id'])
- }
- }
- export async function movePacs(institution_id, offset) {
- if(institution_id) {
- await moveSinglePacs(institution_id, offset)
- return
- }
- const ids = await Institution.findAll({attributes: ['id']})
- for(let i = 0; i < ids.length; i++) {
- await moveSinglePacs(ids[i]['id'])
- }
- }
- const moveSinglePacs = async (institution_id, offset = 0) => {
- let count = limit, sum = 0
- while(count === limit) {
- count = await moveExamsPacs(institution_id, offset)
- sum += count
- }
- logSum('pacs-' + institution_id, sum)
- }
- const moveExamsPacs = async (institution_id, offset) => {
- let exmas = await OldExams.findAll({where: {institution_id, flag: FLAG_REPORT}, limit, order, offset})
- for(let i = 0; i < exmas.length; i++) {
- await moveExamPacs(exmas[i])
- }
- return exmas.length
- }
- const moveExamPacs = async (exam) => {
- await fixPacs(exam.study_id, exam.patient_id)
- await updateExamFlag(exam, FLAG_PACS)
- }
- const moveSingle = async institution_id => {
- let count = limit, sum = 0
- while(count === limit) {
- count = await moveExams(institution_id)
- sum += count
- }
- logSum(institution_id, sum)
- }
- const moveExams = async (institution_id) => {
- let exmas = await OldExams.findAll({where: {institution_id, flag: FLAG_DELAULT}, limit, order})
- for(let i = 0; i < exmas.length; i++) {
- await moveExam(exmas[i])
- }
- return exmas.length
- }
- const moveExam = async (exam) => {
- return await fixCommon(NewExams, OldExams, {id: exam.id}, fixExam)
- }
- const fixExam = async(exam) => {
- // 同步PACS
- // await fixPacs(exam.study_id, exam.patient_id)
- // 同步报告
- await fixReports(exam.id)
- // 同步远程诊断
- await fixRemotes(exam.id)
- // 同步登记
- await fixRegisters(exam.id)
- // 更新检查标识
- await updateExamFlag(exam, 1)
- }
- const fixCommon = async (NewClass, OldClass, where, cb) => {
- let newObj = await NewClass.findOne({where})
- if(!newObj) {
- const old = await OldClass.findOne({where})
- newObj = await copy(NewClass, old)
- }
- if(!cb || typeof cb !== 'function') {
- return newObj
- }
- return await cb(newObj)
- }
- const fixCommons = async (NewClass, OldClass, where, cb) => {
- let olds = await OldClass.findAll({where})
- if(!(olds && olds.length)) {
- return false
- }
- for(let i = 0; i < olds.length; i++) {
- await fixCommon(NewClass, OldClass, {id: olds[i].id}, cb)
- }
- return olds
- }
- const fixReports = async exam_id => {
- await fixCommons(NewReport, OldReport, {exam_id}, fixReportChild)
- }
- const fixReportChild = async report => {
- // 同步报告记录
- await fixReportRecords(report.id)
- // 同步质控
- await fixQualityControls(report.id)
- return report
- }
- const fixReportRecords = async report_id => {
- // 同步报告记录
- await fixCommons(NewReportRecord, OldReportRecord, {report_id})
- }
- const fixQualityControls = async report_id => {
- // 同步质控
- await fixCommons(NewQualityControl, OldQualityControl, {report_id}, fixQualityControlChild)
- }
- const fixQualityControlChild = async qualityControl => {
- // 同步质控评价因子
- await fixCommons(NewQualityCause, OldQualityCause, {control_id: qualityControl.id})
- }
- const fixRemotes = async exam_id => {
- // 同步远程诊断申请
- await fixCommons(NewRemoteApplication, OldRemoteApplication, {exam_id}, fixRemoteChild)
- }
- const fixRemoteChild = async remoteApplication => {
- // 同步远程诊断申请流程日志
- await fixCommons(NewApplicationProgressLog, OldApplicationProgressLog, {application_id: remoteApplication.id})
- // 同步BBS
- await fixCommons(NewBbs, OldBbs, {remote_application_id: remoteApplication.id})
- // 同步远程诊断订单
- await fixCommons(NewRemoteOrder, OldRemoteOrder, {application_id: remoteApplication.id}, fixRemoteOrderChild)
- }
- const fixRemoteOrderChild = async remoteOrder => {
- // 同步远程诊断订单流水
- await fixCommons(NewRemoteWater, OldRemoteWater, {order_id: remoteOrder.id})
- }
- const fixRegisters = async exam_id => {
- // 同步登记
- await fixCommons(NewRegister, OldRegister, {exam_id})
- }
- const fixPacs = async (study_id, patient_id) => {
- // 同步患者
- await fixCommon(NewPatientInfos, OldPatientInfos, {id: patient_id})
- // 同步检查
- await fixCommon(NewStudies, OldStudies, {id: study_id})
- // 同步系列
- await fixCommons(NewSeries, OldSeries, {study_id}, fixSerieChild)
- }
- const fixSerieChild = async serie => {
- // 同步image
- await fixCommons(NewImages, OldImages, {series_id: serie.id})
- }
- const updateExamFlag = async (exam, flag) => {
- const old = await OldExams.findOne({where: {id: exam.id}})
- old.flag = flag;
- await old.save()
- const newExam = await NewExams.findOne({where: {id: exam.id}})
- newExam.flag = flag;
- await newExam.save()
- return true
- }
- const copy = async (obj, old) => {
- if(!(old && obj)) {
- return false
- }
- logSingle(obj['tableName'])
- return await obj.create(old['dataValues'])
- }
|