Bläddra i källkod

jsonb数据带有默认及给现有数据添加默认值,避免出现错误

lrf 9 månader sedan
förälder
incheckning
65c255cde3

+ 10 - 6
src/controller/home.controller.ts

@@ -1,22 +1,25 @@
 import { Controller, Get, Inject } from '@midwayjs/core';
-import { initTwoService } from '../service/initData/initTwo.service';
+import { InitTwoService } from '../service/initData/initTwo.service';
 import { InitRegionService } from '../service/initData/initRegion.service';
-import { initOneService } from '../service/initData/initOne.service';
+import { InitOneService } from '../service/initData/initOne.service';
 import { QichachaService } from '../service/thirdParty/qichacha.service';
-import { initThreeService } from '../service/initData/initThree.service';
+import { InitThreeService } from '../service/initData/initThree.service';
+import { DataDealService } from '../service/initData/dataDeal.service';
 
 @Controller('/')
 export class HomeController {
   @Inject()
-  threeService: initThreeService;
+  threeService: InitThreeService;
   @Inject()
-  twoService: initTwoService;
+  twoService: InitTwoService;
   @Inject()
-  oneService: initOneService;
+  oneService: InitOneService;
   @Inject()
   regionService: InitRegionService;
   @Inject()
   qichachaService: QichachaService;
+  @Inject()
+  dataDealService: DataDealService;
   @Get('/')
   async home(): Promise<any> {
     // await this.oneService.addTags();
@@ -29,6 +32,7 @@ export class HomeController {
     // await this.threeService.import2021Company();
     // await this.threeService.import2024Company();
     // await this.threeService.importMatching();
+    // await this.dataDealService.JsonbAddDefault();
     return 'ok';
     // return 'starting...';
   }

+ 3 - 3
src/entity/platform/achievement.entity.ts

@@ -9,7 +9,7 @@ export class Achievement extends BaseModel {
   user: number;
   @Column({ type: 'character varying', nullable: true, comment: '所属产业' })
   industry: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '专利号' })
   patent: string;
@@ -25,7 +25,7 @@ export class Achievement extends BaseModel {
   field: string;
   @Column({ type: 'character varying', nullable: true, comment: '技术分类' })
   technology: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '成果地区' })
+  @Column({ type: 'jsonb', nullable: true, comment: '成果地区', default: [] })
   area: Array<any>;
   @Column({ type: 'timestamp without time zone', nullable: true, comment: '发布时间', transformer: { from: value => dayjs(value).format('YYYY-MM-DD HH:mm:ss'), to: value => value } })
   time: Date;
@@ -33,7 +33,7 @@ export class Achievement extends BaseModel {
   money: string;
   @Column({ type: 'text', nullable: true, comment: '简介' })
   brief: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '附件' })
+  @Column({ type: 'jsonb', nullable: true, comment: '附件', default: [] })
   file: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '成果状态', default: '0' })
   achievement_status: string;

+ 2 - 2
src/entity/platform/demand.entity.ts

@@ -5,7 +5,7 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Demand extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '平台用户id' })
   user: number;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
@@ -23,7 +23,7 @@ export class Demand extends BaseModel {
   end_time: string;
   @Column({ type: 'character varying', nullable: true, comment: '价格' })
   money: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '需求地区' })
+  @Column({ type: 'jsonb', nullable: true, comment: '需求地区', default: [] })
   area: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '简介' })
   brief: string;

+ 5 - 5
src/entity/platform/design.entity.ts

@@ -13,14 +13,14 @@ export class Design extends BaseModel {
   agreement: string;
   @Column({ type: 'text', nullable: true, comment: '关于我们' })
   brief: string;
-  @Column({ type: 'jsonb', nullable: true, comment: 'logo' })
+  @Column({ type: 'jsonb', nullable: true, comment: 'logo', default: [] })
   logoUrl: Array<any>;
-  @Column({ type: 'jsonb', nullable: true, comment: '轮播图' })
+  @Column({ type: 'jsonb', nullable: true, comment: '轮播图', default: [] })
   carouselUrl: Array<any>;
-  @Column({ type: 'jsonb', nullable: true, comment: '合作伙伴' })
+  @Column({ type: 'jsonb', nullable: true, comment: '合作伙伴', default: [] })
   friend: Array<any>;
-  @Column({ type: 'jsonb', nullable: true, comment: '友情连接' })
+  @Column({ type: 'jsonb', nullable: true, comment: '友情连接', default: [] })
   friendship: Array<any>;
-  @Column({ type: 'jsonb', nullable: true, comment: '网站底部信息' })
+  @Column({ type: 'jsonb', nullable: true, comment: '网站底部信息', default: {} })
   footInfo: object;
 }

