You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.5 KiB
67 lines
2.5 KiB
'use strict';
|
|
|
|
const Service = require('egg').Service;
|
|
const _ = require("lodash")
|
|
class UserService extends Service {
|
|
async index(params) {
|
|
let { ctx } = this
|
|
let modelLocal = ctx.model.User
|
|
let returnData = { hasError: false, msg: "数据错误", list: [] }
|
|
//新增
|
|
let subType = params.subType
|
|
let returnComponent = params.returnData || []
|
|
let paramsLocal = _.omit(params, ["subType", "returnData"])
|
|
if (subType === "add") {
|
|
paramsLocal["update_at"] = Date.now();
|
|
let res = await modelLocal.create(paramsLocal)
|
|
returnData.hasError = false
|
|
returnData.msg = "数据新增成功"
|
|
returnData.list = res
|
|
} else if (subType === "multi") {
|
|
paramsLocal["update_at"] = Date.now();
|
|
let res = await modelLocal.insertMany(paramsLocal.list)
|
|
returnData.hasError = false
|
|
returnData.msg = "数据新增成功"
|
|
returnData.list = res
|
|
} else if (subType === "del") {
|
|
let res = await modelLocal.updateMany({ _id: { $in: paramsLocal.ids } }, { isExit: 0, update_at: Date.now() })
|
|
returnData.hasError = false
|
|
returnData.msg = "数据删除成功"
|
|
returnData.list = res
|
|
} else if (subType === "edit") {
|
|
paramsLocal["update_at"] = Date.now();
|
|
let res = await modelLocal.updateOne({ _id: paramsLocal._id }, paramsLocal)
|
|
returnData.hasError = false
|
|
returnData.msg = "数据更新成功"
|
|
returnData.list = res
|
|
} else if (subType === "get") {
|
|
let pageSize = paramsLocal.pageSize
|
|
let currentPage = paramsLocal.currentPage
|
|
let paramsLocalInfo = _.omit(paramsLocal, ["pageSize", "currentPage"])
|
|
let paramsLocalInfoNotDel = { ...paramsLocalInfo, isExit: 1 }
|
|
let res = await modelLocal.find(paramsLocalInfoNotDel).sort({ update_at: -1 }).skip((currentPage - 1) * pageSize).limit(pageSize)
|
|
let count = await modelLocal.find(paramsLocalInfoNotDel).count()
|
|
let resData = [];
|
|
for (let i = 0; i < res.length; i++) {
|
|
let elei = res[i];
|
|
let resObj = { _id: elei._id }
|
|
if (returnComponent.length) {
|
|
for (let j = 0; j < returnComponent.length; j++) {
|
|
let elej = returnComponent[j];
|
|
resObj[elej] = elei[elej]
|
|
}
|
|
} else {
|
|
resObj = elei
|
|
}
|
|
resData.push(resObj)
|
|
}
|
|
returnData.hasError = false
|
|
returnData.msg = "查询成功"
|
|
returnData.list = resData
|
|
returnData["count"] = count
|
|
}
|
|
return returnData
|
|
}
|
|
}
|
|
|
|
module.exports = UserService;
|
|
|