'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 GroupService extends CrudService { constructor(ctx) { super(ctx, 'group'); this.model = this.ctx.model.Group.Group; this.goodsModel = this.ctx.model.Shop.Goods; this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec; } /** * 生成团 * @param {Object} orderDetail 订单详情数据 * @param {Transaction} tran 数据库事务实例 */ async create(orderDetail, tran) { const { goods: goodsInfo, customer, shop } = orderDetail; const goods_id = _.get(goodsInfo, 'goods._id'); const goods = await this.goodsModel.findById(goods_id); const goodsSpec_id = _.get(goodsInfo, '_id'); const goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id); const person_limit = _.get(goodsSpec, 'group_config.need_person'); const leader = customer; const persons = [{ customer, status: '0', join_time: moment().format('YYYY-MM-DD HH:mm:ss') }]; const obj = { shop, goods, goodsSpec, leader, persons, person_limit }; const id = tran.insert('Group', obj); return id; } /** * 加入团 * @param {String} customer 用户id * @param {String} group_id 团id * @param {Transaction} tran 数据库事务实例 */ async join(customer, group_id, tran) { const result = await this.checkGroupCanJoin({ id: group_id }); if (!result.result) throw new BusinessError(ErrorCode.DATA_INVALID, result.msg); const data = await this.model.findById(group_id); if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到团信息'); const { persons = [], person_limit } = data; const nps = JSON.parse(JSON.stringify(persons)); nps.push({ customer, status: '0', join_time: moment().format('YYYY-MM-DD HH:mm:ss') }); const updateData = { persons: nps }; if (person_limit <= nps.length) updateData.status = '1'; tran.update('Group', group_id, updateData); } // 检查是否可以加入团 async checkGroupCanJoin({ id }) { const data = await this.model.findById(id); if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到团数据'); const { person_limit, persons = [], status } = data; if (status === '1') return { result: false, msg: '当前团已结束' }; else if (status === '-1') return { result: false, msg: '当前团已关闭' }; // 为0是正常的团员 const realPersons = persons.filter(f => f.status === '0'); if (realPersons.length < person_limit) return { result: true }; return { result: false, msg: '当前参团人数已足够' }; } /** * 团购退货(订单取消&售后退款/退货) * @param {Object} params 参数 * @param params.group 团id * @param params.customer 用户id * @param tran */ async refund({ group, customer }, tran) { const groupData = await this.model.findById(group); if (!groupData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到拼团数据'); const { persons, leader } = groupData; const newPersons = JSON.parse(JSON.stringify(persons)); const findPersonCondition = (c1, c2) => c1 === c2; const p = newPersons.find(f => findPersonCondition(f.customer, customer)); if (!p) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退团的用户'); const i = newPersons.findIndex(f => findPersonCondition(f.customer, customer)); p.status = '1'; newPersons[i] = p; const updateData = { newPersons }; // 团长退团了,根据入团时间降序,顺位成为团长 if (leader === customer) { let newLeader; const orderList = _.orderBy(newPersons, [ 'status', 'join_time' ], [ 'asc', 'asc' ]); const head = _.head(orderList); if (head && head.status === '0') { newLeader = _.get(head, 'customer'); updateData.leader = newLeader; } } tran.update('Group', group, updateData); } } module.exports = GroupService;