LotteryActivity.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. use app\common\Enum\LotteryEnum;
  6. /**
  7. * 抽奖活动模型
  8. */
  9. class LotteryActivity extends Model
  10. {
  11. use SoftDelete;
  12. // 表名
  13. protected $table = 'shop_lottery_activity';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. // 追加属性
  21. protected $append = [
  22. 'status_text',
  23. 'lottery_type_text',
  24. // 'user_limit_type_text'
  25. ];
  26. /**
  27. * 关联奖品
  28. */
  29. public function prizes()
  30. {
  31. return $this->hasMany('LotteryPrize', 'activity_id');
  32. }
  33. /**
  34. * 关联参与条件
  35. */
  36. public function conditions()
  37. {
  38. return $this->hasMany('LotteryCondition', 'activity_id');
  39. }
  40. /**
  41. * 关联抽奖记录
  42. */
  43. public function drawRecords()
  44. {
  45. return $this->hasMany('LotteryDrawRecord', 'activity_id');
  46. }
  47. /**
  48. * 关联中奖记录
  49. */
  50. public function winRecords()
  51. {
  52. return $this->hasMany('LotteryWinRecord', 'activity_id');
  53. }
  54. /**
  55. * 关联用户机会
  56. */
  57. public function userChances()
  58. {
  59. return $this->hasMany('LotteryUserChance', 'activity_id');
  60. }
  61. /**
  62. * 获取状态文本
  63. */
  64. public function getStatusTextAttr($value, $data)
  65. {
  66. return LotteryEnum::getActivityStatusText($data['status']);
  67. }
  68. /**
  69. * 获取开奖方式文本
  70. */
  71. public function getLotteryTypeTextAttr($value, $data)
  72. {
  73. return LotteryEnum::getLotteryTypeText($data['lottery_type']);
  74. }
  75. /**
  76. * 获取用户群体类型文本
  77. */
  78. // public function getUserLimitTypeTextAttr($value, $data)
  79. // {
  80. // $value = $value ?: ($data['user_limit_type'] ?? '');
  81. // $types = LotteryEnum::getUserLimitTypeMap();
  82. // return $types[$value];
  83. // }
  84. /**
  85. * 获取用户限制值(自动解析JSON)
  86. */
  87. public function getUserLimitValueAttr($value, $data)
  88. {
  89. return $value ? json_decode($value, true) : [];
  90. }
  91. /**
  92. * 设置用户限制值(自动转换JSON)
  93. */
  94. public function setUserLimitValueAttr($value)
  95. {
  96. return is_array($value) ? json_encode($value) : $value;
  97. }
  98. }