From 2189eb3d220cd3c9552432fb495bbe53ba9e7548 Mon Sep 17 00:00:00 2001 From: luch1994 <1097650398@qq.com> Date: Tue, 16 Jan 2024 14:17:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=97=AE=E5=8D=B7=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=B0=E5=A2=9E=E6=90=9C=E7=B4=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/apps/surveyManage/index.ts | 80 +++++++++++++++++++ .../surveyManage/service/surveyService.ts | 30 +++++-- 2 files changed, 103 insertions(+), 7 deletions(-) diff --git a/server/src/apps/surveyManage/index.ts b/server/src/apps/surveyManage/index.ts index b5af65f3..7902af49 100644 --- a/server/src/apps/surveyManage/index.ts +++ b/server/src/apps/surveyManage/index.ts @@ -6,6 +6,17 @@ import { HISTORY_TYPE } from '../../types/index'; import { getValidateValue } from './utils/index'; import * as Joi from 'joi'; +type FilterItem = { + comparator?: string; + condition: Array; +} + +type FilterCondition = { + field: string; + comparator?: string; + value: string & Array; +} + @SurveyApp('/api/surveyManage') export default class SurveyManage { @@ -96,11 +107,30 @@ export default class SurveyManage { const condition = getValidateValue(Joi.object({ curPage: Joi.number().default(1), pageSize: Joi.number().default(10), + filter: Joi.string().allow(null), + order: Joi.string().allow(null), }).validate(req.query, { allowUnknown: true })); + let filter = {}, order = {}; + if (condition.filter) { + try { + filter = this.getFilter(JSON.parse(condition.filter)); + } catch (error) { + console.log(error); + } + } + if (condition.order) { + try { + order = this.getOrder(JSON.parse(condition.order)); + } catch (error) { + console.log(error); + } + } const userData = await userService.checkLogin({ req }); const listRes = await surveyService.list({ pageNum: condition.curPage, pageSize: condition.pageSize, + filter, + order, userData }); return { @@ -108,6 +138,56 @@ export default class SurveyManage { data: listRes, }; } + + private getFilter(filterList: Array) { + const allowFilterField = ['title', 'remark', 'surveyType', 'curStatus.status']; + return filterList.reduce((preItem, curItem) => { + const condition = curItem.condition.filter(item => allowFilterField.includes(item.field)).reduce((pre, cur) => { + switch(cur.comparator) { + case '$ne': + pre[cur.field] = { + $ne: cur.value, + }; + break; + case '$regex': + pre[cur.field] = { + $regex: cur.value, + }; + break; + default: + pre[cur.field] = cur.value; + break; + } + return pre; + }, {}); + switch(curItem.comparator) { + case '$or': + if (!Array.isArray(preItem.$or)) { + preItem.$or = []; + } + preItem.$or.push(condition); + break; + default: + Object.assign(preItem, condition); + break; + } + return preItem; + }, { } as { $or?: Array>; } & Record); + } + + private getOrder(order) { + + const allowOrderField = ['createDate', 'updateDate']; + + const orderList = JSON.parse(order).filter((orderItem) => + allowOrderField.includes(orderItem.field), + ); + return orderList.reduce((pre, cur) => { + pre[cur.field] = cur.value; + return pre; + }, {}); + } + @SurveyServer({ type: 'http', method: 'post', routerName: '/saveConf' }) async saveConf({ req }) { const surveyData = getValidateValue(Joi.object({ diff --git a/server/src/apps/surveyManage/service/surveyService.ts b/server/src/apps/surveyManage/service/surveyService.ts index 04311702..7583fcbe 100644 --- a/server/src/apps/surveyManage/service/surveyService.ts +++ b/server/src/apps/surveyManage/service/surveyService.ts @@ -6,6 +6,7 @@ import * as path from 'path'; import { keyBy, merge, cloneDeep } from 'lodash'; import * as moment from 'moment'; import { DataItem } from '../../../types/survey'; +import { Sort } from 'mongodb'; class SurveyService { async checkSecurity({ content, dictType }: { content: string, dictType: DICT_TYPE }) { @@ -168,17 +169,32 @@ class SurveyService { }; } - async list(condition: { pageNum: number, pageSize: number, userData: UserType }) { + async list(condition: { pageNum: number, pageSize: number, userData: UserType, filter: object, order: object }) { const surveyMeta = await mongo.getCollection({ collectionName: 'surveyMeta' }); - const cond = { - owner: condition.userData.username - }; - const data = await surveyMeta.find(cond) - .sort({ createDate: -1 }) + + const query = Object.assign( + {}, + { + owner: condition.userData.username, + 'curStatus.status': { + $ne: 'removed', + }, + }, + condition.filter, + ); + const order = Object.assign( + {}, + { + createDate: -1, + }, + condition.order, + ) as Sort; + const data = await surveyMeta.find(query) + .sort(order) .limit(condition.pageSize) .skip((condition.pageNum - 1) * condition.pageSize) .toArray(); - const count = await surveyMeta.countDocuments(cond); + const count = await surveyMeta.countDocuments(query); return { data: mongo.convertId2StringByList(data), count }; }