gljModel.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /**
  2. * Created by Zhong on 2017/8/11.
  3. */
  4. const mongoose = require('mongoose');
  5. const gljMapModel = mongoose.model('std_glj_lib_map');
  6. const gljModel = mongoose.model('std_glj_lib_gljList');
  7. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  8. const compilationModel = mongoose.model('compilation');
  9. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  10. const rationModel = mongoose.model('std_ration_lib_ration_items');
  11. import {OprDao} from "./gljMapModel";
  12. import moment from "moment";
  13. import counter from "../../../public/counter/counter";
  14. import async from "async";
  15. class GljDao extends OprDao{
  16. getGljTypes (gljLibId, callback){
  17. gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false}, {deleted: false} ]},
  18. '-_id', {lean: true}, function(err,data){
  19. if(err) callback("获取工料机类型错误!",false)
  20. else {
  21. callback(0, data);
  22. }
  23. });
  24. }
  25. _exist(data, attr){
  26. return data && data[attr] !== 'undefined' && data[attr];
  27. }
  28. sortToNumber(datas){
  29. for(let i = 0, len = datas.length; i < len; i++){
  30. let data = datas[i]._doc;
  31. if(this._exist(data, 'basePrice')){
  32. data['basePrice'] = parseFloat(data['basePrice']);
  33. }
  34. if(this._exist(data, 'component')){
  35. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  36. let comGljObj = data['component'][j]._doc;
  37. if(this._exist(comGljObj, 'consumeAmt')){
  38. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  39. }
  40. }
  41. }
  42. }
  43. }
  44. async getGljItemsByRep(repositoryId,callback = null){
  45. try {
  46. let rst = await gljModel.find({repositoryId: repositoryId}).lean();
  47. callback(0, rst);
  48. } catch (err) {
  49. callback(1, null);
  50. }
  51. /*//批量获取异步
  52. let functions = [];
  53. let count = await gljModel.find({repositoryId: repositoryId, $or: [{deleted: null}, {deleted: false}]}).count();
  54. let findCount = Math.ceil(count/500);
  55. for(let i = 0, len = findCount; i < len; i++){
  56. functions.push((function(flag) {
  57. return function (cb) {
  58. gljModel.find({repositoryId: repositoryId, deleted: null}, cb).skip(flag).sort({ID: 1}).limit(500);
  59. }
  60. })(i*500));
  61. }
  62. async.parallel(functions, function (err, results) {
  63. if(err){
  64. callback(err, null);
  65. }
  66. else{
  67. for(let stdGljs of results){
  68. rst = rst.concat(stdGljs);
  69. }
  70. me.sortToNumber(rst);
  71. callback(0, rst);
  72. }
  73. });*/
  74. }
  75. getGljItemByType (repositoryId, type, callback){
  76. let me = this;
  77. gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  78. if(err) callback(true, "");
  79. else {
  80. me.sortToNumber(data);
  81. callback(false, data);
  82. }
  83. })
  84. };
  85. getGljItem (repositoryId, code, callback){
  86. let me = this;
  87. gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  88. if(err) callback(true, "")
  89. else {
  90. me.sortToNumber(data);
  91. callback(false, data);
  92. }
  93. })
  94. };
  95. getGljItems (gljIds, callback){
  96. let me = this;
  97. gljModel.find({"ID": {"$in": gljIds}},function(err,data){
  98. if(err) callback(true, "")
  99. else {
  100. me.sortToNumber(data);
  101. callback(false, data);
  102. }
  103. })
  104. };
  105. getGljItemsByCode (repositoryId, codes, callback){
  106. let me = this;
  107. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  108. if(err) callback(true, "");
  109. else {
  110. me.sortToNumber(data);
  111. callback(false, data);
  112. }
  113. })
  114. };
  115. updateComponent(libId, oprtor, updateArr, callback){
  116. let parallelFucs = [];
  117. for(let i = 0; i < updateArr.length; i++){
  118. parallelFucs.push((function(obj){
  119. return function (cb) {
  120. if(typeof obj.component === 'undefined'){
  121. obj.component = [];
  122. }
  123. gljModel.update({repositoryId: libId, ID: obj.ID}, obj, function (err, result) {
  124. if(err){
  125. cb(err);
  126. }
  127. else{
  128. cb(null, obj);
  129. }
  130. })
  131. }
  132. })(updateArr[i]));
  133. }
  134. parallelFucs.push((function () {
  135. return function (cb) {
  136. GljDao.updateOprArr({ID: libId}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  137. if(err){
  138. cb(err);
  139. }
  140. else{
  141. cb(null);
  142. }
  143. })
  144. }
  145. })());
  146. async.parallel(parallelFucs, function (err, result) {
  147. if(err){
  148. callback(err, '更新组成物错误!', null);
  149. }
  150. else{
  151. callback(null, '成功!', result);
  152. }
  153. });
  154. }
  155. mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  156. if (updateItems.length == 0 && rIds.length == 0) {
  157. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  158. }
  159. else if(rIds.length > 0 && updateItems.length > 0){
  160. async.parallel([
  161. function (cb) {
  162. GljDao.removeGljItems(repId, lastOpr, rIds, cb);
  163. },
  164. function (cb) {
  165. GljDao.updateGljItems(repId, lastOpr, updateItems, cb);
  166. }
  167. ], function (err) {
  168. if(err){
  169. callback(true, "Fail to update and delete", false)
  170. }
  171. else{
  172. callback(false, "Save successfully", false);
  173. }
  174. })
  175. }
  176. else if (rIds.length > 0 && updateItems.length === 0) {
  177. GljDao.removeGljItems(repId, lastOpr, rIds, callback);
  178. }
  179. else if(updateItems.length > 0 || addItems.length > 0){
  180. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  181. if (err) {
  182. callback(true, "Fail to update", false);
  183. } else {
  184. if (addItems && addItems.length > 0) {
  185. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  186. } else {
  187. callback(false, "Save successfully", results);
  188. }
  189. }
  190. });
  191. }
  192. }
  193. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  194. if (updateItems.length == 0 && rIds.length == 0) {
  195. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  196. } else if (rIds.length > 0) {
  197. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  198. });
  199. }else{
  200. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  201. if (err) {
  202. callback(true, "Fail to update", false);
  203. } else {
  204. if (addItems && addItems.length > 0) {
  205. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  206. } else {
  207. callback(false, "Save successfully", results);
  208. }
  209. }
  210. });
  211. }
  212. };*/
  213. static removeGljItems (repId, lastOpr, rIds, callback) {
  214. if (rIds && rIds.length > 0) {
  215. gljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  216. if (err) {
  217. callback(true, "Fail to remove", false);
  218. } else {
  219. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  220. if(err){
  221. callback(true, "Fail to update operator", false);
  222. }
  223. else{
  224. callback(false, "Remove successfully", docs);
  225. }
  226. });
  227. }
  228. })
  229. } else {
  230. callback(false, "No records were deleted!", null);
  231. }
  232. }
  233. static addGljItems (repId, lastOpr, items, callback) {
  234. if (items && items.length > 0) {
  235. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  236. var maxId = result.sequence_value;
  237. var arr = [];
  238. for (var i = 0; i < items.length; i++) {
  239. var obj = new gljModel(items[i]);
  240. obj.ID = (maxId - (items.length - 1) + i);
  241. obj.repositoryId = repId;
  242. arr.push(obj);
  243. }
  244. gljModel.collection.insert(arr, null, function(err, docs){
  245. if (err) {
  246. callback(true, "Fail to add", false);
  247. } else {
  248. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  249. if(err){
  250. callback(true, "Fail to update Operator", false);
  251. }
  252. else{
  253. callback(false, "Add successfully", docs);
  254. }
  255. });
  256. }
  257. })
  258. });
  259. } else {
  260. callback(true, "No source", false);
  261. }
  262. }
  263. static updateGljItems(repId, lastOpr, items, callback) {
  264. var functions = [];
  265. for (var i=0; i < items.length; i++) {
  266. functions.push((function(doc) {
  267. return function(cb) {
  268. var filter = {};
  269. if (doc.ID) {
  270. filter.ID = doc.ID;
  271. } else {
  272. filter.repositoryId = repId;
  273. filter.code = doc.code;
  274. }
  275. gljModel.update(filter, doc, cb);
  276. };
  277. })(items[i]));
  278. }
  279. functions.push((function () {
  280. return function (cb) {
  281. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  282. if(err){
  283. cb(err);
  284. }
  285. else{
  286. cb(null);
  287. }
  288. })
  289. }
  290. })());
  291. async.parallel(functions, function(err, results) {
  292. callback(err, results);
  293. });
  294. }
  295. getRationGljIds(rationLibs, callback){
  296. }
  297. updateNodes (updateData, lastOpr, callback) {
  298. let functions = [];
  299. for (let i = 0, len = updateData.length; i < len; i++) {
  300. functions.push((function(doc) {
  301. return function(cb) {
  302. if(doc.updateType === 'update' && !doc.updateData.deleted){
  303. gljClassModel.update({repositoryId: doc.updateData.repositoryId, ID: doc.updateData.ID}, doc.updateData, function (err) {
  304. if(err){
  305. cb(err);
  306. }
  307. else {
  308. cb(null);
  309. }
  310. });
  311. }
  312. else if(doc.updateType === 'update' && doc.updateData.deleted){
  313. gljClassModel.remove({repositoryId: doc.updateData.repositoryId, ID: doc.updateData.ID}, function (err) {
  314. if(err){
  315. cb(err);
  316. }
  317. else {
  318. gljModel.remove({repositoryId: doc.updateData.repositoryId, gljClass: doc.updateData.ID}, function (err) {
  319. if(err){
  320. cb(err);
  321. }
  322. else{
  323. cb(null);
  324. }
  325. });
  326. }
  327. });
  328. }
  329. else if(doc.updateType === 'new'){
  330. gljClassModel.create(doc.updateData, function (err) {
  331. if(err){
  332. cb(err);
  333. }
  334. else {
  335. cb(null);
  336. }
  337. });
  338. }
  339. };
  340. })(updateData[i]));
  341. }
  342. if(updateData.length > 0){
  343. functions.push((function () {
  344. return function (cb) {
  345. GljDao.updateOprArr({ID: updateData[0].updateData.rationRepId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  346. if(err){
  347. cb(err);
  348. }
  349. else{
  350. cb(null);
  351. }
  352. })
  353. }
  354. })());
  355. }
  356. async.parallel(functions, function(err, results) {
  357. if(!err){
  358. err = 0;
  359. }
  360. callback(err, results);
  361. });
  362. }
  363. /* updateNodes (repId, lastOpr, nodes, callback) {
  364. var functions = [];
  365. for (var i=0; i < nodes.length; i++) {
  366. functions.push((function(doc) {
  367. return function(cb) {
  368. gljClassModel.update({ID: doc.ID}, doc, cb);
  369. };
  370. })(nodes[i]));
  371. }
  372. functions.push((function () {
  373. return function (cb) {
  374. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  375. if(err){
  376. cb(err);
  377. }
  378. else{
  379. cb(null);
  380. }
  381. })
  382. }
  383. })());
  384. async.parallel(functions, function(err, results) {
  385. callback(err, results);
  386. });
  387. }*/
  388. removeNodes (repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  389. var functions = [];
  390. if (preNodeId != -1) {
  391. functions.push((function(nodeId, nextId) {
  392. return function(cb) {
  393. gljClassModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  394. };
  395. })(preNodeId, preNodeNextId));
  396. }
  397. for (var i=0; i < nodeIds.length; i++) {
  398. functions.push((function(nodeId) {
  399. return function(cb) {
  400. gljClassModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  401. };
  402. })(nodeIds[i]));
  403. }
  404. functions.push((function () {
  405. return function (cb) {
  406. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  407. if(err){
  408. cb(err);
  409. }
  410. else{
  411. cb(null);
  412. }
  413. })
  414. }
  415. })());
  416. async.parallel(functions, function(err, results) {
  417. callback(err, results);
  418. });
  419. }
  420. createNewNode(repId, lastOpr, lastNodeId, nodeData, callback) {
  421. return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){
  422. nodeData.repositoryId = repId;
  423. nodeData.ID = result.sequence_value;
  424. var node = new gljModel(nodeData);
  425. async.parallel([
  426. function (cb) {
  427. node.save(function (err, result) {
  428. if (err) {
  429. cb("章节树ID错误!", false);
  430. } else {
  431. if (lastNodeId > 0) {
  432. gljClassModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  433. if (err) {
  434. cb("章节树ID错误!", false);
  435. } else {
  436. cb(false, result);
  437. }
  438. });
  439. } else cb(false, result);
  440. }
  441. });
  442. },
  443. function (cb) {
  444. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  445. if(err){
  446. cb(err);
  447. }
  448. else{
  449. cb(null);
  450. }
  451. })
  452. }
  453. ], function (err, result) {
  454. if(err){
  455. callback(true, "章节树错误!", false);
  456. }
  457. else{
  458. callback(false, '', result[0]);
  459. }
  460. })
  461. });
  462. }
  463. getGljItemsOccupied(repId, occupation, callback){
  464. gljModel.find({repositoryId: repId}, occupation, function (err, result) {
  465. if(err) callback(true, 'fail', null);
  466. else callback(false, 'sc', result);
  467. });
  468. }
  469. async getGljItemsByRepId(repositoryId, returnFields = ''){
  470. return gljModel.find({"repositoryId": repositoryId}, returnFields);
  471. }
  472. async batchUpdateGljPrice(gljLibId, sheetData){
  473. let gljLib = await gljMapModel.findOne({ID: gljLibId});
  474. if(!gljLib){
  475. throw '不存在此人材机库';
  476. }
  477. let compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(gljLib.compilationId)});
  478. if(!compilation){
  479. throw '不存在此费用定额';
  480. }
  481. let priceProperties = compilation.priceProperties ? compilation.priceProperties : [];
  482. //根据第一行数据,获取列下标与字段名映射
  483. let colMapping = {};
  484. for(let col = 0; col < sheetData[0].length; col++){
  485. let cData = sheetData[0][col];
  486. if(cData === '编码'){
  487. colMapping['code'] = col;
  488. }
  489. else {
  490. if(priceProperties.length === 0){
  491. if(cData === '定额价'){
  492. colMapping['basePrice'] = col;
  493. break;
  494. }
  495. }
  496. else {
  497. for(let priceProp of priceProperties){
  498. if(priceProp.price.dataName === cData){
  499. colMapping[priceProp.price.dataCode] = col;
  500. break;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. let colMappingKeys = Object.keys(colMapping);
  507. if(colMappingKeys.length < 2){
  508. throw 'excel数据不正确'
  509. }
  510. let updateBulk = [];
  511. //避免重复
  512. let updateCodes = [];
  513. //库中存在的人材机
  514. let dateA = Date.now();
  515. let existGljs = await gljModel.find({repositoryId: gljLibId}, '-_id code ID');
  516. let existMapping = {};
  517. for (let glj of existGljs) {
  518. existMapping[glj.code] = glj;
  519. }
  520. for(let row = 0; row < sheetData.length; row++){
  521. if(row === 0){
  522. continue;
  523. }
  524. let gljCode = sheetData[row][colMapping.code],
  525. existGlj = existMapping[gljCode];
  526. //更新多单价、不覆盖priceProperty字段,覆盖priceProperty下的子字段'priceProperty.x'
  527. if(gljCode && gljCode !== '' && !updateCodes.includes(gljCode) && existGlj){
  528. if(priceProperties.length > 0){
  529. for(let priceProp of priceProperties){
  530. let dataCode = priceProp.price.dataCode;
  531. let priceCellData = sheetData[row][colMapping[dataCode]];
  532. //Excel中没有这个单价则跳过
  533. if (!colMapping[dataCode]) {
  534. continue;
  535. }
  536. let updateSet = {};
  537. updateSet['priceProperty.' + dataCode] = priceCellData && !isNaN(priceCellData) ?
  538. scMathUtil.roundTo(parseFloat(priceCellData), -2) : 0;
  539. updateBulk.push({
  540. updateOne: {filter: {ID: existGlj.ID}, update: {$set: updateSet}}
  541. });
  542. }
  543. updateCodes.push(gljCode);
  544. }
  545. else {
  546. if(colMapping.basePrice){
  547. let priceCellData = sheetData[row][colMapping.basePrice];
  548. let basePrice = priceCellData && !isNaN(priceCellData) ?
  549. scMathUtil.roundTo(priceCellData, -2) : 0;
  550. updateCodes.push(gljCode);
  551. updateBulk.push({
  552. updateOne: {filter: {ID: existGlj.ID}, update: {$set: {basePrice: basePrice}}}
  553. });
  554. }
  555. }
  556. }
  557. }
  558. if(updateBulk.length > 0){
  559. while (updateBulk.length > 0) {
  560. let sliceBulk = updateBulk.splice(0, 1000);
  561. await gljModel.bulkWrite(sliceBulk);
  562. }
  563. }
  564. }
  565. }
  566. export default GljDao;