| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 | 
							- /**
 
-  * Created by Tony on 2017/4/28.
 
-  */
 
- var mongoose = require("mongoose");
 
- var dbm = require("../../../config/db/db_manager");
 
- var db = dbm.getCfgConnection("scConstruct");
 
- var async = require("async");
 
- var Schema = mongoose.Schema;
 
- var rationGljItemSchema = mongoose.Schema({
 
-     gljId: Number,
 
-     consumeAmt: Number,
 
-     proportion: Number //配合比,暂时无需使用,默认0
 
- }, { _id: false });
 
- var rationAssItemSchema = mongoose.Schema({
 
-     name: String,
 
-     assistID: Number,
 
-     assistCode: String,
 
-     stdValue: String,
 
-     stepValue: String,
 
-     decimal: Number,
 
-     carryBit: String,
 
-     minValue: String,
 
-     maxValue: String
 
- }, { _id: false });
 
- var rationItemSchema = mongoose.Schema({
 
-     ID:Number,
 
-     code: String,
 
-     name: String,
 
-     unit: String,
 
-     labourPrice: Number,
 
-     materialPrice: Number,
 
-     machinePrice: Number,
 
-     basePrice: Number,
 
-     sectionId: Number,
 
-     rationRepId: Number,
 
-     caption: String,
 
-     feeType: Number,
 
-     rationGljList: [rationGljItemSchema],
 
-     rationCoeList: Array,
 
-     rationAssList: [rationAssItemSchema]
 
- });
 
- var rationItemModel = db.model("std_ration_lib_ration_items",rationItemSchema, "std_ration_lib_ration_items")
 
- var counter = require('../../../public/counter/counter');
 
- let gljDao = require('./glj_repository');
 
- let moment = require('moment');
 
- let rationRepositoryDao = require('./repository_map');
 
- var rationItemDAO = function(){};
 
- rationItemDAO.prototype.getRationItemsBySection = function(sectionId,callback){
 
-     rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
 
-         if(err) callback(true, "Fail to get items", "")
 
-         else callback(false,"Get items successfully", data);
 
-     })
 
- };
 
- rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, lastOpr, sectionId, updateItems, addItems, rIds, callback){
 
-     var me = this;
 
-     if (updateItems.length == 0 && rIds.length == 0) {
 
-         me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
 
-     } else {
 
-         me.removeRationItems(rationLibId, lastOpr, rIds, function(err, message, docs) {
 
-             if (err) {
 
-                 callback(true, "Fail to remove", false);
 
-             } else {
 
-                 me.updateRationItems(rationLibId, lastOpr, sectionId, updateItems, function(err, results){
 
-                     if (err) {
 
-                         callback(true, "Fail to save", false);
 
-                     } else {
 
-                         if (addItems && addItems.length > 0) {
 
-                             me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
 
-                         } else {
 
-                             callback(false, "Save successfully", results);
 
-                         }
 
-                     }
 
-                 });
 
-             }
 
-         })
 
-     }
 
- };
 
- rationItemDAO.prototype.removeRationItems = function(rationLibId, lastOpr, rIds,callback){
 
-     if (rIds.length > 0) {
 
-         rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
 
-             if (err) {
 
-                 callback(true, "Fail to remove", false);
 
-             } else {
 
-                 rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
 
-                     if(!err){
 
-                         callback(false, "Remove successfully", docs);
 
-                     }
 
-                 })
 
-             }
 
-         })
 
-     } else {
 
-         callback(false, "No records were deleted!", null);
 
-     }
 
- };
 
- rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
 
-     rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
 
-         if(err) callback(true, "Fail to get items", "")
 
-         else callback(false,"Get items successfully", data);
 
-     })
 
- };
 
- rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
 
-     var filter = {
 
-         'rationRepId': repId,
 
-         '$and': [{
 
-             '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
 
-         }, {
 
-             '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
 
-         }]
 
-     };
 
-     rationItemModel.find(filter, function (err, data) {
 
-         if (err) {
 
-             callback(true, 'Fail to find ration', null);
 
-         } else {
 
-             callback(false, '', data);
 
-         }
 
-     })
 
- }
 
- rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
 
-     if (callback) {
 
-         rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
 
-             .then(function (result, err) {
 
-                 if (err) {
 
-                     callback(1, '找不到定额“' + code +'”' , null);
 
-                 } else {
 
-                     callback(0, '', result);
 
-                 }
 
-             });
 
-         return null;
 
-     } else {
 
-         return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
 
-     }
 
- };
 
- rationItemDAO.prototype.addRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
 
