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.
 
 
 
 

71 lines
2.9 KiB

'use strict';
const Service = require('egg').Service;
const _ = require("lodash")
class ChanpinService extends Service {
async index(params) {
let { ctx } = this
let modelLocal = ctx.model.Chanpin
let returnData = { hasError: false, msg: "数据错误", list: [] }
//新增
let subType = params.subType
let returnComponent = params.returnData || []
let paramsLocal = _.omit(params, ["subType", "returnData", "url"])
if (subType === "add") {
paramsLocal = paramsLocal.list[0]
paramsLocal["update_at"] = Date.now();
let res = await modelLocal.create(paramsLocal)
returnData.hasError = false
returnData.msg = "数据新增成功"
} else if (subType === "multi") {
paramsLocal["update_at"] = Date.now();
let res = await modelLocal.insertMany(paramsLocal.list)
returnData.hasError = false
returnData.msg = "批量新增成功"
} else if (subType === "del") {
let res = await modelLocal.updateMany({ _id: { $in: paramsLocal.ids } }, { isExit: 0, update_at: Date.now() })
returnData.hasError = false
returnData.msg = "数据删除成功"
} else if (subType === "delAll") {
await modelLocal.deleteMany({ "update_at": { $lt: Date.now() + 1000 * 60 * 60 * 24 } })
returnData.hasError = false
returnData.msg = "数据已全部删除"
} else if (subType === "edit") {
paramsLocal = paramsLocal.list[0]
paramsLocal["update_at"] = Date.now();
let res = await modelLocal.updateOne({ _id: paramsLocal._id }, paramsLocal)
returnData.hasError = false
returnData.msg = "数据更新成功"
} else if (subType === "get") {
let pageSize = paramsLocal.pageSize
let currentPage = paramsLocal.currentPage
let paramsLocalInfo = _.omit(paramsLocal, ["pageSize", "currentPage", "url"])
let paramsLocalInfoNotDel = { ...paramsLocalInfo, isExit: 1 }
let res = await modelLocal.find(paramsLocalInfoNotDel).sort({ update_at: -1 }).skip((currentPage - 1) * pageSize).limit(pageSize)
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
}
let allParamsLocalInfo = _.omit(paramsLocal, ["pageSize", "currentPage", "url", "list", "update_at"])
let allParamsLocalInfoNotDel = { ...allParamsLocalInfo, isExit: 1 }
let count = await modelLocal.find(allParamsLocalInfoNotDel).count()
returnData["count"] = count
return returnData
}
}
module.exports = ChanpinService;