gljModel.js 22 KB

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