123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- use app\common\Enum\LotteryEnum;
- /**
- * 用户抽奖机会获取记录模型
- */
- class LotteryUserChanceRecord extends Model
- {
- protected $table = 'shop_lottery_user_chance_record';
-
- protected $autoWriteTimestamp = true;
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
-
- // 定义字段类型
- protected $type = [
- 'get_time' => 'integer',
- 'createtime' => 'integer',
- 'updatetime' => 'integer',
- 'condition_value' => 'float',
- 'recharge_amount' => 'float',
- ];
- /**
- * 关联活动
- */
- public function activity()
- {
- return $this->belongsTo(LotteryActivity::class, 'activity_id', 'id');
- }
- /**
- * 关联用户
- */
- public function user()
- {
- return $this->belongsTo('app\common\model\User', 'user_id', 'id');
- }
- /**
- * 关联条件
- */
- public function condition()
- {
- return $this->belongsTo(LotteryCondition::class, 'condition_id', 'id');
- }
- /**
- * 关联订单
- */
- public function order()
- {
- return $this->belongsTo('app\common\model\Order', 'order_id', 'id');
- }
- /**
- * 关联管理员
- */
- public function admin()
- {
- return $this->belongsTo('app\common\model\Admin', 'admin_id', 'id');
- }
- /**
- * 获取器 - 获取类型文本
- */
- public function getGetTypeTextAttr($value, $data)
- {
- return LotteryEnum::getChanceGetTypeText($data['get_type']);
- }
- /**
- * 获取器 - 获取时间格式化
- */
- public function getGetTimeTextAttr($value, $data)
- {
- return $data['get_time'] ? date('Y-m-d H:i:s', $data['get_time']) : '';
- }
- /**
- * 获取器 - 创建时间格式化
- */
- public function getCreatetimeTextAttr($value, $data)
- {
- return $data['createtime'] ? date('Y-m-d H:i:s', $data['createtime']) : '';
- }
- /**
- * 修改器 - 设置获取时间
- */
- public function setGetTimeAttr($value)
- {
- return $value ? strtotime($value) : time();
- }
- /**
- * 获取指定活动的用户机会获取记录
- *
- * @param int $activityId 活动ID
- * @param int $userId 用户ID
- * @param int $page 页码
- * @param int $limit 每页数量
- * @return array
- */
- public static function getUserChanceRecords($activityId, $userId, $page = 1, $limit = 20)
- {
- return self::where('activity_id', $activityId)
- ->where('user_id', $userId)
- ->with(['condition', 'order', 'admin'])
- ->order('get_time desc')
- ->page($page, $limit)
- ->select();
- }
- /**
- * 获取指定活动的机会获取统计
- *
- * @param int $activityId 活动ID
- * @return array
- */
- public static function getActivityChanceStats($activityId)
- {
- $records = self::where('activity_id', $activityId)->select();
-
- $stats = [
- 'total_records' => count($records),
- 'total_chances' => 0,
- 'type_stats' => [],
- ];
-
- foreach ($records as $record) {
- $stats['total_chances'] += $record->chances;
-
- $getType = $record->get_type;
- if (!isset($stats['type_stats'][$getType])) {
- $stats['type_stats'][$getType] = [
- 'type' => $getType,
- 'type_text' => LotteryEnum::getChanceGetTypeText($getType),
- 'count' => 0,
- 'chances' => 0,
- ];
- }
-
- $stats['type_stats'][$getType]['count']++;
- $stats['type_stats'][$getType]['chances'] += $record->chances;
- }
-
- return $stats;
- }
- /**
- * 获取用户机会获取统计
- *
- * @param int $userId 用户ID
- * @param int $activityId 活动ID(可选)
- * @return array
- */
- public static function getUserChanceStats($userId, $activityId = null)
- {
- $query = self::where('user_id', $userId);
-
- if ($activityId) {
- $query->where('activity_id', $activityId);
- }
-
- $records = $query->select();
-
- $stats = [
- 'total_records' => count($records),
- 'total_chances' => 0,
- 'type_stats' => [],
- 'recent_records' => [],
- ];
-
- foreach ($records as $record) {
- $stats['total_chances'] += $record->chances;
-
- $getType = $record->get_type;
- if (!isset($stats['type_stats'][$getType])) {
- $stats['type_stats'][$getType] = [
- 'type' => $getType,
- 'type_text' => LotteryEnum::getChanceGetTypeText($getType),
- 'count' => 0,
- 'chances' => 0,
- ];
- }
-
- $stats['type_stats'][$getType]['count']++;
- $stats['type_stats'][$getType]['chances'] += $record->chances;
- }
-
- // 获取最近的记录
- $stats['recent_records'] = self::where('user_id', $userId)
- ->with(['activity'])
- ->order('get_time desc')
- ->limit(5)
- ->select();
-
- return $stats;
- }
- /**
- * 批量创建机会获取记录
- *
- * @param array $records 记录数组
- * @return bool
- */
- public static function batchCreateRecords($records)
- {
- if (empty($records)) {
- return false;
- }
-
- // 为每条记录添加创建时间
- foreach ($records as &$record) {
- $record['createtime'] = time();
- $record['get_time'] = $record['get_time'] ?? time();
- }
-
- return self::insertAll($records);
- }
- /**
- * 验证记录数据
- *
- * @param array $data 记录数据
- * @return bool|string true表示验证通过,string表示错误信息
- */
- public static function validateRecord($data)
- {
- // 验证必填字段
- if (empty($data['activity_id'])) {
- return '活动ID不能为空';
- }
-
- if (empty($data['user_id'])) {
- return '用户ID不能为空';
- }
-
- if (empty($data['get_type'])) {
- return '获取类型不能为空';
- }
-
- if (empty($data['chances']) || $data['chances'] <= 0) {
- return '机会次数必须大于0';
- }
-
- // 验证获取类型是否有效
- if (!LotteryEnum::isValidChanceGetType($data['get_type'])) {
- return '无效的获取类型';
- }
-
- // 根据获取类型验证相关字段
- switch ($data['get_type']) {
- case LotteryEnum::CHANCE_GET_TYPE_BUY_GOODS:
- case LotteryEnum::CHANCE_GET_TYPE_ORDER_AMOUNT:
- if (empty($data['order_id'])) {
- return '订单ID不能为空';
- }
- break;
-
- case LotteryEnum::CHANCE_GET_TYPE_RECHARGE:
- if (empty($data['recharge_amount']) || $data['recharge_amount'] <= 0) {
- return '充值金额必须大于0';
- }
- break;
-
- case LotteryEnum::CHANCE_GET_TYPE_ADMIN_GRANT:
- if (empty($data['admin_id'])) {
- return '管理员ID不能为空';
- }
- break;
- }
-
- return true;
- }
- }
|