group.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 GroupService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'group');
  11. this.model = this.ctx.model.Group.Group;
  12. this.goodsModel = this.ctx.model.Shop.Goods;
  13. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  14. }
  15. /**
  16. * 生成团
  17. * @param {Object} orderDetail 订单详情数据
  18. * @param {Transaction} tran 数据库事务实例
  19. */
  20. async create(orderDetail, tran) {
  21. const { goods: goodsInfo, customer, shop } = orderDetail;
  22. const goods_id = _.get(goodsInfo, 'goods._id');
  23. const goods = await this.goodsModel.findById(goods_id);
  24. const goodsSpec_id = _.get(goodsInfo, '_id');
  25. const goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
  26. const person_limit = _.get(goodsSpec, 'group_config.need_person');
  27. const leader = customer;
  28. const persons = [{ customer, status: '0', join_time: moment().format('YYYY-MM-DD HH:mm:ss') }];
  29. const obj = { shop, goods, goodsSpec, leader, persons, person_limit };
  30. const id = tran.insert('Group', obj);
  31. return id;
  32. }
  33. /**
  34. * 加入团
  35. * @param {String} customer 用户id
  36. * @param {String} group_id 团id
  37. * @param {Transaction} tran 数据库事务实例
  38. */
  39. async join(customer, group_id, tran) {
  40. const result = await this.checkGroupCanJoin({ id: group_id });
  41. if (!result.result) throw new BusinessError(ErrorCode.DATA_INVALID, result.msg);
  42. const data = await this.model.findById(group_id);
  43. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到团信息');
  44. const { persons = [], person_limit } = data;
  45. const nps = JSON.parse(JSON.stringify(persons));
  46. nps.push({ customer, status: '0', join_time: moment().format('YYYY-MM-DD HH:mm:ss') });
  47. const updateData = { persons: nps };
  48. if (person_limit <= nps.length) updateData.status = '1';
  49. tran.update('Group', group_id, updateData);
  50. }
  51. // 检查是否可以加入团
  52. async checkGroupCanJoin({ id }) {
  53. const data = await this.model.findById(id);
  54. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到团数据');
  55. const { person_limit, persons = [], status } = data;
  56. if (status === '1') return { result: false, msg: '当前团已结束' };
  57. else if (status === '-1') return { result: false, msg: '当前团已关闭' };
  58. // 为0是正常的团员
  59. const realPersons = persons.filter(f => f.status === '0');
  60. if (realPersons.length < person_limit) return { result: true };
  61. return { result: false, msg: '当前参团人数已足够' };
  62. }
  63. /**
  64. * 团购退货(订单取消&售后退款/退货)
  65. * @param {Object} params 参数
  66. * @param params.group 团id
  67. * @param params.customer 用户id
  68. * @param tran
  69. */
  70. async refund({ group, customer }, tran) {
  71. const groupData = await this.model.findById(group);
  72. if (!groupData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到拼团数据');
  73. const { persons, leader } = groupData;
  74. const newPersons = JSON.parse(JSON.stringify(persons));
  75. const findPersonCondition = (c1, c2) => c1 === c2;
  76. const p = newPersons.find(f => findPersonCondition(f.customer, customer));
  77. if (!p) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退团的用户');
  78. const i = newPersons.findIndex(f => findPersonCondition(f.customer, customer));
  79. p.status = '1';
  80. newPersons[i] = p;
  81. const updateData = { newPersons };
  82. // 团长退团了,根据入团时间降序,顺位成为团长
  83. if (leader === customer) {
  84. let newLeader;
  85. const orderList = _.orderBy(newPersons, [ 'status', 'join_time' ], [ 'asc', 'asc' ]);
  86. const head = _.head(orderList);
  87. if (head && head.status === '0') {
  88. newLeader = _.get(head, 'customer');
  89. updateData.leader = newLeader;
  90. }
  91. }
  92. tran.update('Group', group, updateData);
  93. }
  94. }
  95. module.exports = GroupService;