shopNotice.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class ShopNoticeService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'shopnotice');
  11. this.model = this.ctx.model.Shop.ShopNotice;
  12. this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
  13. this.afterSaleModel = this.ctx.model.Trade.AfterSale;
  14. }
  15. /**
  16. * 提醒发货; 可单发,可群发
  17. * @param body 发送消息参数
  18. * @property source_id orderDetail的id
  19. */
  20. async remindToSend(body) {
  21. const { source_id } = body;
  22. const type = _.get(body, 'type', '1');
  23. const list = [];
  24. if (_.isString(source_id)) {
  25. const od = await this.orderDetailModel.findById(source_id);
  26. if (!od) return;
  27. const content = this.toSendMsg(_.get(od, 'no'));
  28. const obj = { shop: _.get(od, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '0' };
  29. list.push(obj);
  30. } else if (_.isArray(source_id)) {
  31. for (const id of source_id) {
  32. const od = await this.orderDetailModel.findById(id);
  33. if (!od) continue;
  34. const content = this.toSendMsg(_.get(od, 'no'));
  35. 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' };
  36. list.push(obj);
  37. }
  38. }
  39. await this.model.insertMany(list);
  40. // 发mq消息
  41. for (const i of list) {
  42. await this.ctx.service.util.rabbitMq.shopMsg(i.shop);
  43. }
  44. }
  45. /**
  46. * 提醒发货消息
  47. * @param no 订单号
  48. */
  49. toSendMsg(no) {
  50. return `您的订单号为: ${no} 的订单需要发货,请您及时处理`;
  51. }
  52. /**
  53. * 提醒处理售后
  54. * @param body
  55. * @property source_id 售后数据id
  56. */
  57. async remindToAfterSale(body) {
  58. const { source_id } = body;
  59. const type = _.get(body, 'type', '1');
  60. const list = [];
  61. if (_.isString(source_id)) {
  62. const data = await this.afterSaleModel.findById(source_id);
  63. const od = await this.orderDetailModel.findById(data.order_detail);
  64. const content = this.toAfterSaleMsg(_.get(od, 'no'));
  65. const obj = { shop: _.get(data, 'shop'), source: '1', source_id, time: moment().format('YYYY-MM-DD HH:mm:ss'), content, type, source_type: '1' };
  66. list.push(obj);
  67. } else if (_.isArray(source_id)) {
  68. for (const id of source_id) {
  69. const data = await this.afterSaleModel.findById(id);
  70. const od = await this.orderDetailModel.findById(data.order_detail);
  71. const content = this.toAfterSaleMsg(_.get(od, 'no'));
  72. 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' };
  73. list.push(obj);
  74. }
  75. }
  76. await this.model.insertMany(list);
  77. // 发mq消息
  78. for (const i of list) {
  79. await this.ctx.service.util.rabbitMq.shopMsg(i.shop);
  80. }
  81. }
  82. /**
  83. * 提示售后消息
  84. * @param {String} no 订单号
  85. */
  86. toAfterSaleMsg(no) {
  87. return `您的订单号为: ${no} 的订单有售后未处理,请您及时处理`;
  88. }
  89. async countNotRead({ shop }) {
  90. return this.model.count({ shop, status: '0' });
  91. }
  92. }
  93. module.exports = ShopNoticeService;