Sfoglia il codice sorgente

修正导入数据是富文本的情况

lrf 8 mesi fa
parent
commit
409b70a13d

+ 1 - 0
src/config/config.default.ts

@@ -20,4 +20,5 @@ export default {
   PathConfig: {
     path: 'C:\\temp\\cxyy\\',
   },
+  showDBSub: false,
 } as MidwayConfig;

+ 1 - 0
src/controller/home.controller.ts

@@ -31,6 +31,7 @@ export class HomeController {
 
   @Get('/')
   async home(@Query() query: object): Promise<any> {
+    await this.dataDealService.correctImportData()
     // await this.esService.initES()
     // await this.dataDealService.initUserMenus();
     // await this.dataDealService.initRoleMenus();

+ 8 - 6
src/event/dbSubscriber.ts

@@ -14,11 +14,13 @@ import { Methods } from '../public/var';
 export class DbSubscriber implements EntitySubscriberInterface {
   @Config('modulesConfig.es')
   esServiceHttpPrefix: any;
+  @Config('showDBSub')
+  showDBSub: any;
   /**
    * 添加前触发
    */
   beforeInsert(event: InsertEvent<any>) {
-    console.log('BEFORE ENTITY INSERTED: ', event.entity);
+    if (this.showDBSub) console.log('BEFORE ENTITY INSERTED: ', event.entity);
     // console.log(this.ctx.request);
     // console.log(event.metadata.givenTableName);
   }
@@ -27,21 +29,21 @@ export class DbSubscriber implements EntitySubscriberInterface {
    * 修改前触发
    */
   beforeUpdate(event: UpdateEvent<any>) {
-    console.log('BEFORE ENTITY UPDATED: ', event.entity);
+    if (this.showDBSub) console.log('BEFORE ENTITY UPDATED: ', event.entity);
   }
 
   /**
    * 删除前触发
    */
   beforeRemove(event: RemoveEvent<any>) {
-    console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
+    if (this.showDBSub) console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
   }
 
   /**
    * 添加后触发
    */
   afterInsert(event: InsertEvent<any>) {
-    console.log('AFTER ENTITY INSERTED: ', event.entity);
+    if (this.showDBSub) console.log('AFTER ENTITY INSERTED: ', event.entity);
     const tableName = get(event, 'metadata.givenTableName');
     const data = event.entity;
     const m = Methods.CREATE;
@@ -52,7 +54,7 @@ export class DbSubscriber implements EntitySubscriberInterface {
    * 修改后触发
    */
   afterUpdate(event: UpdateEvent<any>) {
-    console.log('AFTER ENTITY UPDATED: ', event.entity);
+    if (this.showDBSub) console.log('AFTER ENTITY UPDATED: ', event.entity);
     const tableName = get(event, 'metadata.givenTableName');
     const data = event.entity;
     const m = Methods.UPDATE;
@@ -63,7 +65,7 @@ export class DbSubscriber implements EntitySubscriberInterface {
    * 删除后触发
    */
   afterRemove(event: RemoveEvent<any>) {
-    console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
+    if (this.showDBSub) console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
     const tableName = get(event, 'metadata.givenTableName');
     const data = event.entity;
     const m = Methods.DELETE;

+ 62 - 2
src/service/initData/dataDeal.service.ts

@@ -2,7 +2,7 @@ import { Provide } from '@midwayjs/core';
 import { InjectDataSource, InjectEntityModel } from '@midwayjs/typeorm';
 import { DataSource, In, Repository } from 'typeorm';
 import { Menus } from '../../entity/system/menus.entity';
-import { get, isNull, isUndefined, omit } from 'lodash';
+import { cloneDeep, get, isNull, isNumber, isObject, isUndefined, omit } from 'lodash';
 import { User } from '../../entity/system/user.entity';
 import { Admin } from '../../entity/system/admin.entity';
 import { Achievement } from '../../entity/platform/achievement.entity';
@@ -19,6 +19,8 @@ import { Supply } from '../../entity/platform/supply.entity';
 import { Support } from '../../entity/platform/support.entity';
 import { UserMenus } from '../../entity/system/userMenus.entity';
 import { Role } from '../../entity/system/role.entity';
+import { Expert } from '../../entity/users/expert.entity';
+import { Company } from '../../entity/users/company.entity';
 
 @Provide()
 export class DataDealService {
@@ -55,12 +57,70 @@ export class DataDealService {
 
   @InjectEntityModel(UserMenus)
   UserMenus: Repository<UserMenus>;
-
+  @InjectEntityModel(Expert)
+  Expert: Repository<Expert>;
+  @InjectEntityModel(Company)
+  Company: Repository<Company>;
   @InjectEntityModel(Role)
   Role: Repository<Role>;
 
   @InjectDataSource('default')
   defaultDataSource: DataSource;
+  /**修正导入的数据,主要是内容变成object的数据,把richText取出来拼一起 */
+  async correctImportData() {
+    const config = [
+      { model: this.Demand, entity: Demand, fields: ['field', 'company', 'brief', 'contacts', 'year', 'industry', 'company', 'tec_name', 'company_brief', 'question', 'month', 'tel'] },
+      { model: this.Achievement, entity: Achievement, fields: ['field', 'name', 'brief', 'source', 'person', 'tel'] },
+      { model: this.Expert, entity: Expert, fields: ['industry_type', 'industry', 'work_type', 'name', 'work', 'title'] },
+      { model: this.Company, entity: Company, fields: ['name', 'products', 'type', 'person'] },
+      { model: this.Supply, entity: Supply, fields: ['industry', 'field', 'name', 'brief', 'source'] },
+      { model: this.Project, entity: Project, fields: ['name', 'brief', 'main', 'progress', 'track_unit', 'source'] },
+      { model: this.Match, entity: Match, fields: ['name', 'address'] },
+    ];
+    const limit = 50;
+    for (const c of config) {
+      const { model, fields, entity } = c;
+      let skip = 0;
+      let isEmpty = false;
+      while (!isEmpty) {
+        const list = await model.find({ skip, take: limit });
+        if (list.length <= 0) {
+          isEmpty = true;
+          break;
+        }
+        for (const i of list) {
+          let needUpdate = false;
+          const updateObj = {};
+          for (const f of fields) {
+            const fd = get(i, f);
+            if (!fd) continue;
+            try {
+              if (isNumber(fd)) {
+                continue;
+              } else if (fd.includes('richText')) {
+                // 处理值,然后修改 修改标记 过滤完所有字段后,进行修改
+                try {
+                  const obj = JSON.parse(fd);
+                  const str = obj.richText.reduce((p, n) => `${p}${n.text}`, '');
+                  updateObj[f] = str;
+                  needUpdate = true;
+                } catch (error) {
+                  console.log('update field check', model.metadata.name, `id:${i.id}`);
+                }
+              }
+              if (needUpdate) {
+                await model.createQueryBuilder().update(entity).set(updateObj).where('id =:id', { id: i.id }).execute();
+              }
+            } catch (error) {
+              console.log('field data check', model.metadata.name, `id:${i.id}`, f);
+              console.log(i);
+            }
+          }
+        }
+        skip = skip + limit;
+      }
+    }
+  }
 
   /**初始化用户目录,先去数据库把表清空,顺便重置下id索引 */
   async initUserMenus() {