MemberLevel.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2017/12/8
  5. * Time: 上午11:54
  6. */
  7. namespace app\frontend\modules\member\models;
  8. use app\common\models\Goods;
  9. class MemberLevel extends \app\common\models\MemberLevel
  10. {
  11. protected $hidden = ['uniacid'];
  12. /**
  13. * 获取会员等级信息
  14. * @return array $data 等级升级的信息
  15. *
  16. */
  17. public function getLevelData($type)
  18. {
  19. $content = 'order_money';
  20. if ($type == 1) {
  21. $content = 'order_count';
  22. } elseif ($type == 4) {
  23. $content = 'balance_recharge';
  24. }
  25. $data = self::select('id', 'level_name', 'discount', 'freight_reduction', $content, 'description')
  26. ->uniacid()
  27. ->orderBy('level')
  28. ->get()->toArray();
  29. return $data;
  30. }
  31. /**
  32. * 等级升级依据为购买指定商品
  33. * @return array $data 等级升级的信息
  34. *
  35. */
  36. public function getLevelGoods()
  37. {
  38. $data = self::select('id', 'level_name','goods_id', 'discount', 'freight_reduction', 'description')->uniacid()->orderBy('level')->get()->toArray();
  39. foreach ($data as $k => $v) {
  40. if ($v['goods_id']) {
  41. $goods_ids = array_unique(explode(',', $v['goods_id']));
  42. foreach ($goods_ids as $key => $value) {
  43. $goods = Goods::where('uniacid', \YunShop::app()->uniacid)->where('id', $value)->select(['id', 'thumb', 'price', 'title'])->first();
  44. $data[$k]['goods'][$key]['id'] = $goods['id'];
  45. $data[$k]['goods'][$key]['thumb'] = yz_tomedia($goods['thumb']);
  46. $data[$k]['goods'][$key]['price'] = $goods['price'];
  47. $data[$k]['goods'][$key]['title'] = $goods['title'];
  48. }
  49. }
  50. $data[$k]['goods'] = $data[$k]['goods'] ?: null;
  51. }
  52. return $data;
  53. }
  54. //模型关联 关联商品
  55. public function goods()
  56. {
  57. return $this->hasOne('app\common\models\Goods', 'id', 'goods_id');
  58. }
  59. //关联会员
  60. public function member()
  61. {
  62. return $this->hasMany('app\common\models\MemberShopInfo', 'level_id', 'id'); //注意yz_member数据表记录和关联的是member_level表的主键id, 而不是level值
  63. }
  64. }