BqTrafficController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/1/7 0007
  6. * Time: 13:19
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Models\BqFlow;
  11. use App\Http\Models\BqFlowInfo;
  12. use App\Http\Models\FlowInfoTpl;
  13. use App\Http\Models\OptimizationFlow;
  14. use App\Http\Models\OptimizationFlowInfo;
  15. use App\Http\Models\Site;
  16. use App\Http\Models\User;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\DB;
  19. class BqTrafficController extends Controller
  20. {
  21. //百千计划
  22. public function index(Request $request, $siteId)
  23. {
  24. $clientInquiryStatistics = DB::table('clientInquiry_statistics')->where('site_id', $siteId)->first();
  25. if (empty(!$clientInquiryStatistics)) {
  26. $clientInquiryStatisticsList = \GuzzleHttp\json_decode($clientInquiryStatistics->clientInquiry_list, true);
  27. }
  28. $item = Site::query()
  29. ->where('id', $siteId)->where('is_bq', 1)
  30. ->whereIn('status', [2, 3])->first();
  31. if (empty($item)) {
  32. return view('admin/site/tips', [
  33. 'siteId' => $siteId,
  34. 'tips' => '此项目暂无百千计划'
  35. ]);
  36. }
  37. //询盘不读这里这个
  38. $listReportMap = DB::connection('rank')->table('project_listreport')
  39. ->select('Ym', 'traffic', 'inquire')
  40. ->where('project_id', $item->old_id)
  41. ->get()->toArray();
  42. //百千约访时间
  43. $bqAt = date('Y-m-d', strtotime($item->bq_at));
  44. $currentMonth = date('Y-m-d', strtotime('-2 Month', strtotime($bqAt)));
  45. $bqAtList = [];
  46. for ($i = 1; $i < 13; $i++) {
  47. $bqAtList[] = date('Ym', strtotime("+{$i} Month", strtotime($currentMonth)));
  48. }
  49. $trafficList = [];
  50. $inquireList = [];
  51. foreach ($bqAtList as $key => $value) {
  52. $trafficList[$key]['date'] = $value;
  53. $trafficList[$key]['traffic'] = '';
  54. }
  55. foreach ($bqAtList as $key => $value) {
  56. $inquireList[$key]['date'] = $value;
  57. $inquireList[$key]['inquire'] = '';
  58. }
  59. //实时询盘
  60. $realInquireList = $this->getSiteInquireList($item);
  61. if (!empty($realInquireList)) {
  62. foreach ($inquireList as $key => $value) {
  63. foreach ($realInquireList as $kk => $vv) {
  64. if ($value['date'] == $vv->date) {
  65. $inquireList[$key]['inquire'] = $vv->sum;
  66. }
  67. }
  68. }
  69. }
  70. foreach ($listReportMap as $key => $value) {
  71. $item->traffic = '';
  72. $item->inquire = '';
  73. foreach ($trafficList as $kk => $vv) {
  74. if ($value->Ym == $vv['date']) {
  75. $trafficList[$kk]['traffic'] = $value->traffic;
  76. }
  77. if ($kk == 0) {
  78. $item->traffic = $value->traffic;
  79. }
  80. }
  81. if ($item->reach_300_at && $item->online_at) {
  82. $one = date_create($item->online_at);
  83. $two = date_create($item->reach_300_at);
  84. $diff = date_diff($one, $two);
  85. $item->duration_300 = $diff->days;
  86. }
  87. if ($item->reach_500_at && $item->online_at) {
  88. $one = date_create($item->online_at);
  89. $two = date_create($item->reach_500_at);
  90. $diff = date_diff($one, $two);
  91. $item->duration_500 = $diff->days;
  92. }
  93. if ($item->reach_1000_at && $item->online_at) {
  94. $one = date_create($item->online_at);
  95. $two = date_create($item->reach_1000_at);
  96. $diff = date_diff($one, $two);
  97. $item->duration_1000 = $diff->days;
  98. }
  99. if ($item->reach_1500_at && $item->online_at) {
  100. $one = date_create($item->online_at);
  101. $two = date_create($item->reach_1500_at);
  102. $diff = date_diff($one, $two);
  103. $item->duration_1500 = $diff->days;
  104. }
  105. if ($item->reach_2000_at && $item->online_at) {
  106. $one = date_create($item->online_at);
  107. $two = date_create($item->reach_2000_at);
  108. $diff = date_diff($one, $two);
  109. $item->duration_2000 = $diff->days;
  110. }
  111. }
  112. //百千约访流量
  113. $trafficTable = [];
  114. foreach ($trafficList as $key => $value) {
  115. $trafficTable['xAxis'][] = $value['date'];
  116. $trafficTable['yAxis'][] = $value['traffic'];
  117. }
  118. //百千约访后台询盘
  119. $inquireTable = [];
  120. foreach ($inquireList as $key => $value) {
  121. $inquireTable['xAxis'][] = $value['date'];
  122. $inquireTable['yAxis'][] = $value['inquire'];
  123. }
  124. $result[] = $item->toArray();
  125. $resList = collect($result);
  126. $clientInquireList = [];
  127. foreach ($bqAtList as $key => $value) {
  128. $clientInquireList[$key]['date'] = $value;
  129. $clientInquireList[$key]['clientInquire'] = '';
  130. }
  131. $clientInquireList1 = $clientInquireList;
  132. if (!empty($clientInquiryStatisticsList)) {
  133. $clientInquireList1 = $clientInquiryStatisticsList;
  134. //百千约访客户询盘
  135. $clientInquireList2 = [];
  136. foreach ($clientInquiryStatisticsList as $key => $value) {
  137. $clientInquireList2['xAxis'][] = $value['date'];
  138. $clientInquireList2['yAxis'][] = $value['clientInquire'];
  139. }
  140. } else {
  141. //百千约访客户询盘
  142. $clientInquireList2 = [];
  143. foreach ($bqAtList as $key => $value) {
  144. $clientInquireList2['xAxis'][] = '';
  145. $clientInquireList2['yAxis'][] = '';
  146. }
  147. }
  148. if (!$request->ajax()) {
  149. return view('admin/bqTraffic/index', [
  150. 'siteId' => $siteId,
  151. 'siteInfo' => $item,
  152. 'trafficList' => $trafficTable,
  153. 'inquireList' => $inquireTable,
  154. 'clientInquireList' => $clientInquireList2,
  155. 'clientInquireDate' => $clientInquireList1
  156. ]);
  157. }
  158. return response()->json([
  159. 'rows' => $resList ?? [],
  160. 'total' => count($result) ?? 0,
  161. ]);
  162. }
  163. public function saveClientInquiry(Request $request, $siteId)
  164. {
  165. $data = $request->all();
  166. $clientInquiryStatisticsList1 = [];
  167. $array = [];
  168. foreach ($data as $key => $value) {
  169. $clientInquiryStatisticsList1['date'] = $key;
  170. $clientInquiryStatisticsList1['clientInquire'] = $value;
  171. $array[] = $clientInquiryStatisticsList1;
  172. }
  173. $result = DB::table('clientInquiry_statistics')->where('site_id', $siteId)->first();
  174. if (empty($result)) {
  175. DB::table('clientInquiry_statistics')->insert(['clientInquiry_list' => json_encode($array), 'site_id' => $siteId]);
  176. } else {
  177. DB::table('clientInquiry_statistics')->where('site_id', $siteId)->update(['clientInquiry_list' => json_encode($array)]);
  178. }
  179. return $this->success('success' . $siteId);
  180. }
  181. public function getSiteInquireList($site)
  182. {
  183. $config = [
  184. 'connection_name' => sprintf('connection_name_%s', $site->id),
  185. 'host' => $site->server->server_ip,
  186. 'port' => '3306',
  187. 'database' => $site->database,
  188. 'username' => $site->server->mysql_user_name,
  189. 'password' => $site->server->mysql_passwd,
  190. ];
  191. config_connection($config);
  192. $list = DB::connection($config['connection_name'])->table('user_msg')
  193. ->selectRaw('FROM_UNIXTIME(create_time, "%Y%m") as date, count(*) as count')
  194. ->groupBy('date')
  195. ->get()->toArray() ?? [];
  196. if (!empty($list)) {
  197. foreach ($list as $key => $value) {
  198. $num = 0;
  199. foreach ($list as $kk => $vv) {
  200. if (strtotime($value->date) >= strtotime($vv->date)) {
  201. $num += $vv->count;
  202. }
  203. }
  204. $list[$key]->sum = $num;
  205. }
  206. }
  207. return $list;
  208. }
  209. //百千进度流程
  210. public function bqProcess($siteId)
  211. {
  212. $tplStageList = BqFlow::query()->where('site_id', $siteId)->with('bqFlowInfo')->get()->toArray();
  213. return view('admin.bqTraffic.bq_process', [
  214. 'siteId' => $siteId,
  215. 'tplStageList' => $tplStageList ?? [],
  216. 'userList' => $this->getFlowUserList($siteId),
  217. ]);
  218. }
  219. //百千进度模版
  220. public function bqProcessTpl($siteId)
  221. {
  222. $tplStageList = BqFlow::query()->where('site_id', 0)->with('bqFlowInfo')->get()->toArray();
  223. return view('admin.bqTraffic.bq_process_tpl', [
  224. 'siteId' => $siteId,
  225. 'tplStageList' => $tplStageList,
  226. ]);
  227. }
  228. //初始化模版
  229. public function bqProcessReset($siteId)
  230. {
  231. try {
  232. DB::transaction(function () use ($siteId) {
  233. BqFlow::query()->where('site_id', $siteId)->delete();
  234. BqFlowInfo::query()->where('site_id', $siteId)->delete();
  235. $tplStageList = BqFlow::query()->where('site_id', 0)->with('bqFlowInfo')->get()->toArray();
  236. $result = [];
  237. foreach ($tplStageList as $item) {
  238. $data = [
  239. 'site_id' => $siteId,
  240. 'title' => $item['title'],
  241. 'created_at' => date('Y-m-d H:i:s')
  242. ];
  243. $bqFlowId = BqFlow::query()->insertGetId($data);
  244. foreach ($item['bq_flow_info'] as $value) {
  245. $result[] = [
  246. 'site_id' => $siteId,
  247. 'stage_id' => $bqFlowId,
  248. 'detail_list' => \GuzzleHttp\json_encode($value['detail_list']),
  249. 'created_at' => date('y-m-d H:i:s'),
  250. ];
  251. }
  252. }
  253. BqFlowInfo::query()->insert($result);
  254. });
  255. return $this->success('success');
  256. } catch (\Throwable $exception) {
  257. return response()->json(['message' => $exception->getMessage()], 400);
  258. }
  259. }
  260. public function bqProcessSave(Request $request, $siteId)
  261. {
  262. $dataList = $request->input('dataList') ?? [];
  263. if (!empty($dataList)) {
  264. try {
  265. DB::transaction(function () use ($dataList, $siteId) {
  266. BqFlow::query()->where('site_id', $siteId)->delete();
  267. $data = [];
  268. foreach ($dataList as $key => $value) {
  269. $title = [
  270. 'site_id' => $siteId,
  271. 'title' => $value['step_title'],
  272. 'expected_date' => $value['expected_date'] ?? null,
  273. 'created_at' => date('Y-m-d H:i:s'),
  274. ];
  275. $id = BqFlow::query()->insertGetId($title);
  276. $data[] = [
  277. 'detail_list' => json_encode($value['children']),
  278. 'site_id' => $siteId,
  279. 'created_at' => date('Y-m-d H:i:s'),
  280. 'stage_id' => $id,
  281. ];
  282. }
  283. BqFlowInfo::query()->where('site_id', $siteId)->delete();
  284. BqFlowInfo::query()->insert($data);
  285. });
  286. } catch (\Throwable $exception) {
  287. return response()->json(['message' => $exception->getMessage()], 400);
  288. }
  289. } else {
  290. BqFlow::query()->where('site_id', $siteId)->delete();
  291. BqFlowInfo::query()->where('site_id', $siteId)->delete();
  292. }
  293. return response()->json(['message' => '操作成功']);
  294. }
  295. public function optimizationProcess($siteId)
  296. {
  297. $tplStageList = OptimizationFlow::query()->where('site_id', $siteId)->with('optimizationFlowInfoList')->get()->toArray();
  298. return view('admin.bqTraffic.optimization_process', [
  299. 'siteId' => $siteId,
  300. 'tplStageList' => $tplStageList,
  301. ]);
  302. }
  303. public function optimizationProcessSave(Request $request, $siteId)
  304. {
  305. $dataList = $request->input('dataList') ?? [];
  306. if (!empty($dataList)) {
  307. try {
  308. DB::transaction(function () use ($dataList, $siteId) {
  309. OptimizationFlow::query()->where('site_id', $siteId)->delete();
  310. $data = [];
  311. foreach ($dataList as $key => $value) {
  312. $title = [
  313. 'site_id' => $siteId,
  314. 'title' => $value['step_title'],
  315. 'created_at' => date('Y-m-d H:i:s'),
  316. ];
  317. $id = OptimizationFlow::query()->insertGetId($title);
  318. $data[] = [
  319. 'detail_list' => json_encode($value['children']),
  320. 'site_id' => $siteId,
  321. 'created_at' => date('Y-m-d H:i:s'),
  322. 'stage_id' => $id,
  323. ];
  324. }
  325. OptimizationFlowInfo::query()->where('site_id', $siteId)->delete();
  326. OptimizationFlowInfo::query()->insert($data);
  327. });
  328. } catch (\Throwable $exception) {
  329. return response()->json(['message' => $exception->getMessage()], 400);
  330. }
  331. } else {
  332. OptimizationFlow::query()->where('site_id', $siteId)->delete();
  333. OptimizationFlowInfo::query()->where('site_id', $siteId)->delete();
  334. }
  335. return response()->json(['message' => '操作成功']);
  336. }
  337. public function getFlowUserList($siteId)
  338. {
  339. $roleScope = array_keys(FlowInfoTpl::RoleScope);
  340. unset($roleScope[1]); //删除客户
  341. $userList = User::query()->select(['id', 'nickname', 'role_id'])->whereIn('role_id', $roleScope)
  342. ->get()->toArray();
  343. $site = Site::query()->select(['cn_title'])->find($siteId);
  344. $userList[] = ['id' => -1, 'role_id' => -1, 'nickname' => $site->cn_title ?? '站点名称'];
  345. return $userList;
  346. }
  347. }