123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\admin\controller\commission;
- use app\common\controller\Backend;
- use app\common\model\commission\CommissionGoods as CommissionGoodsModel;
- use app\common\model\Goods as GoodsModel;
- use think\Db;
- class Goods extends Backend
- {
- protected $model = null;
- protected $goodsModel;
-
- /**
- * 快速搜索时执行查找的字段
- */
- protected $searchFields = 'id,title,subtitle';
-
- /**
- * 是否是关联查询
- */
- protected $relationSearch = false;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new GoodsModel(); // 使用商品模型作为主模型,以便使用FastAdmin的标准功能
- $this->goodsModel = new GoodsModel();
- }
- public function index()
- {
- if (!$this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
-
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
-
- $list = $this->model
- ->with('commission_goods')
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
-
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 详情
- *
- * @param $id
- */
- public function detail($id)
- {
- $goodsList = collection(GoodsModel::with(['commission_goods'])->whereIn('id', $id)->select())->each(function ($goods) {
- $goods->skus = $goods->skus;
- $goods->sku_prices = $goods->sku_prices;
- });
- $config = sheep_config('shop.commission');
- $this->success('分销商品详情', null, [
- 'goods' => $goodsList,
- 'config' => $config
- ]);
- }
- /**
- * 设置佣金(支持批量)
- *
- * @param $id
- */
- public function edit($id = null)
- {
- if (!$this->request->isAjax()) {
- return $this->view->fetch();
- }
- // 接受全部参数
- $params = $this->request->only(['status', 'self_rules', 'commission_order_status', 'commission_config', 'commission_rules']);
- $result = Db::transaction(function () use ($id, $params) {
- $count = 0;
- $ids = explode(',', $id);
- foreach ($ids as $goods_id) {
- if ($row = $this->model->get($goods_id)) {
- $row->save($params);
- } else {
- $model = new CommissionGoodsModel();
- $params['goods_id'] = $goods_id;
- $model->save($params);
- }
- $count++;
- }
- return $count;
- });
- if ($result) {
- $this->success('更新成功', null, $result);
- } else {
- $this->error('更新失败');
- }
- }
- }
|