+ 3 - 3
src/entity/platform/footplate.entity.ts

@@ -5,11 +5,11 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Footplate extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '平台用户id' })
   user: number;
-  @Column({ type: 'jsonb', nullable: true, comment: '封面' })
+  @Column({ type: 'jsonb', nullable: true, comment: '封面', default: [] })
   file: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '所属产业' })
   industry: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
@@ -19,7 +19,7 @@ export class Footplate extends BaseModel {
   operate: string;
   @Column({ type: 'character varying', nullable: true, comment: '服务产业领域' })
   field: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '所在地区' })
+  @Column({ type: 'jsonb', nullable: true, comment: '所在地区', default: [] })
   area: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '地址' })
   address: string;

+ 1 - 1
src/entity/platform/journal.entity.ts

@@ -5,7 +5,7 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Journal extends BaseModel {
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '封面' })
+  @Column({ type: 'jsonb', nullable: true, comment: '封面', default: [] })
   file: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '简介' })
   brief: string;

+ 2 - 2
src/entity/platform/match.entity.ts

@@ -5,7 +5,7 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Match extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '平台用户id' })
   user: number;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
@@ -29,7 +29,7 @@ export class Match extends BaseModel {
   rules: object;
   @Column({ type: 'text', nullable: true, comment: '简介' })
   brief: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '封面' })
+  @Column({ type: 'jsonb', nullable: true, comment: '封面', default: [] })
   file: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '赛事状态', default: '0' })
   match_status: string;

+ 1 - 1
src/entity/platform/news.entity.ts

@@ -4,7 +4,7 @@ import * as dayjs from 'dayjs';
 // 新闻
 @Entity('news')
 export class News extends BaseModel {
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '标题' })
   title: string;

+ 1 - 1
src/entity/platform/notes.entity.ts

@@ -7,7 +7,7 @@ export class Notes extends BaseModel {
   journal: number;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '封面' })
+  @Column({ type: 'jsonb', nullable: true, comment: '封面', default: [] })
   file: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '委托方' })
   client: string;

+ 2 - 2
src/entity/platform/project.entity.ts

@@ -5,7 +5,7 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Project extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '平台用户id' })
   user: number;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
@@ -21,7 +21,7 @@ export class Project extends BaseModel {
   field: string;
   @Column({ type: 'character varying', nullable: true, comment: '合作类型' })
   cooperate: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '项目地区' })
+  @Column({ type: 'jsonb', nullable: true, comment: '项目地区', default: [] })
   area: Array<any>;
   @Column({ type: 'text', nullable: true, comment: '简介' })
   brief: string;

+ 2 - 2
src/entity/platform/sector.entity.ts

@@ -7,9 +7,9 @@ export class Sector extends BaseModel {
   title: string;
   @Column({ type: 'character varying', nullable: true, comment: '路由' })
   href: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '背景图' })
+  @Column({ type: 'jsonb', nullable: true, comment: '背景图', default: [] })
   file: Array<any>;
-  @Column({ type: 'jsonb', nullable: true, comment: '合作伙伴' })
+  @Column({ type: 'jsonb', nullable: true, comment: '合作伙伴', default: [] })
   partner: Array<any>;
   @Column({ type: 'integer', nullable: true, comment: '排序' })
   sort: number;

+ 2 - 2
src/entity/platform/supply.entity.ts

@@ -5,7 +5,7 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Supply extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '平台用户id' })
   user: number;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签', default: [] })
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
@@ -23,7 +23,7 @@ export class Supply extends BaseModel {
   end_time: string;
   @Column({ type: 'character varying', nullable: true, comment: '价格' })
   money: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '供给地区' })
