LotteryDrawRecord.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. use app\common\Enum\LotteryEnum;
  5. /**
  6. * 抽奖记录模型
  7. */
  8. class LotteryDrawRecord extends Model
  9. {
  10. // 表名
  11. protected $table = 'shop_lottery_draw_record';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = false;
  17. protected $deleteTime = false;
  18. // 追加属性
  19. protected $append = [
  20. 'trigger_type_text',
  21. 'win_info_data',
  22. 'status_text'
  23. ];
  24. /**
  25. * 关联活动
  26. */
  27. public function activity()
  28. {
  29. return $this->belongsTo('LotteryActivity', 'activity_id');
  30. }
  31. /**
  32. * 关联用户
  33. */
  34. public function user()
  35. {
  36. return $this->belongsTo('app\common\model\User', 'user_id');
  37. }
  38. /**
  39. * 关联奖品
  40. */
  41. public function prize()
  42. {
  43. return $this->belongsTo('LotteryPrize', 'prize_id');
  44. }
  45. /**
  46. * 关联中奖记录
  47. */
  48. public function winRecord()
  49. {
  50. return $this->hasOne('LotteryWinRecord', 'draw_record_id');
  51. }
  52. /**
  53. * 关联订单
  54. */
  55. public function order()
  56. {
  57. return $this->belongsTo('app\common\model\Order', 'trigger_order_id');
  58. }
  59. /**
  60. * 获取触发类型文本
  61. */
  62. public function getTriggerTypeTextAttr($value, $data)
  63. {
  64. $types = LotteryEnum::getTriggerTypeMap();
  65. return isset($types[$data['trigger_type']]) ? $types[$data['trigger_type']] : '未知';
  66. }
  67. /**
  68. * 获取中奖信息数据
  69. */
  70. public function getWinInfoDataAttr($value, $data)
  71. {
  72. return !empty($data['win_info']) ? json_decode($data['win_info'], true) : [];
  73. }
  74. /**
  75. * 设置中奖信息
  76. */
  77. public function setWinInfoAttr($value)
  78. {
  79. return is_array($value) ? json_encode($value) : $value;
  80. }
  81. /**
  82. * 获取状态文本
  83. */
  84. public function getStatusTextAttr($value, $data)
  85. {
  86. return LotteryEnum::getDrawStatusText($data['status']);
  87. }
  88. }