Active.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\common\model\sharing;
  3. use think\Hook;
  4. use app\common\model\BaseModel;
  5. use app\common\service\Message as MessageService;
  6. use app\common\enum\sharing\ActiveStatus as ActiveStatusEnum;
  7. /**
  8. * 拼团拼单模型
  9. * Class Active
  10. * @package app\common\model\sharing
  11. */
  12. class Active extends BaseModel
  13. {
  14. protected $name = 'sharing_active';
  15. protected $append = ['surplus_people'];
  16. /**
  17. * 拼团拼单模型初始化
  18. */
  19. public static function init()
  20. {
  21. parent::init();
  22. // 监听订单处理事件
  23. $static = new static;
  24. Hook::listen('sharing_active', $static);
  25. }
  26. /**
  27. * 获取器:拼单状态
  28. * @param $value
  29. * @return array
  30. */
  31. public function getStatusAttr($value)
  32. {
  33. return ['text' => ActiveStatusEnum::data()[$value]['name'], 'value' => $value];
  34. }
  35. /**
  36. * 获取器:结束时间
  37. * @param $value
  38. * @return array
  39. */
  40. public function getEndTimeAttr($value)
  41. {
  42. return ['text' => date('Y-m-d H:i:s', $value), 'value' => $value];
  43. }
  44. /**
  45. * 获取器:剩余拼团人数
  46. * @param $value
  47. * @return array
  48. */
  49. public function getSurplusPeopleAttr($value, $data)
  50. {
  51. return $data['people'] - $data['actual_people'];
  52. }
  53. /**
  54. * 关联拼团商品表
  55. * @return \think\model\relation\BelongsTo
  56. */
  57. public function goods()
  58. {
  59. return $this->belongsTo('Goods');
  60. }
  61. /**
  62. * 关联用户表(团长)
  63. * @return \think\model\relation\BelongsTo
  64. */
  65. public function user()
  66. {
  67. $module = self::getCalledModule() ?: 'common';
  68. return $this->belongsTo("app\\{$module}\\model\\User", 'creator_id');
  69. }
  70. /**
  71. * 关联拼单成员表
  72. * @return \think\model\relation\HasMany
  73. */
  74. public function users()
  75. {
  76. return $this->hasMany('ActiveUsers', 'active_id')
  77. ->order(['is_creator' => 'desc', 'create_time' => 'asc']);
  78. }
  79. /**
  80. * 拼单详情
  81. * @param $active_id
  82. * @param array $with
  83. * @return static|null
  84. * @throws \think\exception\DbException
  85. */
  86. public static function detail($active_id, $with = [])
  87. {
  88. return static::get($active_id, array_merge(['goods', 'users' => ['user', 'sharingOrder']], $with));
  89. }
  90. /**
  91. * 验证当前拼单是否允许加入新成员
  92. * @return bool
  93. */
  94. public function checkAllowJoin()
  95. {
  96. if (!in_array($this['status']['value'], [0, 10])) {
  97. $this->error = '当前拼单已结束';
  98. return false;
  99. }
  100. if (time() > $this['end_time']) {
  101. $this->error = '当前拼单已结束';
  102. return false;
  103. }
  104. if ($this['actual_people'] >= $this['people']) {
  105. $this->error = '当前拼单人数已满';
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * 新增拼单记录
  112. * @param $creator_id
  113. * @param $order_id
  114. * @param OrderGoods $goods
  115. * @return false|int
  116. */
  117. public function onCreate($creator_id, $order_id, $goods)
  118. {
  119. // 新增拼单记录
  120. $this->save([
  121. 'goods_id' => $goods['goods_id'],
  122. 'people' => $goods['people'],
  123. 'actual_people' => 1,
  124. 'creator_id' => $creator_id,
  125. 'end_time' => time() + ($goods['group_time'] * 60 * 60),
  126. 'status' => 10,
  127. 'wxapp_id' => $goods['wxapp_id']
  128. ]);
  129. // 新增拼单成员记录
  130. ActiveUsers::add([
  131. 'active_id' => $this['active_id'],
  132. 'order_id' => $order_id,
  133. 'user_id' => $creator_id,
  134. 'is_creator' => 1,
  135. 'wxapp_id' => $goods['wxapp_id']
  136. ]);
  137. return true;
  138. }
  139. /**
  140. * 更新拼单记录
  141. * @param $userId
  142. * @param $orderId
  143. * @return bool
  144. * @throws \think\exception\DbException
  145. */
  146. public function onUpdate($userId, $orderId)
  147. {
  148. // 验证当前拼单是否允许加入新成员
  149. if (!$this->checkAllowJoin()) {
  150. return false;
  151. }
  152. // 新增拼单成员记录
  153. ActiveUsers::add([
  154. 'active_id' => $this['active_id'],
  155. 'order_id' => $orderId,
  156. 'user_id' => $userId,
  157. 'is_creator' => 0,
  158. 'wxapp_id' => $this['wxapp_id']
  159. ]);
  160. // 累计已拼人数
  161. $actual_people = $this['actual_people'] + 1;
  162. // 更新拼单记录:当前已拼人数、拼单状态
  163. $status = $actual_people >= $this['people'] ? 20 : 10;
  164. $this->save([
  165. 'actual_people' => $actual_people,
  166. 'status' => $status
  167. ]);
  168. // 拼单成功, 发送订阅消息
  169. if ($status == 20) {
  170. $model = static::detail($this['active_id']);
  171. MessageService::send('sharing.active_status', [
  172. 'active' => $model,
  173. 'status' => ActiveStatusEnum::ACTIVE_STATE_SUCCESS,
  174. ]);
  175. }
  176. return true;
  177. }
  178. }