base_model.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * 数据模型基类
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/22
  6. * @version
  7. */
  8. const MongooseHelper = require('../helper/mongoose_helper');
  9. const mongoose = require('mongoose');
  10. let counterModel = mongoose.model('counter');
  11. class BaseModel {
  12. /**
  13. * mongoose数据模型
  14. *
  15. * @var {object}
  16. */
  17. model = null;
  18. /**
  19. * 构造函数
  20. *
  21. * @return {void}
  22. */
  23. constructor() {
  24. /* if (new.target === BaseModel) {
  25. throw new Error('BaseModel不能实例化,只能继承使用。');
  26. }*/
  27. }
  28. /**
  29. * 初始化函数
  30. *
  31. * @return {void}
  32. */
  33. init() {
  34. if (this.model === null) {
  35. throw new Error('子类数据有误');
  36. }
  37. this.db = new MongooseHelper();
  38. this.db.model = this.model;
  39. }
  40. /**
  41. * 根据id查找对应数据
  42. *
  43. * @param {Object} condition
  44. * @param {Object} fields
  45. * @param {boolean} singleData
  46. * @param {String} indexBy
  47. * @return {Promise}
  48. */
  49. async findDataByCondition(condition, fields = null, singleData = true, indexBy = null) {
  50. let result = [];
  51. if (Object.keys(condition).length <= 0) {
  52. return result;
  53. }
  54. result = singleData ? await this.db.findOne(condition, fields) : await this.db.find(condition, fields);
  55. if (indexBy !== null && !singleData && result.length > 0) {
  56. let tmpResult = {};
  57. for(let tmp of result) {
  58. let key="";
  59. if (indexBy instanceof Array){
  60. key = this.getIndex(tmp,indexBy);
  61. }else {
  62. key =tmp[indexBy]?tmp[indexBy]:"";
  63. }
  64. tmpResult[key] = tmp;
  65. }
  66. result = tmpResult;
  67. }
  68. return result;
  69. }
  70. /**
  71. * 根据条件返回数据数量
  72. *
  73. * @param {object} condition
  74. * @return {Promise}
  75. */
  76. async count(condition = null) {
  77. let total = 0;
  78. try {
  79. total = await this.db.count(condition);
  80. } catch (error) {
  81. total = 0;
  82. }
  83. return total;
  84. }
  85. /**
  86. * 根据id删除
  87. *
  88. * @param {Number} id
  89. * @return {Promise}
  90. */
  91. async deleteById(id) {
  92. let result = false;
  93. id = parseInt(id);
  94. if (isNaN(id) || id <= 0) {
  95. return false;
  96. }
  97. try {
  98. let deleteResult = await this.db.delete({id: id});
  99. result = deleteResult.result.ok === 1;
  100. } catch (error) {
  101. console.log(error);
  102. result = false;
  103. }
  104. return result;
  105. }
  106. async setIDfromCounter(name,list,map,keyfield){
  107. let update = {$inc: {sequence_value: list.length}};
  108. let condition = {_id: name};
  109. let options = {new: true};
  110. // 先查找更新
  111. let counter = await counterModel.findOneAndUpdate(condition, update, options);
  112. let firstID = counter.sequence_value - (list.length - 1);
  113. for(let a of list){
  114. console.log(firstID)
  115. a.id = firstID;
  116. firstID+=1
  117. if(map && keyfield){
  118. let key = a[keyfield];
  119. map[key]?map[key].push(a):map[key]=[a]
  120. }
  121. }
  122. }
  123. /**
  124. * 更新数据
  125. *
  126. * @param {Number} id
  127. * @param {Object} updateData
  128. * @return {Promise}
  129. */
  130. async updateById(id, updateData) {
  131. id = parseInt(id);
  132. if (isNaN(id) || id <= 0 || Object.keys(updateData).length <= 0) {
  133. return false;
  134. }
  135. let result = await this.db.update({id: id}, updateData);
  136. return result.ok !== undefined && result.ok === 1;
  137. }
  138. getIndex(obj,tpops){
  139. let pops = tpops?tpops:['code','name','specs','unit','type'];
  140. let t_index = '';
  141. let k_arr=[];
  142. for(let p of pops){
  143. let tmpK = (obj[p]==undefined||obj[p]==null||obj[p]=='')?'null':obj[p];
  144. k_arr.push(tmpK);
  145. }
  146. t_index=k_arr.join("|-|");
  147. return t_index;
  148. }
  149. }
  150. module.exports = BaseModel;