接口功能是批量确认发货,代码如下:
/**
* 确认发货(无快递账号)
*/
async function sureSendNoAccount(orderList) {
const result = {}
const sendList = []
console.log("sureSendNoAccount count== ", orderList.length)
for (let i = 0; i < orderList.length; i++) {
const orderMsg = orderList[i]
const orders = await db.collection("orders").where({
status: 1,
order_no: orderMsg.order_no,
}).get()
if (orders.data.length > 0) {
const resultData = {}
const order = orders.data[0]
const sendResult = await toSendOrderNoAccount(order, orderMsg)
resultData.send_msg = sendResult
sendList.push(resultData)
} else {
const noAdd = {}
noAdd.msg = "订单不存在"
noAdd.order_no = orderMsg.order_no
sendList.push(noAdd)
}
}
result.data = sendList
result.code = 200
console.log("sureSendNoAccount result==", result)
return result
}
async function toSendOrderNoAccount(order, orderMsg) {
//发送模板消息
try {
// 发货
const time = new Date().getTime()
const result = db.collection("orders").where({
order_no: order.order_no
}).update({
data: {
status: 2,
send_time: time,
waybil: {
deliveryId: orderMsg.deliveryId,
deliveryName: orderMsg.deliveryName,
waybillId: orderMsg.waybillId
}
}
})
const orderData = {}
orderData.open_id = order.open_id
orderData.order_no = order.order_no
orderData.goods_name = order.goods.length > 1 ? (order.goods[0].goods_name + "等商品") : order.goods[0].goods_name
orderData.deliveryName = orderMsg.deliveryName
orderData.waybillId = orderMsg.waybillId
console.log("send_order_msg addMsg== ", orderData)
const sendData=await db.collection("send_order_msg").where(
{
order_no:orderData.order_no
}
).count()
if(sendData.total>0)
{
}else{
await db.collection("send_order_msg").add(
{
data:orderData
}
)
}
return result
} catch (error) {
return error
}
}
场景:后台批量发货200单(已确认这200单都是未发货状态,status值为1)
出现问题如下:1,这200单只有部分发货成功,一部分订单未发货成功,执行第一个方法中订单不存在这种情况
返回值如下(部分数据):
{
"data": [{
"msg": "订单不存在",
"order_no": "HN262171581581981990"
}, {
"send_msg": {
"stats": {
"updated": 1
},
"errMsg": "collection.update:ok"
}
}],
"code": 200
}
2,方法toSendOrderNoAccount 中添加待发送模板消息数据到 send_order_msg 最终添加数量大于已确认发货订单数量
最终导致今天快递单重复打印了一百单左右
请问大神们,我哪里写错了吗?求大神指点,跪求,拜谢 ,急急急