StoreBargain.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\activity\bargain;
  12. use app\model\product\product\StoreDescription;
  13. use app\model\product\product\StoreProduct;
  14. use crmeb\basic\BaseModel;
  15. use crmeb\traits\ModelTrait;
  16. use think\Model;
  17. /**
  18. * TODO 砍价商品Model
  19. * Class StoreBargain
  20. * @package app\model\activity
  21. */
  22. class StoreBargain extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'store_bargain';
  34. use ModelTrait;
  35. public function getImagesAttr($value)
  36. {
  37. return json_decode($value, true) ?? [];
  38. }
  39. /**
  40. * 一对一关联
  41. * 商品关联商品商品详情
  42. * @return \think\model\relation\HasOne
  43. */
  44. public function description()
  45. {
  46. return $this->hasOne(StoreDescription::class, 'product_id', 'id')->where('type', 2)->bind(['description']);
  47. }
  48. /**
  49. * 原价
  50. * @return \think\model\relation\HasOne
  51. */
  52. public function product()
  53. {
  54. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field(['id', 'ot_price','cate_id'])->bind([
  55. 'ot_price'=>'ot_price',
  56. 'cate_id'=>'cate_id'
  57. ]);
  58. }
  59. /**
  60. * 添加时间获取器
  61. * @param $value
  62. * @return false|string
  63. */
  64. protected function getAddTimeAttr($value)
  65. {
  66. if ($value) return date('Y-m-d H:i:s', (int)$value);
  67. return '';
  68. }
  69. /**
  70. * 砍价商品名称搜索器
  71. * @param Model $query
  72. * @param $value
  73. * @param $data
  74. */
  75. public function searchStoreNameAttr($query, $value, $data)
  76. {
  77. if ($value != '') $query->where('title|id', 'like', '%' . $value . '%');
  78. }
  79. /**
  80. * 状态搜索器
  81. * @param Model $query
  82. * @param $value
  83. * @param $data
  84. */
  85. public function searchStatusAttr($query, $value, $data)
  86. {
  87. if ($value != '') $query->where('status', $value ?? 1);
  88. }
  89. /**
  90. * 是否推荐搜索器
  91. * @param Model $query
  92. * @param $value
  93. * @param $data
  94. */
  95. public function searchIsHotAttr($query, $value, $data)
  96. {
  97. $query->where('is_hot', $value ?? 0);
  98. }
  99. /**
  100. * 是否删除搜索器
  101. * @param Model $query
  102. * @param $value
  103. * @param $data
  104. */
  105. public function searchIsDelAttr($query, $value, $data)
  106. {
  107. $query->where('is_del', $value ?? 0);
  108. }
  109. /**
  110. * 商品ID搜索器
  111. * @param Model $query
  112. * @param $value
  113. * @param $data
  114. */
  115. public function searchProductIdAttr($query, $value, $data)
  116. {
  117. if ($value) {
  118. if (is_array($value)) {
  119. $query->whereIn('product_id', $value);
  120. } else {
  121. $query->where('product_id', $value);
  122. }
  123. }
  124. }
  125. /**
  126. * 活动有效时间搜索器
  127. * @param $query
  128. * @param $value
  129. */
  130. public function searchBargainTimeAttr($query, $value)
  131. {
  132. if ($value == 1) {
  133. $time = time();
  134. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time);
  135. }
  136. }
  137. }