UNPKG

youzanyun-devtool-worker

Version:

151 lines (150 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const path_1 = tslib_1.__importDefault(require("path")); const reverse_1 = tslib_1.__importDefault(require("lodash/reverse")); const spring4js_nodejs_1 = require("spring4js-nodejs"); let H5PageImplDao = class H5PageImplDao { constructor() { this.dataFilePath = ''; } async start() { const dataDir = this.configService.getDataDir(); const dataFilePath = path_1.default.join(dataDir, 'h5PageImpl.json'); await fs_extra_1.default.ensureFile(dataFilePath); this.dataFilePath = dataFilePath; } async getData() { const dataBuffer = await fs_extra_1.default.readFile(this.dataFilePath); const dataStr = dataBuffer.toString("utf8") || '{}'; const data = JSON.parse(dataStr); return data; } async getAllData() { const data = await this.getData(); const result = (0, reverse_1.default)(data.list || []); return result; } async getDataById(id) { const allData = await this.getAllData(); return allData.find((item) => +item.id === +id) || {}; } async getDataWithCondition(params) { const { pageNo, pageSize } = params, otherParams = tslib_1.__rest(params, ["pageNo", "pageSize"]); const allData = await this.getAllData(); let result = allData.slice(0); if (otherParams) { const conditions = Object.keys(otherParams); const filterRecords = (data, key, value) => { return data.filter((item) => { if (typeof value === 'object' && value instanceof Array) { return value.indexOf(item[key]) > -1; } else { return item[key] === value; } }); }; for (let i = 0; i < conditions.length; i++) { const key = conditions[i]; const value = params[key]; result = filterRecords(result, key, value); } } if (pageNo && pageSize) { const startIndex = (pageNo - 1) * pageSize; const endIndex = pageNo * pageSize; const content = result.slice(startIndex, endIndex); result = { content, pageNo, pageSize, total: result.length }; } return result; } async getDataIndexWithCondition(params) { const allData = await this.getAllData(); let result = -1; if (params) { result = allData.findIndex((item) => { const conditions = Object.keys(params).map(name => `${item[name]} === ${params[name]}`); const conditionStr = conditions.join('&&'); return conditionStr; }); } return result; } async save(item) { const data = await this.getData(); const maxId = (data.maxId || 0) + 1; const allData = data.list || []; item.id = maxId; allData.push(item); const result = { maxId, list: allData }; await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify(result)); return item; } async update(item) { const data = await this.getData(); const allData = data.list || []; const index = allData.findIndex((d) => d.id === item.id); if (index > -1) { allData[index] = item; } const result = { maxId: data.maxId, list: allData }; await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify(result)); return item; } async saveOrUpdateByKey(keyCondition, item) { const data = await this.getData(); const maxId = (data.maxId || 0) + 1; const allData = data.list || []; const index = await this.getDataIndexWithCondition(keyCondition); if (index > -1) { allData[index] = item; } else { data.maxId = maxId; item.id = maxId; allData.push(item); } data.list = allData; await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify(data)); return item; } async deleteByKey(key, value) { const data = await this.getData(); const allData = data.list || []; const type = typeof value; const list = allData.filter((item) => { if (type === 'number' || type === 'string') { return item[key] != value; } else if (type === 'object' && value instanceof Array) { return value.indexOf(item[key]) === -1; } else { return true; } }); await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify({ maxId: data.maxId, list })); return 'ok'; } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], H5PageImplDao.prototype, "configService", void 0); H5PageImplDao = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], H5PageImplDao); exports.default = H5PageImplDao; ;