ThreadsController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 我的线索
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-11-20 02:55:06
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\User;
  10. use App\Exports\ThreadsExport;
  11. use App\Http\Controllers\Admin\Controller;
  12. use App\Models\CallListModel;
  13. use App\Models\CompanyContactsModel;
  14. use App\Models\ThreadsProgressModel;
  15. use App\Models\UserThreadsModel;
  16. use Illuminate\Http\Request;
  17. use App\Repositories\Base\Criteria\OrderBy;
  18. use App\Repositories\User\Threads\Criteria\MultiWhere;
  19. use App\Repositories\User\ThreadsRepository;
  20. use Maatwebsite\Excel\Facades\Excel;
  21. class ThreadsController extends Controller
  22. {
  23. private $repository;
  24. public function __construct(ThreadsRepository $repository)
  25. {
  26. if (!$this->repository) $this->repository = $repository;
  27. }
  28. /**
  29. * 列表页
  30. */
  31. function index(Request $request)
  32. {
  33. $user_id = \Auth::guard('admin')->user()->id;
  34. $search = $request->all();
  35. $search['keyword'] = $request->input('keyword');
  36. if(is_numeric($search['keyword'])){
  37. $list = UserThreadsModel::whereHas('contact', function ($query) use ($search) {
  38. $query->where('phone', 'like', '%' . $search['keyword'] . '%');
  39. })->where('ower_id',$user_id)->orderBy('updated_at','desc');
  40. }else{
  41. $list = UserThreadsModel::whereHas('company', function ($query) use ($search) {
  42. $query->where('companyName', 'like', '%' . $search['keyword'] . '%')
  43. ->orWhere('regNo', 'like', '%' . $search['keyword'] . '%');
  44. })->where('ower_id',$user_id)->orderBy('updated_at','desc');
  45. }
  46. if(isset($search['process']) && $search['process'] ==1 ){
  47. $list = $list->has('progress');
  48. }
  49. if(isset($search['process']) && $search['process'] == 2){
  50. $list = $list->has('progress','=',0);
  51. }
  52. $allIds = $list->pluck('id');
  53. $list = $list->paginate(10);
  54. if ($request->ajax()) {
  55. $view = view('admin.user.threads.data', compact('list','allIds'))->render();
  56. return response()->json(['html' => $view]);
  57. }
  58. return view('admin.user.threads.index', compact('list','allIds'));
  59. }
  60. /**
  61. * 添加线索
  62. */
  63. public function create(Request $request)
  64. {
  65. $data['ower_id'] = \Auth::guard('admin')->user()->id;
  66. $data['company_id'] = $request->get('company_id');
  67. $data['contact_id'] = $request->get('contact_id');
  68. $data['status'] = 0;
  69. $res = $this->repository->create($data);
  70. /*添加初始化跟进备注*/
  71. if($request->get('remark')){
  72. ThreadsProgressModel::create(['threads_id'=>$res->id,'remark'=>$request->get('remark')]);
  73. }
  74. if ($res) {
  75. $url[] = array('url' => U('User/Threads/index'), 'title' => '我的线索');
  76. $this->showMessage('添加成功', $url);
  77. } else {
  78. $url[] = array('url' => U('User/Threads/index'), 'title' => '我的线索');
  79. return $this->showWarning('添加失败', $url);
  80. }
  81. }
  82. /**
  83. * 添加线索跟进
  84. */
  85. public function update(Request $request)
  86. {
  87. if ($request->method() == 'POST') {
  88. return $this->_updateSave();
  89. }
  90. $data = $this->repository->find($request->get('id'));
  91. $progress = $data->progress()->orderBy('id', 'desc')->get();
  92. return view('admin.user.threads.edit', compact('data', 'progress'));
  93. }
  94. /**
  95. * 保存修改
  96. */
  97. private function _updateSave()
  98. {
  99. $data = (array)request('data');
  100. ThreadsProgressModel::create($data);
  101. $progress = ThreadsProgressModel::where('threads_id', $data['threads_id'])->orderBy('id', 'desc')->get();
  102. $view = view('admin.user.threads.progress', compact('progress'))->render();
  103. return response()->json(['html' => $view]);
  104. }
  105. public function view(Request $request)
  106. {
  107. $data = $this->repository->find(request('id'));
  108. return view('admin.user.threads.view', compact('data'));
  109. }
  110. /**
  111. * 删除
  112. */
  113. public function destroy(Request $request)
  114. {
  115. $bool = $this->repository->destroy($request->get('id'));
  116. if ($bool) {
  117. return $this->showMessage('操作成功');
  118. } else {
  119. return $this->showWarning("操作失败");
  120. }
  121. }
  122. /*
  123. * 添加到AI电话拨打列表
  124. * */
  125. public function addCallList(Request $request){
  126. $thread_ids = $request->get('contact_ids');
  127. $ip = $request->get('ip');
  128. foreach ($thread_ids as $thread_id){
  129. $thread = $this->repository->find($thread_id);
  130. if($thread->contact()->count()){
  131. $phone = $thread->contact->phone;
  132. $hasAdd = CallListModel::where('phone',$phone)->where('sync',0)->count();
  133. if(!$hasAdd){
  134. CallListModel::create(['phone'=>$phone,'sync'=>0,'ip'=>$ip]);
  135. ThreadsProgressModel::create(['threads_id'=>$thread->id,'remark'=>'添加到AI电话列表']);
  136. }
  137. }
  138. }
  139. return 200;
  140. }
  141. /***
  142. * 导出我的线索
  143. * @param Request $request
  144. * @return int|\Symfony\Component\HttpFoundation\BinaryFileResponse
  145. */
  146. public function export_threads(Request $request){
  147. $type = $request->get('type');
  148. if($type){
  149. return 200;
  150. }
  151. $thread_ids = $request->get('threads_ids');
  152. \Log::info($thread_ids);
  153. $threads = UserThreadsModel::whereIn('id',$thread_ids)->get();
  154. return Excel::download(new ThreadsExport($threads),'我的线索.xlsx');
  155. }
  156. }