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.
 
 
 
 

238 lines
12 KiB

'use strict';
const Service = require('egg').Service;
const _ = require("lodash");
class DingdanService extends Service {
async index(params) {
let { ctx } = this
let modelLocal = ctx.model.Dingdan
let chanpinModel = ctx.model.Chanpin
let returnData = { hasError: false, msg: "数据错误", list: [], allxiaoshoujine: 0 }
//新增
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 list = await this.dealList(paramsLocal.list)
let res = await modelLocal.insertMany(list)
returnData.hasError = false
returnData.msg = "批量新增成功"
} else if (subType === "del") {
await modelLocal.updateMany({ _id: { $in: paramsLocal.ids } }, { isExit: 0, update_at: Date.now() })
let editlist = await modelLocal.find({ tiaoma: { $in: paramsLocal.tiaomas }, isExit: 1 })
let list = await this.dealList(editlist)
for (let i = 0; i < list.length; i++) {
let element = list[i];
await modelLocal.updateOne({ _id: element._id }, element)
}
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") {
let list = await this.dealList(paramsLocal.list)
let updateItem = list[0]
updateItem["update_at"] = Date.now();
let chanpinItemA = await chanpinModel.find({ tiaoxingma: updateItem.tiaoma })
let chanpinItemB = await chanpinModel.find({ xiangtiaoxingma: updateItem.tiaoma })
let chanpinItemC = await chanpinModel.find({ zuidaxiangzhuangtiaoma: updateItem.tiaoma })
if (chanpinItemA.length || chanpinItemB.length || chanpinItemC.length) {
updateItem = { ...updateItem }
let res = await modelLocal.updateOne({ _id: updateItem._id }, updateItem)
returnData.hasError = false
returnData.msg = "数据更新成功"
} else {
returnData.hasError = true
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({ shunxu: 1 }).skip((currentPage - 1) * pageSize).limit(pageSize)
let whereOther = paramsLocalInfoNotDel["$where"]
let paramsLocalInfoAgg = _.omit(paramsLocalInfoNotDel, ["$where"])
if (whereOther === "this.zuigaocaigoujia > this.xiaoshoujine") {
paramsLocalInfoAgg["$expr"] = {
"$gt": ["$zuigaocaigoujia", "$xiaoshoujine"]
}
} else if (whereOther === "this.pinmingxuanxiang.length>1") {
paramsLocalInfoAgg["$expr"] = {
"$gt": [{ "$size": "$pinmingxuanxiang" }, 1]
}
}
let allxiaoshoujine = await modelLocal.aggregate([{
$match: paramsLocalInfoAgg,
},
{
$group: { _id: null, totalSaleAmount: { $sum: '$xiaoshoujine' } }
}])
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.allxiaoshoujine = Number(Number((_.get(allxiaoshoujine, [0, "totalSaleAmount"], 0)) || 0).toFixed(2))
returnData.list = resData
}
let allParamsLocalInfo = _.omit(paramsLocal, ["pageSize", "currentPage", "url"])
let allParamsLocalInfoNotDel = { ...allParamsLocalInfo, isExit: 1 }
let count = await modelLocal.find(allParamsLocalInfoNotDel).count()
returnData["count"] = count
return returnData
}
async dealList(list) {
let { ctx } = this
let listLocal = []
let modelLocal = ctx.model.Dingdan
let allList = await modelLocal.find({ isExit: 1 })
let listCopy = _.concat(list, allList)
let pinmingGroup = _.groupBy(listCopy, o => o.tiaoma)
let pinmingOptionObj = {}
for (let key in pinmingGroup) {
let value = pinmingGroup[key]
let pinmingxuxnaing = []
for (let i = 0; i < value.length; i++) {
let pinmingOptionItem = {}
let element = value[i];
pinmingOptionItem["key"] = element.pinming
pinmingOptionItem["value"] = element.pinming
pinmingxuxnaing.push(pinmingOptionItem)
}
pinmingOptionObj[key] = _.unionBy(pinmingxuxnaing, "value")
}
let userModel = ctx.model.User
let chanpinModel = ctx.model.Chanpin
for (let i = 0; i < list.length; i++) {
let item = _.cloneDeep(list[i]);
item["pinmingxuanxiang"] = pinmingOptionObj[item.tiaoma]
item["danjudanweishuliang"] = item.xiaoshoushuliang
let userItem = await userModel.find({ usercode: item.kehubianhao })
let chanpinItemA = await chanpinModel.find({
'tiaoxingma': { $regex: `${item.tiaoma}$`, $options: 'i' }
})
let chanpinItemB = await chanpinModel.find({
'xiangtiaoxingma': { $regex: `${item.tiaoma}$`, $options: 'i' }
})
// 销售准备表 : 销售订单 条码 需要匹配 产品表的 【条形码】【箱条形码】【最大箱装条码】。
// 如果 匹配到了【条形码】,则 判断产品规则 是两级 还是 三级 ,如果两级是多少就显示多少,如果是三级 则 销售数量 除以 箱装量 如果 有余数则 只取整数, 然后 整数 * 相撞量。一个* 是两级,两个* 是三级
// 如果 匹配到了【箱条形码】,则 销售数量 * 箱装量 。
// 如果 匹配到了【最大箱装条码】,则 销售数量 * 最大箱装数。
let chanpinItemC = await chanpinModel.find({
'zuidaxiangzhuangtiaoma': { $regex: `${item.tiaoma}$`, $options: 'i' }
})
if (userItem.length) {
item["kehubianhao"] = _.get(userItem, [0, "usercode"], "")
item["changbian"] = _.get(userItem, [0, "usercode"], "")
item["username"] = _.get(userItem, [0, "username"], "")
} else {
item["kehubianhao"] = ""
item["changbian"] = ""
item["username"] = ""
}
if (chanpinItemA.length) {
item["huohao"] = _.get(chanpinItemA, [0, "daima"], "")
item["chanpindaima"] = _.get(chanpinItemA, [0, "daima"], "")
item["zuixiaodanweitiaoma"] = _.get(chanpinItemA, [0, "tiaoxingma"], "")
item["xiaoshoushuliang"] = item.xiaoshoushuliang
item.isNotSale = false
let guigexinghao = _.get(chanpinItemA, [0, "guigexinghao"], "")
let guigexinghaoArr = _.split(guigexinghao, "*")
if (guigexinghaoArr.length === 2) {
item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang
} else if (guigexinghaoArr.length === 3) {
let jishu = Number(guigexinghaoArr[2])
if (item.xiaoshoushuliang > jishu) {
let count = Math.floor(item.xiaoshoushuliang / jishu)
item["xiugaihouxiaoshoushuliang"] = count * jishu
} else {
item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang
}
}
item["xiaoshoujine"] = item.xiaoshoujine
item["zuigaocaigoujia"] = _.get(chanpinItemA, [0, "zuigaocaigoujia"], 0)
item["xiangzhuangliang"] = _.get(chanpinItemA, [0, "xiangzhuangliang"], "")
item["zuidaxiangzhuangliang"] = _.get(chanpinItemA, [0, "zuidadanweixiangzhuangliang"], "")
item["guigexinghao"] = _.get(chanpinItemA, [0, "guigexinghao"], "")
item["jiliangdanwei"] = _.get(chanpinItemA, [0, "jiliangdanwei"], "")
item["xiangtiaoma"] = _.get(chanpinItemA, [0, "xiangtiaoxingma"], "")
item["zuidaxiangtiaoma"] = _.get(chanpinItemA, [0, "zuidaxiangzhuangtiaoma"], "")
} else if (chanpinItemB.length) {
item.isNotSale = false
item["huohao"] = _.get(chanpinItemB, [0, "daima"], "")
item["chanpindaima"] = _.get(chanpinItemB, [0, "daima"], "")
item["zuixiaodanweitiaoma"] = _.get(chanpinItemB, [0, "tiaoxingma"], "")
item["xiaoshoushuliang"] = item.xiaoshoushuliang
let xiangzhuangliang = Number((_.get(chanpinItemB, [0, "xiangzhuangliang"], 1) || 1))
item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang * xiangzhuangliang
item["xiaoshoujine"] = item.xiaoshoujine
item["zuigaocaigoujia"] = _.get(chanpinItemB, [0, "zuigaocaigoujia"], 0)
item["xiangzhuangliang"] = _.get(chanpinItemB, [0, "xiangzhuangliang"], 0)
item["zuidaxiangzhuangliang"] = _.get(chanpinItemB, [0, "zuidadanweixiangzhuangliang"], 0)
item["guigexinghao"] = _.get(chanpinItemB, [0, "guigexinghao"], "")
item["jiliangdanwei"] = _.get(chanpinItemB, [0, "jiliangdanwei"], "")
item["xiangtiaoma"] = _.get(chanpinItemB, [0, "xiangtiaoxingma"], "")
item["zuidaxiangtiaoma"] = _.get(chanpinItemB, [0, "zuidaxiangzhuangtiaoma"], "")
} else if (chanpinItemC.length) {
item.isNotSale = false
item["huohao"] = _.get(chanpinItemC, [0, "daima"], "")
item["chanpindaima"] = _.get(chanpinItemC, [0, "daima"], "")
item["zuixiaodanweitiaoma"] = _.get(chanpinItemC, [0, "tiaoxingma"], "")
item["xiaoshoushuliang"] = item.xiaoshoushuliang
let guigexinghao = _.get(chanpinItemC, [0, "guigexinghao"], "")
let guigexinghaoArr = _.split(guigexinghao, "*")
let jishu = Number(guigexinghaoArr[2]) * Number(guigexinghaoArr[1])
// let zuidadanweixiangzhuangliang = Number((_.get(chanpinItemC, [0, "zuidadanweixiangzhuangliang"], 1) || 1))
item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang * Number(jishu)
item["xiaoshoujine"] = item.xiaoshoujine
item["zuigaocaigoujia"] = _.get(chanpinItemC, [0, "zuigaocaigoujia"], 0)
item["xiangzhuangliang"] = _.get(chanpinItemC, [0, "xiangzhuangliang"], 0)
item["zuidaxiangzhuangliang"] = _.get(chanpinItemC, [0, "zuidadanweixiangzhuangliang"], 0)
item["guigexinghao"] = _.get(chanpinItemC, [0, "guigexinghao"], "")
item["jiliangdanwei"] = _.get(chanpinItemC, [0, "jiliangdanwei"], "")
item["xiangtiaoma"] = _.get(chanpinItemC, [0, "xiangtiaoxingma"], "")
item["zuidaxiangtiaoma"] = _.get(chanpinItemC, [0, "zuidaxiangzhuangtiaoma"], "")
} else {
item["huohao"] = ""
item["chanpindaima"] = ""
item["zuixiaodanweitiaoma"] = ""
item["xiaoshoushuliang"] = item.xiaoshoushuliang
item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang
item["xiaoshoujine"] = item.xiaoshoujine
item["zuigaocaigoujia"] = 0
item["xiangzhuangliang"] = ""
item["zuidaxiangzhuangliang"] = ""
item["guigexinghao"] = ""
item["jiliangdanwei"] = ""
item["xiangtiaoma"] = ""
item["zuidaxiangtiaoma"] = ""
item.isNotSale = true
}
listLocal.push(item)
}
return listLocal
}
}
module.exports = DingdanService;