wangrong
7 天以前 5ebdf612b8291106370bba5e79996b28a7452e67
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { assign, orderBy } from "lodash-es";
import { uuid } from "../utils";
 
const userList = [
    {
        id: 1,
        name: "楚行云",
        createTime: "1996-09-14",
        wages: 73026,
        status: 1,
        account: "chuxingyun",
        occupation: 4,
        phone: 13797353874
    },
    {
        id: 2,
        name: "秦尘",
        createTime: "1977-11-09",
        wages: 74520,
        status: 0,
        account: "qincheng",
        occupation: 3,
        phone: 18593911044
    },
    {
        id: 3,
        name: "叶凡",
        createTime: "1982-11-28",
        wages: 81420,
        status: 0,
        account: "yefan",
        occupation: 1,
        phone: 16234136338
    },
    {
        id: 4,
        name: "白小纯",
        createTime: "2012-12-17",
        wages: 65197,
        status: 1,
        account: "baixiaochun",
        occupation: 2,
        phone: 16325661110
    },
    {
        id: 5,
        name: "韩立",
        createTime: "1982-07-10",
        wages: 99107,
        status: 1,
        account: "hanli",
        occupation: 2,
        phone: 18486594866
    },
    {
        id: 6,
        name: "唐三",
        createTime: "2019-07-31",
        wages: 80658,
        status: 1,
        account: "tangsan",
        occupation: 5,
        phone: 15565014642
    },
    {
        id: 7,
        name: "王林",
        createTime: "2009-07-26",
        wages: 57408,
        status: 1,
        account: "wanglin",
        occupation: 1,
        phone: 13852767084
    },
    {
        id: 8,
        name: "李强",
        createTime: "2016-04-26",
        wages: 71782,
        status: 1,
        account: "liqiang",
        occupation: 3,
        phone: 18365332834
    },
    {
        id: 9,
        name: "秦羽",
        createTime: "1984-01-18",
        wages: 87860,
        status: 1,
        account: "qinyu",
        occupation: 0,
        phone: 18149247129
    }
];
 
class TestService {
    // 分页列表
    async page(params: any) {
        const { keyWord, page, size, sort, order } = params || {};
 
        // 关键字查询
        const keyWordLikeFields = ["phone", "name"];
 
        // 等值查询
        const fieldEq = ["createTime", "occupation", "status"];
 
        // 模糊查询
        const likeFields = ["phone", "name"];
 
        // 过滤后的列表
        const list = orderBy(userList, order, sort).filter((e: any) => {
            let f = true;
 
            if (keyWord !== undefined) {
                f = !!keyWordLikeFields.find((k) => String(e[k]).includes(String(params.keyWord)));
            }
 
            fieldEq.forEach((k) => {
                if (f) {
                    if (params[k] !== undefined) {
                        f = e[k] == params[k];
                    }
                }
            });
 
            likeFields.forEach((k) => {
                if (f) {
                    if (params[k] !== undefined) {
                        f = String(e[k]).includes(String(params[k]));
                    }
                }
            });
 
            return f;
        });
 
        return new Promise((resolve) => {
            // 模拟延迟
            setTimeout(() => {
                resolve({
                    list: list.slice((page - 1) * size, page * size),
                    pagination: {
                        total: list.length,
                        page,
                        size
                    },
                    subData: {
                        wages: list.reduce((a, b) => {
                            return a + b.wages;
                        }, 0)
                    }
                });
            }, 500);
        });
    }
 
    // 更新
    async update(params: { id: any; [key: string]: any }) {
        const item = userList.find((e) => e.id == params.id);
 
        if (item) {
            assign(item, params);
        }
    }
 
    // 新增
    async add(params: any) {
        const id = uuid();
 
        userList.push({
            id,
            ...params
        });
 
        return id;
    }
 
    // 详情
    async info(params: { id: any }) {
        const { id } = params || {};
        return userList.find((e) => e.id == id);
    }
 
    // 删除
    async delete(params: { ids: any[] }) {
        const { ids = [] } = params || {};
 
        ids.forEach((id) => {
            const index = userList.findIndex((e) => e.id == id);
            userList.splice(index, 1);
        });
    }
 
    // 全部列表
    async list() {
        return userList;
    }
 
    search = {
        fieldEq: [
            {
                propertyName: "occupation",
                comment: "工作",
                source: "a.occupation"
            }
        ],
        fieldLike: [
            {
                propertyName: "status",
                comment: "状态",
                dict: ["关闭", "开启"],
                source: "a.status"
            }
        ],
        keyWordLikeFields: [
            {
                propertyName: "name",
                comment: "姓名",
                source: "a.name"
            },
            {
                propertyName: "phone",
                comment: "手机号",
                source: "a.phone"
            }
        ]
    };
}
 
export { TestService };