ming_node 2.2.7 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- package/beforeTest/BaseMapperTest1.js +24 -0
- package/beforeTest/CollectUtilsTest.js +10 -0
- package/index.js +2543 -2523
- package/module/BaseMapper.js +38 -1
- package/package.json +1 -1
- package/utils/common/CollectionUtils.js +111 -0
- package/utils/common/DateUtils.js +36 -0
package/module/BaseMapper.js
CHANGED
@@ -320,7 +320,44 @@ class BaseMapper {
|
|
320
320
|
return sql;
|
321
321
|
}
|
322
322
|
|
323
|
-
|
323
|
+
/**
|
324
|
+
* 给 list 加name
|
325
|
+
* @param tableName 表名
|
326
|
+
* @param list 列表
|
327
|
+
* @param list_idkey 列表中的idkey
|
328
|
+
* @param list_namekey 列表中的namekey
|
329
|
+
* @param db_idkey 库中的namekey
|
330
|
+
* @param db_namekey 库中的namekey
|
331
|
+
* @returns {Promise<void>}
|
332
|
+
*/
|
333
|
+
static async appendListName(
|
334
|
+
{
|
335
|
+
tableName="t_user",
|
336
|
+
list,
|
337
|
+
list_idkey="create_user",
|
338
|
+
list_namekey="name",
|
339
|
+
db_idkey="create_user",
|
340
|
+
db_namekey="create_user_name",
|
341
|
+
}
|
342
|
+
){
|
343
|
+
if(list==null || list.length==0){
|
344
|
+
return
|
345
|
+
}
|
346
|
+
const idKeyList=list.map(u=>u[list_idkey]);
|
347
|
+
let sql=`select ${db_idkey}, ${db_namekey} from ${tableName} where ${db_idkey} in (?)`
|
348
|
+
let dbDataList=await Db.doSql(sql,[idKeyList]);
|
349
|
+
const userMap = {};
|
350
|
+
for (let i = 0; i < dbDataList.length; i++) {
|
351
|
+
const idkeyV = dbDataList[i][db_idkey];
|
352
|
+
userMap[idkeyV] = dbDataList[i];
|
353
|
+
}
|
354
|
+
for (let i=0;i<list.length;i++){
|
355
|
+
if(userMap[list[i][list_idkey]]){
|
356
|
+
list[i][list_namekey]= userMap[list[i][list_idkey]][db_namekey];
|
357
|
+
}
|
358
|
+
}
|
359
|
+
return;
|
360
|
+
}
|
324
361
|
|
325
362
|
}
|
326
363
|
|
package/package.json
CHANGED
@@ -23,6 +23,117 @@ class CollectionUtils{
|
|
23
23
|
}
|
24
24
|
|
25
25
|
|
26
|
+
static list2Csv({list,titlesKeys,titlesNames}){
|
27
|
+
if(list==null||list.length==0){
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
if(!titlesKeys){
|
31
|
+
titlesKeys=Object.keys(list[0])
|
32
|
+
}
|
33
|
+
if(!titlesNames){
|
34
|
+
titlesNames=titlesKeys
|
35
|
+
}
|
36
|
+
let bodyStr="";
|
37
|
+
list.forEach((items, index) => {
|
38
|
+
for(let key of titlesKeys){
|
39
|
+
items[key]=items[key]+"";
|
40
|
+
bodyStr=bodyStr+`"${!items[key]?'null': ""+items[key].replace(/\s/g,"")}"`+","
|
41
|
+
}
|
42
|
+
// console.log(bodyStr)
|
43
|
+
bodyStr=bodyStr+"\n";
|
44
|
+
})
|
45
|
+
return `\ufeff`+titlesNames+'\n'+bodyStr;
|
46
|
+
}
|
47
|
+
|
48
|
+
static n(arr,key="unionId"){
|
49
|
+
function n1(r1,r2,key){
|
50
|
+
if(r1==null || r2==null ||r1==undefined||r2==undefined || r1.length==0 || r2.length==0){
|
51
|
+
return []
|
52
|
+
}
|
53
|
+
let r2keyList=r2.map(u=>u[key])
|
54
|
+
let result= r1.filter(u=>r2keyList.indexOf(u[key])!==-1)
|
55
|
+
return result;
|
56
|
+
}
|
57
|
+
let allResult=[]
|
58
|
+
try{
|
59
|
+
if(arr==null || arr==undefined || arr.length==0){
|
60
|
+
return []
|
61
|
+
}
|
62
|
+
for (let i=0;i<arr.length;i++){
|
63
|
+
if(arr[i]==null||arr[i]==undefined||arr[i].length==0){
|
64
|
+
return []
|
65
|
+
}
|
66
|
+
}
|
67
|
+
allResult= arr.reduce((r1,r2)=>{
|
68
|
+
let ri= n1(r1,r2,key)
|
69
|
+
if(ri==null||ri==undefined||ri.length==0){
|
70
|
+
throw []
|
71
|
+
}
|
72
|
+
return ri;
|
73
|
+
},arr[0])
|
74
|
+
}catch (e){
|
75
|
+
allResult=e;
|
76
|
+
}
|
77
|
+
return allResult;
|
78
|
+
}
|
79
|
+
|
80
|
+
static u(arr,key="unionId"){
|
81
|
+
function u1(r1,r2,key){
|
82
|
+
if(r1==null && r2==null){
|
83
|
+
return []
|
84
|
+
}
|
85
|
+
let r=[...r1,...r2];
|
86
|
+
return r;
|
87
|
+
}
|
88
|
+
let allResult=[]
|
89
|
+
try{
|
90
|
+
if(arr==null || arr==undefined || arr.length==0){
|
91
|
+
return []
|
92
|
+
}
|
93
|
+
for (let i=0;i<arr.length;i++){
|
94
|
+
if(arr[i]==null||arr[i]==undefined||arr[i].length==0){
|
95
|
+
return []
|
96
|
+
}
|
97
|
+
}
|
98
|
+
allResult= arr.reduce((r1,r2)=>{
|
99
|
+
let ri= u1(r1,r2,key)
|
100
|
+
if(ri==null||ri==undefined||ri.length==0){
|
101
|
+
return [];
|
102
|
+
}
|
103
|
+
return ri;
|
104
|
+
},arr[0])
|
105
|
+
}catch (e){
|
106
|
+
allResult=[];
|
107
|
+
}
|
108
|
+
let allResultKeys=allResult.map(u=>u[key]);
|
109
|
+
let allResult1=[];
|
110
|
+
let allResult1KeyObj={};
|
111
|
+
for (let i=0;i<allResult.length;i++){
|
112
|
+
let allResultObj=allResult[i];
|
113
|
+
let k= allResultObj[key];
|
114
|
+
if(allResult1KeyObj[k]){
|
115
|
+
continue;
|
116
|
+
}else {
|
117
|
+
allResult1.push(allResultObj);
|
118
|
+
allResult1KeyObj[k]=true;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
return allResult1;
|
122
|
+
}
|
123
|
+
|
124
|
+
static dif(arr1,arr2,key="unionId"){
|
125
|
+
if(arr1==null || arr1==null|| arr1.length==0 ){
|
126
|
+
return []
|
127
|
+
}
|
128
|
+
if(arr2==null || arr2==null|| arr2.length==0 ){
|
129
|
+
return arr1;
|
130
|
+
}
|
131
|
+
let r2keyList=arr2.map(u=>u[key])
|
132
|
+
let result= arr1.filter(u=>r2keyList.indexOf(u[key])==-1)
|
133
|
+
return result;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
26
137
|
}
|
27
138
|
|
28
139
|
module.exports = CollectionUtils;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class DateUtils {
|
4
|
+
static difDate(startDate="2021-04-01",endDate="2022-02-10"){
|
5
|
+
let flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2];
|
6
|
+
let start = new Date(startDate);
|
7
|
+
let end = new Date(endDate);
|
8
|
+
let year = end.getFullYear() - start.getFullYear();
|
9
|
+
let month = end.getMonth() - start.getMonth();
|
10
|
+
let day = end.getDate() - start.getDate();
|
11
|
+
if (month < 0) {
|
12
|
+
year--;
|
13
|
+
month = end.getMonth() + (12 - start.getMonth());
|
14
|
+
}
|
15
|
+
if (day < 0) {
|
16
|
+
month--;
|
17
|
+
let index = flag.findIndex((temp) => {
|
18
|
+
return temp === start.getMonth() + 1
|
19
|
+
});
|
20
|
+
let monthLength;
|
21
|
+
if (index <= 6) {
|
22
|
+
monthLength = 31;
|
23
|
+
} else if (index > 6 && index <= 10) {
|
24
|
+
monthLength = 30;
|
25
|
+
} else {
|
26
|
+
monthLength = 28;
|
27
|
+
}
|
28
|
+
day = end.getDate() + (monthLength - start.getDate());
|
29
|
+
}
|
30
|
+
return {year,month,day}
|
31
|
+
}
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
|
36
|
+
module.exports = DateUtils;
|