這篇文章終於回到了正軌:為mongodb偽造數據。之前的隨機數、隨機車牌照、隨機時間還有這篇筆記中的獲取指定長度的中文字元串,都是為這篇筆記做準備。看一下我們的準備(基礎代碼) 現在一切都準備好了,那我們就說說正事兒。為了讓這些數據有點意義,我想了三個表單。分別是車輛信息表、車輛耗損表以及車輛營收表 ...
這篇文章終於回到了正軌:為mongodb偽造數據。之前的隨機數、隨機車牌照、隨機時間還有這篇筆記中的獲取指定長度的中文字元串,都是為這篇筆記做準備。看一下我們的準備(基礎代碼)
// 1、獲取指定範圍隨機數-包括最大值和最小值 var getRangeRandomNumber = function(num1,num2){ num1 = Number.isInteger(num1) ? num1: 0; num2 = Number.isInteger(num2) ? num2: 0; var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2); return Math.round(Math.random() * (maxNum - minNum)) + minNum; }; // 2、格式化日期時間-參考:https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html var dateExtendFormat = function(date, format) { var o = { "M+": date.getMonth() + 1, "d+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }; // 3、獲取指定範圍隨機時間,依賴方法 getRangeRandomNumber、dateExtendFormat var getRangeRandomDate=function(date1,date2,format){ var date1ValueOf=new Date(date1).valueOf(),date2ValueOf=new Date(date2).valueOf(); if(isNaN(date1ValueOf)&&isNaN(date2ValueOf)){ date1ValueOf=0; date2ValueOf=new Date().valueOf(); } else{ if(isNaN(date1ValueOf))date1ValueOf=0; if(isNaN(date2ValueOf))date2ValueOf=0; } var retDate=new Date(getRangeRandomNumber(Math.abs(date1ValueOf-date2ValueOf)) + Math.min(date1ValueOf,date2ValueOf)); if(format){ retDate=dateExtendFormat(retDate,format); } return retDate; }; // 4、獲取隨機車牌照 var getRandomLicensePlate=(function f(excludeArr){ if(!Array.isArray(excludeArr))excludeArr=[]; // 生成一個隨機車聯牌照 var strProvinceShorter="京津冀晉蒙遼吉黑滬蘇浙皖閩贛魯豫鄂湘粵桂瓊渝川黔滇藏陝甘青寧新港澳台"; var strNumberLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tempRetLicensePlate=strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length-1)]; for(var i=0;i<6;i++){ tempRetLicensePlate+=strNumberLetter[getRangeRandomNumber(strNumberLetter.length-1)]; } // 判斷這個車聯牌照是否存在 if(excludeArr.indexOf(tempRetLicensePlate)>=0){ tempRetLicensePlate = f(excludeArr); } else { excludeArr.push(tempRetLicensePlate); } return tempRetLicensePlate; }); // 5.0、獲取一個隨機漢字 var getChineseCharacter=function(){ return String.fromCharCode(getRangeRandomNumber(parseInt("4E00",16),parseInt("9FA5",16))); }; // 5.1、獲取指定長度的中文字元串 var getCustomChineseCharacters=function(len){ len=isNaN(Number(len))?1:Math.abs(Math.ceil(Number(len))); var retStr=""; for(var i=0;i<len;i++){ retStr+=getChineseCharacter(); } return retStr; };
現在一切都準備好了,那我們就說說正事兒。為了讓這些數據有點意義,我想了三個表單。分別是車輛信息表、車輛耗損表以及車輛營收表,下麵看一下三張表的結構
車輛信息表(FormId: 507048044944691000, FormVersion: 507048044944691001) |
||||
唯一標識 |
中文描述 |
控制項類型 |
是否必填 |
表單項的其他配置(在表單設計時配置,文本框長度、時間格式等) |
1572493551001 |
車輛品牌名稱 |
單行文本框 |
是 |
|
1572493551002 |
車輛品牌型號 |
單行文本框 |
是 |
|
1572493551003 |
車輛牌照 |
單行文本框 |
是 |
|
1572493551004 |
車輛購買日期 |
日期時間 |
否 |
|
1572493551005 |
車輛購買價格 |
數值文本框 |
否 |
|
1572493551006 |
備註 |
多行文本框 |
否 |
車輛信息表(FormId: 507048044944691000, FormVersion: 507048044944691002) |
||||
唯一標識 |
中文描述 |
控制項類型 |
是否必填 |
表單項的其他配置(在表單設計時配置,文本框長度、時間格式等) |
1572493551001 |
車輛品牌名稱 |
單行文本框 |
是 |
|
1572493551002 |
車輛品牌型號 |
單行文本框 |
是 |
|
1572493551003 |
車輛牌照 |
單行文本框 |
是 |
|
1572493551004 |
車輛購買日期 |
時間控制項 |
否 |
|
1572493551005 |
車輛購買價格 |
數值文本框 |
否 |
|
1572493551006 |
備註 |
多行文本框 |
否 |
|
……業務需求,刪除或者新增一個或者多個欄位,生成一個新的版本號 |
車輛耗損表(FormId: 507048044944692000, FormVersion: 507048044944692001) |
||||
唯一標識 |
中文描述 |
控制項類型 |
是否必填 |
表單項的其他配置(在表單設計時配置,文本框長度、時間格式等) |
1572493552001 |
所屬車輛 |
下拉搜索控制項 |
是 |
|
1572493552002 |
耗損人員 |
選擇人員控制項 |
是 |
|
1572493552003 |
耗損時間 |
時間控制項 |
是 |
|
1572493552004 |
耗損類別 |
單行文本框 |
是 |
|
1572493552005 |
耗損金額 |
數值文本框 |
是 |
|
1572493552006 |
備註 |
多行文本框 |
否 |
車輛營收表(FormId: 507048044944693000, FormVersion: 507048044944693001) |
||||
唯一標識 |
中文描述 |
控制項類型 |
是否必填 |
表單項的其他配置(在表單設計時配置,文本框長度、時間格式等) |
1572493553001 |
所屬車輛 |
下拉搜索控制項 |
是 |
|
1572493553002 |
營收人員 |
選擇人員控制項 |
是 |
|
1572493553003 |
營收時間 |
時間控制項 |
是 |
|
1572493553004 |
營收類別 |
單行文本框 |
是 |
|
1572493553005 |
營收金額 |
數值文本框 |
是 |
|
1572493553006 |
里程 |
數值文本框 |
否 |
|
1572493553007 |
備註 |
多行文本框 |
否 |
現在我們就按照表結構偽造數據,下麵是全部的代碼(可以在Mongodb環境下執行)
// 1、獲取指定範圍隨機數-包括最大值和最小值 var getRangeRandomNumber = function(num1,num2){ num1 = Number.isInteger(num1) ? num1: 0; num2 = Number.isInteger(num2) ? num2: 0; var minNum=Math.min(num1,num2),maxNum=Math.max(num1,num2); return Math.round(Math.random() * (maxNum - minNum)) + minNum; }; // 2、格式化日期時間-參考:https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html var dateExtendFormat = function(date, format) { var o = { "M+": date.getMonth() + 1, "d+": date.getDate(), "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }; // 3、獲取指定範圍隨機時間,依賴方法 getRangeRandomNumber、dateExtendFormat var getRangeRandomDate=function(date1,date2,format){ var date1ValueOf=new Date(date1).valueOf(),date2ValueOf=new Date(date2).valueOf(); if(isNaN(date1ValueOf)&&isNaN(date2ValueOf)){ date1ValueOf=0; date2ValueOf=new Date().valueOf(); } else{ if(isNaN(date1ValueOf))date1ValueOf=0; if(isNaN(date2ValueOf))date2ValueOf=0; } var retDate=new Date(getRangeRandomNumber(Math.abs(date1ValueOf-date2ValueOf)) + Math.min(date1ValueOf,date2ValueOf)); if(format){ retDate=dateExtendFormat(retDate,format); } return retDate; }; // 4、獲取隨機車牌照 var getRandomLicensePlate=(function f(excludeArr){ if(!Array.isArray(excludeArr))excludeArr=[]; // 生成一個隨機車聯牌照 var strProvinceShorter="京津冀晉蒙遼吉黑滬蘇浙皖閩贛魯豫鄂湘粵桂瓊渝川黔滇藏陝甘青寧新港澳台"; var strNumberLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var tempRetLicensePlate=strProvinceShorter[getRangeRandomNumber(strProvinceShorter.length-1)]; for(var i=0;i<6;i++){ tempRetLicensePlate+=strNumberLetter[getRangeRandomNumber(strNumberLetter.length-1)]; } // 判斷這個車聯牌照是否存在 if(excludeArr.indexOf(tempRetLicensePlate)>=0){ tempRetLicensePlate = f(excludeArr); } else { excludeArr.push(tempRetLicensePlate); } return tempRetLicensePlate; }); // 5.0、獲取一個隨機漢字 var getChineseCharacter=function(){ return String.fromCharCode(getRangeRandomNumber(parseInt("4E00",16),parseInt("9FA5",16))); }; // 5.1、獲取指定長度的中文字元串 var getCustomChineseCharacters=function(len){ len=isNaN(Number(len))?1:Math.abs(Math.ceil(Number(len))); var retStr=""; for(var i=0;i<len;i++){ retStr+=getChineseCharacter(); } return retStr; }; // 插入數據方法 var handleBatchInsertData=function(initConfig){ if(initConfig.formId&&initConfig.formVersion&&Array.isArray(initConfig.source)&&initConfig.source.length){ var tempTrs=[], tempCreateDate=ISODate(), tempFormInstaceDataCounter=db.getCollection('FormInstace').find({}).count(); initConfig.source.forEach((formItemObj)=>{ var tempCreateUserIdIndex = Math.floor(Math.random() * GV_CreateUserIds.length); var tempTr={ _id:(++tempFormInstaceDataCounter).toString(), FormItems:[], ExtendData:{}, CreateUserId:GV_CreateUserIds[tempCreateUserIdIndex], CreateUserName:GV_CreateUserIds[tempCreateUserIdIndex], CreateDate:tempCreateDate, LastModifyDate:tempCreateDate, FormId:initConfig.formId, FormVersion:initConfig.formVersion }; for(var i in formItemObj){ if (formItemObj.hasOwnProperty(i)){ tempTr.FormItems.push({key:i,value:formItemObj[i]}); } } tempTrs.push(tempTr); }); try{ db.FormInstace.insertMany(tempTrs); }catch(e){ print(e); } } }; // 創建用戶集合 var GV_CreateUserIds = ["user10000","user10001","user10002","user10003","user10004","user10005","user10006","user10007","user10008","user10009"]; // 車輛信息表插入數據配置 var GV_CarInfoInitConfig = { formId:"507048044944691000", formVersion:"507048044944691001", carTypes:[ { brandName:"紅旗", models:[ {name:"HS7",min:349800,max:459800}, {name:"HS5",min:183800,max:249800}, {name:"H7",min:252800,max:317800}, {name:"H5",min:146800,max:190800}, {name:"HE-HS3",min:225800,max:317800} ] }, { brandName:"比亞迪", models:[ {name:"元",min:65900,max:99900}, {name:"宋",min:79800,max:119800}, {name:"宋Pro",min:89800,max:119800}, {name:"唐",min:129900,max:169900}, {name:"比亞迪F3",min:43900,max:65900}, {name:"速銳",min:59900,max:69900}, {name:"秦Pro",min:79800,max:115900}, {name:"宋MAX",min:79900,max:129900}, {name:"元EV",min:89900,max:139900}, {name:"比亞迪S2",min:89800,max:109800}, {name:"宋DM",min:176900,max:206900}, {name:"宋Pro DM",min:169800,max:259900}, {name:"宋EV",min:189900,max:219900}, {name:"宋Pro EV",min:179800,max:259900}, {name:"唐DM",min:229900,max:329900}, {name:"唐EV",min:259900,max:409900}, {name:"比亞迪e1",min:59900,max:79900}, {name:"比亞迪e2",min:89800,max:119800}, {name:"比亞迪e3",min:103800,max:139800}, {name:"秦DM",min:132900,max:209900}, {name:"秦Pro DM",min:136900,max:206900}, {name:"秦EV",min:149900,max:169900}, {name:"秦Pro EV",min:149900,max:299900}, {name:"宋MAX DM",min:149900,max:196900}, {name:"宋MAX EV",min:179800,max:199800} ] }, { brandName:"奇瑞", models:[ {name:"瑞虎3",min:59900,max:79900}, {name:"瑞虎3x",min:49900,max:62900}, {name:"瑞虎5x",min:59900,max:89900}, {name:"瑞虎7",min:85900,max:150900}, {name:"瑞虎8",min:88800,max:155900}, {name:"艾瑞澤5",min:49900,max:84900}, {name:"艾瑞澤GX",min:74900,max:113900}, {name:"瑞虎3xe",min:175800,max:189800}, {name:"瑞虎e",min:109900,max:143900}, {name:"奇瑞eQ1",min:59800,max:75800}, {name:"艾瑞澤e",min:126800,max:143800}, {name:"艾瑞澤5e",min:192300,max:212300} ] }, { brandName:"吉利", models:[ {name:"遠景X1",min:39900,max:57900}, {name:"遠景X3",min:45900,max:68900}, {name:"繽越",min:78800,max:129800}, {name:"遠景S1",min:59900,max:94900}, {name:"遠景X6",min:68900,max:105900}, {name:"帝豪GS",min:77800,max:116800}, {name:"博越",min:88800,max:161800}, {name:"星越",min:135800,max:195800}, {name:"金剛",min:47900,max:65900}, {name:"遠景",min:47900,max:73900}, {name:"帝豪",min:68900,max:98800}, {name:"帝豪GL",min:78800,max:121800}, {name:"繽瑞",min:75800,max:110800}, {name:"博瑞",min:136800,max:179800}, {name:"嘉際",min:99800,max:148800}, {name:"繽越PHEV",min:149800,max:169800}, {name:"帝豪GSe",min:119800,max:159800}, {name:"星越PHEV",min:188800,max:216800}, {name:"帝豪EV",min:135800,max:238300}, {name:"帝豪PHEV",min:165800,max:185800}, {name:"帝豪GL PHEV",min:152800,max:164800}, {name:"博瑞PHEV",min:176800,max:209800}, {name:"嘉際PHEV",min:169800,max:192800} ] }, { brandName:"長安", models:[ {name:"長安CS15",min:55900,max:80900}, {name:"長安CS35",min:63900,max:87900}, {name:"長安CS35 PLUS",min:69900,max:109900}, {name:"長安CS55",min:82900,max:133900}, {name:"長安CS75",min:79800,max:174800}, {name:"長安CS75 PLUS",min:106900,max:154900}, {name:"長安CS85 COUPE",min:119900,max:169900}, {name:"長安CS95",min:165900,max:213900}, {name:"奔奔",min:40900,max:56900}, {name:"悅翔",min:49900,max:67900}, {name:"逸動DT",min:52900,max:80900}, {name:"逸動XT",min:77900,max:111900}, {name:"逸動",min:69900,max:103900}, {name:"睿騁CC",min:84900,max:135900}, {name:"銳程CC",min:94900,max:128900}, {name:"睿騁",min:120800,max:150800}, {name:"凌軒",min:67900,max:110900}, {name:"長安CS15 EV",min:89800,max:98800}, {name:"長安CS75 PHEV",min:175800,max:206800}, {name:"奔奔EV",min:49800,max:81800}, {name:"逸動ET",min:132900,max:142900}, {name:"逸動PHEV",min:160900,max:169900}, {name:"逸動EV",min:129900,max:139900}, ] }, { brandName:"長安歐尚", models:[ {name:"長安歐尚科賽3",min:59900,max:76900}, {name:"長安歐尚科賽5",min:69900,max:82900}, {name:"長安歐尚X70A",min:49900,max:859500}, {name:"長安歐尚CX70",min:59900,max:109900}, {name:"長安歐尚科賽",min:86800,max:152800}, {name:"歐尚S",min:39900,max:55900}, {name:"歐尚長行",min:68900,max:82900}, {name:"長安歐尚A600",min:49900,max:84900}, {name:"長安歐尚A800",min:62900,max:100900}, {name:"長安歐尚科尚",min:79800,max:160800}, {name:"長安之星9",min:47800,max:48800}, {name:"長安星卡",min:32900,max:47900}, {name:"尼歐Ⅱ",min:88800,max:100800}, {name:"歐力威EV",min:105800,max:105800}, {name:"歐諾S EV",min:175800,max:175800}, {name:"歐尚長行EV",min:169800,max:169800}, {name:"歐尚EV",min:169800,max:169800}, {name:"歐尚A600 EV",min:149800,max:149800}, {name:"長安之星9 EV",min:152800,max:156000}, {name:"長安星卡EV",min:123800,max:125800}, ] }, { brandName:"長安輕型車", models:[ {name:"睿行S50",min:48900,max:77900}, {name:"睿行S50T",min:61900,max:78900}, {name:"睿行M60",min:51900,max:59900}, {name:"睿行M70",min:60500,max:74000}, {name:"睿行M80",min:58500,max:72000}, {name:"睿行M90",min:68500,max:92500}, {name:"神騏F30",min:47600,max:63800}, {name:"神騏T10",min:39900,max:50900}, {name:"神騏T20",min:33900,max:59700}, {name:"凱程F70",min:92800,max:139800}, {name:"長安星卡L系列",min:39900,max:47900}, {name:"長安星卡C系列",min:30900,max:33600}, {name:"睿行ES30",min:66800,max:69800}, {name:"睿行EM60",min:122800,max:122800}, {name:"睿行EM80",min:96800,max:120000} ] }, { brandName:"長安跨越", models:[ {name:"長安跨越V3",min:33500,max:34300}, {name:"跨越王X1",min:39600,max:51800}, {name:"跨越王X3",min:45700,max:58400}, {name:"長安跨越王X5",min:47400,max:65600}, {name:"長安新豹2",min:40700,max:58600}, {name:"新豹3",min:46200,max:59900}, {name:"新豹T3",min:41300,max:51800}, {name:"長安新豹MINI",min:32100,max:50500}, {name:"跨越者D5",min:59300,max:67700}, {name:"長安跨越新能源V3 EV",min:79200,max:79200}, {name:"長安跨越新能源V5 EV",min:93800,max:93800} ] }, { brandName:"力帆", models:[ {name:"邁威",min:56800,max:73800}, {name:"力帆X80",min:109900,max:149900}, {name:"力帆820",min:76800,max:119800}, {name:"軒朗",min:69800,max:106800}, {name:"樂途",min:35800,max:59800}, {name:"力帆650EV",min:168900,max:175800}, {name:"力帆820EV",min:256800,max:279800} ] }, { brandName:"長城", models:[ {name:"風駿5",min:68800,max:112800}, {name:"風駿6",min:86800,max:117800}, {name:"風駿7",min:86800,max:138800}, {name:"炮",min:97800,max:159800}, {name:"長城C30 EV",min:150000,max:154000} ] }, { brandName:"哈弗", models:[ {name:"哈弗H2",min:74900,max:95900}, {name:"哈弗H2s",min:70000,max:85000}, {name:"哈弗H4",min:73900,max:115000}, {name:"哈弗F5",min:100000,max:130000}, {name:"哈弗H5",min:107800,max:136800}, {name:"哈弗M6",min:66000,max:82000}, {name:"哈弗H6",min:102000,max:136000}, {name:"哈弗H6 Coupe",min:79900,max:119000}, {name:"哈弗H7",min:142000,max:180000}, {name:"哈弗F7",min:109000,max:153700}, {name:"哈弗F7x",min:119900,max:154900}, {name:"哈弗H9",min:209800,max:272800}, ] }, { brandName:"江鈴", models:[ {name:"寶典",min:78800,max:105300}, {name:"凱銳800",min:120800,max:129100}, {name:"凱運強勁版",min:99500,max:117400}, {name:"凱運升級版",min:94800,max:106800}, {name:"順達寬體",min:90800,max:100000}, {name:"順達窄體",min:84000,max:89000}, {name:"特順",min:101300,max:142700}, {name:"域虎3",min:89800,max:122200}, {name:"域虎5",min:96800,max:136300}, {name:"域虎7",min:119800,max:176300}, {name:"江鈴E100B",min:73800,max:73800}, {name:"江鈴E160",min:95800,max:98800}, {name:"江鈴E200L",min:87800,max:90800}, {name:"江鈴E200N",min:90800,max:93800}, {name:"易至EV3",min:66800,max:83800}, {name:"易至EX5",min:89800,max:122800}, {name:"特順EV",min:206000,max:206000}, {name:"騏鈴T5",min:65800,max:86800}, {name:"騏鈴T7",min:72800,max:113800}, {name:"騏鈴T100",min:53800,max:71800}, {name:"D-MAX房車",min:283000,max:396000}, {name:"考斯特房車",min:318000,max:380000}, {name:"旅居房車",min:536000,max:596000}, {name:"羅莎房車租賃款",min:268000,max:288000}, {name:"騏鈴T7皮卡房車",min:258000,max:299800}, {name:"全順商旅房車",min:468000,max:518000}, {name:"全順T型房車",min:408000,max:448000}, {name:"商旅房車",min:438000,max:488000}, {name:"商旅房車經典款",min:388000,max:428000}, {name:"途睿歐商務車",min:298000,max:398000} ] }, ], source:[] }; var GV_TempCarLicensePlates=[]; GV_CarInfoInitConfig.carTypes.forEach((carType)=>{ if(Array.isArray(carType.models)&&carType.models.length){ carType.models.forEach((model)=>{ for(var i=0;i<getRangeRandomNumber(10,100);i++){ var tempPlate = getRandomLicensePlate(GV_TempCarLicensePlates); GV_CarInfoInitConfig.source.push({ "1572493551001":carType.brandName, "1572493551002":model.name, "1572493551003":tempPlate, "1572493551004":getRangeRandomDate("2010-01-01","2018-06-06","yyyy-MM-dd HH:mm:ss"), "1572493551005":getRangeRandomNumber(model.min,model.max), "1572493551006":getCustomChineseCharacters(getRangeRandomNumber(200)) }); } }); } }); // 車輛花費表插入數據配置 var GV_CarWastageInitConfig = { formId:"507048044944692000", formVersion:"507048044944692001", wastageType:[ {text:"違章",detailed:["信號燈","壓線","單雙號","規定方向行駛","避讓特殊車輛","避讓行人","時間道路禁行"]}, {text:"洗車",detailed:["全套","外圍"]}, {text:"修車",detailed:["輪胎","前玻璃","後玻璃","坐墊","前大燈","後大燈","車窗","發動機","電瓶","變速箱"]}, {text:"加油",detailed:["92","93","95","97"]}, {text:"保險",detailed:["太平洋","平安","人保","泰康"]}, {text:"肇事",detailed:["傷人","護欄","撞車","間接事故","其他"]} ], source:[] }; // 車輛營收表插入數據配置 var GV_CarRevenueInitConfig = { formId:"507048044944693000", formVersion:"507048044944693001", revenueTypes:[ {text:"租賃",detailed:["一天","一周","一月","一年"]}, {text:"載客",detailed:["線下","滴滴","Uber","快的","其他"]}, {text:"送貨",detailed:["手機","筆記本","照相機","微波爐","電磁率","其他"]}, {text:"其他",detailed:["婚車","其他"]} ], source:[] }; if(GV_CarInfoInitConfig.source.length){ GV_CarInfoInitConfig.source.forEach((carInfo,carInfoIndex)=>{ for(var i=0;i<getRangeRandomNumber(50,100);i++){ var tempUserIdIndexWastageType = getRangeRandomNumber(GV_CreateUserIds.length-1); var tempWastageTypeIndex = getRangeRandomNumber(GV_CarWastageInitConfig.wastageType.length-1); var tempWastageTypeDetailIndex = getRangeRandomNumber(GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].detailed.length-1); GV_CarWastageInitConfig.source.push({ "1572493552001": { id: (carInfoIndex+1).toString(), name: carInfo["1572493551003"], value: "" }, "1572493552002": [{id:GV_CreateUserIds[tempUserIdIndexWastageType],name:GV_CreateUserIds[tempUserIdIndexWastageType],face:""}], "1572493552003": getRangeRandomDate(carInfo["1572493551004"],"2019-11-07","yyyy-MM-dd HH:mm:ss"), "1572493552004": GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].text, "1572493552005": getRangeRandomNumber(20,10000), "1572493552006": GV_CarWastageInitConfig.wastageType[tempWastageTypeIndex].detailed[tempWastageTypeDetailIndex] }); } for(var i=0;i<getRangeRandomNumber(100,200);i++){ var tempUserIdIndexRevenue = getRangeRandomNumber(GV_CreateUserIds.length-1); var tempRevenueIndex = getRangeRandomNumber(GV_CarRevenueInitConfig.revenueTypes.length-1); var tempRevenueDetailIndex = getRangeRandomNumber(GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].detailed.length-1); GV_CarRevenueInitConfig.source.push({ "1572493553001": { id: (carInfoIndex+1).toString(), name: carInfo["1572493551003"], value: "" }, "1572493553002": [{id:GV_CreateUserIds[tempUserIdIndexRevenue],name:GV_CreateUserIds[tempUserIdIndexRevenue],face:""}], "1572493553003": getRangeRandomDate(carInfo["1572493551004"],"2019-11-07","yyyy-MM-dd HH:mm:ss"), "1572493553004": GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].text, "1572493553005": getRangeRandomNumber(20,10000), "1572493553006": getRangeRandomNumber(50,3000), "1572493553007": GV_CarRevenueInitConfig.revenueTypes[tempRevenueIndex].detailed[tempRevenueDetailIndex] }); } }); } handleBatchInsertData(GV_CarInfoInitConfig); handleBatchInsertData(GV_CarWastageInitConfig); handleBatchInsertData(GV_CarRevenueInitConfig);View Code
看一下資料庫中的截圖
一共生成了656084條數據,我覺得還是不錯了,沒白折騰……當然你可以將隨機數調的大一些,便可以生成更多的數據,就到這裡吧!