AttrController.php 4.6 KB

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