LotteryDrawRecord.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. /**
  5. * 抽奖记录模型
  6. */
  7. class LotteryDrawRecord extends Model
  8. {
  9. // 表名
  10. protected $name = 'shop_lottery_draw_record';
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = false;
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'trigger_type_text',
  20. 'win_info_data'
  21. ];
  22. // 触发类型常量
  23. const TRIGGER_GOODS = 1; // 购买商品
  24. const TRIGGER_ORDER = 2; // 订单消费
  25. const TRIGGER_RECHARGE = 3; // 充值
  26. const TRIGGER_ACCUMULATE = 4; // 累计消费
  27. /**
  28. * 关联活动
  29. */
  30. public function activity()
  31. {
  32. return $this->belongsTo('LotteryActivity', 'activity_id');
  33. }
  34. /**
  35. * 关联用户
  36. */
  37. public function user()
  38. {
  39. return $this->belongsTo('app\common\model\User', 'user_id');
  40. }
  41. /**
  42. * 关联奖品
  43. */
  44. public function prize()
  45. {
  46. return $this->belongsTo('LotteryPrize', 'prize_id');
  47. }
  48. /**
  49. * 关联中奖记录
  50. */
  51. public function winRecord()
  52. {
  53. return $this->hasOne('LotteryWinRecord', 'draw_record_id');
  54. }
  55. /**
  56. * 关联订单
  57. */
  58. public function order()
  59. {
  60. return $this->belongsTo('app\common\model\Order', 'trigger_order_id');
  61. }
  62. /**
  63. * 获取触发类型文本
  64. */
  65. public function getTriggerTypeTextAttr($value, $data)
  66. {
  67. $types = [
  68. self::TRIGGER_GOODS => '购买商品',
  69. self::TRIGGER_ORDER => '订单消费',
  70. self::TRIGGER_RECHARGE => '充值',
  71. self::TRIGGER_ACCUMULATE => '累计消费'
  72. ];
  73. return isset($types[$data['trigger_type']]) ? $types[$data['trigger_type']] : '未知';
  74. }
  75. /**
  76. * 获取中奖信息数据
  77. */
  78. public function getWinInfoDataAttr($value, $data)
  79. {
  80. return !empty($data['win_info']) ? json_decode($data['win_info'], true) : [];
  81. }
  82. /**
  83. * 设置中奖信息
  84. */
  85. public function setWinInfoAttr($value)
  86. {
  87. return is_array($value) ? json_encode($value) : $value;
  88. }
  89. /**
  90. * 创建抽奖记录
  91. */
  92. public static function createRecord($activityId, $userId, $prizeId, $isWin, $triggerType, $triggerOrderId = null, $triggerAmount = null, $winInfo = [])
  93. {
  94. $data = [
  95. 'activity_id' => $activityId,
  96. 'user_id' => $userId,
  97. 'prize_id' => $prizeId,
  98. 'is_win' => $isWin ? 1 : 0,
  99. 'trigger_type' => $triggerType,
  100. 'trigger_order_id' => $triggerOrderId,
  101. 'trigger_amount' => $triggerAmount,
  102. 'win_info' => $winInfo ? json_encode($winInfo) : '',
  103. 'draw_ip' => request()->ip(),
  104. 'draw_time' => time(),
  105. 'device_info' => request()->header('user-agent', '')
  106. ];
  107. return static::create($data);
  108. }
  109. /**
  110. * 获取用户抽奖次数
  111. */
  112. public static function getUserDrawCount($activityId, $userId, $timeRange = null)
  113. {
  114. $query = static::where('activity_id', $activityId)
  115. ->where('user_id', $userId);
  116. if ($timeRange) {
  117. if (isset($timeRange['start'])) {
  118. $query->where('draw_time', '>=', $timeRange['start']);
  119. }
  120. if (isset($timeRange['end'])) {
  121. $query->where('draw_time', '<=', $timeRange['end']);
  122. }
  123. }
  124. return $query->count();
  125. }
  126. /**
  127. * 获取用户中奖次数
  128. */
  129. public static function getUserWinCount($activityId, $userId, $timeRange = null)
  130. {
  131. $query = static::where('activity_id', $activityId)
  132. ->where('user_id', $userId)
  133. ->where('is_win', 1);
  134. if ($timeRange) {
  135. if (isset($timeRange['start'])) {
  136. $query->where('draw_time', '>=', $timeRange['start']);
  137. }
  138. if (isset($timeRange['end'])) {
  139. $query->where('draw_time', '<=', $timeRange['end']);
  140. }
  141. }
  142. return $query->count();
  143. }
  144. /**
  145. * 获取活动抽奖统计
  146. */
  147. public static function getActivityStats($activityId, $timeRange = null)
  148. {
  149. $query = static::where('activity_id', $activityId);
  150. if ($timeRange) {
  151. if (isset($timeRange['start'])) {
  152. $query->where('draw_time', '>=', $timeRange['start']);
  153. }
  154. if (isset($timeRange['end'])) {
  155. $query->where('draw_time', '<=', $timeRange['end']);
  156. }
  157. }
  158. return [
  159. 'total_draw' => $query->count(),
  160. 'total_win' => $query->where('is_win', 1)->count(),
  161. 'total_people' => $query->distinct('user_id')->count()
  162. ];
  163. }
  164. }