ThreadsController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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('company_name', 'like', '%' . $search['keyword'] . '%')
  43. ->orWhere('reg_no', '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. public function syncAdd(Request $request)
  83. {
  84. $ower_id = \Auth::guard('admin')->user()->id;
  85. $label = $request->get('label');
  86. $contacts_ids = $request->get('company_ids');
  87. foreach ($contacts_ids as $contacts_id) {
  88. $company_id = CompanyContactsModel::find($contacts_id)->company_id;
  89. $this->repository->create([
  90. 'ower_id' => $ower_id,
  91. 'label' => $label,
  92. 'contact_id' => $contacts_id,
  93. 'company_id' => $company_id,
  94. 'status' => 0
  95. ]);
  96. }
  97. return 200;
  98. }
  99. /**
  100. * 添加线索跟进
  101. */
  102. public function update(Request $request)
  103. {
  104. if ($request->method() == 'POST') {
  105. return $this->_updateSave();
  106. }
  107. $data = $this->repository->find($request->get('id'));
  108. $progress = $data->progress()->orderBy('id', 'desc')->get();
  109. return view('admin.user.threads.edit', compact('data', 'progress'));
  110. }
  111. /**
  112. * 保存修改
  113. */
  114. private function _updateSave()
  115. {
  116. $data = (array)request('data');
  117. ThreadsProgressModel::create($data);
  118. $progress = ThreadsProgressModel::where('threads_id', $data['threads_id'])->orderBy('id', 'desc')->get();
  119. $view = view('admin.user.threads.progress', compact('progress'))->render();
  120. return response()->json(['html' => $view]);
  121. }
  122. public function view(Request $request)
  123. {
  124. $data = $this->repository->find(request('id'));
  125. return view('admin.user.threads.view', compact('data'));
  126. }
  127. /**
  128. * 删除
  129. */
  130. public function destroy(Request $request)
  131. {
  132. $bool = $this->repository->destroy($request->get('id'));
  133. if ($bool) {
  134. return $this->showMessage('操作成功');
  135. } else {
  136. return $this->showWarning("操作失败");
  137. }
  138. }
  139. /*
  140. * 添加到AI电话拨打列表
  141. * */
  142. public function addCallList(Request $request)
  143. {
  144. $thread_ids = $request->get('contact_ids');
  145. $ip = $request->get('ip');
  146. foreach ($thread_ids as $thread_id) {
  147. $thread = $this->repository->find($thread_id);
  148. if ($thread->contact()->count()) {
  149. $phone = $thread->contact->phone;
  150. $hasAdd = CallListModel::where('phone', $phone)->where('sync', 0)->count();
  151. if (!$hasAdd) {
  152. CallListModel::create(['phone' => $phone, 'sync' => 0, 'ip' => $ip]);
  153. ThreadsProgressModel::create(['threads_id' => $thread->id, 'remark' => '添加到AI电话列表']);
  154. }
  155. }
  156. }
  157. return 200;
  158. }
  159. /***
  160. * 导出我的线索
  161. * @param Request $request
  162. * @return int|\Symfony\Component\HttpFoundation\BinaryFileResponse
  163. */
  164. public function export_threads(Request $request)
  165. {
  166. $type = $request->get('type');
  167. if ($type) {
  168. return 200;
  169. }
  170. $thread_ids = $request->get('threads_ids');
  171. $threads = UserThreadsModel::whereIn('id', $thread_ids)->get();
  172. $fileds = $request->get('check_fields');
  173. return Excel::download(new ThreadsExport($threads, $fileds), '我的线索.xlsx');
  174. }
  175. }