Goods.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2017/3/31
  6. * Time: 下午5:55
  7. */
  8. namespace app\frontend\models;
  9. use app\framework\Database\Eloquent\Collection;
  10. use app\frontend\models\goods\Privilege;
  11. use app\frontend\models\goods\Sale;
  12. use app\common\models\Coupon;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Yunshop\StoreCashier\common\models\StoreGoods;
  15. use Yunshop\Supplier\admin\models\SupplierGoods;
  16. /**
  17. * Class Goods
  18. * @package app\frontend\models
  19. * @property int id
  20. * @property string goods_sn
  21. * @property string title
  22. * @property string thumb
  23. * @property float price
  24. * @property float weight
  25. * @property int is_plugin
  26. * @property int plugin_id
  27. * @property Sale hasOneSale
  28. * @property GoodsOption has_option
  29. * @property Privilege hasOnePrivilege
  30. * @property SupplierGoods supplierGoods
  31. * @property StoreGoods storeGoods
  32. * @property Collection belongsToCategorys
  33. * @method static self search(array $search)
  34. */
  35. class Goods extends \app\common\models\Goods
  36. {
  37. public $hidden = ['content', 'description'];
  38. public $appends = ['vip_price','next_level_price'];
  39. //todo 为什么要获取单体商品规格???
  40. public function hasOneOptions()
  41. {
  42. return $this->hasOne(GoodsOption::class);
  43. }
  44. public function hasOneSale()
  45. {
  46. return $this->hasOne(Sale::class);
  47. }
  48. /**
  49. * @param Builder $query
  50. * @param $filters
  51. */
  52. public function scopeSearch(Builder $query, $filters)
  53. {
  54. $query->uniacid();
  55. if (!$filters) {
  56. return;
  57. }
  58. foreach ($filters as $key => $value) {
  59. switch ($key) {
  60. /*case 'category':
  61. $category[] = ['id' => $value * 1];
  62. $query->with("")->where('category_id', $category);
  63. break;*/
  64. case 'plugin_id':
  65. $query->where('plugin_id', $value);
  66. break;
  67. case 'keyword':
  68. $query->where('title', 'LIKE', "%{$value}%");
  69. break;
  70. case 'brand_id':
  71. $query->where('brand_id', $value);
  72. break;
  73. case 'product_attr':
  74. foreach ($value as $attr) {
  75. $query->where($attr, 1);
  76. }
  77. break;
  78. case 'status':
  79. $query->where('status', $value);
  80. break;
  81. case 'min_price':
  82. $query->where('price', '>', $value);
  83. break;
  84. case 'max_price':
  85. $query->where('price', '<', $value);
  86. break;
  87. case 'category':
  88. if (array_key_exists('parentid', $value) || array_key_exists('childid', $value) || array_key_exists('thirdid', $value)) {
  89. $id = $value['parentid'] ? $value['parentid'] : '';
  90. $id = $value['childid'] ? $value['childid'] : $id;
  91. $id = $value['thirdid'] ? $value['thirdid'] : $id;
  92. $query->select([
  93. 'yz_goods.*',
  94. 'yz_goods_category.id as goods_category_id',
  95. 'yz_goods_category.goods_id as goods_id',
  96. 'yz_goods_category.category_id as category_id',
  97. 'yz_goods_category.category_ids as category_ids'
  98. ])->join('yz_goods_category', 'yz_goods_category.goods_id', '=', 'yz_goods.id')->whereRaw('FIND_IN_SET(?,category_ids)', [$id]);
  99. } elseif (strpos($value, ',')) {
  100. $scope = explode(',', $value);
  101. $query->select([
  102. 'yz_goods.*',
  103. 'yz_goods_category.id as goods_category_id',
  104. 'yz_goods_category.goods_id as goods_id',
  105. 'yz_goods_category.category_id as category_id',
  106. 'yz_goods_category.category_ids as category_ids'
  107. ])->join('yz_goods_category', function ($join) use ($scope) {
  108. $join->on('yz_goods_category.goods_id', '=', 'yz_goods.id')
  109. ->whereIn('yz_goods_category.category_id', $scope);
  110. });
  111. } else {
  112. $query->select([
  113. 'yz_goods.*',
  114. 'yz_goods_category.id as goods_category_id',
  115. 'yz_goods_category.goods_id as goods_id',
  116. 'yz_goods_category.category_id as category_id',
  117. 'yz_goods_category.category_ids as category_ids'
  118. ])->join('yz_goods_category', function ($join) use ($value) {
  119. $join->on('yz_goods_category.goods_id', '=', 'yz_goods.id')
  120. ->whereRaw('FIND_IN_SET(?,category_ids)', [$value]);
  121. // ->where('yz_goods_category.category_id', $value);
  122. });
  123. }
  124. break;
  125. case 'couponid': //搜索指定优惠券适用的商品
  126. $res = Coupon::getApplicableScope($value);
  127. switch ($res['type']) {
  128. case Coupon::COUPON_GOODS_USE: //优惠券适用于指定商品
  129. if (is_array($res['scope'])) {
  130. $query->whereIn('id', $res['scope']);
  131. } else {
  132. $query->where('id', $res['scope']);
  133. }
  134. break;
  135. case Coupon::COUPON_CATEGORY_USE: //优惠券适用于指定商品分类
  136. if (is_array($res['scope'])) {
  137. $query->join('yz_goods_category', function ($join) use ($res) {
  138. $join->on('yz_goods_category.goods_id', '=', 'yz_goods.id')
  139. ->whereIn('yz_goods_category.category_id', $res['scope']);
  140. });
  141. } else {
  142. $query->join('yz_goods_category', function ($join) use ($res) {
  143. $join->on('yz_goods_category.goods_id', '=', 'yz_goods.id')
  144. ->where('yz_goods_category.category_id', $res['scope']);
  145. });
  146. }
  147. break;
  148. default: //优惠券适用于整个商城
  149. break;
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. }
  157. public function getVipLevelStatusAttribute()
  158. {
  159. $vip_status = [
  160. 'status' => 0,
  161. 'word' => '',
  162. 'tips' => ''
  163. ];
  164. if (!app('plugins')->isEnabled('price-authority')) {
  165. return $vip_status;
  166. }
  167. //查询会员等级
  168. $level = MemberShopInfo::select('level_id')->where('member_id',\YunShop::app()->getMemberId())->first();
  169. $set = \Setting::get('plugin.price_authority');
  170. if (!empty($set['is_jurisdiction']) && $set['is_jurisdiction']){
  171. $goods = \Yunshop\PriceAuthority\model\Goods::find($this->id);
  172. if (($goods->plugin_id == 0 || $goods->plugin_id == 92)) {
  173. if ($set['supplier_vip_level'] == '' || empty($set['supplier_vip_level'])){
  174. $vip_status = [
  175. 'status' => 0,
  176. 'word' => '',
  177. 'tips' => ''
  178. ];
  179. }else if (!in_array($level->level_id, $set['supplier_vip_level'])) {
  180. $level_name = '';
  181. foreach ($set['supplier_vip_level'] as $item) {
  182. $level = MemberLevel::find($item);
  183. if (!$level) {
  184. $level->level_name = '普通会员';
  185. }
  186. $level_name .= '/' . $level->level_name;
  187. }
  188. $vip_status['status'] = 1;
  189. $vip_status['word'] = $set['supplier_jurisdiction_word'] ?: '无权限';
  190. $vip_status['tips'] = '该商品仅限' . $level_name . '等级购买';
  191. }
  192. }
  193. if (($goods->plugin_id == 31 || $goods->plugin_id == 32)){
  194. if ($set['store_vip_level'] == ''){
  195. $vip_status = [
  196. 'status' => 0,
  197. 'word' => '',
  198. 'tips' => ''
  199. ];
  200. }else if (!in_array($level->level_id, $set['store_vip_level'])){
  201. $level_name = '';
  202. foreach ($set['store_vip_level'] as $item){
  203. $level = MemberLevel::find($item);
  204. if (!$level){
  205. $level->level_name = '普通会员';
  206. }
  207. $level_name .= '/'.$level->level_name ;
  208. }
  209. $vip_status['status'] = 1;
  210. $vip_status['word'] = $set['store_jurisdiction_word'] ?: '无权限';
  211. $vip_status['tips'] = '该商品仅限'.$level_name.'等级购买';
  212. }
  213. }
  214. }
  215. return ($vip_status);
  216. }
  217. }