Active.php 5.1 KB

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