查看: 206|回复: 0

[fastadmin] fastadmin多城市或者查询方式hasmany

[复制链接]

759

主题

39

回帖

3976

积分

管理员

总舵主

积分
3976
QQ
发表于 2026-10-15 00:08:47 | 显示全部楼层 |阅读模式
代码示例:

  1. // 推荐商品
  2.         $pindex = max(1, intval($params['page']));
  3.         $psize = 6;
  4.         $pstart = ($pindex - 1) * $psize;
  5.         $goodslist = Goods::with('prices')->where($where)->where(['status'=>1,'recommend'=>1])->order('weigh', 'desc')->order($pstart, $psize)->select();
  6.         if (!empty($goodslist)) {
  7.             // 遍历
  8.             foreach ($goodslist as &$row) {
  9.                 // 差异价
  10.                 if ($this->diypage->groupprice == 1) {
  11.                     $where['group_id'] = $group_id;
  12.                 }
  13.                 // 开启多城市
  14.                 if ($this->diypage->multiplecity == 1) {
  15.                     // 市级运营
  16.                     if ($this->diypage->citytype == 1) {
  17.                         \think\Log::record('市级运营');
  18.                         $where['pcity_id'] = intval($params['pcity_id']);
  19.                         $where['ccity_id'] = intval($params['ccity_id']);
  20.                         if ($where['pcity_id'] > 0 && $where['ccity_id'] > 0) {
  21.                             // 多规格差异价
  22.                             if ($row->hasoption == 1) {
  23.                                 // 商品规格
  24.                                 $options = $row->prices()->where('status', 1)
  25.                                     ->where(function($query) use ($where) {
  26.                                         $query->where(function($q) use ($where) {
  27.                                             $q->where($where);
  28.                                         })
  29.                                         ->whereOr(function($q) {
  30.                                             $q->where(['pcity_id'=>0,'ccity_id'=>0]);
  31.                                         });
  32.                                     })->fetchSql(true)->select();
  33.                                 // \think\Log::record($options);
  34.                                 if (!empty($options)) {
  35.                                     $row->saleprice = $row->prices()->where('status', 1)
  36.                                     ->where(function($query) use ($where) {
  37.                                         $query->where(function($q) use ($where) {
  38.                                             $q->where($where);
  39.                                         })
  40.                                         ->whereOr(function($q) {
  41.                                             $q->where(['pcity_id'=>0,'ccity_id'=>0]);
  42.                                         });
  43.                                     })->min('saleprice');
  44.                                 }
  45.                             } else {// 无规格差异价
  46.                                 // 城市价
  47.                                 $goodsprice = $row->prices()->where('status', 1)
  48.                                     ->where(function($query) use ($where) {
  49.                                         $query->where(function($q) use ($where) {
  50.                                             $q->where($where);
  51.                                         })
  52.                                         ->whereOr(function($q) {
  53.                                             $q->where(['pcity_id'=>0,'ccity_id'=>0]);
  54.                                         });
  55.                                     })->find();
  56.                                 if (!empty($goodsprice)) {
  57.                                     $row->saleprice = $goodsprice->saleprice;
  58.                                 }
  59.                             }
  60.                         } else {
  61.                            
  62.                         }
  63.                     } else {// 县级运营
  64.                         // \think\Log::record('县级运营');
  65.                         $where['pcity_id'] = $params['pcity_id'];
  66.                         $where['ccity_id'] = $params['ccity_id'];
  67.                         $where['scity_id'] = $params['scity_id'];
  68.                         if ($where['pcity_id'] > 0 && $where['ccity_id'] > 0 && $where['scity_id'] > 0) {
  69.                             // 多规格差异价
  70.                             if ($row->hasoption == 1) {
  71.                                 // 商品规格
  72.                                 $options = $row->prices()->where('status', 1)
  73.                                     ->where(function($query) use ($where) {
  74.                                         $query->where(function($q) use ($where) {
  75.                                             $q->where($where);
  76.                                         })
  77.                                         ->whereOr(function($q) {
  78.                                             $q->where(['pcity_id'=>0,'ccity_id'=>0,'scity_id'=>0]);
  79.                                         });
  80.                                     })->select();
  81.                                 if (!empty($options)) {
  82.                                     $row->saleprice = $row->prices()->where('status', 1)
  83.                                     ->where(function($query) use ($where) {
  84.                                         $query->where(function($q) use ($where) {
  85.                                             $q->where($where);
  86.                                         })
  87.                                         ->whereOr(function($q) {
  88.                                             $q->where(['pcity_id'=>0,'ccity_id'=>0,'scity_id'=>0]);
  89.                                         });
  90.                                     })->min('saleprice');
  91.                                 }
  92.                             } else {// 无规格差异价
  93.                                 // 城市价
  94.                                 $goodsprice = $row->prices()->where('status', 1)
  95.                                     ->where(function($query) use ($where) {
  96.                                         $query->where(function($q) use ($where) {
  97.                                             $q->where($where);
  98.                                         })
  99.                                         ->whereOr(function($q) {
  100.                                             $q->where(['pcity_id'=>0,'ccity_id'=>0,'scity_id'=>0]);
  101.                                         });
  102.                                     })->find();
  103.                                 // \think\Log::record($goodsprice);
  104.                                 if (!empty($goodsprice)) {
  105.                                     $row->saleprice = $goodsprice->saleprice;
  106.                                 }
  107.                             }
  108.                         } else {
  109.                            
  110.                         }
  111.                     }
  112.                 } else {// 未开启多城市
  113.                     // 商品ID
  114.                     $where['goods_id'] = $row->id;
  115.                     // 多规格差异价
  116.                     if ($row->hasoption == 1) {
  117.                         // 商品规格
  118.                         $options = $row->prices()->where($where)->select();
  119.                         if (!empty($options)) {
  120.                             $row->saleprice = $row->prices()->where($where)->min('saleprice');
  121.                         }
  122.                     } else {// 无规格差异价
  123.                         // 城市价
  124.                         $goodsprice = $row->prices()->where($where)->find();
  125.                         if (!empty($goodsprice)) {
  126.                             $row->saleprice = $goodsprice->saleprice;
  127.                         }
  128.                     }
  129.                 }
  130.             }
  131.             unset($row);
  132.             $return['goodslist'] = $goodslist;
  133.             $return['page'] = intval($params['page']) + 1;
  134.         }
复制代码
模型:
  1. public function prices()
  2.     {
  3.         return $this->hasMany('GoodsPrice', 'goods_id', 'id');
  4.     }
复制代码



道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部