gljModel.js 22 KB

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