+  @Column({ type: 'jsonb', nullable: true, comment: '供给地区', default: [] })
   area: Array<any>;
   @Column({ type: 'text', nullable: true, comment: '简介' })
   brief: string;

+ 3 - 3
src/entity/platform/support.entity.ts

@@ -5,11 +5,11 @@ import { BaseModel } from '../../frame/BaseModel';
 export class Support extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '平台用户id' })
   user: number;
-  @Column({ type: 'jsonb', nullable: true, comment: '封面' })
+  @Column({ type: 'jsonb', nullable: true, comment: '封面', default: [] })
   file: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '所属产业' })
   industry: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '标签' })
+  @Column({ type: 'jsonb', nullable: true, comment: '标签' , default: []})
   tags: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '名称' })
   name: string;
@@ -17,7 +17,7 @@ export class Support extends BaseModel {
   field: string;
   @Column({ type: 'character varying', nullable: true, comment: '登记时间' })
   time: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '所在地区' })
+  @Column({ type: 'jsonb', nullable: true, comment: '所在地区', default: [] })
   area: Array<any>;
   @Column({ type: 'character varying', nullable: true, comment: '地址' })
   address: string;

+ 1 - 1
src/entity/system/admin.entity.ts

@@ -23,7 +23,7 @@ export class Admin extends BaseModel {
     },
   })
   password: string; //TODO:需要重新加密
-  @Column({ type: 'jsonb', nullable: true, comment: '角色id数组' })
+  @Column({ type: 'jsonb', nullable: true, comment: '角色id数组', default: [] })
   role: string[];
   @Column({ type: 'integer', nullable: true, comment: '部门id' })
   dept: number;

+ 1 - 1
src/entity/system/menus.entity.ts

@@ -27,7 +27,7 @@ export class Menus extends BaseModel {
   type: string;
   @Column({ type: 'character varying', nullable: true, comment: '图标' })
   icon: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '功能列表 不在功能列表中的功能不能使用' })
+  @Column({ type: 'jsonb', nullable: true, comment: '功能列表 不在功能列表中的功能不能使用', default: [] })
   config: Array<any>;
   @Column({ type: 'character varying', comment: '是否为默认目录 默认:0,非默认:1; 默认不能删除', default: '1' })
   is_default: string;

+ 1 - 1
src/entity/system/user.entity.ts

