123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2020/1/7 0007
- * Time: 13:19
- */
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Http\Models\BqFlow;
- use App\Http\Models\BqFlowInfo;
- use App\Http\Models\FlowInfoTpl;
- use App\Http\Models\OptimizationFlow;
- use App\Http\Models\OptimizationFlowInfo;
- use App\Http\Models\Site;
- use App\Http\Models\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- class BqTrafficController extends Controller
- {
- //百千计划
- public function index(Request $request, $siteId)
- {
- $clientInquiryStatistics = DB::table('clientInquiry_statistics')->where('site_id', $siteId)->first();
- if (empty(!$clientInquiryStatistics)) {
- $clientInquiryStatisticsList = \GuzzleHttp\json_decode($clientInquiryStatistics->clientInquiry_list, true);
- }
- $item = Site::query()
- ->where('id', $siteId)->where('is_bq', 1)
- ->whereIn('status', [2, 3])->first();
- if (empty($item)) {
- return view('admin/site/tips', [
- 'siteId' => $siteId,
- 'tips' => '此项目暂无百千计划'
- ]);
- }
- //询盘不读这里这个
- $listReportMap = DB::connection('rank')->table('project_listreport')
- ->select('Ym', 'traffic', 'inquire')
- ->where('project_id', $item->old_id)
- ->get()->toArray();
- //百千约访时间
- $bqAt = date('Y-m-d', strtotime($item->bq_at));
- $currentMonth = date('Y-m-d', strtotime('-2 Month', strtotime($bqAt)));
- $bqAtList = [];
- for ($i = 1; $i < 13; $i++) {
- $bqAtList[] = date('Ym', strtotime("+{$i} Month", strtotime($currentMonth)));
- }
- $trafficList = [];
- $inquireList = [];
- foreach ($bqAtList as $key => $value) {
- $trafficList[$key]['date'] = $value;
- $trafficList[$key]['traffic'] = '';
- }
- foreach ($bqAtList as $key => $value) {
- $inquireList[$key]['date'] = $value;
- $inquireList[$key]['inquire'] = '';
- }
- //实时询盘
- $realInquireList = $this->getSiteInquireList($item);
- if (!empty($realInquireList)) {
- foreach ($inquireList as $key => $value) {
- foreach ($realInquireList as $kk => $vv) {
- if ($value['date'] == $vv->date) {
- $inquireList[$key]['inquire'] = $vv->sum;
- }
- }
- }
- }
- foreach ($listReportMap as $key => $value) {
- $item->traffic = '';
- $item->inquire = '';
- foreach ($trafficList as $kk => $vv) {
- if ($value->Ym == $vv['date']) {
- $trafficList[$kk]['traffic'] = $value->traffic;
- }
- if ($kk == 0) {
- $item->traffic = $value->traffic;
- }
- }
- if ($item->reach_300_at && $item->online_at) {
- $one = date_create($item->online_at);
- $two = date_create($item->reach_300_at);
- $diff = date_diff($one, $two);
- $item->duration_300 = $diff->days;
- }
- if ($item->reach_500_at && $item->online_at) {
- $one = date_create($item->online_at);
- $two = date_create($item->reach_500_at);
- $diff = date_diff($one, $two);
- $item->duration_500 = $diff->days;
- }
- if ($item->reach_1000_at && $item->online_at) {
- $one = date_create($item->online_at);
- $two = date_create($item->reach_1000_at);
- $diff = date_diff($one, $two);
- $item->duration_1000 = $diff->days;
- }
- if ($item->reach_1500_at && $item->online_at) {
- $one = date_create($item->online_at);
- $two = date_create($item->reach_1500_at);
- $diff = date_diff($one, $two);
- $item->duration_1500 = $diff->days;
- }
- if ($item->reach_2000_at && $item->online_at) {
- $one = date_create($item->online_at);
- $two = date_create($item->reach_2000_at);
- $diff = date_diff($one, $two);
- $item->duration_2000 = $diff->days;
- }
- }
- //百千约访流量
- $trafficTable = [];
- foreach ($trafficList as $key => $value) {
- $trafficTable['xAxis'][] = $value['date'];
- $trafficTable['yAxis'][] = $value['traffic'];
- }
- //百千约访后台询盘
- $inquireTable = [];
- foreach ($inquireList as $key => $value) {
- $inquireTable['xAxis'][] = $value['date'];
- $inquireTable['yAxis'][] = $value['inquire'];
- }
- $result[] = $item->toArray();
- $resList = collect($result);
- $clientInquireList = [];
- foreach ($bqAtList as $key => $value) {
- $clientInquireList[$key]['date'] = $value;
- $clientInquireList[$key]['clientInquire'] = '';
- }
- $clientInquireList1 = $clientInquireList;
- if (!empty($clientInquiryStatisticsList)) {
- $clientInquireList1 = $clientInquiryStatisticsList;
- //百千约访客户询盘
- $clientInquireList2 = [];
- foreach ($clientInquiryStatisticsList as $key => $value) {
- $clientInquireList2['xAxis'][] = $value['date'];
- $clientInquireList2['yAxis'][] = $value['clientInquire'];
- }
- } else {
- //百千约访客户询盘
- $clientInquireList2 = [];
- foreach ($bqAtList as $key => $value) {
- $clientInquireList2['xAxis'][] = '';
- $clientInquireList2['yAxis'][] = '';
- }
- }
- if (!$request->ajax()) {
- return view('admin/bqTraffic/index', [
- 'siteId' => $siteId,
- 'siteInfo' => $item,
- 'trafficList' => $trafficTable,
- 'inquireList' => $inquireTable,
- 'clientInquireList' => $clientInquireList2,
- 'clientInquireDate' => $clientInquireList1
- ]);
- }
- return response()->json([
- 'rows' => $resList ?? [],
- 'total' => count($result) ?? 0,
- ]);
- }
- public function saveClientInquiry(Request $request, $siteId)
- {
- $data = $request->all();
- $clientInquiryStatisticsList1 = [];
- $array = [];
- foreach ($data as $key => $value) {
- $clientInquiryStatisticsList1['date'] = $key;
- $clientInquiryStatisticsList1['clientInquire'] = $value;
- $array[] = $clientInquiryStatisticsList1;
- }
- $result = DB::table('clientInquiry_statistics')->where('site_id', $siteId)->first();
- if (empty($result)) {
- DB::table('clientInquiry_statistics')->insert(['clientInquiry_list' => json_encode($array), 'site_id' => $siteId]);
- } else {
- DB::table('clientInquiry_statistics')->where('site_id', $siteId)->update(['clientInquiry_list' => json_encode($array)]);
- }
- return $this->success('success' . $siteId);
- }
- public function getSiteInquireList($site)
- {
- $config = [
- 'connection_name' => sprintf('connection_name_%s', $site->id),
- 'host' => $site->server->server_ip,
- 'port' => '3306',
- 'database' => $site->database,
- 'username' => $site->server->mysql_user_name,
- 'password' => $site->server->mysql_passwd,
- ];
- config_connection($config);
- $list = DB::connection($config['connection_name'])->table('user_msg')
- ->selectRaw('FROM_UNIXTIME(create_time, "%Y%m") as date, count(*) as count')
- ->groupBy('date')
- ->get()->toArray() ?? [];
- if (!empty($list)) {
- foreach ($list as $key => $value) {
- $num = 0;
- foreach ($list as $kk => $vv) {
- if (strtotime($value->date) >= strtotime($vv->date)) {
- $num += $vv->count;
- }
- }
- $list[$key]->sum = $num;
- }
- }
- return $list;
- }
- //百千进度流程
- public function bqProcess($siteId)
- {
- $tplStageList = BqFlow::query()->where('site_id', $siteId)->with('bqFlowInfo')->get()->toArray();
- return view('admin.bqTraffic.bq_process', [
- 'siteId' => $siteId,
- 'tplStageList' => $tplStageList ?? [],
- 'userList' => $this->getFlowUserList($siteId),
- ]);
- }
- //百千进度模版
- public function bqProcessTpl($siteId)
- {
- $tplStageList = BqFlow::query()->where('site_id', 0)->with('bqFlowInfo')->get()->toArray();
- return view('admin.bqTraffic.bq_process_tpl', [
- 'siteId' => $siteId,
- 'tplStageList' => $tplStageList,
- ]);
- }
- //初始化模版
- public function bqProcessReset($siteId)
- {
- try {
- DB::transaction(function () use ($siteId) {
- BqFlow::query()->where('site_id', $siteId)->delete();
- BqFlowInfo::query()->where('site_id', $siteId)->delete();
- $tplStageList = BqFlow::query()->where('site_id', 0)->with('bqFlowInfo')->get()->toArray();
- $result = [];
- foreach ($tplStageList as $item) {
- $data = [
- 'site_id' => $siteId,
- 'title' => $item['title'],
- 'created_at' => date('Y-m-d H:i:s')
- ];
- $bqFlowId = BqFlow::query()->insertGetId($data);
- foreach ($item['bq_flow_info'] as $value) {
- $result[] = [
- 'site_id' => $siteId,
- 'stage_id' => $bqFlowId,
- 'detail_list' => \GuzzleHttp\json_encode($value['detail_list']),
- 'created_at' => date('y-m-d H:i:s'),
- ];
- }
- }
- BqFlowInfo::query()->insert($result);
- });
- return $this->success('success');
- } catch (\Throwable $exception) {
- return response()->json(['message' => $exception->getMessage()], 400);
- }
- }
- public function bqProcessSave(Request $request, $siteId)
- {
- $dataList = $request->input('dataList') ?? [];
- if (!empty($dataList)) {
- try {
- DB::transaction(function () use ($dataList, $siteId) {
- BqFlow::query()->where('site_id', $siteId)->delete();
- $data = [];
- foreach ($dataList as $key => $value) {
- $title = [
- 'site_id' => $siteId,
- 'title' => $value['step_title'],
- 'expected_date' => $value['expected_date'] ?? null,
- 'created_at' => date('Y-m-d H:i:s'),
- ];
- $id = BqFlow::query()->insertGetId($title);
- $data[] = [
- 'detail_list' => json_encode($value['children']),
- 'site_id' => $siteId,
- 'created_at' => date('Y-m-d H:i:s'),
- 'stage_id' => $id,
- ];
- }
- BqFlowInfo::query()->where('site_id', $siteId)->delete();
- BqFlowInfo::query()->insert($data);
- });
- } catch (\Throwable $exception) {
- return response()->json(['message' => $exception->getMessage()], 400);
- }
- } else {
- BqFlow::query()->where('site_id', $siteId)->delete();
- BqFlowInfo::query()->where('site_id', $siteId)->delete();
- }
- return response()->json(['message' => '操作成功']);
- }
- public function optimizationProcess($siteId)
- {
- $tplStageList = OptimizationFlow::query()->where('site_id', $siteId)->with('optimizationFlowInfoList')->get()->toArray();
- return view('admin.bqTraffic.optimization_process', [
- 'siteId' => $siteId,
- 'tplStageList' => $tplStageList,
- ]);
- }
- public function optimizationProcessSave(Request $request, $siteId)
- {
- $dataList = $request->input('dataList') ?? [];
- if (!empty($dataList)) {
- try {
- DB::transaction(function () use ($dataList, $siteId) {
- OptimizationFlow::query()->where('site_id', $siteId)->delete();
- $data = [];
- foreach ($dataList as $key => $value) {
- $title = [
- 'site_id' => $siteId,
- 'title' => $value['step_title'],
- 'created_at' => date('Y-m-d H:i:s'),
- ];
- $id = OptimizationFlow::query()->insertGetId($title);
- $data[] = [
- 'detail_list' => json_encode($value['children']),
- 'site_id' => $siteId,
- 'created_at' => date('Y-m-d H:i:s'),
- 'stage_id' => $id,
- ];
- }
- OptimizationFlowInfo::query()->where('site_id', $siteId)->delete();
- OptimizationFlowInfo::query()->insert($data);
- });
- } catch (\Throwable $exception) {
- return response()->json(['message' => $exception->getMessage()], 400);
- }
- } else {
- OptimizationFlow::query()->where('site_id', $siteId)->delete();
- OptimizationFlowInfo::query()->where('site_id', $siteId)->delete();
- }
- return response()->json(['message' => '操作成功']);
- }
- public function getFlowUserList($siteId)
- {
- $roleScope = array_keys(FlowInfoTpl::RoleScope);
- unset($roleScope[1]); //删除客户
- $userList = User::query()->select(['id', 'nickname', 'role_id'])->whereIn('role_id', $roleScope)
- ->get()->toArray();
- $site = Site::query()->select(['cn_title'])->find($siteId);
- $userList[] = ['id' => -1, 'role_id' => -1, 'nickname' => $site->cn_title ?? '站点名称'];
- return $userList;
- }
- }
|