-     if (items && items.length > 0) {
 
-         counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
 
-             var maxId = result.value.sequence_value;
 
-             var arr = [];
 
-             for (var i = 0; i < items.length; i++) {
 
-                 var obj = new rationItemModel(items[i]);
 
-                 obj.ID = (maxId - (items.length - 1) + i);
 
-                 obj.sectionId = sectionId;
 
-                 obj.rationRepId = rationLibId;
 
-                 arr.push(obj);
 
-             }
 
-             rationItemModel.collection.insert(arr, null, function(err, docs){
 
-                 if (err) {
 
-                     callback(true, "Fail to save", false);
 
-                 } else {
 
-                     rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
 
-                         if(err){
 
-                             callback(true, "Fail to sava operator", false);
 
-                         }
 
-                         else{
 
-                             callback(false, "Save successfully", docs);
 
-                         }
 
-                     })
 
-                 }
 
-             })
 
-         });
 
-     } else {
 
-         callback(true, "Source error!", false);
 
-     }
 
- };
 
- rationItemDAO.prototype.updateRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
 
-     var functions = [];
 
-     for (var i=0; i < items.length; i++) {
 
-         functions.push((function(doc) {
 
-             return function(cb) {
 
-                 var filter = {};
 
-                 if (doc.ID) {
 
-                     filter.ID = doc.ID;
 
-                 } else {
 
-                     filter.sectionId = sectionId;
 
-                     if (rationLibId) filter.rationRepId = rationLibId;
 
-                     filter.code = doc.code;
 
-                 }
 
-                 rationItemModel.update(filter, doc, cb);
 
-             };
 
-         })(items[i]));
 
-     }
 
-     functions.push((function () {
 
-         return function (cb) {
 
-             rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
 
-                 if(err){
 
-                     cb(err);
 
-                 }
 
-                 else{
 
-                     cb(null);
 
-                 }
 
-             });
 
-         }
 
-     })());
 
-     async.parallel(functions, function(err, results) {
 
-         callback(err, results);
 
-     });
 
- };
 
- //ration round func
 
- function round(v,e){
 
-     var t=1;
 
-     for(;e>0;t*=10,e--);
 
-     for(;e<0;t/=10,e++);
 
-     return Math.round(v*t)/t;
 
- }
 
