123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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();
- }
- }
|