/** * 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 (basePrcArr, 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) { if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){ rationItemModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, result) { if(err){ cb(err); } else{ //删除 rationItemModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) { if(err){ cb(err); } else{ cb(null, result); } }); } }); } else{ rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) { if(err){ cb(err); } else{ cb(null, result); } }); } /* 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 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 0){ let sumMtP = 0; for(let i= 0; i 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 (err) { if(err){ finalCb(err); } else{ finalCb(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()