GoodsController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * 产品管理
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-07-20 11:16:25
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Furniture;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\FurnitureGoodsCategoryModel;
  12. use Illuminate\Http\Request;
  13. use App\Repositories\Base\Criteria\OrderBy;
  14. use App\Repositories\Furniture\Criteria\MultiWhere;
  15. use App\Repositories\Furniture\GoodsRepository;
  16. class GoodsController extends Controller
  17. {
  18. private $repository;
  19. public function __construct(GoodsRepository $repository) {
  20. if(!$this->repository) $this->repository = $repository;
  21. }
  22. function index(Request $request) {
  23. $search['keyword'] = $request->input('keyword');
  24. $search['store_id'] = $this->getStoreId();
  25. $query = $this->repository->pushCriteria(new MultiWhere($search));
  26. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  27. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  28. }
  29. $list = $query->paginate(10);
  30. return view('admin.furniture.goods.index',compact('list'));
  31. }
  32. function check(Request $request) {
  33. $request = $request->all();
  34. $search['keyword'] = $request->input('keyword');
  35. $orderby = array();
  36. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  37. $orderby[$request['sort_field']] = $request['sort_field_by'];
  38. }
  39. $list = $this->repository->search($search,$orderby);
  40. return view('admin.furniture.goods.check',compact('list'));
  41. }
  42. /**
  43. * 添加
  44. *
  45. */
  46. public function create(Request $request)
  47. {
  48. if($request->method() == 'POST') {
  49. return $this->_createSave();
  50. }
  51. $cate = $this->cate(0);
  52. return view('admin.furniture.goods.edit',compact('cate'));
  53. }
  54. /**
  55. * 保存修改
  56. */
  57. private function _createSave(){
  58. $data = (array) request('data');
  59. $data['store_id'] =$this->getStoreId();
  60. if(!empty($data['img']))
  61. $data['img'] = $this->formatImgUrl($data['img']);
  62. $id = $this->repository->create($data);
  63. if($id) {
  64. $url[] = array('url'=>U( 'Furniture/Goods/index'),'title'=>'返回列表');
  65. $url[] = array('url'=>U( 'Furniture/Goods/create'),'title'=>'继续添加');
  66. $this->showMessage('添加成功',$url);
  67. }else{
  68. $url[] = array('url'=>U( 'Furniture/Goods/index'),'title'=>'返回列表');
  69. return $this->showWarning('添加失败',$url);
  70. }
  71. }
  72. /**
  73. *
  74. * 修改
  75. *
  76. *
  77. */
  78. public function update(Request $request) {
  79. if($request->method() == 'POST') {
  80. return $this->_updateSave();
  81. }
  82. $data = $this->repository->find($request->get('id'));
  83. $cate = $this->cate(0);
  84. return view('admin.furniture.goods.edit',compact('data','cate'));
  85. }
  86. /**
  87. * 保存修改
  88. */
  89. private function _updateSave() {
  90. $data = (array) request('data');
  91. if(!empty($data['img']))
  92. $data['img'] = $this->formatImgUrl($data['img']);
  93. $ok = $this->repository->update(request('id'),$data);
  94. if($ok) {
  95. $url[] = array('url'=>U( 'Furniture/Goods/index'),'title'=>'返回列表');
  96. return $this->showMessage('操作成功',urldecode(request('_referer')));
  97. }else{
  98. $url[] = array('url'=>U( 'Furniture/Goods/index'),'title'=>'返回列表');
  99. return $this->showWarning('操作失败',$url);
  100. }
  101. }
  102. public function view(Request $request) {
  103. $data = $this->repository->find(request('id'));
  104. return view('admin.furniture.goods.view',compact('data'));
  105. }
  106. /**
  107. *
  108. * 状态改变
  109. *
  110. */
  111. public function status(Request $request) {
  112. $ok = $this->repository->updateStatus(request('id'),request('status'));
  113. if($ok) {
  114. return $this->showMessage('操作成功');
  115. }else{
  116. return $this->showWarning('操作失败');
  117. }
  118. }
  119. /**
  120. * 删除
  121. */
  122. public function destroy(Request $request) {
  123. $bool = $this->repository->destroy($request->get('id'));
  124. if($bool) {
  125. return $this->showMessage('操作成功');
  126. }else{
  127. return $this->showWarning("操作失败");
  128. }
  129. }
  130. public function cate($pid)
  131. {
  132. $res = FurnitureGoodsCategoryModel::where('pid', '=', $pid)
  133. ->where('status','1')
  134. ->where('store_id',$this->getStoreId())
  135. ->get();
  136. $data = [];
  137. foreach ($res as $key => $val) {
  138. $val->sub = self::cate($val->id);
  139. $data[] = $val;
  140. }
  141. return $data;
  142. }
  143. }