FullPieceController.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2018/5/18
  6. * Time: 下午17:28
  7. */
  8. namespace app\backend\modules\enoughReduce\controllers;
  9. use app\backend\modules\goods\models\Goods;
  10. use app\common\components\BaseController;
  11. use app\common\helpers\Url;
  12. class FullPieceController extends BaseController
  13. {
  14. public function index()
  15. {
  16. $setting = \Setting::get('shop.fullPieceNew');
  17. if ($setting && $setting['fullPiece']) {
  18. $goodsIds = [];
  19. array_walk_recursive(array_column($setting['fullPiece'],'goods'), function($value) use (&$goodsIds) {
  20. array_push($goodsIds, $value);
  21. });
  22. $goods = collect(Goods::uniacid()
  23. ->select('id','title','thumb')
  24. ->where('status',1)
  25. ->whereIn('id',$goodsIds)
  26. ->get()->toArray());
  27. foreach ($setting['fullPiece'] as &$fullPiece) {
  28. $fullPiece['goods'] = array_values($goods->whereIn('id',$fullPiece['goods'])->all());
  29. }
  30. unset($fullPiece);
  31. }
  32. return view('goods.enoughReduce.full_piece.index', [
  33. 'setting' => json_encode($setting),
  34. ])->render();
  35. }
  36. public function getGoods()
  37. {
  38. $keyword = request()->keyword;
  39. if (!$keyword) {
  40. return $this->errorJson('请输入查询条件');
  41. }
  42. $model = Goods::uniacid()
  43. ->select('id','title','thumb')
  44. ->where('status',1);
  45. $model->where(function ($query) use ($keyword) {
  46. $query->where('yz_goods.id',$keyword)
  47. ->orWhere('title','like','%'.$keyword.'%');
  48. });
  49. $model = $model->whereIn('plugin_id',[0])
  50. ->orderBy('id', 'desc')->paginate(20);//平台
  51. foreach ($model as $goods) {
  52. $goods->thumb = yz_tomedia($goods->thumb);
  53. }
  54. return $this->successJson('ok',$model->toArray());
  55. }
  56. public function store()
  57. {
  58. $setting = request()->setting;
  59. $data = [
  60. "open" => $setting['open'] ? true : false,
  61. "fullPiece" => $setting['fullPiece'] ? : [],
  62. ];
  63. $has_goods = [];
  64. foreach ($data['fullPiece'] as $k=>$fullPiece) {
  65. foreach ($fullPiece['rules'] as $rule) {
  66. if (empty($rule['enough']) || empty($rule['reduce'])) {
  67. return $this->errorJson('规则中的条件以及折扣必填');
  68. }
  69. if ($fullPiece['discount_type'] == 1 && ($rule['reduce'] < 0 || $rule['reduce'] > 10)) {
  70. return $this->errorJson('折扣必须大于0折小于10折');
  71. }
  72. }
  73. if (empty($fullPiece['goods']) ) {
  74. return $this->errorJson('规则必须选择商品');
  75. }
  76. $data['fullPiece'][$k]['goods'] = array_column($fullPiece['goods'],'id') ? : [];
  77. $intersect = array_intersect($has_goods,$data['fullPiece'][$k]['goods']);
  78. if (!empty($intersect)) {
  79. return $this->errorJson('一个商品不能设置在两个优惠规则中');
  80. }
  81. $has_goods = array_merge($has_goods,$data['fullPiece'][$k]['goods']);
  82. }
  83. \Setting::set('shop.fullPieceNew',$data);
  84. return $this->successJson("设置保存成功", Url::absoluteWeb('goods.enoughReduce.full_piece.index'));
  85. }
  86. }