@@ -33,7 +33,7 @@ export class User extends BaseModel {
   phone: string;
   @Column({ type: 'character varying', nullable: true, comment: '电子邮箱' })
   email: string;
-  @Column({ type: 'jsonb', nullable: true, comment: '角色id数组' })
+  @Column({ type: 'jsonb', nullable: true, comment: '角色id数组', default: [] })
   role: string[];
   @Column({ type: 'character varying', default: '1', comment: '状态: 0:未审核;1:已审核' })
   status: string;

+ 107 - 0
src/service/initData/dataDeal.service.ts

@@ -0,0 +1,107 @@
+import { Provide } from '@midwayjs/core';
+import { InjectEntityModel } from '@midwayjs/typeorm';
+import { Repository } from 'typeorm';
+import { Menus } from '../../entity/system/menus.entity';
+import { get, isNull, isUndefined } from 'lodash';
+import { User } from '../../entity/system/user.entity';
+import { Admin } from '../../entity/system/admin.entity';
+import { Achievement } from '../../entity/platform/achievement.entity';
+import { Demand } from '../../entity/platform/demand.entity';
+import { Design } from '../../entity/platform/design.entity';
+import { Footplate } from '../../entity/platform/footplate.entity';
+import { Journal } from '../../entity/platform/journal.entity';
+import { Match } from '../../entity/platform/match.entity';
+import { News } from '../../entity/platform/news.entity';
+import { Notes } from '../../entity/platform/notes.entity';
+import { Project } from '../../entity/platform/project.entity';
+import { Sector } from '../../entity/platform/sector.entity';
+import { Supply } from '../../entity/platform/supply.entity';
+import { Support } from '../../entity/platform/support.entity';
+
+@Provide()
+export class DataDealService {
+  @InjectEntityModel(Menus)
+  Menus: Repository<Menus>;
+  @InjectEntityModel(User)
+  User: Repository<User>;
+  @InjectEntityModel(Admin)
+  Admin: Repository<Admin>;
+  @InjectEntityModel(Achievement)
+  Achievement: Repository<Achievement>;
+  @InjectEntityModel(Demand)
+  Demand: Repository<Demand>;
+  @InjectEntityModel(Design)
+  Design: Repository<Design>;
+  @InjectEntityModel(Footplate)
+  Footplate: Repository<Footplate>;
+  @InjectEntityModel(Journal)
+  Journal: Repository<Journal>;
+  @InjectEntityModel(Match)
+  Match: Repository<Match>;
+  @InjectEntityModel(News)
+  News: Repository<News>;
+  @InjectEntityModel(Notes)
+  Notes: Repository<Notes>;
+  @InjectEntityModel(Project)
+  Project: Repository<Project>;
+  @InjectEntityModel(Sector)
+  Sector: Repository<Sector>;
+  @InjectEntityModel(Supply)
+  Supply: Repository<Supply>;
+  @InjectEntityModel(Support)
+  Support: Repository<Support>;
+
+  count = 0;
+
+  isNoValue(val) {
+    return isNull(val) || isUndefined(val);
+  }
+
+  async JsonbAddDefault() {
+    // const menusRes = await this.menusModel.createQueryBuilder().where()
+    const modelList = ['Menus', 'User', 'Admin', 'Achievement', 'Demand', 'Design', 'Footplate', 'Journal', 'Match', 'News', 'Notes', 'Project', 'Sector', 'Supply', 'Support'];
+    // const modelList = ['Design'];
+    for (const mn of modelList) {
+      const model = get(this, mn);
+      if (!model) continue;
+      await this.getModelJsonbConfig(model);
+    }
+    console.log(this.count);
+  }
+  async getModelJsonbConfig(model) {
+    const columns = model.metadata.columns;
+    const defaultDataObject = {};
+    const props = [];
+    for (const c of columns) {
+      if (get(c, 'type') !== 'jsonb') continue;
+      const propertyName = get(c, 'propertyName');
+      const defaultVal = get(c, 'default');
+      defaultDataObject[propertyName] = defaultVal;
+      props.push(propertyName);
+    }
+    // console.group(get(model, 'name'));
+    // console.log(defaultDataObject);
+    // console.log(props);
+    if (props.length <= 0) return;
+    const builder = model.createQueryBuilder();
+    for (let i = 0; i < props.length; i++) {
+      const prop = props[i];
+      if (i === 0) {
+        builder.where(`"${prop}" Is NULL`);
+      } else {
+        builder.orWhere(`"${prop}" Is NULL`);
+      }
+    }
+    const list = await builder.getMany();
+    for (const i of list) {
+      const updateData = {};
+      for (const p of props) {
+        const val = get(i, p);
+        if (this.isNoValue(val)) updateData[p] = defaultDataObject[p];
+      }
+      await model.update(i.id, updateData);
+      this.count = this.count + 1;
+    }
+    // console.groupEnd();
+  }
+}

+ 1 - 1
src/service/initData/initOne.service.ts

@@ -10,7 +10,7 @@ import * as path from 'path';
 import { Tags } from '../../entity/system/tags.entity';
 // 2024-06-11 导入
 @Provide()
-export class initOneService {
+export class InitOneService {
   // 本次导入公共标签
   tags = ['20240611'];
   @InjectEntityModel(Tags)

+ 1 - 1
src/service/initData/initThree.service.ts

@@ -11,7 +11,7 @@ import dayjs = require('dayjs');
 import { Match } from '../../entity/platform/match.entity';
 import { Sign } from '../../entity/platform/sign.entity';
 @Provide()
-export class initThreeService {
+export class InitThreeService {
   tags = ['20240802'];
   @InjectEntityModel(Tags)
   tagsModel: Repository<Tags>;

+ 1 - 1
src/service/initData/initTwo.service.ts

@@ -14,7 +14,7 @@ import { Project } from '../../entity/platform/project.entity';
 import { Tags } from '../../entity/system/tags.entity';
 // 2024-06-24 导入
 @Provide()
-export class initTwoService {
+export class InitTwoService {
   tags = ['20240624'];
   @InjectEntityModel(Tags)
   tagsModel: Repository<Tags>;