ThreadsController.php 5.0 KB

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