CategoryController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * 产品分类
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-07-20 11:16:57
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Furniture\Goods;
  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\Goods\Criteria\MultiWhere;
  15. use App\Repositories\Furniture\Goods\CategoryRepository;
  16. class CategoryController extends Controller
  17. {
  18. private $repository;
  19. public function __construct(CategoryRepository $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.category.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.category.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.category.edit',compact('cate'));
  53. }
  54. /**
  55. * 保存修改
  56. */
  57. private function _createSave(){
  58. $data = (array) request('data');
  59. $data['store_id'] =$this->getStoreId();
  60. $id = $this->repository->create($data);
  61. if($id) {
  62. $url[] = array('url'=>U( 'Furniture/Goods/Category/index'),'title'=>'返回列表');
  63. $url[] = array('url'=>U( 'Furniture/Goods/Category/create'),'title'=>'继续添加');
  64. $this->showMessage('添加成功',$url);
  65. }else{
  66. $url[] = array('url'=>U( 'Furniture/Goods/Category/index'),'title'=>'返回列表');
  67. return $this->showWarning('添加失败',$url);
  68. }
  69. }
  70. /**
  71. *
  72. * 修改
  73. *
  74. *
  75. */
  76. public function update(Request $request) {
  77. if($request->method() == 'POST') {
  78. return $this->_updateSave();
  79. }
  80. $data = $this->repository->find($request->get('id'));
  81. $cate = $this->cate(0);
  82. return view('admin.furniture.goods.category.edit',compact('data','cate'));
  83. }
  84. /**
  85. * 保存修改
  86. */
  87. private function _updateSave() {
  88. $data = (array) request('data');
  89. $ok = $this->repository->update(request('id'),$data);
  90. if($ok) {
  91. $url[] = array('url'=>U( 'Furniture/Goods/Category/index'),'title'=>'返回列表');
  92. return $this->showMessage('操作成功',urldecode(request('_referer')));
  93. }else{
  94. $url[] = array('url'=>U( 'Furniture/Goods/Category/index'),'title'=>'返回列表');
  95. return $this->showWarning('操作失败',$url);
  96. }
  97. }
  98. public function view(Request $request) {
  99. $data = $this->repository->find(request('id'));
  100. return view('admin.furniture.goods.category.view',compact('data'));
  101. }
  102. /**
  103. *
  104. * 状态改变
  105. *
  106. */
  107. public function status(Request $request) {
  108. $ok = $this->repository->updateStatus(request('id'),request('status'));
  109. if($ok) {
  110. return $this->showMessage('操作成功');
  111. }else{
  112. return $this->showWarning('操作失败');
  113. }
  114. }
  115. /**
  116. * 删除
  117. */
  118. public function destroy(Request $request) {
  119. $bool = $this->repository->destroy($request->get('id'));
  120. if($bool) {
  121. return $this->showMessage('操作成功');
  122. }else{
  123. return $this->showWarning("操作失败");
  124. }
  125. }
  126. /*
  127. * 父分类的递归
  128. */
  129. public function cate($pid)
  130. {
  131. $res = FurnitureGoodsCategoryModel::where('pid', '=', $pid)
  132. ->where('status','1')
  133. ->where('store_id',$this->getStoreId())
  134. ->get();
  135. $data = [];
  136. foreach ($res as $key => $val) {
  137. $val->sub = self::cate($val->id);
  138. $data[] = $val;
  139. }
  140. return $data;
  141. }
  142. }