| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace App\Module\Point\Models;
- use App\Module\Point\Enums\POINT_TYPE;
- use App\Module\Point\Enums\EXTERNAL_STATUS;
- use UCore\ModelCore;
- /**
- * 积分订单表
- *
- * 记录积分相关的订单信息,如积分兑换、积分消费等
- * 专注于整数积分处理,不涉及小数运算
- *
- * field start
- * @property int $id 自增
- * @property string $order_no 订单号
- * @property int $user_id 用户ID
- * @property \App\Module\Point\Enums\POINT_TYPE $point_id 积分类型ID
- * @property int $amount 积分数量
- * @property string $order_type 订单类型
- * @property string $title 订单标题
- * @property string $description 订单描述
- * @property \App\Module\Point\Enums\EXTERNAL_STATUS $status 订单状态
- * @property array $extra_data 额外数据(JSON格式)
- * @property int $create_time 创建时间
- * @property int $update_time 更新时间
- * field end
- */
- class PointOrderModel extends ModelCore
- {
- protected $table = 'point_order';
- // attrlist start
- protected $fillable = [
- 'id',
- 'order_no',
- 'user_id',
- 'point_id',
- 'amount',
- 'order_type',
- 'title',
- 'description',
- 'status',
- 'extra_data',
- 'create_time',
- 'update_time',
- ];
- // attrlist end
- public $timestamps = false;
- protected $casts = [
- 'point_id' => POINT_TYPE::class,
- 'status' => EXTERNAL_STATUS::class,
- 'extra_data' => 'array',
- ];
- // 订单类型常量
- const TYPE_EXCHANGE = 'exchange'; // 积分兑换
- const TYPE_CONSUME = 'consume'; // 积分消费
- const TYPE_REWARD = 'reward'; // 积分奖励
- const TYPE_REFUND = 'refund'; // 积分退款
- /**
- * 创建积分订单
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @param int $amount 积分数量
- * @param string $orderType 订单类型
- * @param string $title 订单标题
- * @param string $description 订单描述
- * @param array $extraData 额外数据
- * @return PointOrderModel|false 成功返回模型,失败返回false
- */
- public static function createOrder(
- int $userId,
- int $pointId,
- int $amount,
- string $orderType,
- string $title,
- string $description = '',
- array $extraData = []
- ) {
- $order = new self();
- $order->order_no = self::generateOrderNo();
- $order->user_id = $userId;
- $order->point_id = $pointId;
- $order->amount = $amount;
- $order->order_type = $orderType;
- $order->title = $title;
- $order->description = $description;
- $order->status = EXTERNAL_STATUS::PENDING;
- $order->extra_data = $extraData;
- $order->create_time = time();
- $order->update_time = time();
- if ($order->save()) {
- return $order;
- }
- return false;
- }
- /**
- * 生成订单号
- *
- * @return string 订单号
- */
- private static function generateOrderNo(): string
- {
- return 'PO' . date('YmdHis') . mt_rand(1000, 9999);
- }
- /**
- * 根据订单号获取订单
- *
- * @param string $orderNo 订单号
- * @return PointOrderModel|null 订单模型
- */
- public static function getByOrderNo(string $orderNo): ?PointOrderModel
- {
- return self::where('order_no', $orderNo)->first();
- }
- /**
- * 更新订单状态
- *
- * @param EXTERNAL_STATUS $status 状态
- * @return bool 是否成功
- */
- public function updateStatus(EXTERNAL_STATUS $status): bool
- {
- $this->status = $status;
- $this->update_time = time();
- return $this->save();
- }
- /**
- * 标记为已完成
- *
- * @return bool 是否成功
- */
- public function markCompleted(): bool
- {
- return $this->updateStatus(EXTERNAL_STATUS::COMPLETED);
- }
- /**
- * 标记为已失败
- *
- * @return bool 是否成功
- */
- public function markFailed(): bool
- {
- return $this->updateStatus(EXTERNAL_STATUS::FAILED);
- }
- /**
- * 标记为已取消
- *
- * @return bool 是否成功
- */
- public function markCancelled(): bool
- {
- return $this->updateStatus(EXTERNAL_STATUS::CANCELLED);
- }
- /**
- * 获取用户订单
- *
- * @param int $userId 用户ID
- * @param int $limit 限制数量
- * @return \Illuminate\Database\Eloquent\Collection 订单集合
- */
- public static function getUserOrders(int $userId, int $limit = 50)
- {
- return self::where('user_id', $userId)
- ->orderBy('create_time', 'desc')
- ->limit($limit)
- ->get();
- }
- /**
- * 获取指定类型的订单
- *
- * @param int $userId 用户ID
- * @param string $orderType 订单类型
- * @param int $limit 限制数量
- * @return \Illuminate\Database\Eloquent\Collection 订单集合
- */
- public static function getUserOrdersByType(int $userId, string $orderType, int $limit = 50)
- {
- return self::where('user_id', $userId)
- ->where('order_type', $orderType)
- ->orderBy('create_time', 'desc')
- ->limit($limit)
- ->get();
- }
- /**
- * 获取订单类型名称
- *
- * @return string 订单类型名称
- */
- public function getOrderTypeName(): string
- {
- return match($this->order_type) {
- self::TYPE_EXCHANGE => '积分兑换',
- self::TYPE_CONSUME => '积分消费',
- self::TYPE_REWARD => '积分奖励',
- self::TYPE_REFUND => '积分退款',
- default => '未知类型',
- };
- }
- /**
- * 获取状态名称
- *
- * @return string 状态名称
- */
- public function getStatusName(): string
- {
- return $this->status->getStatusName();
- }
- /**
- * 判断是否为最终状态
- *
- * @return bool 是否为最终状态
- */
- public function isFinalStatus(): bool
- {
- return $this->status->isFinalStatus();
- }
- /**
- * 获取积分类型名称
- *
- * @return string 积分类型名称
- */
- public function getPointTypeName(): string
- {
- return $this->point_id->getTypeName();
- }
- /**
- * 判断是否为积分支出订单
- *
- * @return bool 是否为支出订单
- */
- public function isExpenseOrder(): bool
- {
- return in_array($this->order_type, [self::TYPE_EXCHANGE, self::TYPE_CONSUME]);
- }
- /**
- * 判断是否为积分收入订单
- *
- * @return bool 是否为收入订单
- */
- public function isIncomeOrder(): bool
- {
- return in_array($this->order_type, [self::TYPE_REWARD, self::TYPE_REFUND]);
- }
- }
|