AgentController.php 4.7 KB

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