StoreCombination.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\combination;
  12. use app\model\product\product\StoreDescription;
  13. use app\model\product\product\StoreProduct;
  14. use crmeb\traits\ModelTrait;
  15. use crmeb\basic\BaseModel;
  16. use think\Model;
  17. /**
  18. * TODO 拼团商品Model
  19. * Class StoreCombination
  20. * @package app\model\activity
  21. */
  22. class StoreCombination extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'store_combination';
  34. use ModelTrait;
  35. /**
  36. * 一对一获取原价
  37. * @return \think\model\relation\HasOne
  38. */
  39. public function getPrice()
  40. {
  41. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->bind(['ot_price', 'product_price' => 'price']);
  42. }
  43. /**
  44. * 一对一获取商品分类
  45. * @return \think\model\relation\HasOne
  46. */
  47. public function getCategory()
  48. {
  49. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->bind(['cate_id']);
  50. }
  51. /**
  52. * 一对一关联
  53. * 商品关联商品商品详情
  54. * @return \think\model\relation\HasOne
  55. */
  56. public function total()
  57. {
  58. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->where('is_show', 1)->where('is_del', 0)->field(['SUM(sales+ficti) as total', 'id', 'price'])->bind([
  59. 'total' => 'total', 'product_price' => 'price'
  60. ]);
  61. }
  62. /**
  63. * 一对一关联
  64. * 商品关联商品商品详情
  65. * @return \think\model\relation\HasOne
  66. */
  67. public function description()
  68. {
  69. return $this->hasOne(StoreDescription::class, 'product_id', 'id')->where('type', 3)->bind(['description']);
  70. }
  71. /**
  72. * 添加时间获取器
  73. * @param $value
  74. * @return false|string
  75. */
  76. protected function getAddTimeAttr($value)
  77. {
  78. if ($value) return date('Y-m-d H:i:s', (int)$value);
  79. return '';
  80. }
  81. /**
  82. * 轮播图获取器
  83. * @param $value
  84. * @return mixed
  85. */
  86. public function getImagesAttr($value)
  87. {
  88. return json_decode($value, true) ?? [];
  89. }
  90. /**
  91. * 拼团商品名称搜索器
  92. * @param Model $query
  93. * @param $value
  94. * @param $data
  95. */
  96. public function searchStoreNameAttr($query, $value, $data)
  97. {
  98. if ($value) $query->where('title|id', 'like', '%' . $value . '%');
  99. }
  100. /**
  101. * 是否推荐搜索器
  102. * @param Model $query
  103. * @param $value
  104. * @param $data
  105. */
  106. public function searchIsHostAttr($query, $value, $data)
  107. {
  108. $query->where('is_host', $value ?? 1);
  109. }
  110. /**
  111. * 状态搜索器
  112. * @param Model $query
  113. * @param $value
  114. * @param $data
  115. */
  116. public function searchIsShowAttr($query, $value, $data)
  117. {
  118. if ($value != '') $query->where('is_show', $value ?: 0);
  119. }
  120. /**
  121. * 是否删除搜索器
  122. * @param Model $query
  123. * @param $value
  124. * @param $data
  125. */
  126. public function searchIsDelAttr($query, $value, $data)
  127. {
  128. $query->where('is_del', $value ?? 0);
  129. }
  130. /**
  131. * 商品ID搜索器
  132. * @param Model $query
  133. * @param $value
  134. * @param $data
  135. */
  136. public function searchProductIdAttr($query, $value, $data)
  137. {
  138. if ($value) {
  139. if (is_array($value)) {
  140. $query->whereIn('product_id', $value);
  141. } else {
  142. $query->where('product_id', $value);
  143. }
  144. }
  145. }
  146. }