- rationItemDAO.prototype.updateRationBasePrc = function (data, callback) {
 
-     let basePrcArr = data.basePrcArr,
 
-        // adjGljId = data.gljId, adjBasePrice = data.basePrice, adjGljType = data.gljType,
 
-         repId = data.repId, lastOpr = data.lastOpr;
 
-     //
 
-     let updateArr;
 
-     async.each(basePrcArr, function (basePrcObj, finalCb) {
 
-         let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
 
-         async.waterfall([
 
-             function (cb) {
 
-                 rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
 
-                     if(err){
 
-                         cb(err);
 
-                     }
 
-                     else{
 
-                         cb(null, result);
 
-                     }
 
-                 });
 
-             },
 
-             function (result, cb) {
 
-                 async.each(result, function (rationItem, ecb) {
 
-                     let rationGljList = rationItem.rationGljList,
 
-                         gljIds = [];
 
-                     rationGljList.forEach(function (rationGlj) {
 
-                         gljIds.push(rationGlj.gljId);
 
-                     });
 
-                     gljDao.getGljItems(gljIds, function(err, gljItems){
 
-                         if(err){
 
-                             ecb(err);
 
-                         }
 
-                         else{
 
-                             let gljArr = [];
 
-                             for(let i=0; i<gljItems.length; i++){
 
-                                 let gljParentType = -1;
 
-                                 if(gljItems[i].ID === adjGljId){
 
-                                     gljItems[i].gljType = adjGljType;
 
-                                 }
 
-                                 if(gljItems[i].gljType <= 3){
 
-                                     gljParentType = gljItems[i].gljType;
 
-                                 }
 
-                                 if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
 
-                                     gljParentType = 2;
 
-                                 }
 
-                                 if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
 
-                                     gljParentType = 3;
 
-                                 }
 
-                                 if(gljItems[i].ID === adjGljId){
 
-                                     gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
 
-                                 }
 
-                                 else {
 
-                                     gljArr.push({gljId: gljItems[i].ID, basePrice: gljItems[i].basePrice, gljParentType: gljParentType});
 
-                                 }
 
-                             }
 
-                             gljArr.forEach(function (gljItem) {
 
-                                 rationGljList.forEach(function (rationGlj) {
 
-                                     if(gljItem.gljId === rationGlj.gljId){
 
-                                         gljItem.consumeAmt = rationGlj.consumeAmt;
 
-                                     }
 
-                                 })
 
-                             });
 
-                             //recalculate the price of ration
 
-                             let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
 
-                             gljArr.forEach(function (gljItem) {
 
-                                 if(gljItem.gljParentType !== -1){
 
-                                     singlePrc = round(gljItem.basePrice * gljItem.consumeAmt, 3);
 
-                                     if(gljItem.gljParentType === 1){
 
-                                         labourPrc.push(singlePrc);
 
-                                     }
 
-                                     else if(gljItem.gljParentType ===2){
 
-                                         materialPrc.push(singlePrc);
 
-                                     }
 
-                                     else{
 
-                                         machinePrc.push(singlePrc);
 
-                                     }
 
-                                 }
 
-                             });
 
-                             if(labourPrc.length > 0){
 
-                                 let sumLaP = 0;
 
-                                 for(let i=0; i<labourPrc.length; i++){
 
-                                     sumLaP += labourPrc[i];
 
-                                 }
 
-                                 updatePrc.labourPrice = round(sumLaP, 2);
 
-                             }
 
-                             if(materialPrc.length > 0){
 
-                                 let sumMtP = 0;
 
-                                 for(let i= 0; i<materialPrc.length; i++){
 
-                                     sumMtP += materialPrc[i];
 
-                                 }
 
-                                 updatePrc.materialPrice = round(sumMtP, 2);
 
-                             }
 
-                             if(machinePrc.length > 0){
 
-                                 let sumMaP = 0;
 
-                                 for(let i =0; i< machinePrc.length; i++){
 
-                                     sumMaP += machinePrc[i];
 
-                                 }
 
-                                 updatePrc.machinePrice = round(sumMaP, 2);
 
-                             }
 
-                             updatePrc.basePrice = updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice;
 
-                             //updateDataBase
 
-                             rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice, materialPrice: updatePrc.materialPrice,
 
-                                     machinePrice: updatePrc.machinePrice, basePrice: updatePrc.basePrice}},
 
-                                 function (err, result) {
 
-                                     if(err){
 
-                                         ecb(err);
 
-                                     }
 
-                                     else {
 
-                                         ecb(null);
 
-                                     }
 
-                                 });
 
-                         }
 
-                     });
 
-                 }, function(err){
 
-                     if(err){
 
-                         cb(err);
 
-                     }
 
-                     else {
 
-                         cb(null);
 
-                     }
 
-                 });
 
-             },
 
-             function (cb) {
 
-                 rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
 
-                     if(err){
 
-                         cb(err);
 
-                     }
 
-                     else{
 
-                         cb(null);
 
-                     }
 
-                 })
 
-             }
 
-         ], function (err) {
 
-             if(err){
 
-                 finalCb(err);
 
-             }
 
-             else{
 
-                 finalCb(null);
 
-             }
 
-         });
 
-     }, function (err) {
 
-         if(err){
 
-             callback(err, 'Error');
 
-         }
 
-         else{
 
-             callback(null, '');
 
-         }
 
-     });
 
-     //
 
