ThreadsController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $list = $list->paginate(20);
  53. if ($request->ajax()) {
  54. $view = view('admin.user.threads.data', compact('list'))->render();
  55. return response()->json(['html' => $view]);
  56. }
  57. return view('admin.user.threads.index', compact('list'));
  58. }
  59. /**
  60. * 添加线索
  61. */
  62. public function create(Request $request)
  63. {
  64. $data['ower_id'] = \Auth::guard('admin')->user()->id;
  65. $data['company_id'] = $request->get('company_id');
  66. $data['contact_id'] = $request->get('contact_id');
  67. $data['status'] = 0;
  68. $res = $this->repository->create($data);
  69. /*添加初始化跟进备注*/
  70. if ($request->get('remark')) {
  71. ThreadsProgressModel::create(['threads_id' => $res->id, 'remark' => $request->get('remark')]);
  72. }
  73. if ($res) {
  74. $url[] = array('url' => U('User/Threads/index'), 'title' => '我的线索');
  75. $this->showMessage('添加成功', $url);
  76. } else {
  77. $url[] = array('url' => U('User/Threads/index'), 'title' => '我的线索');
  78. return $this->showWarning('添加失败', $url);
  79. }
  80. }
  81. /**
  82. * 添加线索跟进
  83. */
  84. public function update(Request $request)
  85. {
  86. if ($request->method() == 'POST') {
  87. return $this->_updateSave();
  88. }
  89. $data = $this->repository->find($request->get('id'));
  90. $progress = $data->progress()->orderBy('id', 'desc')->get();
  91. return view('admin.user.threads.edit', compact('data', 'progress'));
  92. }
  93. /**
  94. * 保存修改
  95. */
  96. private function _updateSave()
  97. {
  98. $data = (array)request('data');
  99. ThreadsProgressModel::create($data);
  100. $progress = ThreadsProgressModel::where('threads_id', $data['threads_id'])->orderBy('id', 'desc')->get();
  101. $view = view('admin.user.threads.progress', compact('progress'))->render();
  102. return response()->json(['html' => $view]);
  103. }
  104. public function view(Request $request)
  105. {
  106. $data = $this->repository->find(request('id'));
  107. return view('admin.user.threads.view', compact('data'));
  108. }
  109. /**
  110. * 删除
  111. */
  112. public function destroy(Request $request)
  113. {
  114. $bool = $this->repository->destroy($request->get('id'));
  115. if ($bool) {
  116. return $this->showMessage('操作成功');
  117. } else {
  118. return $this->showWarning("操作失败");
  119. }
  120. }
  121. /*
  122. * 添加到AI电话拨打列表
  123. * */
  124. public function addCallList(Request $request)
  125. {
  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. {
  148. $type = $request->get('type');
  149. if ($type) {
  150. return 200;
  151. }
  152. $thread_ids = $request->get('threads_ids');
  153. \Log::info($thread_ids);
  154. $threads = UserThreadsModel::whereIn('id', $thread_ids)->get();
  155. return Excel::download(new ThreadsExport($threads), '我的线索.xlsx');
  156. }
  157. }