mix_ratio_model.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * 配合比业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/12
  6. * @version
  7. */
  8. import mongoose from "mongoose";
  9. import BaseModel from "../../common/base/base_model";
  10. import CounterModel from "./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. for(let tmp in data) {
  50. data[tmp].id = await counterModel.getId(collectionName);
  51. }
  52. } else {
  53. data.id = await counterModel.getId(collectionName);
  54. }
  55. return this.db.model.create(data);
  56. }
  57. /**
  58. * 更新数据
  59. *
  60. * @param {Number} id
  61. * @param {Object} updateData
  62. * @return {Promise}
  63. */
  64. async updateById(id, updateData) {
  65. id = parseInt(id);
  66. if (isNaN(id) || id <= 0 || Object.keys(updateData).length <= 0) {
  67. return false;
  68. }
  69. let result = await this.db.update({id: id}, updateData);
  70. return result.ok !== undefined && result.ok === 1;
  71. }
  72. //复制组成物到切换后的组成物映射表
  73. async copyNotExist(currentUnitPriceId, changeUnitPriceId,gljMap){
  74. let currentMap = {},targetMap = {}, insertData = [];
  75. //取原单价文件所有的的组成物
  76. let currentList = await this.db.find({'unit_price_file_id':currentUnitPriceId});
  77. // 过滤mongoose格式
  78. currentList = JSON.stringify(currentList);
  79. currentList = JSON.parse(currentList);
  80. this.getConnectionMap(currentMap,currentList);
  81. //切换后的单价文件所有的的组成物
  82. let targetList = await this.db.find({'unit_price_file_id':changeUnitPriceId});
  83. this.getConnectionMap(targetMap,targetList);
  84. for(let ckey in currentMap){
  85. if(targetMap[ckey]){//如果切换后的单价文件已经存在,则不用复
  86. continue;
  87. }
  88. if(gljMap[ckey]){//在本项目中有用到
  89. for(let ratio of currentMap[ckey]){
  90. delete ratio._id; // 删除原有id信息
  91. delete ratio.id;
  92. ratio.unit_price_file_id = changeUnitPriceId;
  93. insertData.push(ratio);
  94. }
  95. }
  96. }
  97. return insertData.length > 0 ? this.add(insertData) : true;
  98. }
  99. getConnectionMap(map,list){
  100. for(let l of list){
  101. if(map[l.connect_key]){
  102. map[l.connect_key].push(l);
  103. }else {
  104. map[l.connect_key] = [l];
  105. }
  106. }
  107. }
  108. }
  109. export default MixRatioModel;