UNPKG

flexbiz-server

Version:

Flexible Server

19 lines (18 loc) 8.42 kB
const moment=require("moment"),{onAfterCommit,getCurrentSession}=require("../libs/sessionContext"),socainppSchema=new Schema({id_app:{type:String,required:!0,maxlength:1024},ma_dvcs:{type:String,required:!0,maxlength:1024},id_ct:{type:String,required:!0,maxlength:1024,trim:!0},ma_ct:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},ma_gd:{type:String,default:"0",maxlength:32,trim:!0},ngay_ct:{type:Date,required:!0},so_ct:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},ngay_hd:{type:Date}, so_hd:{type:String,uppercase:!0,maxlength:32,trim:!0},tk_no:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},tk_co:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},ma_kh_no:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},ma_kh_co:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},ma_nt:{type:String,default:"VND",required:!0,uppercase:!0,maxlength:32,trim:!0},ty_gia:{type:Number,default:1},tien_nt:{type:Number,default:0},tien:{type:Number,default:0},dien_giai:{type:String, default:"",maxlength:1024},ong_ba:{type:String,default:"",maxlength:1024},ma_kho:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_bp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_phi:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_hd:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_dt:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_nv:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_lo:{type:String,default:"",uppercase:!0, maxlength:32,trim:!0},han_sd:{type:Date},ma_tt1:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt2:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt3:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_ca:{type:String,uppercase:!0,maxlength:32,trim:!0},ma_cp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_sp:{type:String,default:"",uppercase:!0,maxlength:1024,trim:!0},ma_vt:{type:String,default:"",uppercase:!0,maxlength:1024,trim:!0},ma_dvt_sp:{type:String, maxlength:32,trim:!0},ma_tt1_sp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt2_sp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt3_sp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_cum_chi_tiet:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},ten_cum_chi_tiet:{type:String,default:"",maxlength:1024,trim:!0},ma_cong_doan:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},id_lenhsx:{type:String,maxlength:1024},id_cs:{type:String,maxlength:1024}, id_link:String,ma_ct_link:String,so_ct_link:String,id_ct_goc:String,ma_ct_goc:String,so_ct_goc:String,ngay_ct_goc:Date,pt_thanh_toan_goc:String,pt_thanh_toan:String,pt_thanh_toan2:String,trang_thai:{type:String,maxlength:32},nh_dk:{type:Number,default:0},id_kbbtpb:{type:String,maxlength:1024},exfields:Schema.Types.Mixed,exfields_detail:Schema.Types.Mixed,flag:{type:String,maxlength:1024},gio:Number,ngay:Number,thang:Number,quy:Number,nam:Number,ma_kenh:String,ma_td1:Schema.Types.Mixed,ma_td2:Schema.Types.Mixed, ma_td3:Schema.Types.Mixed,ma_td4:Schema.Types.Mixed,ma_td5:Schema.Types.Mixed,ma_td6:Schema.Types.Mixed,ma_td7:Schema.Types.Mixed,ma_td8:Schema.Types.Mixed,ma_td9:Schema.Types.Mixed,status:{type:Boolean,default:!0},date_created:{type:Date,default:Date.now},date_updated:{type:Date,default:Date.now},user_created:{type:String,default:""},user_updated:{type:String,default:""},id_client:String,listinfo_code:String},{toJSON:{virtuals:!0}}); socainppSchema.pre("save",function($next$$){this.tien=this.ma_nt=="VND"?this.tien_nt=Math.roundBy(this.tien_nt,0):Math.roundBy(this.tien,0);let $ngay_ct$$=moment(this.ngay_ct);this.gio=$ngay_ct$$.get("hours");this.ngay=$ngay_ct$$.get("dates");this.quy=$ngay_ct$$.get("quarters");this.thang=$ngay_ct$$.get("months")+1;this.nam=$ngay_ct$$.get("year");this.ngay_ct=$ngay_ct$$.startOf("date").toDate();$next$$()}); (global.configs||{}).createIndexes&&(socainppSchema.index({id_app:1}),socainppSchema.index({id_app:1,ma_dvcs:1}),socainppSchema.index({id_app:1,ma_ct:1,so_ct:1}),socainppSchema.index({id_app:1,ma_ct:1}),socainppSchema.index({id_app:1,ngay_ct:-1,ma_nt:1}),socainppSchema.index({id_app:1,so_ct:1,ngay_ct:-1}),socainppSchema.index({id_app:1,gio:1}),socainppSchema.index({id_app:1,ngay:1}),socainppSchema.index({id_app:1,thang:1}),socainppSchema.index({id_app:1,quy:1}),socainppSchema.index({id_app:1,nam:-1}), socainppSchema.index({id_ct:1}),socainppSchema.index({id_app:1,id_ct:1}),socainppSchema.index({id_app:1,ma_ct_goc:1,id_ct_goc:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_bp:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_phi:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_hd:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_dt:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_nv:1}),socainppSchema.index({id_app:1, ngay_ct:-1,tk_no:1,tk_co:1,ma_sp:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_kho:1,ma_vt:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_kenh:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_td1:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_td2:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_td3:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1,tk_co:1,ma_vt:1,ma_tt1:1,ma_tt2:1,ma_tt3:1,ma_lo:1,han_sd:-1}), socainppSchema.index({id_app:1,id_lenhsx:1}),socainppSchema.index({id_app:1,id_link:1}),socainppSchema.index({id_app:1,id_cs:1}),socainppSchema.index({id_app:1,id_cs_ck:1}),socainppSchema.index({id_app:1,id_ct:1,so_ct:-1,ngay_ct:-1,ma_kh_no:1,ma_kh_co:1,tk_no:1,tk_co:1}),socainppSchema.index({id_app:1,ngay_ct:-1,ma_kh_no:1,ma_kh_co:1,tk_no:1,tk_co:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_no:1}),socainppSchema.index({id_app:1,ngay_ct:-1,tk_co:1}),socainppSchema.index({id_app:1,date_created:-1})); socainppSchema.post("save",async function($doc$$){const $session$$=$doc$$.$session()||getCurrentSession(),$handleSaveAfterCommit$$=async()=>{if(["SO1","HD2","HD1","PBL"].indexOf($doc$$.ma_ct)>=0){if($doc$$.ma_kh_no){var $dhgn_dhgn$$=await global.getModel("socainpp").findOne({ma_kh:$doc$$.ma_kh_no,id_app:$doc$$.id_app},{ngay_ct:1}).sort({ngay_ct:-1});if($dhgn_dhgn$$){var $so_ngay_da_mua_so_ngay_da_mua$$=moment().diff(moment($dhgn_dhgn$$.ngay_ct),"days");model.updateOne({ma_kh:$doc$$.ma_kh_no,id_app:$doc$$.id_app}, {ngay_mua_gan_nhat:$dhgn_dhgn$$.ngay_ct,so_ngay_da_mua:$so_ngay_da_mua_so_ngay_da_mua$$})}}$doc$$.ma_kh_co&&$doc$$.ma_kh_co!=$doc$$.ma_kh_no&&($dhgn_dhgn$$=await global.getModel("socainpp").findOne({ma_kh:$doc$$.ma_kh_co,id_app:$doc$$.id_app},{ngay_ct:1}).sort({ngay_ct:-1}))&&($so_ngay_da_mua_so_ngay_da_mua$$=moment().diff(moment($dhgn_dhgn$$.ngay_ct),"days"),model.updateOne({ma_kh:$doc$$.ma_kh_co,id_app:$doc$$.id_app},{ngay_mua_gan_nhat:$dhgn_dhgn$$.ngay_ct,so_ngay_da_mua:$so_ngay_da_mua_so_ngay_da_mua$$}))}}; if($session$$)onAfterCommit($handleSaveAfterCommit$$,"X\u1eed l\u00fd s\u1ef1 ki\u1ec7n sau khi l\u01b0u socainpp",$session$$);else try{await $handleSaveAfterCommit$$()}catch($e$$){Logger.error("\u274c[socainpp] post save immediate error:",$e$$)}});const model=mongoose.models.socainpp||mongoose.model("socainpp",socainppSchema); model.getArise=async($query$$,$options$$={fields:void 0})=>{let $condition$$={ngay_ct:{$gte:$query$$.tu_ngay,$lte:$query$$.den_ngay},id_app:$query$$.id_app};$query$$.ma_dvcs&&($condition$$.ma_dvcs=$query$$.ma_dvcs);$query$$.ma_bp&&($condition$$.ma_bp=$query$$.ma_bp);$query$$.ma_hd&&($condition$$.ma_hd=$query$$.ma_hd);$query$$.ma_dt&&($condition$$.ma_dt=$query$$.ma_dt);$query$$.ma_sp&&($condition$$.ma_sp=$query$$.ma_sp);$query$$.ma_nv&&($condition$$.ma_nv=$query$$.ma_nv);$query$$.ma_phi&&($condition$$.ma_phi= $query$$.ma_phi);$query$$.ma_kho&&($condition$$.ma_kho=$query$$.ma_kho);if($query$$.tk_no&&Array.isArray($query$$.tk_no)){var $tk_co_tk_no$$=$query$$.tk_no.reg();$condition$$.tk_no=$query$$.giam_tru_no?{$not:new RegExp($tk_co_tk_no$$)}:{$regex:$tk_co_tk_no$$,$options:"i"}}$query$$.tk_co&&Array.isArray($query$$.tk_co)&&($tk_co_tk_no$$=$query$$.tk_co.reg(),$condition$$.tk_co=$query$$.giam_tru_co&&$tk_co_tk_no$$?{$not:new RegExp($tk_co_tk_no$$)}:{$regex:$tk_co_tk_no$$,$options:"i"});return await model.find($condition$$, $options$$.fields).lean()};module.exports=model;