flexbiz-server
Version:
Flexible Server
11 lines (10 loc) • 4.91 kB
JavaScript
const gt_ctgt=global.getModel("gt_ctgt"),xoadulieucu=require("./tinhGtXoa"),tinhgiathanhtt=require("./tinhGtTrucTiep"),tinhsoluong=require("./tinhGtSltk"),taphopcppstk=require("./tinhGtThcppstk"),phanbotheosl=require("./tinhGtPhanboSltp"),phanbotheohs=require("./tinhGtPhanboHeSo"),phanbotheoyt=require("./tinhGtPhanboYtcp"),capnhatpnk=require("./tinhGtCapnhatPNK"),tinhgiatb=require("./tinhgiatb"),{onAfterCommit,executeInTransaction,runWithoutSession}=require("./sessionContext");
module.exports=async function($condition$$,$callback$$){Logger.info("\ud83d\udd25 [tinhgiathanh] b\u1eaft \u0111\u1ea7u t\u00ednh gi\u00e1 th\u00e0nh v\u1edbi \u0111i\u1ec1u ki\u1ec7n",JSON.stringify($condition$$,null,2));if(!($condition$$&&$condition$$.thang&&$condition$$.nam&&$condition$$.id_app))return $callback$$("T\u00ednh n\u0103ng n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: thang,nam,id_app");$condition$$.nam=Number($condition$$.nam);$condition$$.thang=Number($condition$$.thang);(new Promise(($resolve$$,
$reject$$)=>{Logger.info("[tinhgiathanh] xo\u00e1 d\u1eef li\u1ec7u c\u0169...");xoadulieucu($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resolve$$($rs$$)})})).then(()=>{Logger.info("[tinhgiathanh] T\u00ednh s\u1ed1 l\u01b0\u1ee3ng s\u1ea3n xu\u1ea5t...");return new Promise(($resole$$,$reject$$)=>{tinhsoluong($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] T\u1eadp h\u1ee3p chi ph\u00ed ph\u00e1t sinh trong k\u1ef3...");
return new Promise(($resole$$,$reject$$)=>{taphopcppstk($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] Chi ph\u00ed t\u1eadp h\u1ee3p tr\u1ef1c ti\u1ebfp theo s\u1ea3n ph\u1ea9m...");return new Promise(($resole$$,$reject$$)=>{tinhgiathanhtt($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] Ph\u00e2n b\u1ed5 chi ph\u00ed theo s\u1ed1 l\u01b0\u1ee3ng th\u00e0nh ph\u1ea9m...");
return new Promise(($resole$$,$reject$$)=>{phanbotheosl($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] Ph\u00e2n b\u1ed5 chi ph\u00ed theo h\u1ec7 s\u1ed1...");return new Promise(($resole$$,$reject$$)=>{phanbotheohs($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] Ph\u00e2n b\u1ed5 chi ph\u00ed theo y\u00eau t\u1ed1 l\u1ea7n 1...");return new Promise(($resole$$,
$reject$$)=>{phanbotheoyt($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] Ph\u00e2n b\u1ed5 chi ph\u00ed theo y\u00eau t\u1ed1 l\u1ea7n 2...");return new Promise(($resole$$,$reject$$)=>{phanbotheoyt($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(()=>{Logger.info("[tinhgiathanh] C\u1eadp nh\u1eadt phi\u1ebfu nh\u1eadp kho th\u00e0nh ph\u1ea9m...");return new Promise(($resole$$,$reject$$)=>
{capnhatpnk($condition$$,($e$$,$rs$$)=>{if($e$$)return $reject$$($e$$);$resole$$($rs$$)})})}).then(async()=>{Logger.info("[tinhgiathanh] Chu\u1ea9n b\u1ecb d\u1eef li\u1ec7u \u0111\u1ec3 ch\u1ea1y l\u1ea1i gi\u00e1 trung b\u00ecnh...");var $cond_ctgt$$={id_app:$condition$$.id_app,nam:$condition$$.nam,thang:$condition$$.thang};$condition$$.ma_bp&&($cond_ctgt$$.ma_bp=$condition$$.ma_bp.toUpperCase());$condition$$.id_lenhsx&&global.mongoose.Types.ObjectId.isValid($condition$$.id_lenhsx)&&($cond_ctgt$$.id_lenhsx=
$condition$$.id_lenhsx);$cond_ctgt$$=await gt_ctgt.find($cond_ctgt$$).inTxn().lean();const $ds_ma_vt$$=[...(new Set($cond_ctgt$$.filter($r$$=>$r$$.ma_sp).map($r$$=>$r$$.ma_sp)))];onAfterCommit(()=>{Logger.info("\ud83d\ude80 [onAfterCommit] B\u1eaft \u0111\u1ea7u ti\u1ebfn tr\u00ecnh t\u00ednh gi\u00e1 trung b\u00ecnh n\u1ec1n...");runWithoutSession(async()=>{try{const $query_dmvt$$={id_app:$condition$$.id_app,nam:$condition$$.nam,tu_thang:$condition$$.thang,den_thang:$condition$$.thang,ma_vt:{$in:$ds_ma_vt$$}};
await executeInTransaction(async $newSession$$=>{Logger.info(`\ud83d\udd04 [onAfterCommit] \u0110ang ch\u1ea1y t\u00ednh gi\u00e1 TB trong Transaction m\u1edbi (${$newSession$$.id})...`);await new Promise(($resolveCalc$$,$rejectCalc$$)=>{tinhgiatb($query_dmvt$$,$err$$=>{if($err$$)return $rejectCalc$$($err$$);$resolveCalc$$()})})});Logger.info("\u2705 [onAfterCommit] T\u00ednh gi\u00e1 trung b\u00ecnh n\u1ec1n ho\u00e0n t\u1ea5t.")}catch($e$$){Logger.error("\u274c [onAfterCommit] L\u1ed7i khi ch\u1ea1y t\u00ednh gi\u00e1 trung b\u00ecnh n\u1ec1n:",
$e$$)}})});return $cond_ctgt$$}).then($ctgt$$=>{Logger.info("\u2705[tinhgiathanh] \u0110\u00e3 ho\u00e0n th\u00e0nh");$callback$$(null,$ctgt$$)}).catch($e$$=>{Logger.error("\u274c [tinhgiathanh] \u0110\u00e3 c\u00f3 l\u1ed7i x\u1ea3y ra:",$e$$);$callback$$($e$$)})};