compleRationModel.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**
  2. * Created by Zhong on 2017/12/21.
  3. */
  4. const mongoose = require('mongoose');
  5. const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree');
  6. const compleRationModel = mongoose.model('complementary_ration_items');
  7. const installSectionModel = mongoose.model("std_ration_lib_installationSection");
  8. const installFeeItemModel = mongoose.model("std_ration_lib_installation");
  9. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  10. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  11. import async from 'async';
  12. let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
  13. let counter = require('../../../public/counter/counter');
  14. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  15. class CompleRatoinDao {
  16. async updateRation(userID, compilationId, updateData, callback){
  17. try{
  18. for(let i = 0, len = updateData.length; i < len; i++){
  19. let updateObj = updateData[i];
  20. if(updateObj.updateType === 'new'){
  21. updateObj.updateData.userID = userID;
  22. updateObj.updateData.compilationId = compilationId;
  23. await compleRationModel.create(updateObj.updateData);
  24. }
  25. else if(updateObj.updateType === 'update'){
  26. await compleRationModel.update({userID: userID, rationRepId: updateObj.updateData.rationRepId}, updateObj.updateData);
  27. }
  28. }
  29. callback(0, '');
  30. }
  31. catch(err){
  32. callback(err, null);
  33. }
  34. }
  35. async getRationItems(userID, rationRepId, sectionId, callback){
  36. try{
  37. let stdRations = await stdRationModel.find({rationRepId: rationRepId, sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  38. //mark std
  39. for(let i = 0, len = stdRations.length; i < len; i++){
  40. stdRations[i]._doc.type = 'std';
  41. }
  42. let compleRations = await compleRationModel.find({userId: userID, rationRepId: rationRepId, sectionId: sectionId, deleteInfo: null});
  43. //mark complementary
  44. for(let i = 0, len = compleRations.length; i < len; i++){
  45. compleRations[i]._doc.type = 'complementary';
  46. }
  47. callback(0, stdRations.concat(compleRations));
  48. }
  49. catch(err){
  50. callback(err, null);
  51. }
  52. }
  53. async getRationsCodes(userID, rationRepId, callback){
  54. try{
  55. let stdRationCodes = await stdRationModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}, '-_id code');
  56. let compleRationCodes = await compleRationModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}, '-_id code');
  57. let rstCodes = [];
  58. stdRationCodes.concat(compleRationCodes).forEach(function (rationItem) {
  59. rstCodes.push(rationItem.code);
  60. });
  61. callback(0, rstCodes);
  62. }
  63. catch(err){
  64. callback(err, null);
  65. }
  66. }
  67. async getGljItems(gljLibId, callback){
  68. try{
  69. let stdGljs = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]});
  70. callback(0, stdGljs);
  71. }
  72. catch(err){
  73. callback(err, null);
  74. }
  75. }
  76. async getGljItemsOccupied(gljLibId, occupation, callback){
  77. try{
  78. let stdGls = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]}, occupation);
  79. callback(0, stdGls);
  80. }
  81. catch (err){
  82. callback(err, null);
  83. }
  84. }
  85. async getGljItemsByIds(userID, ids, callback){
  86. try{
  87. let rst = [];
  88. for(let i = 0, len = ids.length; i < len; i++){
  89. if(ids[i].type === 'std'){
  90. let stdGlj = await stdGljModel.find({ID: ids[i].id, deleteInfo: null});
  91. if(stdGlj.length > 0){
  92. stdGlj[0]._doc.type = 'std';
  93. rst.push(stdGlj[0]);
  94. }
  95. }
  96. else if(ids[i].type === 'complementary'){
  97. let compleGlj = await complementaryGljModel.find({userId: userID, ID: ids[i].id, deleteInfo: null});
  98. if(compleGlj.length > 0){
  99. compleGlj[0]._doc.type = 'complementary';
  100. rst.push(compleGlj[0]);
  101. }
  102. }
  103. }
  104. callback(0, rst);
  105. }
  106. catch(err){
  107. callback(err, null);
  108. }
  109. }
  110. async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){
  111. try{
  112. let rst = [];
  113. for(let i = 0, len = codes.length; i < len; i++){
  114. let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});
  115. if(stdGlj.length > 0){
  116. stdGlj[0]._doc.type = 'std';
  117. rst.push(stdGlj[0]);
  118. }
  119. else {
  120. let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});
  121. if(compleGlj.length > 0){
  122. compleGlj[0]._doc.type = 'complementary';
  123. rst.push(compleGlj[0]);
  124. }
  125. }
  126. }
  127. callback(0, rst);
  128. }
  129. catch(err){
  130. callback(err, null);
  131. }
  132. }
  133. //造价书定额库
  134. async getRationGljItemsBySection(userId, sectionId, callback){
  135. try{
  136. const perHintHeight = 17.2;
  137. let stdRations = await stdRationModel.find({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  138. for(let ration of stdRations){
  139. ration._doc.type = 'std';
  140. }
  141. let compleRations = await compleRationModel.find({userId: userId, sectionId: sectionId, deleteInfo: null});
  142. for(let ration of compleRations){
  143. ration._doc.type = 'complementary';
  144. }
  145. let rations = stdRations.concat(compleRations);
  146. rations.sort(function (a, b) {
  147. let rst = 0;
  148. if(a.code > b.code){
  149. rst = 1;
  150. }
  151. else if(a.code < b.code){
  152. rst = -1;
  153. }
  154. return rst;
  155. });
  156. for(let ration of rations){
  157. let hintsArr = [];
  158. for(let rationGlj of ration.rationGljList){
  159. let glj;
  160. if(!isDef(rationGlj.type) || rationGlj.type === 'std'){
  161. glj = await stdGljModel.findOne({ID: rationGlj.gljId});
  162. }
  163. else {
  164. glj = await complementaryGljModel.findOne({uesrId: userId, ID: rationGlj.gljId});
  165. }
  166. if(isDef(glj)){
  167. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  168. }
  169. }
  170. hintsArr.push(`基价 元 ${ration.basePrice}`);
  171. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  172. hintsArr.push(`工作内容:`);
  173. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  174. }
  175. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  176. hintsArr.push(`附注:`);
  177. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  178. }
  179. ration._doc.hint = hintsArr.join('<br>');
  180. // ration._doc.hintHeight = hintsArr.length * perHintHeight;//控制定额库悬浮提示位置
  181. }
  182. callback(0, rations);
  183. }
  184. catch(err){
  185. callback(err, null);
  186. }
  187. }
  188. updateRationBasePrc(userID, basePrcArr, callback){
  189. let me = this;
  190. async.each(basePrcArr, function (basePrcObj, finalCb) {
  191. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  192. async.waterfall([
  193. function (cb) {
  194. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  195. //补充定额
  196. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  197. if(err){
  198. cb(err);
  199. }
  200. else {
  201. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  202. if(err){
  203. cb(err);
  204. }
  205. else {
  206. cb(null, compleRst);
  207. }
  208. });
  209. }
  210. });
  211. }
  212. else{
  213. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  214. if(err){
  215. cb(err);
  216. }
  217. else {
  218. cb(null, compleRst);
  219. }
  220. });
  221. }
  222. },
  223. function (result, cb) {
  224. async.each(result, function (rationItem, ecb) {
  225. let rationGljList = rationItem.rationGljList,
  226. gljIds = [];
  227. rationGljList.forEach(function (rationGlj) {
  228. let idObj = Object.create(null);
  229. idObj.id = rationGlj.gljId;
  230. idObj.type = rationGlj.type;
  231. gljIds.push(idObj);
  232. });
  233. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  234. if(err){
  235. ecb(err);
  236. }
  237. else{
  238. let gljArr = [];
  239. for(let i=0; i<gljItems.length; i++){
  240. let gljParentType = -1;
  241. if(gljItems[i].ID === adjGljId){
  242. gljItems[i].gljType = adjGljType;
  243. }
  244. if(gljItems[i].gljType <= 3){
  245. gljParentType = gljItems[i].gljType;
  246. }
  247. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  248. gljParentType = 2;
  249. }
  250. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  251. gljParentType = 3;
  252. }
  253. if(gljItems[i].ID === adjGljId){
  254. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  255. }
  256. else {
  257. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  258. }
  259. }
  260. gljArr.forEach(function (gljItem) {
  261. rationGljList.forEach(function (rationGlj) {
  262. if(gljItem.gljId === rationGlj.gljId){
  263. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  264. }
  265. })
  266. });
  267. //recalculate the price of ration
  268. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  269. gljArr.forEach(function (gljItem) {
  270. if(gljItem.gljParentType !== -1){
  271. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  272. if(gljItem.gljParentType === 1){
  273. labourPrc.push(singlePrc);
  274. }
  275. else if(gljItem.gljParentType ===2){
  276. materialPrc.push(singlePrc);
  277. }
  278. else{
  279. machinePrc.push(singlePrc);
  280. }
  281. }
  282. });
  283. if(labourPrc.length > 0){
  284. let sumLaP = 0;
  285. for(let i=0; i<labourPrc.length; i++){
  286. sumLaP += labourPrc[i];
  287. }
  288. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  289. }
  290. if(materialPrc.length > 0){
  291. let sumMtP = 0;
  292. for(let i= 0; i<materialPrc.length; i++){
  293. sumMtP += materialPrc[i];
  294. }
  295. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  296. }
  297. if(machinePrc.length > 0){
  298. let sumMaP = 0;
  299. for(let i =0; i< machinePrc.length; i++){
  300. sumMaP += machinePrc[i];
  301. }
  302. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  303. }
  304. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  305. //updateDataBase
  306. compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  307. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  308. function (err, result) {
  309. if(err){
  310. ecb(err);
  311. }
  312. else {
  313. ecb(null);
  314. }
  315. });
  316. }
  317. });
  318. }, function(err){
  319. if(err){
  320. cb(err);
  321. }
  322. else {
  323. cb(null);
  324. }
  325. });
  326. },
  327. ], function (err) {
  328. if(err){
  329. finalCb(err);
  330. }
  331. else{
  332. finalCb(null);
  333. }
  334. });
  335. }, function (err) {
  336. if(err){
  337. callback(err, 'Error');
  338. }
  339. else{
  340. callback(0, '');
  341. }
  342. });
  343. }
  344. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  345. let me = this;
  346. if (updateItems.length == 0 && rIds.length == 0) {
  347. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  348. } else {
  349. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  350. if (err) {
  351. callback(true, false);
  352. } else {
  353. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  354. if (err) {
  355. callback(true, false);
  356. } else {
  357. if (addItems && addItems.length > 0) {
  358. me.addRationItems(rationLibId, sectionId, addItems, callback);
  359. } else {
  360. callback(0, results);
  361. }
  362. }
  363. });
  364. }
  365. })
  366. }
  367. }
  368. removeRationItems(rationRepId, rIds,callback){
  369. if (rIds.length > 0) {
  370. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  371. if (err) {
  372. callback(true, false);
  373. } else {
  374. callback(0, docs);
  375. }
  376. })
  377. } else {
  378. callback(0, null);
  379. }
  380. }
  381. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  382. if (items && items.length > 0) {
  383. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  384. let maxId = result.sequence_value;
  385. let arr = [];
  386. for (let i = 0; i < items.length; i++) {
  387. let obj = new compleRationModel(items[i]);
  388. obj.ID = (maxId - (items.length - 1) + i);
  389. obj.sectionId = sectionId;
  390. obj.rationRepId = rationLibId;
  391. obj.userId = userID;
  392. obj.compilationId = compilationId;
  393. arr.push(obj);
  394. }
  395. compleRationModel.collection.insert(arr, null, function(err, docs){
  396. if (err) {
  397. callback(true, false);
  398. } else {
  399. callback(0, docs);
  400. }
  401. })
  402. });
  403. } else {
  404. callback(true, "Source error!", false);
  405. }
  406. }
  407. updateRationItems(userID, rationLibId, sectionId, items,callback){
  408. let functions = [];
  409. for (let i=0; i < items.length; i++) {
  410. functions.push((function(doc) {
  411. return function(cb) {
  412. var filter = {};
  413. if (doc.ID) {
  414. filter.ID = doc.ID;
  415. } else {
  416. filter.sectionId = sectionId;
  417. filter.userId = userID;
  418. if (rationLibId) filter.rationRepId = rationLibId;
  419. filter.code = doc.code;
  420. }
  421. compleRationModel.update(filter, doc, cb);
  422. };
  423. })(items[i]));
  424. }
  425. async.parallel(functions, function(err, results) {
  426. if(!err){
  427. err = 0;
  428. }
  429. callback(err, results);
  430. });
  431. }
  432. async getInstallation(rationRepId, callback){
  433. try {
  434. let feeItems = await installFeeItemModel.find({rationRepId: rationRepId, $or: [{deleted: false}, {deleted: null}]});
  435. for(let feeItem of feeItems){
  436. let sids = [];
  437. for(let sec of feeItem.section){
  438. sids.push(sec.ID);
  439. }
  440. if(sids.length > 0){
  441. let sections = await installSectionModel.find({ID: {$in: sids}, $or: [{deleted: false}, {deleted: null}]});
  442. feeItem._doc.section = sections;
  443. }
  444. }
  445. callback(0, feeItems);
  446. }
  447. catch(err){
  448. callback(err, null);
  449. }
  450. }
  451. }
  452. function isDef(v){
  453. return v !== undefined && v !== null;
  454. }
  455. export default CompleRatoinDao;