dict.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class DictService extends Service {
  4. async updateDict(data) {
  5. let {ctx} = this;
  6. let ret = await ctx.model.Dict.findByIdAndUpdate(data._id,
  7. {$set: {dictName:data.dictName, dictCode:data.dictCode, description:data.description}},
  8. {new:true, lean:true}).exec();
  9. return ret;
  10. }
  11. async editItem(id, data){
  12. console.log("update item ", id, data);
  13. let {app} = this;
  14. const mongoose = app.mongoose;
  15. let itemId = data._id;
  16. // delete data._id;
  17. //编辑
  18. let ret = await this.ctx.model.Dict.findByIdAndUpdate( id, {"$set":{"dictItems.$[i]": data}}, {
  19. arrayFilters:[
  20. {'i._id': mongoose.Types.ObjectId(itemId)},
  21. ],
  22. lean:true
  23. });
  24. return true;
  25. }
  26. async addDict( data ) {
  27. let {ctx} = this;
  28. let ret = await ctx.model.Dict.findOne({dictCode: data.dictCode});
  29. if( ret ) {
  30. throw "字典编码已经存在";
  31. }
  32. let dict = new ctx.model.Dict();
  33. dict.dictCode = data.dictCode;
  34. dict.dictName = data.dictName;
  35. dict.type = 0;
  36. dict.dictItems = [];
  37. dict.description = data.description;
  38. ret = await dict.save();
  39. return ret;
  40. }
  41. async addItem(id, data ) {
  42. console.log("add item ", id, data);
  43. let ret = await this.ctx.model.Dict.findByIdAndUpdate(id, {
  44. $push: {
  45. dictItems: data
  46. }
  47. }, {lean:true});
  48. return ret;
  49. }
  50. async check( data ) {
  51. let ret = await this.ctx.model.Dict.findOne({dictCode: data.fieldVal});
  52. console.log("checking",data, ret);
  53. return ret == null;
  54. }
  55. async deleteDict( id ) {
  56. let ret = await this.ctx.model.Dict.findByIdAndDelete( id );
  57. return ret;
  58. }
  59. async deleteDictItem(id, itemValue) {
  60. console.log("delete item", id, itemValue);
  61. let ret = await this.ctx.model.Dict.findByIdAndUpdate( id, {"$pull":{"dictItems":{itemValue:itemValue}}}, {lean:true});
  62. return ret;
  63. }
  64. createItemsAggregate(ctx, dictId, itemText, status) {
  65. const mongoose = ctx.app.mongoose;
  66. let aggregate = ctx.model.Dict.aggregate([
  67. {$match: {_id: mongoose.Types.ObjectId(dictId)}},
  68. ])
  69. .project({_id: 0, dictItems:1})
  70. .unwind("dictItems")
  71. .project({_id:"$dictItems._id", itemValue:"$dictItems.itemValue",itemText:"$dictItems.itemText",letter:"$dictItems.letter",sortOrder: "$dictItems.sortOrder",status:"$dictItems.status",description:"$dictItems.description"});
  72. if( itemText != undefined || status !=undefined) {
  73. let query = {};
  74. if( itemText ) query.itemText = {"$regex": itemText};
  75. if( status != undefined ) query.status = parseInt(status);
  76. aggregate.match( query );
  77. }
  78. return aggregate;
  79. }
  80. async listItems(page, size, dictId, itemText, status)
  81. {
  82. console.log("listitem", page, size, dictId, itemText, status);
  83. let {ctx, app} = this;
  84. let aggregate = this.createItemsAggregate(ctx, dictId, itemText, status);
  85. let total = await aggregate.count('total').exec();
  86. let dict = [];
  87. if( total.length > 0 ) {
  88. total = total[0].total;
  89. aggregate = this.createItemsAggregate(ctx, dictId, itemText, status);
  90. dict = await aggregate.sort("sortOrder").skip((page-1)*size ).limit(size)
  91. .exec();
  92. } else {
  93. total = 0;
  94. }
  95. let ret = {
  96. records: dict,
  97. "total": total,
  98. "size": size,
  99. "current":page,
  100. "orders":[],
  101. "searchCount":true,
  102. "pages": Math.round(total * 1.0 / size)
  103. };
  104. return ret;
  105. }
  106. async listPage(page, size, dictName, dictCode)
  107. {
  108. let {ctx} = this;
  109. let query = {};
  110. if( dictName ) query.dictName = {"$regex": dictName};
  111. if( dictCode ) query.dictCode = {"$regex": dictCode};
  112. let total = await ctx.model.Dict.find(query).count()
  113. console.log("total", total, query);
  114. let list = await ctx.model.Dict.find(query, {dictCode:1, description:1,dictName:1, type: 1}).skip( (page -1) * size ).limit( size );
  115. let ret = {
  116. records: list,
  117. "total": total,
  118. "size": size,
  119. "current":page,
  120. "orders":[],
  121. "searchCount":true,
  122. "pages": Math.round(total * 1.0 / size)
  123. };
  124. return ret;
  125. }
  126. async dictAll( dict ) {
  127. // let total = await ctx.model.Dict.find(query).count()
  128. let {ctx} = this;
  129. let list = await ctx.model.Dict.find({dictCode: dict}, {dictItems:-1});
  130. if( list.length < 1 ) return [];
  131. let dictId = list[0]._id;
  132. let aggregate = this.createItemsAggregate(ctx, dictId, undefined, 1);
  133. dict = await aggregate.sort("sortOrder").exec();
  134. return dict;
  135. }
  136. }
  137. module.exports = DictService;