base_model.js 3.3 KB

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