Activity.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\admin\model\lottery;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. class Activity extends Model
  6. {
  7. use SoftDelete;
  8. // 表名
  9. protected $table = 'shop_lottery_activity';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'integer';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = 'deletetime';
  16. // 追加属性
  17. protected $append = [
  18. 'type_text',
  19. 'status_text',
  20. 'lottery_type_text',
  21. 'user_limit_type_text',
  22. 'guide_style_text',
  23. 'start_time_text',
  24. 'end_time_text',
  25. 'lottery_time_text'
  26. ];
  27. /**
  28. * 活动类型列表
  29. */
  30. public function getTypeList()
  31. {
  32. return ['1' => __('消费抽奖')];
  33. }
  34. /**
  35. * 活动状态列表
  36. */
  37. public function getStatusList()
  38. {
  39. return [
  40. '0' => __('草稿'),
  41. '1' => __('进行中'),
  42. '2' => __('已结束'),
  43. '3' => __('已暂停')
  44. ];
  45. }
  46. /**
  47. * 开奖方式列表
  48. */
  49. public function getLotteryTypeList()
  50. {
  51. return [
  52. '1' => __('即抽即中'),
  53. '2' => __('按时间开奖'),
  54. '3' => __('按人数开奖')
  55. ];
  56. }
  57. /**
  58. * 适用人群类型列表
  59. */
  60. public function getUserLimitTypeList()
  61. {
  62. return [
  63. '1' => __('全部会员'),
  64. '2' => __('会员等级'),
  65. '3' => __('会员标签')
  66. ];
  67. }
  68. /**
  69. * 引导样式列表
  70. */
  71. public function getGuideStyleList()
  72. {
  73. return [
  74. '1' => __('默认样式'),
  75. '2' => __('自定义')
  76. ];
  77. }
  78. // 获取器
  79. public function getTypeTextAttr($value, $data)
  80. {
  81. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  82. $list = $this->getTypeList();
  83. return isset($list[$value]) ? $list[$value] : '';
  84. }
  85. public function getStatusTextAttr($value, $data)
  86. {
  87. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  88. $list = $this->getStatusList();
  89. return isset($list[$value]) ? $list[$value] : '';
  90. }
  91. public function getLotteryTypeTextAttr($value, $data)
  92. {
  93. $value = $value ? $value : (isset($data['lottery_type']) ? $data['lottery_type'] : '');
  94. $list = $this->getLotteryTypeList();
  95. return isset($list[$value]) ? $list[$value] : '';
  96. }
  97. public function getUserLimitTypeTextAttr($value, $data)
  98. {
  99. $value = $value ? $value : (isset($data['user_limit_type']) ? $data['user_limit_type'] : '');
  100. $list = $this->getUserLimitTypeList();
  101. return isset($list[$value]) ? $list[$value] : '';
  102. }
  103. public function getGuideStyleTextAttr($value, $data)
  104. {
  105. $value = $value ? $value : (isset($data['guide_style']) ? $data['guide_style'] : '');
  106. $list = $this->getGuideStyleList();
  107. return isset($list[$value]) ? $list[$value] : '';
  108. }
  109. public function getStartTimeTextAttr($value, $data)
  110. {
  111. $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
  112. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  113. }
  114. public function getEndTimeTextAttr($value, $data)
  115. {
  116. $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
  117. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  118. }
  119. public function getLotteryTimeTextAttr($value, $data)
  120. {
  121. $value = $value ? $value : (isset($data['lottery_time']) ? $data['lottery_time'] : '');
  122. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  123. }
  124. // 修改器
  125. protected function setStartTimeAttr($value)
  126. {
  127. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  128. }
  129. protected function setEndTimeAttr($value)
  130. {
  131. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  132. }
  133. protected function setLotteryTimeAttr($value)
  134. {
  135. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  136. }
  137. // 关联关系
  138. public function prize()
  139. {
  140. return $this->hasMany('LotteryPrize', 'activity_id', 'id');
  141. }
  142. public function condition()
  143. {
  144. return $this->hasMany('Condition', 'activity_id', 'id');
  145. }
  146. public function drawRecord()
  147. {
  148. return $this->hasMany('DrawRecord', 'activity_id', 'id');
  149. }
  150. public function winRecord()
  151. {
  152. return $this->hasMany('WinRecord', 'activity_id', 'id');
  153. }
  154. public function userChance()
  155. {
  156. return $this->hasMany('UserChance', 'activity_id', 'id');
  157. }
  158. public function statistics()
  159. {
  160. return $this->hasMany('Statistics', 'activity_id', 'id');
  161. }
  162. }