bills.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * Created by jimiz on 2017/4/1.
  3. */
  4. let mongoose = require("mongoose");
  5. let async = require("async");
  6. let counter = require("../../../public/counter/counter.js");
  7. let consts = require('./project_consts');
  8. let projectConsts = consts.projectConst;
  9. let commonConsts = consts.commonConst;
  10. let quantity_detial = require('../facade/quantity_detail_facade');
  11. let projectModel = mongoose.model('projects');
  12. let rationModel = mongoose.model('ration');
  13. let rationGljModel = mongoose.model('ration_glj');
  14. let rationCoeModel = mongoose.model('ration_coe');
  15. let rationInstModel = mongoose.model('ration_installation');
  16. let quantityDelModel = mongoose.model('quantity_detail');
  17. const { fixedFlag } = require('../../../public/common_constants');
  18. let bills = mongoose.model("bills");
  19. let baseModel = require('./base_model');
  20. const uuidV1 = require('uuid/v1');
  21. const billType ={
  22. DXFY:1,//大项费用
  23. FB:2,//分部
  24. FX:3,//分项
  25. BILL:4,//清单
  26. BX:5//补项
  27. };
  28. class billsModel extends baseModel {
  29. constructor () {
  30. super(bills);
  31. };
  32. getQuery(projectID){
  33. return {'$or': [{projectID: projectID, deleteInfo: null}, {projectID: projectID, 'deleteInfo.deleted': {$in: [null, false]}}]}
  34. }
  35. getData (projectID, callback, isReport = false) {
  36. this.model.find(this.getQuery(projectID), '-_id', function(err, datas){
  37. if (!err) {
  38. if (isReport){ // 调价中间件机制
  39. for (let i = 0; i < datas.length; i++) {
  40. let fees = datas[i]._doc.fees;
  41. if (fees){
  42. for (let i = 0; i < fees.length; i++) {
  43. let doc = fees[i]._doc;
  44. if (doc){
  45. if (doc.tenderTotalFee) doc.totalFee = doc.tenderTotalFee;
  46. if (doc.tenderUnitFee) doc.unitFee = doc.tenderUnitFee;
  47. }
  48. }
  49. }
  50. }
  51. };
  52. callback(0, projectConsts.BILLS, datas);
  53. } else {
  54. callback(1, projectConsts.BILLS, null);
  55. };
  56. });
  57. };
  58. async getDataSync(projectID){
  59. return await this.model.find(this.getQuery(projectID),'-_id');
  60. }
  61. save (user_id, datas, callback) {
  62. let functions = [];
  63. let data;
  64. function saveOne(doc) {
  65. return function (cb) {
  66. switch (doc.updateType) {
  67. case commonConsts.UT_UPDATE:
  68. async.parallel([ // CSL,2018.01.10 如果是总造价清单,要将4个汇总金额写到projects表中
  69. function (asyncCB) {
  70. bills.update({projectID: doc.updateData.projectID, ID: doc.updateData.ID,deleteInfo: null}, doc.updateData, asyncCB);
  71. },
  72. function (asyncCB) {
  73. if (doc.updateData.summaryFees){
  74. // console.log('%%%%%%%%%%%%%%%%%%% ' + doc.updateData.projectID + ' | ' + JSON.stringify(doc.updateData.summaryFees));
  75. projectModel.update({ID: doc.updateData.projectID}, {"summaryFees": doc.updateData.summaryFees}, asyncCB);
  76. }else {
  77. asyncCB(null, {});
  78. };
  79. }
  80. ], function(err,result){
  81. cb(err, {});
  82. });
  83. break;
  84. case commonConsts.UT_CREATE:
  85. bills.create(doc.updateData, cb);
  86. break;
  87. case commonConsts.UT_DELETE:
  88. doc.updateData.deleteInfo = {deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
  89. //bills.update({projectID: doc.updateData.projectID, ID: doc.updateData.ID}, doc.updateData, cb);
  90. bills.deleteOne({projectID: doc.updateData.projectID, ID: doc.updateData.ID},cb);
  91. break;
  92. }
  93. }
  94. }
  95. for (let i = 0; i < datas.length; i++){
  96. data = datas[i];
  97. functions.push(saveOne(data));
  98. quantity_detial.quantityEditChecking(data,'bills',functions);
  99. }
  100. async.parallel(functions, function(err,result){
  101. let returnData = {
  102. moduleName:'bills',
  103. data:result
  104. };
  105. callback(err, returnData);
  106. });
  107. };
  108. getItemTemplate (callback) {
  109. let data = new bills;
  110. /* to do: 需要根据标准配置库填充fees和flags字段,是否需要更多的参数? */
  111. callback(0, '', data);
  112. };
  113. allocIDs (IDStep, callback) {
  114. counter.counterDAO.getIDAfterCount(counter.moduleName.bills, IDStep, function(err, highID){
  115. let lowID = highID - IDStep + 1;
  116. callback(0, '', {lowID: lowID, highID: highID});
  117. });
  118. };
  119. //zhong 2017-9-1
  120. updateCharacterContent(findSet, updateObj, txtObj, callback) {
  121. let updateSet = {};
  122. updateSet[updateObj.field] = updateObj.updateArr;
  123. if(txtObj && typeof txtObj !== 'undefined'){
  124. updateSet[txtObj.field] = txtObj.text;
  125. }
  126. bills.update(findSet, updateSet, function (err) {
  127. if(err){
  128. callback(1, '更新失败');
  129. }
  130. else{
  131. callback(0, '更新成功');
  132. }
  133. });
  134. };
  135. async updateBills(updateDatas){
  136. let bulk = [];
  137. for(let updateData of updateDatas){
  138. bulk.push({updateOne: {filter: updateData.findSet, update: {$set: updateData.updateData}}});
  139. }
  140. await bills.bulkWrite(bulk);
  141. };
  142. async updateBill(findSet, updateData) {
  143. let update = {};
  144. if (!updateData instanceof Array) {
  145. return false;
  146. }
  147. for (const tmp of updateData) {
  148. if (tmp === undefined) {
  149. continue;
  150. }
  151. update[tmp.field] = tmp.value;
  152. }
  153. if (Object.keys(update).length <= 0) {
  154. return false;
  155. }
  156. return bills.update(findSet, update);
  157. };
  158. async importBills(bills){
  159. /* let operations = [];
  160. for(let bill of bills){
  161. operations.push({insertOne: {document: bill}});
  162. } */
  163. if (bills.length) {
  164. await this.model.insertMany(bills)
  165. }
  166. }
  167. //删除清单节点的所有子节点及其他附带数据
  168. async deepDeleteBill(bill_ids, ration_ids){
  169. let me = this;
  170. //deep delete datas
  171. if(bill_ids.length > 0){
  172. //删除bills
  173. await me.model.deleteMany({ID: {$in: bill_ids}});
  174. //删除bill-quantity_detail
  175. await quantityDelModel.deleteMany({billID: {$in: bill_ids}});
  176. }
  177. if(ration_ids.length > 0){
  178. //删除rations
  179. await rationModel.deleteMany({ID: {$in: ration_ids}});
  180. //删除ration-glj
  181. await rationGljModel.deleteMany({rationID: {$in: ration_ids}});
  182. //删除ration-coe
  183. await rationCoeModel.deleteMany({rationID: {$in: ration_ids}});
  184. //删除ration-installation
  185. await rationInstModel.deleteMany({rationID: {$in: ration_ids}});
  186. //删除ration-quantity_detail
  187. await quantityDelModel.deleteMany({rationID: {$in: ration_ids}});
  188. }
  189. return {bill: bill_ids, ration: ration_ids};
  190. }
  191. }
  192. module.exports = new billsModel();