Active.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace addon\mall\app\model\active;
  12. use addon\mall\app\dict\active\ActiveDict;
  13. use core\base\BaseModel;
  14. use think\db\Query;
  15. /**
  16. * 营销活动模型
  17. */
  18. class Active extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'active_id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'mall_active';
  30. protected $type = [
  31. 'start_time' => 'timestamp',
  32. 'end_time' => 'timestamp',
  33. 'create_time' => 'timestamp',
  34. 'update_time' => 'timestamp',
  35. ];
  36. // 设置json类型字段
  37. protected $json = [ 'active_goods_info', 'active_value'];
  38. // 设置JSON数据返回数组
  39. protected $jsonAssoc = true;
  40. /**
  41. * 活动商品项
  42. * @return \think\model\relation\HasMany
  43. */
  44. public function activeGoods()
  45. {
  46. return $this->hasMany(ActiveGoods::class, 'active_id', 'active_id');
  47. }
  48. /**
  49. * 活动状态
  50. * @param $value
  51. * @param $data
  52. * @return mixed|string
  53. */
  54. public function getActiveStatusNameAttr($value, $data)
  55. {
  56. if (empty($data['active_status']))
  57. {
  58. return '';
  59. }
  60. return ActiveDict::getStatus()[$data['active_status']] ?? '';
  61. }
  62. /**
  63. * 活动类型
  64. * @param $value
  65. * @param $data
  66. * @return mixed|string
  67. */
  68. public function getActiveTypeNameAttr($value, $data)
  69. {
  70. if (empty($data['active_type']))
  71. {
  72. return '';
  73. }
  74. return ActiveDict::getType()[$data['active_type']] ?? '';
  75. }
  76. /**
  77. * 活动商品类型
  78. * @param $value
  79. * @param $data
  80. * @return mixed|string
  81. */
  82. public function getActiveGoodsTypeNameAttr($value, $data)
  83. {
  84. if (empty($data['active_goods_type']))
  85. {
  86. return '';
  87. }
  88. return ActiveDict::getGoodsType()[$data['active_goods_type']] ?? '';
  89. }
  90. /**
  91. * 活动类别
  92. * @param $value
  93. * @param $data
  94. * @return mixed|string
  95. */
  96. public function getActiveClassNameAttr($value, $data)
  97. {
  98. if (empty($data['active_class']))
  99. {
  100. return '';
  101. }
  102. return ActiveDict::getClass()[$data['active_class']] ?? '';
  103. }
  104. /**
  105. * 搜索器:标题
  106. * @param $value
  107. * @param $data
  108. */
  109. public function searchActiveNameAttr($query, $value, $data)
  110. {
  111. if ($value) {
  112. $query->where("active_name", 'like', '%'.$value.'%');
  113. }
  114. }
  115. /**
  116. * 搜索器:状态
  117. * @param $value
  118. * @param $data
  119. */
  120. public function searchActiveStatusAttr($query, $value, $data)
  121. {
  122. if ($value) {
  123. $query->where("active_status", '=', $value);
  124. }
  125. }
  126. /**
  127. * 活动结束时间搜索器
  128. * @param Query $query
  129. * @param $value
  130. * @param $data
  131. */
  132. public function searchEndTimeAttr(Query $query, $value, $data)
  133. {
  134. $start_time = empty($value[0]) ? 0 : strtotime($value[0]);
  135. $end_time = empty($value[1]) ? 0 : strtotime($value[1]);
  136. if ($start_time > 0 && $end_time > 0) {
  137. $query->whereBetweenTime('end_time', $start_time, $end_time);
  138. } else if ($start_time > 0 && $end_time == 0) {
  139. $query->where([['end_time', '>=', $start_time]]);
  140. } else if ($start_time == 0 && $end_time > 0) {
  141. $query->where([['end_time', '<=', $end_time]]);
  142. }
  143. }
  144. }