flexbiz-server
Version:
Flexible Server
10 lines (9 loc) • 3.66 kB
JavaScript
const async=require("async"),_=require("lodash"),moment=require("moment");
exports.find=function($query$$,$fn$$,$options$$){const $socai$$=global.getModel("socai");async.parallel({ps_no:function($callback$$){setImmediate(async()=>{let $query_no$$={};_.extend($query_no$$,$query$$);$query_no$$.tk&&($query_no$$.tk_no=$query_no$$.tk,delete $query_no$$.tk);$query_no$$.ma_kh&&($query_no$$.ma_kh_no=$query_no$$.ma_kh,delete $query_no$$.ma_kh);let $page$$=0,$_data$$=[];const $check$$=await $socai$$.find($query_no$$).skip($page$$*1E6).limit(1E6).explain("executionStats");$check$$.length>
0&&$check$$[0].executionStats.totalKeysExamined!=$check$$[0].executionStats.totalDocsExamined&&Logger.error("vsocai: indexes c\u1ee7a socai ch\u01b0a hi\u1ec7u qu\u1ea3 cho query",$query_no$$,$check$$[0].executionStats);async.forever($callback$$=>{$socai$$.find($query_no$$).skip($page$$*1E6).limit(1E6).lean().then(function($rs$$){Logger.info("vsocai: ps no",{page:$page$$,rows_number:$rs$$.length});$rs$$.forEach($r$$=>{$r$$.tk=$r$$.tk_no;$r$$.tk_du=$r$$.tk_co;$r$$.ma_kh=$r$$.ma_kh_no;!$r$$.ma_kh&&
$r$$.ma_kh_co&&($r$$.ma_kh=$r$$.ma_kh_co);$r$$.ps_no_nt=$r$$.tien_nt;$r$$.ps_no=Math.roundBy($r$$.tien,0);$r$$.ps_co_nt=0;$r$$.ps_co=0});$_data$$=$_data$$.concat($rs$$);if($rs$$.length<1E6)return $callback$$("0");$page$$+=1;$callback$$()}).catch($e$$=>{$callback$$($e$$)})},$e$$=>{if($e$$!="0")return $callback$$($e$$);$callback$$(null,$_data$$)})})},ps_co:function($callback$$){setImmediate(()=>{let $query_co$$={};_.extend($query_co$$,$query$$);$query_co$$.tk&&($query_co$$.tk_co=$query_co$$.tk,delete $query_co$$.tk);
$query_co$$.ma_kh&&($query_co$$.ma_kh_co=$query_co$$.ma_kh,delete $query_co$$.ma_kh);let $page$$=0,$_data$$=[];async.forever($callback$$=>{$socai$$.find($query_co$$).skip($page$$*1E6).limit(1E6).lean().then(function($rs$$){Logger.info("vsocai: ps co",{page:$page$$,rows_number:$rs$$.length});$rs$$.forEach($r$$=>{$r$$.tk=$r$$.tk_co;$r$$.tk_du=$r$$.tk_no;$r$$.ma_kh=$r$$.ma_kh_co;!$r$$.ma_kh&&$r$$.ma_kh_no&&($r$$.ma_kh=$r$$.ma_kh_no);$r$$.ps_co_nt=$r$$.tien_nt;$r$$.ps_co=Math.roundBy($r$$.tien,0);$r$$.ps_no_nt=
0;$r$$.ps_no=0});$_data$$=$_data$$.concat($rs$$);if($rs$$.length<1E6)return $callback$$("0");$page$$+=1;$callback$$()}).catch($e$$=>{$callback$$($e$$)})},$e$$=>{if($e$$!="0")return $callback$$($e$$);$callback$$(null,$_data$$)})})}},function($e$$,$rs$$){if($e$$)return $fn$$($e$$);$e$$=[...$rs$$.ps_no,...$rs$$.ps_co];Logger.info("vsocai: handle data",{rows_number:$e$$.length});$options$$&&$options$$.sortData&&($e$$=$e$$.sort(($a$$,$b$$)=>{let $a_ngay_ct$$=moment($a$$.ngay_ct).endOf("date").toDate().getTime(),
$b_ngay_ct$$=moment($b$$.ngay_ct).endOf("date").toDate().getTime();return $a_ngay_ct$$<$b_ngay_ct$$?-1:$a_ngay_ct$$>$b_ngay_ct$$?1:$a$$.so_ct<$b$$.so_ct?-1:$a$$.so_ct>$b$$.so_ct?1:$a$$.id_ct<$b$$.id_ct?-1:$a$$.id_ct>$b$$.id_ct?1:$a$$.nh_dk<$b$$.nh_dk?-1:$a$$.nh_dk>$b$$.nh_dk||$a$$.ps_no<$b$$.ps_no?1:$a$$.ps_no>$b$$.ps_no||$a$$.ps_co<$b$$.ps_co?-1:$a$$.ps_co>$b$$.ps_co?1:0}));if($options$$&&$options$$.groupBy){let $groupBy$$;$options$$.groupBy&&(_.isArray($options$$.groupBy)&&$options$$.groupBy.length>
0&&($groupBy$$=$options$$.groupBy),$groupBy$$=$groupBy$$||"id_app ma_dvcs id_ct ma_ct ma_gd ngay_ct so_ct ngay_hd so_hd tk tk_du ma_kh ong_ba ma_nt ty_gia dien_giai ma_bp id_lenhsx ma_phi ma_hd ma_dt ma_nv ma_sp ma_cp ma_kho ma_vt ma_kenh ma_td1 ma_td2 ma_td3 ma_td4 ma_td5 ma_td6 ma_td7 ma_td8 ma_td9 gio ngay thang quy nam date_created date_updated".split(" "));$e$$.groupBy($groupBy$$,["ps_no","ps_co","ps_no_nt","ps_co_nt"],($e$$,$rs$$)=>{if($e$$)return $fn$$($e$$);$fn$$(null,$rs$$)})}else $fn$$(null,
$e$$)})};