StoreBranchProduct.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\product\branch;
  12. use app\model\product\product\StoreProduct;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\ModelTrait;
  15. use think\Model;
  16. /**
  17. * 门店商品
  18. * Class StoreBranchProduct
  19. * @package app\model\product\branch
  20. */
  21. class StoreBranchProduct extends BaseModel
  22. {
  23. use ModelTrait;
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'store_branch_product';
  34. /**
  35. * @return \think\model\relation\HasOne
  36. */
  37. public function product()
  38. {
  39. return $this->hasOne(StoreProduct::class, 'id', 'product_id');
  40. }
  41. /**
  42. * 库存搜索器
  43. * @param Model $query
  44. * @param int $value
  45. */
  46. public function searchStockAttr($query, $value)
  47. {
  48. $query->where('stock', $value);
  49. }
  50. /**
  51. * is_del搜索器
  52. * @param $query
  53. * @param $value
  54. */
  55. public function searchIsDelAttr($query, $value)
  56. {
  57. $query->where('is_del', $value);
  58. }
  59. /**
  60. * product_id搜索器
  61. * @param $query
  62. * @param $value
  63. */
  64. public function searchProductIdAttr($query, $value)
  65. {
  66. if (is_array($value)) {
  67. if ($value) $query->whereIn('product_id', $value);
  68. } else {
  69. if ($value !== '') $query->where('product_id', $value);
  70. }
  71. }
  72. /**
  73. * is_show搜索器
  74. * @param $query
  75. * @param $value
  76. */
  77. public function searchIsShowAttr($query, $value)
  78. {
  79. if ($value !== '') $query->where('is_show', $value);
  80. }
  81. /**
  82. * store_id
  83. * @param $query
  84. * @param $value
  85. */
  86. public function searchStoreIdAttr($query, $value)
  87. {
  88. if ($value !== '') $query->where('store_id', $value);
  89. }
  90. /**
  91. * 商品数量条件搜索器
  92. * @param Model $query
  93. * @param $value
  94. * @param $data
  95. */
  96. public function searchTypeAttr($query, $value, $data)
  97. {
  98. switch ((int)$value) {
  99. case 1:
  100. $query->where(['is_del' => 0])->where('is_show', 1);
  101. break;
  102. case 2:
  103. $query->where(['is_del' => 0])->where('is_show', 0);
  104. break;
  105. case 3:
  106. $query->where(['is_del' => 0]);
  107. break;
  108. case 4:
  109. $query->where(['is_del' => 0])->where(function ($query) {
  110. $query->whereIn('id', function ($query) {
  111. $query->name('store_branch_product_attr_value')->where('stock', 0)->where('type', 0)->field('product_id')->select();
  112. })->whereOr('stock', 0);
  113. });
  114. break;
  115. case 5:
  116. if (isset($data['store_stock']) && $data['store_stock']) {
  117. $store_stock = $data['store_stock'];
  118. $query->where(['is_del' => 0])->where('stock', '<=', $store_stock)->where('stock', '>', 0);
  119. } else {
  120. $query->where(['is_del' => 0])->where('stock', '>', 0);
  121. }
  122. break;
  123. case 6:
  124. $query->where(['is_del' => 1]);
  125. break;
  126. case 7:
  127. $query->where(function ($q) {
  128. $q->where(['is_del' => 1])->whereOr('is_show', 0);
  129. });
  130. break;
  131. };
  132. }
  133. }