-     /*async.waterfall([
 
-         function (cb) {
 
-             rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
 
-                 if(err){
 
-                     cb(err);
 
-                 }
 
-                 else{
 
-                     cb(null, result);
 
-                 }
 
-             });
 
-         },
 
-         function (result, cb) {
 
-             async.each(result, function (rationItem, ecb) {
 
-                 let rationGljList = rationItem.rationGljList,
 
-                     gljIds = [];
 
-                 rationGljList.forEach(function (rationGlj) {
 
-                     gljIds.push(rationGlj.gljId);
 
-                 });
 
-                 gljDao.getGljItems(gljIds, function(err, gljItems){
 
-                     if(err){
 
-                         ecb(err);
 
-                     }
 
-                     else{
 
-                         let gljArr = [];
 
-                         for(let i=0; i<gljItems.length; i++){
 
-                             let gljParentType = -1;
 
-                             if(gljItems[i].ID === adjGljId){
 
-                                 gljItems[i].gljType = adjGljType;
 
-                             }
 
-                             if(gljItems[i].gljType <= 3){
 
-                                 gljParentType = gljItems[i].gljType;
 
-                             }
 
-                             if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
 
-                                 gljParentType = 2;
 
-                             }
 
-                             if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
 
-                                 gljParentType = 3;
 
-                             }
 
-                             if(gljItems[i].ID === adjGljId){
 
-                                 gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
 
-                             }
 
-                             else {
 
-                                 gljArr.push({gljId: gljItems[i].ID, basePrice: gljItems[i].basePrice, gljParentType: gljParentType});
 
-                             }
 
-                         }
 
-                         gljArr.forEach(function (gljItem) {
 
-                             rationGljList.forEach(function (rationGlj) {
 
-                                 if(gljItem.gljId === rationGlj.gljId){
 
-                                     gljItem.consumeAmt = rationGlj.consumeAmt;
 
-                                 }
 
-                             })
 
-                         });
 
-                         //recalculate the price of ration
 
-                         let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
 
-                         gljArr.forEach(function (gljItem) {
 
-                             if(gljItem.gljParentType !== -1){
 
-                                 singlePrc = round(gljItem.basePrice * gljItem.consumeAmt, 3);
 
-                                 if(gljItem.gljParentType === 1){
 
-                                     labourPrc.push(singlePrc);
 
-                                 }
 
-                                 else if(gljItem.gljParentType ===2){
 
-                                     materialPrc.push(singlePrc);
 
-                                 }
 
-                                 else{
 
-                                     machinePrc.push(singlePrc);
 
-                                 }
 
-                             }
 
-                         });
 
-                         if(labourPrc.length > 0){
 
-                             let sumLaP = 0;
 
-                             for(let i=0; i<labourPrc.length; i++){
 
-                                 sumLaP += labourPrc[i];
 
-                             }
 
-                             updatePrc.labourPrice = round(sumLaP, 2);
 
-                         }
 
-                         if(materialPrc.length > 0){
 
-                             let sumMtP = 0;
 
-                             for(let i= 0; i<materialPrc.length; i++){
 
-                                 sumMtP += materialPrc[i];
 
-                             }
 
-                             updatePrc.materialPrice = round(sumMtP, 2);
 
-                         }
 
-                         if(machinePrc.length > 0){
 
-                             let sumMaP = 0;
 
-                             for(let i =0; i< machinePrc.length; i++){
 
-                                 sumMaP += machinePrc[i];
 
-                             }
 
-                             updatePrc.machinePrice = round(sumMaP, 2);
 
-                         }
 
-                         updatePrc.basePrice = updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice;
 
-                         //updateDataBase
 
-                         rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice, materialPrice: updatePrc.materialPrice,
 
-                             machinePrice: updatePrc.machinePrice, basePrice: updatePrc.basePrice}},
 
-                             function (err, result) {
 
-                                 if(err){
 
-                                     ecb(err);
 
-                                 }
 
-                                 else {
 
-                                     ecb(null);
 
-                                 }
 
-                             });
 
-                     }
 
-                 });
 
-             }, function(err){
 
-                 if(err){
 
-                     cb(err);
 
-                 }
 
-                 else {
 
-                     cb(null);
 
-                 }
 
-             });
 
-         },
 
-         function (cb) {
 
-             rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
 
-                 if(err){
 
-                     cb(err);
 
-                 }
 
-                 else{
 
-                     cb(null);
 
-                 }
 
-             })
 
-         }
 
-     ], function (err) {
 
-         if(err){
 
-             callback(err, 'Error');
 
-         }
 
-         else{
 
-             callback(null, '');
 
-         }
 
-     });*/
 
- };
 
- rationItemDAO.prototype.getRationGljIds = function (data, callback) {
 
-     let repId = data.repId;
 
-     rationItemModel.find({rationRepId: repId}, function (err, result) {
 
-         if(err){
 
-             callback(err, 'Error', null);
 
-         }
 
-         else{
 
-             let rstIds = [], newRst = [];
 
-             result.forEach(function (data) {
 
-                 if(data.rationGljList.length >0){
 
-                     data.rationGljList.forEach(function (gljObj) {
 
-                         rstIds.push(gljObj.gljId);
 
-                     })
 
-                 }
 
-             });
 
-             for(let i= 0; i< rstIds.length; i++){
 
-                 if(newRst.indexOf(rstIds[i]) === -1){
 
-                     newRst.push(rstIds[i]);
 
-                 }
 
-             }
 
-             callback(null, '', newRst);
 
-         }
 
-     });
 
- };
 
- rationItemDAO.prototype.getRationsCodes = function (data, callback) {
 
-     let repId = data.repId;
 
-     rationItemModel.find({rationRepId: repId}, function (err, result) {
 
-         if(err){
 
-             callback(err, 'Error', null);
 
-         }
 
-         else{
 
-             let rstCodes = [];
 
-             result.forEach(function (rationItem) {
 
-                 rstCodes.push(rationItem.code);
 
-             });
 
-             callback(null, 'get all rationCodes success', rstCodes);
 
-         }
 
-     })
 
- };
 
- module.exports = new rationItemDAO()
 
 
  |