Goods.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\model\commission\CommissionGoods as CommissionGoodsModel;
  5. use app\common\model\Goods as GoodsModel;
  6. use think\Db;
  7. class Goods extends Backend
  8. {
  9. protected $model = null;
  10. protected $goodsModel;
  11. /**
  12. * 快速搜索时执行查找的字段
  13. */
  14. protected $searchFields = 'id,title,subtitle';
  15. /**
  16. * 是否是关联查询
  17. */
  18. protected $relationSearch = false;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new GoodsModel(); // 使用商品模型作为主模型,以便使用FastAdmin的标准功能
  23. $this->goodsModel = new GoodsModel();
  24. }
  25. public function index()
  26. {
  27. if (!$this->request->isAjax()) {
  28. return $this->view->fetch();
  29. }
  30. //如果发送的来源是 Selectpage,则转发到 Selectpage
  31. if ($this->request->request('keyField')) {
  32. return $this->selectpage();
  33. }
  34. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  35. $list = $this->model
  36. ->with('commission_goods')
  37. ->where($where)
  38. ->order($sort, $order)
  39. ->paginate($limit);
  40. $result = ['total' => $list->total(), 'rows' => $list->items()];
  41. return json($result);
  42. }
  43. /**
  44. * 详情
  45. *
  46. * @param $id
  47. */
  48. public function detail($id)
  49. {
  50. $goodsList = collection(GoodsModel::with(['commission_goods'])->whereIn('id', $id)->select())->each(function ($goods) {
  51. $goods->skus = $goods->skus;
  52. $goods->sku_prices = $goods->sku_prices;
  53. });
  54. $config = sheep_config('shop.commission');
  55. $this->success('分销商品详情', null, [
  56. 'goods' => $goodsList,
  57. 'config' => $config
  58. ]);
  59. }
  60. /**
  61. * 设置佣金(支持批量)
  62. *
  63. * @param $id
  64. */
  65. public function edit($id = null)
  66. {
  67. if (!$this->request->isAjax()) {
  68. return $this->view->fetch();
  69. }
  70. // 接受全部参数
  71. $params = $this->request->only(['status', 'self_rules', 'commission_order_status', 'commission_config', 'commission_rules']);
  72. $result = Db::transaction(function () use ($id, $params) {
  73. $count = 0;
  74. $ids = explode(',', $id);
  75. foreach ($ids as $goods_id) {
  76. if ($row = $this->model->get($goods_id)) {
  77. $row->save($params);
  78. } else {
  79. $model = new CommissionGoodsModel();
  80. $params['goods_id'] = $goods_id;
  81. $model->save($params);
  82. }
  83. $count++;
  84. }
  85. return $count;
  86. });
  87. if ($result) {
  88. $this->success('更新成功', null, $result);
  89. } else {
  90. $this->error('更新失败');
  91. }
  92. }
  93. }