ration_item.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var mongoose = require("mongoose");
  5. var dbm = require("../../../config/db/db_manager");
  6. var db = dbm.getCfgConnection("scConstruct");
  7. var async = require("async");
  8. var Schema = mongoose.Schema;
  9. var rationGljItemSchema = mongoose.Schema({
  10. gljId: Number,
  11. consumeAmt: Number,
  12. proportion: Number //配合比,暂时无需使用,默认0
  13. }, { _id: false });
  14. var rationAssItemSchema = mongoose.Schema({
  15. name: String,
  16. assistID: Number,
  17. assistCode: String,
  18. stdValue: String,
  19. stepValue: String,
  20. decimal: Number,
  21. carryBit: String,
  22. minValue: String,
  23. maxValue: String
  24. }, { _id: false });
  25. var rationItemSchema = mongoose.Schema({
  26. ID:Number,
  27. code: String,
  28. name: String,
  29. unit: String,
  30. labourPrice: Number,
  31. materialPrice: Number,
  32. machinePrice: Number,
  33. basePrice: Number,
  34. sectionId: Number,
  35. rationRepId: Number,
  36. caption: String,
  37. feeType: Number,
  38. rationGljList: [rationGljItemSchema],
  39. rationCoeList: Array,
  40. rationAssList: [rationAssItemSchema]
  41. });
  42. var rationItemModel = db.model("std_ration_lib_ration_items",rationItemSchema, "std_ration_lib_ration_items")
  43. var counter = require('../../../public/counter/counter');
  44. let gljDao = require('./glj_repository');
  45. let moment = require('moment');
  46. let rationRepositoryDao = require('./repository_map');
  47. var rationItemDAO = function(){};
  48. rationItemDAO.prototype.getRationItemsBySection = function(sectionId,callback){
  49. rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  50. if(err) callback(true, "Fail to get items", "")
  51. else callback(false,"Get items successfully", data);
  52. })
  53. };
  54. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, lastOpr, sectionId, updateItems, addItems, rIds, callback){
  55. var me = this;
  56. if (updateItems.length == 0 && rIds.length == 0) {
  57. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  58. } else {
  59. me.removeRationItems(rationLibId, lastOpr, rIds, function(err, message, docs) {
  60. if (err) {
  61. callback(true, "Fail to remove", false);
  62. } else {
  63. me.updateRationItems(rationLibId, lastOpr, sectionId, updateItems, function(err, results){
  64. if (err) {
  65. callback(true, "Fail to save", false);
  66. } else {
  67. if (addItems && addItems.length > 0) {
  68. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  69. } else {
  70. callback(false, "Save successfully", results);
  71. }
  72. }
  73. });
  74. }
  75. })
  76. }
  77. };
  78. rationItemDAO.prototype.removeRationItems = function(rationLibId, lastOpr, rIds,callback){
  79. if (rIds.length > 0) {
  80. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  81. if (err) {
  82. callback(true, "Fail to remove", false);
  83. } else {
  84. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  85. if(!err){
  86. callback(false, "Remove successfully", docs);
  87. }
  88. })
  89. }
  90. })
  91. } else {
  92. callback(false, "No records were deleted!", null);
  93. }
  94. };
  95. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  96. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  97. if(err) callback(true, "Fail to get items", "")
  98. else callback(false,"Get items successfully", data);
  99. })
  100. };
  101. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  102. var filter = {
  103. 'rationRepId': repId,
  104. '$and': [{
  105. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  106. }, {
  107. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  108. }]
  109. };
  110. rationItemModel.find(filter, function (err, data) {
  111. if (err) {
  112. callback(true, 'Fail to find ration', null);
  113. } else {
  114. callback(false, '', data);
  115. }
  116. })
  117. }
  118. rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
  119. if (callback) {
  120. rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
  121. .then(function (result, err) {
  122. if (err) {
  123. callback(1, '找不到定额“' + code +'”' , null);
  124. } else {
  125. callback(0, '', result);
  126. }
  127. });
  128. return null;
  129. } else {
  130. return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
  131. }
  132. };
  133. rationItemDAO.prototype.addRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  134. if (items && items.length > 0) {
  135. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  136. var maxId = result.value.sequence_value;
  137. var arr = [];
  138. for (var i = 0; i < items.length; i++) {
  139. var obj = new rationItemModel(items[i]);
  140. obj.ID = (maxId - (items.length - 1) + i);
  141. obj.sectionId = sectionId;
  142. obj.rationRepId = rationLibId;
  143. arr.push(obj);
  144. }
  145. rationItemModel.collection.insert(arr, null, function(err, docs){
  146. if (err) {
  147. callback(true, "Fail to save", false);
  148. } else {
  149. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  150. if(err){
  151. callback(true, "Fail to sava operator", false);
  152. }
  153. else{
  154. callback(false, "Save successfully", docs);
  155. }
  156. })
  157. }
  158. })
  159. });
  160. } else {
  161. callback(true, "Source error!", false);
  162. }
  163. };
  164. rationItemDAO.prototype.updateRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  165. var functions = [];
  166. for (var i=0; i < items.length; i++) {
  167. functions.push((function(doc) {
  168. return function(cb) {
  169. var filter = {};
  170. if (doc.ID) {
  171. filter.ID = doc.ID;
  172. } else {
  173. filter.sectionId = sectionId;
  174. if (rationLibId) filter.rationRepId = rationLibId;
  175. filter.code = doc.code;
  176. }
  177. rationItemModel.update(filter, doc, cb);
  178. };
  179. })(items[i]));
  180. }
  181. functions.push((function () {
  182. return function (cb) {
  183. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  184. if(err){
  185. cb(err);
  186. }
  187. else{
  188. cb(null);
  189. }
  190. });
  191. }
  192. })());
  193. async.parallel(functions, function(err, results) {
  194. callback(err, results);
  195. });
  196. };
  197. //ration round func
  198. function round(v,e){
  199. var t=1;
  200. for(;e>0;t*=10,e--);
  201. for(;e<0;t/=10,e++);
  202. return Math.round(v*t)/t;
  203. }
  204. rationItemDAO.prototype.updateRationBasePrc = function (basePrcArr, callback) {
  205. // let basePrcArr = data.basePrcArr;
  206. // adjGljId = data.gljId, adjBasePrice = data.basePrice, adjGljType = data.gljType,
  207. // repId = data.repId, lastOpr = data.lastOpr;
  208. //
  209. // let updateArr;
  210. async.each(basePrcArr, function (basePrcObj, finalCb) {
  211. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  212. async.waterfall([
  213. function (cb) {
  214. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  215. rationItemModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, result) {
  216. if(err){
  217. cb(err);
  218. }
  219. else{
  220. //删除
  221. rationItemModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  222. if(err){
  223. cb(err);
  224. }
  225. else{
  226. cb(null, result);
  227. }
  228. });
  229. }
  230. });
  231. }
  232. else{
  233. rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  234. if(err){
  235. cb(err);
  236. }
  237. else{
  238. cb(null, result);
  239. }
  240. });
  241. }
  242. /* rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  243. if(err){
  244. cb(err);
  245. }
  246. else{
  247. cb(null, result);
  248. }
  249. });*/
  250. },
  251. function (result, cb) {
  252. async.each(result, function (rationItem, ecb) {
  253. let rationGljList = rationItem.rationGljList,
  254. gljIds = [];
  255. rationGljList.forEach(function (rationGlj) {
  256. gljIds.push(rationGlj.gljId);
  257. });
  258. gljDao.getGljItems(gljIds, function(err, gljItems){
  259. if(err){
  260. ecb(err);
  261. }
  262. else{
  263. let gljArr = [];
  264. for(let i=0; i<gljItems.length; i++){
  265. let gljParentType = -1;
  266. if(gljItems[i].ID === adjGljId){
  267. gljItems[i].gljType = adjGljType;
  268. }
  269. if(gljItems[i].gljType <= 3){
  270. gljParentType = gljItems[i].gljType;
  271. }
  272. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  273. gljParentType = 2;
  274. }
  275. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  276. gljParentType = 3;
  277. }
  278. if(gljItems[i].ID === adjGljId){
  279. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  280. }
  281. else {
  282. gljArr.push({gljId: gljItems[i].ID, basePrice: gljItems[i].basePrice, gljParentType: gljParentType});
  283. }
  284. }
  285. gljArr.forEach(function (gljItem) {
  286. rationGljList.forEach(function (rationGlj) {
  287. if(gljItem.gljId === rationGlj.gljId){
  288. gljItem.consumeAmt = rationGlj.consumeAmt;
  289. }
  290. })
  291. });
  292. //recalculate the price of ration
  293. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  294. gljArr.forEach(function (gljItem) {
  295. if(gljItem.gljParentType !== -1){
  296. singlePrc = round(gljItem.basePrice * gljItem.consumeAmt, 3);
  297. if(gljItem.gljParentType === 1){
  298. labourPrc.push(singlePrc);
  299. }
  300. else if(gljItem.gljParentType ===2){
  301. materialPrc.push(singlePrc);
  302. }
  303. else{
  304. machinePrc.push(singlePrc);
  305. }
  306. }
  307. });
  308. if(labourPrc.length > 0){
  309. let sumLaP = 0;
  310. for(let i=0; i<labourPrc.length; i++){
  311. sumLaP += labourPrc[i];
  312. }
  313. updatePrc.labourPrice = round(sumLaP, 2);
  314. }
  315. if(materialPrc.length > 0){
  316. let sumMtP = 0;
  317. for(let i= 0; i<materialPrc.length; i++){
  318. sumMtP += materialPrc[i];
  319. }
  320. updatePrc.materialPrice = round(sumMtP, 2);
  321. }
  322. if(machinePrc.length > 0){
  323. let sumMaP = 0;
  324. for(let i =0; i< machinePrc.length; i++){
  325. sumMaP += machinePrc[i];
  326. }
  327. updatePrc.machinePrice = round(sumMaP, 2);
  328. }
  329. updatePrc.basePrice = updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice;
  330. //updateDataBase
  331. rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice, materialPrice: updatePrc.materialPrice,
  332. machinePrice: updatePrc.machinePrice, basePrice: updatePrc.basePrice}},
  333. function (err, result) {
  334. if(err){
  335. ecb(err);
  336. }
  337. else {
  338. ecb(null);
  339. }
  340. });
  341. }
  342. });
  343. }, function(err){
  344. if(err){
  345. cb(err);
  346. }
  347. else {
  348. cb(null);
  349. }
  350. });
  351. },
  352. ], function (err) {
  353. if(err){
  354. finalCb(err);
  355. }
  356. else{
  357. finalCb(null);
  358. }
  359. });
  360. }, function (err) {
  361. if(err){
  362. callback(err, 'Error');
  363. }
  364. else{
  365. callback(null, '');
  366. }
  367. });
  368. };
  369. rationItemDAO.prototype.getRationGljIds = function (data, callback) {
  370. let repId = data.repId;
  371. rationItemModel.find({rationRepId: repId}, function (err, result) {
  372. if(err){
  373. callback(err, 'Error', null);
  374. }
  375. else{
  376. let rstIds = [], newRst = [];
  377. result.forEach(function (data) {
  378. if(data.rationGljList.length >0){
  379. data.rationGljList.forEach(function (gljObj) {
  380. rstIds.push(gljObj.gljId);
  381. })
  382. }
  383. });
  384. for(let i= 0; i< rstIds.length; i++){
  385. if(newRst.indexOf(rstIds[i]) === -1){
  386. newRst.push(rstIds[i]);
  387. }
  388. }
  389. callback(null, '', newRst);
  390. }
  391. });
  392. };
  393. rationItemDAO.prototype.getRationsCodes = function (data, callback) {
  394. let repId = data.repId;
  395. rationItemModel.find({rationRepId: repId}, function (err, result) {
  396. if(err){
  397. callback(err, 'Error', null);
  398. }
  399. else{
  400. let rstCodes = [];
  401. result.forEach(function (rationItem) {
  402. rstCodes.push(rationItem.code);
  403. });
  404. callback(null, 'get all rationCodes success', rstCodes);
  405. }
  406. })
  407. };
  408. module.exports = new rationItemDAO()