'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: [] } //新增 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() }) returnData.hasError = false returnData.msg = "数据删除成功" } else if (subType === "edit") { paramsLocal = paramsLocal.list[0] paramsLocal["update_at"] = Date.now(); let chanpinItemA = await chanpinModel.find({ tiaoxingma: paramsLocal.tiaoma }) let chanpinItemB = await chanpinModel.find({ xiangtiaoxingma: paramsLocal.tiaoma }) let chanpinItemC = await chanpinModel.find({ zuidaxiangzhuangtiaoma: paramsLocal.tiaoma }) if (chanpinItemA.length || chanpinItemB.length || chanpinItemC.length) { paramsLocal = { ...paramsLocal, isNotSale: false } let res = await modelLocal.updateOne({ _id: paramsLocal._id }, paramsLocal) 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({ 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"]) 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 listCopy = _.cloneDeep(list) 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] = pinmingxuxnaing } let userModel = ctx.model.User let chanpinModel = ctx.model.Chanpin for (let i = 0; i < list.length; i++) { let item = _.cloneDeep(list[i]); item["huohao"] = item.tiaoma item["pinmingxuanxiang"] = pinmingOptionObj[item.tiaoma] item["danjudanweishuliang"] = item.xiaoshoushuliang let userItem = await userModel.find({ usercode: item.kehubianhao }) let chanpinItemA = await chanpinModel.find({ tiaoxingma: item.tiaoma }) let chanpinItemB = await chanpinModel.find({ xiangtiaoxingma: item.tiaoma }) let chanpinItemC = await chanpinModel.find({ zuidaxiangzhuangtiaoma: item.tiaoma }) if (userItem.length) { item["kehubianhao"] = _.get(userItem, [0, "usercode"], "") item["username"] = _.get(userItem, [0, "username"], "") } else { item["kehubianhao"] = "" item["username"] = "" } if (chanpinItemA.length) { item["changbian"] = _.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, "*") let jishu = Number(guigexinghaoArr[1]) 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"], "") 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["changbian"] = _.get(chanpinItemB, [0, "daima"], "") item["chanpindaima"] = _.get(chanpinItemB, [0, "daima"], "") item["zuixiaodanweitiaoma"] = _.get(chanpinItemB, [0, "tiaoxingma"], "") item["xiaoshoushuliang"] = item.xiaoshoushuliang let guigexinghao = _.get(chanpinItemB, [0, "guigexinghao"], "") let guigexinghaoArr = _.split(guigexinghao, "*") let jishu = Number(guigexinghaoArr[1]) item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang * jishu item["xiaoshoujine"] = item.xiaoshoujine item["zuigaocaigoujia"] = _.get(chanpinItemB, [0, "zuigaocaigoujia"], "") item["xiangzhuangliang"] = _.get(chanpinItemB, [0, "xiangzhuangliang"], "") item["zuidaxiangzhuangliang"] = _.get(chanpinItemB, [0, "zuidadanweixiangzhuangliang"], "") 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["changbian"] = _.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[1]) item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang * jishu * Number((_.get(chanpinItemC, [0, "xiangzhuangliang"], 1) || 1)) item["xiaoshoujine"] = item.xiaoshoujine item["zuigaocaigoujia"] = _.get(chanpinItemC, [0, "zuigaocaigoujia"], "") item["xiangzhuangliang"] = _.get(chanpinItemC, [0, "xiangzhuangliang"], "") item["zuidaxiangzhuangliang"] = _.get(chanpinItemC, [0, "zuidadanweixiangzhuangliang"], "") 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["changbian"] = "" item["chanpindaima"] = "" item["zuixiaodanweitiaoma"] = "" item["xiaoshoushuliang"] = item.xiaoshoushuliang item["xiugaihouxiaoshoushuliang"] = item.xiaoshoushuliang item["xiaoshoujine"] = item.xiaoshoujine item["zuigaocaigoujia"] = "" item["xiangzhuangliang"] = "" item["zuidaxiangzhuangliang"] = "" item["guigexinghao"] = "" item["jiliangdanwei"] = "" item["xiangtiaoma"] = "" item["zuidaxiangtiaoma"] = "" item.isNotSale = true } listLocal.push(item) } return listLocal } } module.exports = DingdanService;