Active.php 5.0 KB

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