StoreDiscounts.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\store\product;
  12. use app\common\model\BaseModel;
  13. use app\common\model\system\merchant\Merchant;
  14. class StoreDiscounts extends BaseModel
  15. {
  16. public static function tablePk(): string
  17. {
  18. return 'discount_id';
  19. }
  20. public static function tableName(): string
  21. {
  22. return 'store_discounts';
  23. }
  24. public function discountsProduct()
  25. {
  26. return $this->hasMany(StoreDiscountProduct::class, 'discount_id', 'discount_id');
  27. }
  28. public function merchant()
  29. {
  30. return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
  31. }
  32. public function getTimeAttr()
  33. {
  34. if ($this->is_time) {
  35. return [date('Y-m-d H:i:s',$this->start_time),date('Y-m-d H:i:s', $this->stop_time)];
  36. }
  37. return [];
  38. }
  39. public function searchTitleAttr($query, $value)
  40. {
  41. $query->whereLike('title', "%{$value}%");
  42. }
  43. public function searchMerIdAttr($query, $value)
  44. {
  45. $query->where('mer_id', $value);
  46. }
  47. public function searchStoreNameAttr($query, $value)
  48. {
  49. $id = StoreDiscountProduct::whereLike('store_name', "%{$value}%")->column('discount_id');
  50. $query->whereIn('discount_id', $id);
  51. }
  52. public function searchDiscountIdAttr($query, $value)
  53. {
  54. if (is_array($value)) {
  55. $query->whereIn('discount_id', $value);
  56. } else {
  57. $query->where('discount_id', $value);
  58. }
  59. }
  60. public function searchStatusAttr($query, $value)
  61. {
  62. $query->where('status', $value);
  63. }
  64. public function searchIsShowAttr($query, $value)
  65. {
  66. $query->where('is_show', $value);
  67. }
  68. public function searchIsDelAttr($query, $value)
  69. {
  70. $query->where('is_del', $value);
  71. }
  72. public function searchTypeAttr($query, $value)
  73. {
  74. $query->where('type', $value);
  75. }
  76. public function searchEndTimeAttr($query, $value)
  77. {
  78. $query->where(function ($query) {
  79. $query->where('is_time', 0)->whereOr(function ($query) {
  80. $query->where('is_time', 1)->where('start_time', '<', time())->where('stop_time', '>', time());
  81. });
  82. });
  83. }
  84. }