flexbiz-server
Version:
Flexible Server
24 lines (23 loc) • 9.79 kB
JavaScript
const _=require("underscore"),async=require("async"),{round}=require("./utils"),lodash=require("lodash"),moment=require("moment");require("./joinData");Math.roundBy=round;
Date.prototype.nextDay=function($days_of_week_distances$$){if(!$days_of_week_distances$$||$days_of_week_distances$$.length===0)return null;let $d$$=new Date(this.getTime());$days_of_week_distances$$=$days_of_week_distances$$.map($day_of_week_distance$$=>{($day_of_week_distance$$=($day_of_week_distance$$+7-$d$$.getDay())%7)||($day_of_week_distance$$=7);return $day_of_week_distance$$}).sort(($a$$,$b$$)=>$a$$-$b$$);return $days_of_week_distances$$.length>0?($d$$.setDate($d$$.getDate()+$days_of_week_distances$$[0]),
$d$$):null};Array.prototype.csum=function($field$$,$condition$$,$exclude$$){return _.reduce(this,function($sum$$,$row_v$$){if(!_.isMatch($row_v$$,$condition$$)||$exclude$$&&_.isMatch($row_v$$,$exclude$$))return $sum$$;$row_v$$=Number($row_v$$[$field$$]||0)||0;isNaN($row_v$$)&&($row_v$$=0);return $sum$$+$row_v$$},0)};
Array.prototype.cmax=function($field$$,$condition$$){return _.reduce(this,function($max$$,$row$$){let $v$$=$row$$[$field$$];return $condition$$&&!_.isMatch($row$$,$condition$$)||lodash.isUndefined($v$$)?$max$$:$max$$?lodash.isNumber($v$$)?$max$$>$v$$?$max$$:$v$$:lodash.isString($v$$)?$v$$.localeCompare($max$$)==1?$v$$:$max$$:$v$$:$v$$},null)};Array.prototype.reg=function(){if(this.length==0)return"";let $tk$$;this.forEach(function($t$$){$t$$!==""&&($tk$$=$tk$$?$tk$$+"|^"+$t$$:"^"+$t$$)});return $tk$$};
Array.prototype.groupBy=function($fieldkey$$,$fieldsums$$,$fn$$,$options$$){const $self$$=this;setImmediate(()=>{const $groups$$=_.groupBy($self$$.filter($i$$=>$i$$),function($item$$){let $key$$="";if(lodash.isArray($fieldkey$$)){for(let $k$$ of $fieldkey$$){let $_key$$=$item$$[$k$$];lodash.isDate($_key$$)&&($_key$$=moment($_key$$).format("YYYY-MM-DD"));$_key$$===!1&&($_key$$="false");$_key$$||$_key$$==0||($_key$$="");$key$$=$key$$||$key$$==0?$key$$+"_"+$_key$$.toString():$_key$$}lodash.isString($key$$)&&
($key$$=$key$$.trim());$key$$||="ZZZ"}else $key$$=$item$$[$fieldkey$$],lodash.isDate($key$$)&&($key$$=moment($key$$).format("YYYY-MM-DD")),$key$$===!1&&($key$$="false"),$key$$||$key$$==0||($key$$="__");return $key$$});async.map(_.keys($groups$$),function($key$$,$callback$$){setImmediate(()=>{let $value$$=$groups$$[$key$$],$r$$={};if(lodash.isArray($fieldkey$$)&&$value$$.length>0)for(let $k$$ of $fieldkey$$)$r$$[$k$$]=$value$$[0][$k$$],$r$$[$k$$]||$r$$[$k$$]==0||$r$$[$k$$]==0||($r$$[$k$$]="");else $r$$[$fieldkey$$]=
$key$$;$fieldsums$$.forEach(function($f$$){lodash.isObject($f$$)?$r$$[$f$$.name]=$value$$.map($v$$=>$v$$[$f$$.value]||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0):$r$$[$f$$]=$value$$.map($v$$=>$v$$[$f$$]||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0)});$options$$&&$options$$.fieldmaxs&&$options$$.fieldmaxs.forEach(function($f$$){lodash.isObject($f$$)?$r$$[$f$$.name]=$value$$.cmax($f$$.value):$r$$[$f$$]=$value$$.cmax($f$$)});$callback$$(null,$r$$)})},function($error$$,$result$$){if($error$$)return $fn$$($error$$);$fn$$(null,
$result$$)})})};Array.prototype.asyncGroupBy=function($fieldkey$$,$fieldsums$$,$options$$){let $_this$$=this;return new Promise(($resolve$$,$reject$$)=>{$_this$$.groupBy($fieldkey$$,$fieldsums$$,($error$$,$rs$$)=>{if($error$$)return $reject$$($error$$);$resolve$$($rs$$)},$options$$)})};
Array.prototype.joinModel=function($id_app$$,$model$$,$joinFields$$,$fn$$){lodash.isArray($joinFields$$)&&$joinFields$$.forEach($join$$=>{if($join$$.where){let $_where$$={};for(let $key$$ in $join$$.where)$_where$$[$join$$.where[$key$$]]=$key$$;$join$$.where=$_where$$}});lodash.isArray($id_app$$)&&lodash.isArray($model$$)&&$model$$.forEach($join$$=>{if($join$$.where){let $_where$$={};for(let $key$$ in $join$$.where)$_where$$[$join$$.where[$key$$]]=$key$$;$join$$.where=$_where$$}});this.joinModel2($id_app$$,
$model$$,$joinFields$$,$fn$$)};
Array.prototype.asyncJoinModel2=async function($id_app$$,$model$$,$joinFields$$,$options$$={cache:!0}){let $_this$$=this,$rs$$;if($_this$$.length>0&&($model$$=="trangthai"||$model$$?.modelName=="trangthai")){_.isArray($joinFields$$)||($joinFields$$=[$joinFields$$]);if($_this$$[0].listinfo_code&&$_this$$[0].listinfo_code.toUpperCase()!=$_this$$[0].ma_ct){$joinFields$$.forEach($join$$=>{typeof $join$$.where=="object"&&($join$$.where_org={...$join$$.where},$join$$.where=$item$$=>{const $query$$={};for(let $key$$ in $join$$.where_org)$query$$[$join$$.where[$key$$]]=
$key$$=="ma_ct"?($item$$.listinfo_code||$item$$[$key$$]||"").toUpperCase():$item$$[$key$$];return $query$$})});$rs$$=await new Promise($resolve$$=>{$_this$$.joinModel9($id_app$$,$model$$,$joinFields$$,$rs$$=>{$resolve$$($rs$$)},$options$$)});let $joinFields_org$$=$joinFields$$.filter($j$$=>$j$$.where_org);$joinFields_org$$.length>0&&$_this$$.filter($r$$=>!$r$$.ten_trang_thai).length>0&&($joinFields_org$$.forEach($join$$=>{$join$$.where={...$join$$.where_org}}),await new Promise($resolve$$=>{$_this$$.filter($r$$=>
!$r$$.ten_trang_thai).joinModel9($id_app$$,$model$$,$joinFields_org$$,$rs$$=>{$resolve$$($rs$$)},$options$$)}))}else $rs$$=await new Promise($resolve$$=>{$_this$$.joinModel9($id_app$$,$model$$,$joinFields$$,$rs$$=>{$resolve$$($rs$$)},$options$$)});$_this$$[0].id_app&&$_this$$[0].ten_trang_thai&&await new Promise($resolve$$=>{$joinFields$$.forEach($join$$=>{$join$$.fields=[];$join$$.setFields=($item$$,$ref$$)=>{$ref$$&&$ref$$.ten_trang_thai&&($item$$.ten_trang_thai=$ref$$.ten_trang_thai,$item$$.color=
$ref$$.color||$item$$.color)}});$_this$$.filter($r$$=>$r$$.id_app).joinModel9($_this$$[0].id_app,"trangthaiapp",$joinFields$$,$rs$$=>{$resolve$$($rs$$)},$options$$)});return $rs$$}return new Promise($resolve$$=>{$_this$$.joinModel9($id_app$$,$model$$,$joinFields$$,$rs$$=>{$resolve$$($rs$$)},$options$$)})};
Array.prototype.joinModel2=function($id_app$$,$model$$,$joinFields$$,$fn$$,$options$$={cache:!0}){this.length>0&&!lodash.isArray($id_app$$)&&(typeof $model$$=="string"||$model$$.collection)?this.asyncJoinModel2($id_app$$,$model$$,$joinFields$$,$options$$).then($rs$$=>{$fn$$&&$fn$$($rs$$)}):this.joinModel9($id_app$$,$model$$,$joinFields$$,$fn$$,$options$$)};
Array.prototype.crossJoin=function($otherArray$$,$where$$={},$fn$$){setImmediate(()=>{let $newArray$$=[];for(let $e1$$ of this){const $_otherArray$$=$otherArray$$.filter($otherItem$$=>{if(_.isArray($where$$))for(let $key$$ of $where$$){if($e1$$[$key$$]!=$otherItem$$[$key$$])return!1}else for(let $key$$ in $where$$)if($e1$$[$key$$]!=$otherItem$$[$where$$[$key$$]])return!1;return!0});if($_otherArray$$.length===0)$newArray$$.push($e1$$);else for(let $item$$ of $_otherArray$$)$newArray$$.push({...$e1$$,
...$item$$})}$fn$$($newArray$$)})};Array.prototype.asyncCrossJoin=function($otherArray$$,$where$$){let $_this$$=this;return new Promise($resolve$$=>{$_this$$.crossJoin($otherArray$$,$where$$,$rs$$=>{$resolve$$($rs$$)})})};
Array.prototype.pivot=function($rowFields$$,$colPivot$$,$allKeys_colSum$$,$fn$$,$options$$={},$log$$=()=>{}){$log$$("pivot:start");const $data$$=this;if(!$data$$||$data$$.length===0)return $fn$$(null,{data:[],cols:[]});const $sumFields$$=Array.isArray($allKeys_colSum$$)?$allKeys_colSum$$:$allKeys_colSum$$?[$allKeys_colSum$$]:[],$maxFields$$=$options$$.colMax?Array.isArray($options$$.colMax)?$options$$.colMax:[$options$$.colMax]:[];if(!Array.isArray($rowFields$$)||$rowFields$$.length===0){const $firstItem$$=
$data$$[0];$allKeys_colSum$$=Object.keys($firstItem$$);const $excludeKeys$$=new Set([$colPivot$$,...$sumFields$$,...$maxFields$$,"_id","date_created","date_updated","__key","__v"]);$rowFields$$=$allKeys_colSum$$.filter($key$$=>!$excludeKeys$$.has($key$$)&&$firstItem$$[$key$$]&&!lodash.isNumber($firstItem$$[$key$$]))}setImmediate(()=>{try{$log$$("pivot:processing");const $resultMap$$=new Map,$colValuesSet$$=new Set;for(let $i$$=0;$i$$<$data$$.length;$i$$++){const $item$$=$data$$[$i$$],$colVal$$=$item$$[$colPivot$$];
if($colVal$$===void 0||$colVal$$===null)continue;$colValuesSet$$.add($colVal$$);const $rowKeyVal$$=$rowFields$$.map($f$$=>$item$$[$f$$]),$rowKey$$=JSON.stringify($rowKeyVal$$);if(!$resultMap$$.has($rowKey$$)){const $newRow$$={};$rowFields$$.forEach(($f$$,$idx$$)=>$newRow$$[$f$$]=$rowKeyVal$$[$idx$$]);$resultMap$$.set($rowKey$$,$newRow$$)}const $rowData$$=$resultMap$$.get($rowKey$$),$colPrefix$$=$colVal$$.toString();for(const $field$$ of $sumFields$$){const $val$$=Number($item$$[$field$$])||0;var $outKey_outKey$$=
$colPrefix$$+$field$$;$rowData$$[$outKey_outKey$$]=($rowData$$[$outKey_outKey$$]||0)+$val$$;$outKey_outKey$$="total_"+$field$$;$rowData$$[$outKey_outKey$$]=($rowData$$[$outKey_outKey$$]||0)+$val$$}for(const $field$$ of $maxFields$$){$outKey_outKey$$=$colPrefix$$+$field$$;const $val$$=Number($item$$[$field$$])||0;if($rowData$$[$outKey_outKey$$]===void 0||$val$$>$rowData$$[$outKey_outKey$$])$rowData$$[$outKey_outKey$$]=$val$$}}const $report$$=Array.from($resultMap$$.values()),$cols$$=Array.from($colValuesSet$$);
if($options$$.computed&&typeof $options$$.computed==="object"){$log$$("pivot:computing fields");const $prefixes$$=[...$cols$$.map($c$$=>$c$$.toString()),"total_"];for(const $row$$ of $report$$)for(const $prefix$$ of $prefixes$$)for(const [$newFieldSuffix$$,$computeFn$$]of Object.entries($options$$.computed)){const $val$$=$computeFn$$($row$$,$prefix$$);$row$$[$prefix$$+$newFieldSuffix$$]=$val$$}}$log$$("pivot:finish");$fn$$(null,{data:$report$$,cols:$cols$$})}catch($err$$){$log$$("pivot:error "+$err$$.message),
$fn$$($err$$,null)}})};Array.prototype.asyncPivot=function($rowFields$$,$colPivot$$,$colSum$$,$options$$={},$log$$=()=>{}){let $_this$$=this;return new Promise(($resolve$$,$reject$$)=>{$_this$$.pivot($rowFields$$,$colPivot$$,$colSum$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resolve$$($rs$$)},$options$$,$log$$)})};