LotteryPrize.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 LotteryPrize extends Model
  10. {
  11. use SoftDelete;
  12. // 表名
  13. protected $table = 'shop_lottery_prize';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. // 追加属性
  21. protected $append = [
  22. 'type_text',
  23. 'deliver_type_text',
  24. 'exchange_codes_list',
  25. 'used_codes_list'
  26. ];
  27. /**
  28. * 关联活动
  29. */
  30. public function activity()
  31. {
  32. return $this->belongsTo('LotteryActivity', 'activity_id');
  33. }
  34. /**
  35. * 关联商品
  36. */
  37. public function goods()
  38. {
  39. return $this->belongsTo('app\common\model\Goods', 'goods_id');
  40. }
  41. /**
  42. * 关联商品SKU
  43. */
  44. public function goodsSku()
  45. {
  46. return $this->belongsTo('app\common\model\Sku', 'goods_sku_id');
  47. }
  48. /**
  49. * 关联优惠券
  50. */
  51. public function coupon()
  52. {
  53. return $this->belongsTo('app\common\model\Coupon', 'coupon_id');
  54. }
  55. /**
  56. * 获取奖品类型文本
  57. */
  58. public function getTypeTextAttr($value, $data)
  59. {
  60. return LotteryEnum::getPrizeTypeText($data['type']);
  61. }
  62. /**
  63. * 获取发放方式文本
  64. */
  65. public function getDeliverTypeTextAttr($value, $data)
  66. {
  67. $types = LotteryEnum::getDeliverTypeMap();
  68. return isset($types[$value]) ? $types[$value] : '未知';
  69. }
  70. /**
  71. * 获取兑换码列表
  72. */
  73. public function getExchangeCodesListAttr($value, $data)
  74. {
  75. return !empty( $value ) ? json_decode( $value, true) : [];
  76. }
  77. /**
  78. * 设置兑换码列表
  79. */
  80. public function setExchangeCodesAttr($value)
  81. {
  82. return is_array($value) ? json_encode($value) : $value;
  83. }
  84. /**
  85. * 获取已使用兑换码列表
  86. */
  87. public function getUsedCodesListAttr($value, $data)
  88. {
  89. return !empty( $value ) ? json_decode( $value, true) : [];
  90. }
  91. /**
  92. * 设置已使用兑换码列表
  93. */
  94. public function setUsedCodesAttr($value)
  95. {
  96. return is_array($value) ? json_encode($value) : $value;
  97. }
  98. }