mix_ratio_model.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * 配合比业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/12
  6. * @version
  7. */
  8. const mongoose = require("mongoose");
  9. const BaseModel = require("../../common/base/base_model");
  10. const CounterModel = require("./counter_model");
  11. let collectionName = 'mix_ratio';
  12. class MixRatioModel extends BaseModel {
  13. /**
  14. * 构造函数
  15. *
  16. * @return {void}
  17. */
  18. constructor() {
  19. let parent = super();
  20. parent.model = mongoose.model(collectionName);
  21. parent.init();
  22. }
  23. /**
  24. * 设置场景
  25. *
  26. * @param {string} scene
  27. * @return {void}
  28. */
  29. setScene(scene = '') {
  30. switch (scene) {
  31. // 新增数据的验证规则
  32. case 'add':
  33. //this.model.schema.path('glj_id').required(true);
  34. this.model.schema.path('consumption').required(true);
  35. this.model.schema.path('unit_price_file_id').required(true);
  36. this.model.schema.path('connect_key').required(true);
  37. break;
  38. }
  39. }
  40. /**
  41. * 新增配合比数据(自动判断是否存在)
  42. *
  43. * @param {object} data
  44. * @return {Promise}
  45. */
  46. async add(data) {
  47. let counterModel = new CounterModel();
  48. if (data instanceof Array) {
  49. await this.setIDfromCounter(collectionName,data);
  50. /* for(let tmp in data) {
  51. data[tmp].id = await counterModel.getId(collectionName);
  52. } */
  53. } else {
  54. data.id = await counterModel.getId(collectionName);
  55. }
  56. return await this.db.model.create(data);
  57. }
  58. /**
  59. * 更新数据
  60. *
  61. * @param {Number} id
  62. * @param {Object} updateData
  63. * @return {Promise}
  64. */
  65. async updateById(id, updateData) {
  66. id = parseInt(id);
  67. if (isNaN(id) || id <= 0 || Object.keys(updateData).length <= 0) {
  68. return false;
  69. }
  70. let result = await this.db.update({id: id}, updateData);
  71. return result.ok !== undefined && result.ok === 1;
  72. }
  73. //复制组成物到切换后的组成物映射表
  74. async copyNotExist(currentUnitPriceId, changeUnitPriceId,gljMap,newFile = false){
  75. let currentMap = {},targetMap = {}, insertData = [];
  76. //取原单价文件所有的的组成物
  77. let currentList = await this.model.find({'unit_price_file_id':currentUnitPriceId}).lean();;
  78. this.getConnectionMap(currentMap,currentList);
  79. //切换后的单价文件所有的的组成物
  80. let targetList = await this.model.find({'unit_price_file_id':changeUnitPriceId}).lean();
  81. this.getConnectionMap(targetMap,targetList);
  82. for(let ckey in currentMap){
  83. if(targetMap[ckey]){//如果切换后的单价文件已经存在,则不用复
  84. continue;
  85. }
  86. if(gljMap[ckey] || newFile == true){//在本项目中有用到
  87. for(let ratio of currentMap[ckey]){
  88. delete ratio._id; // 删除原有id信息
  89. delete ratio.id;
  90. ratio.unit_price_file_id = changeUnitPriceId;
  91. insertData.push(ratio);
  92. }
  93. }
  94. }
  95. return insertData.length > 0 ? await this.add(insertData) : true;
  96. }
  97. getConnectionMap(map,list){
  98. for(let l of list){
  99. if(map[l.connect_key]){
  100. map[l.connect_key].push(l);
  101. }else {
  102. map[l.connect_key] = [l];
  103. }
  104. }
  105. }
  106. }
  107. module.exports = MixRatioModel;