LotteryUserChanceRecord.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. use app\common\Enum\LotteryEnum;
  5. /**
  6. * 用户抽奖机会获取记录模型
  7. */
  8. class LotteryUserChanceRecord extends Model
  9. {
  10. protected $table = 'shop_lottery_user_chance_record';
  11. protected $autoWriteTimestamp = true;
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 定义字段类型
  16. protected $type = [
  17. 'get_time' => 'integer',
  18. 'createtime' => 'integer',
  19. 'updatetime' => 'integer',
  20. 'condition_value' => 'float',
  21. 'recharge_amount' => 'float',
  22. ];
  23. /**
  24. * 关联活动
  25. */
  26. public function activity()
  27. {
  28. return $this->belongsTo(LotteryActivity::class, 'activity_id', 'id');
  29. }
  30. /**
  31. * 关联用户
  32. */
  33. public function user()
  34. {
  35. return $this->belongsTo('app\common\model\User', 'user_id', 'id');
  36. }
  37. /**
  38. * 关联条件
  39. */
  40. public function condition()
  41. {
  42. return $this->belongsTo(LotteryCondition::class, 'condition_id', 'id');
  43. }
  44. /**
  45. * 关联订单
  46. */
  47. public function order()
  48. {
  49. return $this->belongsTo('app\common\model\Order', 'order_id', 'id');
  50. }
  51. /**
  52. * 关联管理员
  53. */
  54. public function admin()
  55. {
  56. return $this->belongsTo('app\common\model\Admin', 'admin_id', 'id');
  57. }
  58. /**
  59. * 获取器 - 获取类型文本
  60. */
  61. public function getGetTypeTextAttr($value, $data)
  62. {
  63. return LotteryEnum::getChanceGetTypeText($data['get_type']);
  64. }
  65. /**
  66. * 获取器 - 获取时间格式化
  67. */
  68. public function getGetTimeTextAttr($value, $data)
  69. {
  70. return $data['get_time'] ? date('Y-m-d H:i:s', $data['get_time']) : '';
  71. }
  72. /**
  73. * 获取器 - 创建时间格式化
  74. */
  75. public function getCreatetimeTextAttr($value, $data)
  76. {
  77. return $data['createtime'] ? date('Y-m-d H:i:s', $data['createtime']) : '';
  78. }
  79. /**
  80. * 修改器 - 设置获取时间
  81. */
  82. public function setGetTimeAttr($value)
  83. {
  84. return $value ? strtotime($value) : time();
  85. }
  86. /**
  87. * 获取指定活动的用户机会获取记录
  88. *
  89. * @param int $activityId 活动ID
  90. * @param int $userId 用户ID
  91. * @param int $page 页码
  92. * @param int $limit 每页数量
  93. * @return array
  94. */
  95. public static function getUserChanceRecords($activityId, $userId, $page = 1, $limit = 20)
  96. {
  97. return self::where('activity_id', $activityId)
  98. ->where('user_id', $userId)
  99. ->with(['condition', 'order', 'admin'])
  100. ->order('get_time desc')
  101. ->page($page, $limit)
  102. ->select();
  103. }
  104. /**
  105. * 获取指定活动的机会获取统计
  106. *
  107. * @param int $activityId 活动ID
  108. * @return array
  109. */
  110. public static function getActivityChanceStats($activityId)
  111. {
  112. $records = self::where('activity_id', $activityId)->select();
  113. $stats = [
  114. 'total_records' => count($records),
  115. 'total_chances' => 0,
  116. 'type_stats' => [],
  117. ];
  118. foreach ($records as $record) {
  119. $stats['total_chances'] += $record->chances;
  120. $getType = $record->get_type;
  121. if (!isset($stats['type_stats'][$getType])) {
  122. $stats['type_stats'][$getType] = [
  123. 'type' => $getType,
  124. 'type_text' => LotteryEnum::getChanceGetTypeText($getType),
  125. 'count' => 0,
  126. 'chances' => 0,
  127. ];
  128. }
  129. $stats['type_stats'][$getType]['count']++;
  130. $stats['type_stats'][$getType]['chances'] += $record->chances;
  131. }
  132. return $stats;
  133. }
  134. /**
  135. * 获取用户机会获取统计
  136. *
  137. * @param int $userId 用户ID
  138. * @param int $activityId 活动ID(可选)
  139. * @return array
  140. */
  141. public static function getUserChanceStats($userId, $activityId = null)
  142. {
  143. $query = self::where('user_id', $userId);
  144. if ($activityId) {
  145. $query->where('activity_id', $activityId);
  146. }
  147. $records = $query->select();
  148. $stats = [
  149. 'total_records' => count($records),
  150. 'total_chances' => 0,
  151. 'type_stats' => [],
  152. 'recent_records' => [],
  153. ];
  154. foreach ($records as $record) {
  155. $stats['total_chances'] += $record->chances;
  156. $getType = $record->get_type;
  157. if (!isset($stats['type_stats'][$getType])) {
  158. $stats['type_stats'][$getType] = [
  159. 'type' => $getType,
  160. 'type_text' => LotteryEnum::getChanceGetTypeText($getType),
  161. 'count' => 0,
  162. 'chances' => 0,
  163. ];
  164. }
  165. $stats['type_stats'][$getType]['count']++;
  166. $stats['type_stats'][$getType]['chances'] += $record->chances;
  167. }
  168. // 获取最近的记录
  169. $stats['recent_records'] = self::where('user_id', $userId)
  170. ->with(['activity'])
  171. ->order('get_time desc')
  172. ->limit(5)
  173. ->select();
  174. return $stats;
  175. }
  176. /**
  177. * 批量创建机会获取记录
  178. *
  179. * @param array $records 记录数组
  180. * @return bool
  181. */
  182. public static function batchCreateRecords($records)
  183. {
  184. if (empty($records)) {
  185. return false;
  186. }
  187. // 为每条记录添加创建时间
  188. foreach ($records as &$record) {
  189. $record['createtime'] = time();
  190. $record['get_time'] = $record['get_time'] ?? time();
  191. }
  192. return self::insertAll($records);
  193. }
  194. /**
  195. * 验证记录数据
  196. *
  197. * @param array $data 记录数据
  198. * @return bool|string true表示验证通过,string表示错误信息
  199. */
  200. public static function validateRecord($data)
  201. {
  202. // 验证必填字段
  203. if (empty($data['activity_id'])) {
  204. return '活动ID不能为空';
  205. }
  206. if (empty($data['user_id'])) {
  207. return '用户ID不能为空';
  208. }
  209. if (empty($data['get_type'])) {
  210. return '获取类型不能为空';
  211. }
  212. if (empty($data['chances']) || $data['chances'] <= 0) {
  213. return '机会次数必须大于0';
  214. }
  215. // 验证获取类型是否有效
  216. if (!LotteryEnum::isValidChanceGetType($data['get_type'])) {
  217. return '无效的获取类型';
  218. }
  219. // 根据获取类型验证相关字段
  220. switch ($data['get_type']) {
  221. case LotteryEnum::CHANCE_GET_TYPE_BUY_GOODS:
  222. case LotteryEnum::CHANCE_GET_TYPE_ORDER_AMOUNT:
  223. if (empty($data['order_id'])) {
  224. return '订单ID不能为空';
  225. }
  226. break;
  227. case LotteryEnum::CHANCE_GET_TYPE_RECHARGE:
  228. if (empty($data['recharge_amount']) || $data['recharge_amount'] <= 0) {
  229. return '充值金额必须大于0';
  230. }
  231. break;
  232. case LotteryEnum::CHANCE_GET_TYPE_ADMIN_GRANT:
  233. if (empty($data['admin_id'])) {
  234. return '管理员ID不能为空';
  235. }
  236. break;
  237. }
  238. return true;
  239. }
  240. }