12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const moment = require('moment');
- //
- class ShopNoticeService extends CrudService {
- constructor(ctx) {
- super(ctx, 'shopnotice');
- this.model = this.ctx.model.Shop.ShopNotice;
- this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
- this.afterSaleModel = this.ctx.model.Trade.AfterSale;
- }
- /**
- * 提醒发货; 可单发,可群发
- * @param body 发送消息参数
- * @property source_id orderDetail的id
- */
- async remindToSend(body) {
- const { source_id } = body;
- const type = _.get(body, 'type', '1');
- const list = [];
- if (_.isString(source_id)) {
- const od = await this.orderDetailModel.findById(source_id);
- if (!od) return;
- const content = this.toSendMsg(_.get(od, 'no'));
- const obj = { shop: _.get(od, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '0' };
- list.push(obj);
- } else if (_.isArray(source_id)) {
- for (const id of source_id) {
- const od = await this.orderDetailModel.findById(id);
- if (!od) continue;
- const content = this.toSendMsg(_.get(od, 'no'));
- const obj = { shop: _.get(od, 'shop'), source: '1', source_id: id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '0' };
- list.push(obj);
- }
- }
- await this.model.insertMany(list);
- // 发mq消息
- for (const i of list) {
- await this.ctx.service.util.rabbitMq.shopMsg(i.shop);
- }
- }
- /**
- * 提醒发货消息
- * @param no 订单号
- */
- toSendMsg(no) {
- return `您的订单号为: ${no} 的订单需要发货,请您及时处理`;
- }
- /**
- * 提醒处理售后
- * @param body
- * @property source_id 售后数据id
- */
- async remindToAfterSale(body) {
- const { source_id } = body;
- const type = _.get(body, 'type', '1');
- const list = [];
- if (_.isString(source_id)) {
- const data = await this.afterSaleModel.findById(source_id);
- const od = await this.orderDetailModel.findById(data.order_detail);
- const content = this.toAfterSaleMsg(_.get(od, 'no'));
- const obj = { shop: _.get(data, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '1' };
- list.push(obj);
- } else if (_.isArray(source_id)) {
- for (const id of source_id) {
- const data = await this.afterSaleModel.findById(id);
- const od = await this.orderDetailModel.findById(data.order_detail);
- const content = this.toAfterSaleMsg(_.get(od, 'no'));
- const obj = { shop: _.get(data, 'shop'), source: '1', source_id: id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '1' };
- list.push(obj);
- }
- }
- await this.model.insertMany(list);
- // 发mq消息
- for (const i of list) {
- await this.ctx.service.util.rabbitMq.shopMsg(i.shop);
- }
- }
- /**
- * 提示售后消息
- * @param {String} no 订单号
- */
- toAfterSaleMsg(no) {
- return `您的订单号为: ${no} 的订单有售后未处理,请您及时处理`;
- }
- async countNotRead({ shop }) {
- return this.model.count({ shop, status: '0' });
- }
- }
- module.exports = ShopNoticeService;
|