UNPKG

flexbiz-server

Version:

Flexible Server

12 lines (11 loc) 4.89 kB
const _=require("lodash"); exports.find=async function($baseQuery_buildCheck_query$$,$fn$$,$options$$={model:"socai"}){try{const $socai$$=global.getModel($options$$.model||"socai"),$pipeline$$=[];$baseQuery_buildCheck_query$$={...$baseQuery_buildCheck_query$$};$options$$.queryByDateHour&&$baseQuery_buildCheck_query$$.ngay_ct&&($baseQuery_buildCheck_query$$.ngay_ct_full=$baseQuery_buildCheck_query$$.ngay_ct,delete $baseQuery_buildCheck_query$$.ngay_ct);let $targetMaKh$$=null,$hasKh$$=!1;if($baseQuery_buildCheck_query$$.ma_kh){if(_.isArray($baseQuery_buildCheck_query$$.ma_kh))if($baseQuery_buildCheck_query$$.ma_kh.length>0)$targetMaKh$$= {$in:$baseQuery_buildCheck_query$$.ma_kh},$hasKh$$=!0;else return $fn$$(null,[]);else $targetMaKh$$=$baseQuery_buildCheck_query$$.ma_kh,$hasKh$$=!0;delete $baseQuery_buildCheck_query$$.ma_kh}let $targetTk$$=$baseQuery_buildCheck_query$$.tk,$hasTk$$=!1;if($targetTk$$)if(typeof $targetTk$$==="string"&&$targetTk$$.trim()!=="")$hasTk$$=!0;else if($targetTk$$.$regex)$hasTk$$=!0;else if($targetTk$$.$in&&Array.isArray($targetTk$$.$in)&&$targetTk$$.$in.length>0)$hasTk$$=!0;else if(Array.isArray($targetTk$$)&& $targetTk$$.length>0)$hasTk$$=!0;else if(Array.isArray($targetTk$$)||$targetTk$$.$in&&Array.isArray($targetTk$$.$in))return $fn$$(null,[]);const $matchStage$$={};if($hasTk$$||$hasKh$$){const $matchNo$$={};$hasTk$$&&($matchNo$$.tk_no=$targetTk$$);$hasKh$$&&($matchNo$$.ma_kh_no=$targetMaKh$$);const $matchCo$$={};$hasTk$$&&($matchCo$$.tk_co=$targetTk$$);$hasKh$$&&($matchCo$$.ma_kh_co=$targetMaKh$$);$matchStage$$.$or=[$matchNo$$,$matchCo$$];delete $baseQuery_buildCheck_query$$.tk}Object.assign($matchStage$$, $baseQuery_buildCheck_query$$);$pipeline$$.push({$match:$matchStage$$});$pipeline$$.push({$addFields:{_side:["NO","CO"]}});$pipeline$$.push({$unwind:"$_side"});$baseQuery_buildCheck_query$$=($fieldTk$$,$fieldKh$$)=>{const $checks$$=[];$hasTk$$&&($targetTk$$.$regex?$checks$$.push({$regexMatch:{input:$fieldTk$$,regex:$targetTk$$.$regex,options:$targetTk$$.$options||"i"}}):$targetTk$$.$in&&Array.isArray($targetTk$$.$in)?$checks$$.push({$in:[$fieldTk$$,$targetTk$$.$in]}):Array.isArray($targetTk$$)?$checks$$.push({$in:[$fieldTk$$, $targetTk$$]}):$checks$$.push({$eq:[$fieldTk$$,$targetTk$$]}));$hasKh$$&&($targetMaKh$$.$in&&Array.isArray($targetMaKh$$.$in)?$checks$$.push({$in:[$fieldKh$$,$targetMaKh$$.$in]}):Array.isArray($targetMaKh$$)?$checks$$.push({$in:[$fieldKh$$,$targetMaKh$$]}):$checks$$.push({$eq:[$fieldKh$$,$targetMaKh$$]}));return $checks$$.length===0?!0:$checks$$.length===1?$checks$$[0]:{$and:$checks$$}};var $checkNo_fallbackMaKh$$=$baseQuery_buildCheck_query$$("$tk_no","$ma_kh_no");const $checkCo$$=$baseQuery_buildCheck_query$$("$tk_co", "$ma_kh_co");$pipeline$$.push({$match:{$expr:{$or:[{$and:[{$eq:["$_side","NO"]},$checkNo_fallbackMaKh$$]},{$and:[{$eq:["$_side","CO"]},$checkCo$$]}]}}});$checkNo_fallbackMaKh$$=($val1$$,$val2$$)=>({$cond:{if:{$in:[$val1$$,[null,"",void 0]]},then:$val2$$,else:$val1$$}});$pipeline$$.push({$addFields:{isNoSide:{$eq:["$_side","NO"]},ps_no:{$cond:{if:{$eq:["$_side","NO"]},then:{$round:[{$ifNull:["$tien",0]},0]},else:0}},ps_co:{$cond:{if:{$eq:["$_side","CO"]},then:{$round:[{$ifNull:["$tien",0]},0]},else:0}}, ps_no_nt:{$cond:{if:{$eq:["$_side","NO"]},then:{$ifNull:["$tien_nt",0]},else:0}},ps_co_nt:{$cond:{if:{$eq:["$_side","CO"]},then:{$ifNull:["$tien_nt",0]},else:0}},tk:{$cond:{if:{$eq:["$_side","NO"]},then:"$tk_no",else:"$tk_co"}},tk_du:{$cond:{if:{$eq:["$_side","NO"]},then:"$tk_co",else:"$tk_no"}},ma_kh:{$cond:{if:{$eq:["$_side","NO"]},then:$checkNo_fallbackMaKh$$("$ma_kh_no","$ma_kh_co"),else:$checkNo_fallbackMaKh$$("$ma_kh_co","$ma_kh_no")}}}});$pipeline$$.push({$project:{_side:0,isNoSide:0}});if($options$$&& $options$$.groupBy){let $groupByFields$$=$options$$.groupBy;_.isArray($groupByFields$$)&&$groupByFields$$.length!==0||($groupByFields$$="id_app ma_dvcs id_ct ma_ct ma_gd ngay_ct listinfo_code 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 gio ngay thang quy nam".split(" "),$options$$.model=="socai"&&$groupByFields$$.push("ngay_ct_full"));const $groupId$$={};$groupByFields$$.forEach($field$$=>$groupId$$[$field$$]= `$${$field$$}`);$pipeline$$.push({$group:{_id:$groupId$$,ps_no:{$sum:"$ps_no"},ps_co:{$sum:"$ps_co"},ps_no_nt:{$sum:"$ps_no_nt"},ps_co_nt:{$sum:"$ps_co_nt"}}});const $projectOutput$$={_id:0,ps_no:1,ps_co:1,ps_no_nt:1,ps_co_nt:1};$groupByFields$$.forEach($field$$=>$projectOutput$$[$field$$]=`$_id.${$field$$}`);$pipeline$$.push({$project:$projectOutput$$})}else $options$$&&$options$$.sortData&&$pipeline$$.push({$sort:{[$options$$.model=="socai"?"ngay_ct_full":"ngay_ct"]:1,so_ct:1,id_ct:1,nh_dk:1,ps_no:-1, ps_co:1}});const $result$$=await $socai$$.aggregate($pipeline$$).allowDiskUse(!0);$fn$$(null,$result$$)}catch($e$$){$fn$$($e$$)}};