123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 'use strict';
- const Service = require('egg').Service;
- class DictService extends Service {
-
- async updateDict(data) {
- let {ctx} = this;
- let ret = await ctx.model.Dict.findByIdAndUpdate(data._id,
- {$set: {dictName:data.dictName, dictCode:data.dictCode, description:data.description}},
- {new:true, lean:true}).exec();
- return ret;
- }
- async editItem(id, data){
- console.log("update item ", id, data);
- let {app} = this;
- const mongoose = app.mongoose;
- let itemId = data._id;
- // delete data._id;
- //编辑
- let ret = await this.ctx.model.Dict.findByIdAndUpdate( id, {"$set":{"dictItems.$[i]": data}}, {
- arrayFilters:[
- {'i._id': mongoose.Types.ObjectId(itemId)},
- ],
- lean:true
- });
- return true;
- }
- async addDict( data ) {
- let {ctx} = this;
- let ret = await ctx.model.Dict.findOne({dictCode: data.dictCode});
- if( ret ) {
- throw "字典编码已经存在";
- }
- let dict = new ctx.model.Dict();
- dict.dictCode = data.dictCode;
- dict.dictName = data.dictName;
- dict.type = 0;
- dict.dictItems = [];
- dict.description = data.description;
- ret = await dict.save();
- return ret;
- }
- async addItem(id, data ) {
- console.log("add item ", id, data);
- let ret = await this.ctx.model.Dict.findByIdAndUpdate(id, {
- $push: {
- dictItems: data
- }
- }, {lean:true});
- return ret;
- }
- async check( data ) {
-
- let ret = await this.ctx.model.Dict.findOne({dictCode: data.fieldVal});
- console.log("checking",data, ret);
- return ret == null;
- }
- async deleteDict( id ) {
- let ret = await this.ctx.model.Dict.findByIdAndDelete( id );
- return ret;
- }
- async deleteDictItem(id, itemValue) {
- console.log("delete item", id, itemValue);
- let ret = await this.ctx.model.Dict.findByIdAndUpdate( id, {"$pull":{"dictItems":{itemValue:itemValue}}}, {lean:true});
-
- return ret;
- }
- createItemsAggregate(ctx, dictId, itemText, status) {
- const mongoose = ctx.app.mongoose;
- let aggregate = ctx.model.Dict.aggregate([
- {$match: {_id: mongoose.Types.ObjectId(dictId)}},
- ])
- .project({_id: 0, dictItems:1})
- .unwind("dictItems")
- .project({_id:"$dictItems._id", itemValue:"$dictItems.itemValue",itemText:"$dictItems.itemText",letter:"$dictItems.letter",sortOrder: "$dictItems.sortOrder",status:"$dictItems.status",description:"$dictItems.description"});
-
- if( itemText != undefined || status !=undefined) {
- let query = {};
- if( itemText ) query.itemText = {"$regex": itemText};
- if( status != undefined ) query.status = parseInt(status);
- aggregate.match( query );
- }
- return aggregate;
- }
- async listItems(page, size, dictId, itemText, status)
- {
- console.log("listitem", page, size, dictId, itemText, status);
- let {ctx, app} = this;
- let aggregate = this.createItemsAggregate(ctx, dictId, itemText, status);
- let total = await aggregate.count('total').exec();
- let dict = [];
- if( total.length > 0 ) {
- total = total[0].total;
- aggregate = this.createItemsAggregate(ctx, dictId, itemText, status);
- dict = await aggregate.sort("sortOrder").skip((page-1)*size ).limit(size)
- .exec();
- } else {
- total = 0;
- }
- let ret = {
- records: dict,
- "total": total,
- "size": size,
- "current":page,
- "orders":[],
- "searchCount":true,
- "pages": Math.round(total * 1.0 / size)
- };
- return ret;
- }
- async listPage(page, size, dictName, dictCode)
- {
- let {ctx} = this;
- let query = {};
- if( dictName ) query.dictName = {"$regex": dictName};
- if( dictCode ) query.dictCode = {"$regex": dictCode};
-
- let total = await ctx.model.Dict.find(query).count()
- console.log("total", total, query);
- let list = await ctx.model.Dict.find(query, {dictCode:1, description:1,dictName:1, type: 1}).skip( (page -1) * size ).limit( size );
- let ret = {
- records: list,
- "total": total,
- "size": size,
- "current":page,
- "orders":[],
- "searchCount":true,
- "pages": Math.round(total * 1.0 / size)
- };
- return ret;
- }
- async dictAll( dict ) {
- // let total = await ctx.model.Dict.find(query).count()
- let {ctx} = this;
- let list = await ctx.model.Dict.find({dictCode: dict}, {dictItems:-1});
-
- if( list.length < 1 ) return [];
-
- let dictId = list[0]._id;
- let aggregate = this.createItemsAggregate(ctx, dictId, undefined, 1);
- dict = await aggregate.sort("sortOrder").exec();
-
- return dict;
- }
- }
- module.exports = DictService;
|