Collect.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\LogService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use app\common\model\OrdersCollect;
  7. use app\main\constants\AdminConstants;
  8. use app\main\constants\ApiConstants;
  9. use app\main\constants\PayConstants;
  10. use app\main\service\PvuvCollectService;
  11. use think\Db;
  12. /**
  13. * 数据统计-订单汇总
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Collect extends Backend
  18. {
  19. /**
  20. * @var OrdersCollect
  21. */
  22. protected $model = null;
  23. protected $groupId;
  24. protected $authId;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = model('OrdersCollect');
  29. $this->view->assign("typeList", $this->model->getTypeList());
  30. $this->view->assign("flagList", $this->model->getFlagList());
  31. $group = model('AuthGroupAccess')->where('uid',$this->auth->id)->find();
  32. $truegroupId = $group->group_id;
  33. if(isset($_GET['channel_id']) && intval($_GET['channel_id'])>0){
  34. $groupObj = model('AuthGroupAccess')->where('uid',$_GET['channel_id'])->find();
  35. if($groupObj){
  36. $thisGroup = $groupObj->group_id;
  37. if($thisGroup > 2){ //判断传过来的不是管理员的id
  38. if($thisGroup==3){ //如果是渠道商
  39. if($truegroupId <3){ //只有管理员才有此操作
  40. $this->authId = $_GET['channel_id'];
  41. $thisChannel = model('Admin')->where('id',$this->authId)->find();
  42. $this->groupId = 3;
  43. $this->assign('channelTitle','当前是渠道商('.$thisChannel->nickname.')的数据');
  44. $this->assignconfig('channelstr','?channel_id='.$this->authId);
  45. }
  46. }else{
  47. if($truegroupId <4){ //管理员或渠道商才有此操作
  48. if($truegroupId == 3){ //如果是从渠道商查来的,判断当前代理商是不是渠道商下属
  49. $isCh = model('AdminExtend')->where(['admin_id'=>$_GET['channel_id'],'create_by'=>$group->uid])->find();
  50. if($isCh){ //如果是当前渠道商的下属
  51. $this->authId = $_GET['channel_id'];
  52. $thisChannel = model('Admin')->where('id',$this->authId)->find();
  53. $this->groupId = 4;
  54. $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
  55. $this->assignconfig('channelstr','?channel_id='.$this->authId);
  56. }
  57. }else{ //管理员查过来的
  58. $this->authId = $_GET['channel_id'];
  59. $thisChannel = model('Admin')->where('id',$this->authId)->find();
  60. $this->groupId = 4;
  61. $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
  62. $this->assignconfig('channelstr','?channel_id='.$this->authId);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }else{
  69. $this->authId = $this->auth->id;
  70. $this->groupId = $truegroupId;
  71. $this->assignconfig('channelstr','?channel_id=0');
  72. }
  73. $this->view->assign("groupId",$this->groupId);
  74. $this->assignconfig('groupId',$this->groupId);
  75. $this->assignconfig('authId',$this->authId);
  76. }
  77. public function index()
  78. {
  79. //今天
  80. $today = date('Ymd', time());
  81. //昨天
  82. $yesterday = date('Ymd', strtotime('-1 days'));
  83. //本月
  84. $month = date('Ym', time()).'01';
  85. $business_line = $this->request->param('business_line');
  86. if (!$business_line) {
  87. $business_line = PayConstants::BUSINESS_WECHAT;
  88. }
  89. if($this->groupId < AdminConstants::ADMIN_GROUP_ID_CHANNEL){ //管理员
  90. $todayData = $this->model->where(['type'=>'1','flag'=>'2','admin_id'=>0,'createdate'=>$today,'business_line'=> $business_line])->find(); //今天的数据
  91. $yesterdayData = $this->model->where(['type'=>'1','flag'=>'2','admin_id'=>0,'createdate'=>$yesterday,'business_line'=> $business_line])->find(); //昨天的数据
  92. // $monthData = $this->model->where(['type'=>'2','flag'=>'2','admin_id'=>0,'createdate'=>$month])->find(); //当月的数据
  93. $monthData = $this->model->sumCollect(0, 1, 2, $month, $yesterday, $business_line); //当月的数据
  94. // $allData = $this->model->where(['type'=>'3','flag'=>'2','admin_id'=>0,])->find(); //汇总的数据
  95. $allData = $this->model->sumCollect(0, 1, 2, null, null, $business_line); //汇总的数据
  96. $this->assign('guide_pvuv',null);
  97. $this->assign('today',$todayData);
  98. $this->assign('yesterday',$yesterdayData);
  99. $this->assign('month',$monthData);
  100. $this->assign('all',$allData);
  101. }elseif($this->groupId == AdminConstants::ADMIN_GROUP_ID_AGENT){ //代理商
  102. $todayData = $this->model->where(['type'=>'1','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$today,'business_line'=> $business_line])->find(); //今天的数据
  103. $yesterdayData = $this->model->where(['type'=>'1','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$yesterday,'business_line'=> $business_line])->find(); //昨天的数据
  104. // $monthData = $this->model->where(['type'=>'2','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$month])->find(); //当月的数据
  105. $monthData = $this->model->sumCollect($this->authId, 1, 1, $month, $yesterday, $business_line); //当月的数据
  106. // $allData = $this->model->where(['type'=>'3','flag'=>'1','admin_id'=>$this->authId,])->find(); //汇总的数据
  107. $allData = $this->model->sumCollect($this->authId, 1, 1, null, null, $business_line); //汇总的数据
  108. $pvuv['pv'] = Redis::instance()->get(PvuvCollectService::instance()->getPvKey($this->authId));
  109. $pvuv['uv'] = Redis::instance()->pfCount(PvuvCollectService::instance()->getUvKey($this->authId));
  110. $this->assign('guide_pvuv',$this->getGuidePvUv($this->authId,$this->groupId,$today));
  111. $this->assign('pvuv',$pvuv);
  112. $this->assign('today',$todayData);
  113. $this->assign('yesterday',$yesterdayData);
  114. $this->assign('month',$monthData);
  115. $this->assign('all',$allData);
  116. }else{ //渠道商
  117. $todayData = $this->model->where(['type'=>'1','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$today,'business_line'=> $business_line])->find(); //今天自己的数据
  118. $todayAgentData = $this->model->where(['type'=>'1','flag'=>'2','admin_id'=>$this->authId,'createdate'=>$today,'business_line'=> $business_line])->find(); //今天代理商的数据
  119. $todayAllData = $this->model->where(['type'=>'1','flag'=>'3','admin_id'=>$this->authId,'createdate'=>$today,'business_line'=> $business_line])->find(); //今天所有的数据
  120. $pvuv['pv'] = Redis::instance()->get(PvuvCollectService::instance()->getPvKey($this->authId));
  121. $pvuv['uv'] = Redis::instance()->pfCount(PvuvCollectService::instance()->getUvKey($this->authId));
  122. $this->assign('pvuv',$pvuv);
  123. if($todayAllData){
  124. if($todayData){
  125. $todayAllData['recharge_money'] = $todayData->recharge_money;
  126. $todayAllData['recharge_money_benefit'] = $todayData->recharge_money_benefit;
  127. }else{
  128. $todayAllData['recharge_money'] = 0;
  129. $todayAllData['recharge_money_benefit'] = 0;
  130. }
  131. if($todayAgentData){
  132. $todayAllData['agent_recharge'] = $todayAgentData->recharge_money;
  133. $todayAllData['agent_recharge_benefit'] = $todayAgentData->recharge_money_benefit;
  134. }else{
  135. $todayAllData['agent_recharge'] = 0;
  136. $todayAllData['agent_recharge_benefit'] = 0;
  137. }
  138. }
  139. $yesterdayData = $this->model->where(['type'=>'1','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$yesterday,'business_line'=> $business_line])->find(); //昨天自己的数据
  140. $yesterdayAgentData = $this->model->where(['type'=>'1','flag'=>'2','admin_id'=>$this->authId,'createdate'=>$yesterday,'business_line'=> $business_line])->find(); //昨天代理商的数据
  141. $yesterdayAllData = $this->model->where(['type'=>'1','flag'=>'3','admin_id'=>$this->authId,'createdate'=>$yesterday,'business_line'=> $business_line])->find(); //昨天所有的数据
  142. if(!empty($yesterdayAllData)){
  143. if($yesterdayData){
  144. $yesterdayAllData['recharge_money'] = $yesterdayData->recharge_money;
  145. $yesterdayAllData['recharge_money_benefit'] = $yesterdayData->recharge_money_benefit;
  146. }else{
  147. $yesterdayAllData['recharge_money'] = 0;
  148. $yesterdayAllData['recharge_money_benefit'] = 0;
  149. }
  150. if($yesterdayAgentData){
  151. $yesterdayAllData['agent_recharge'] = $yesterdayAgentData->recharge_money;
  152. $yesterdayAllData['agent_recharge_benefit'] = $yesterdayAgentData->recharge_money_benefit;
  153. }else{
  154. $yesterdayAllData['agent_recharge'] = 0;
  155. $yesterdayAllData['agent_recharge_benefit'] = 0;
  156. }
  157. }
  158. // $monthData = $this->model->where(['type'=>'2','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$month])->find(); //当月自己的数据
  159. $monthData = $this->model->sumCollect($this->authId,1,1,$month,$yesterday, $business_line); //当月自己的数据
  160. // $monthAgentData = $this->model->where(['type'=>'2','flag'=>'2','admin_id'=>$this->authId,'createdate'=>$month])->find(); //当月代理商的数据
  161. $monthAgentData = $this->model->sumCollect($this->authId,1,2,$month,$yesterday, $business_line); //当月代理商的数据
  162. // $monthAllData = $this->model->where(['type'=>'2','flag'=>'3','admin_id'=>$this->authId,'createdate'=>$month])->find(); //当月所有的数据
  163. $monthAllData = $this->model->sumCollect($this->authId,1,3,$month,$yesterday, $business_line); //当月所有的数据
  164. if(!empty($monthAllData)){
  165. if($monthData){
  166. $monthAllData['recharge_money'] = $monthData->recharge_money;
  167. $monthAllData['recharge_money_benefit'] = $monthData->recharge_money_benefit;
  168. }else{
  169. $monthAllData['recharge_money'] = 0;
  170. $monthAllData['recharge_money_benefit'] = 0;
  171. }
  172. if($monthAgentData){
  173. $monthAllData['agent_recharge'] = $monthAgentData->recharge_money;
  174. $monthAllData['agent_recharge_benefit'] = $monthAgentData->recharge_money_benefit;
  175. }else{
  176. $monthAllData['agent_recharge'] = 0;
  177. $monthAllData['agent_recharge_benefit'] = 0;
  178. }
  179. }
  180. // $allData = $this->model->where(['type'=>'3','flag'=>'1','admin_id'=>$this->authId,])->find(); //汇总自己的数据
  181. $allData = $this->model->sumCollect($this->authId,1,1, null, null, $business_line); //汇总自己的数据
  182. // $allAgentData = $this->model->where(['type'=>'3','flag'=>'2','admin_id'=>$this->authId,])->find(); //汇总代理商的数据
  183. $allAgentData = $this->model->sumCollect($this->authId,1,2, null, null, $business_line); //汇总代理商的数据
  184. // $allAllData = $this->model->where(['type'=>'3','flag'=>'3','admin_id'=>$this->authId,])->find(); //汇总所有的数据
  185. $allAllData = $this->model->sumCollect($this->authId,1,3, null, null, $business_line); //汇总所有的数据
  186. if($allAllData){
  187. if($allData){
  188. $allAllData['recharge_money'] = $allData->recharge_money;
  189. $allAllData['recharge_money_benefit'] = $allData->recharge_money_benefit;
  190. }else{
  191. $allAllData['recharge_money'] = 0;
  192. $allAllData['recharge_money_benefit'] = 0;
  193. }
  194. if($allAgentData){
  195. $allAllData['agent_recharge'] = $allAgentData->recharge_money;
  196. $allAllData['agent_recharge_benefit'] = $allAgentData->recharge_money_benefit;
  197. }else{
  198. $allAllData['agent_recharge'] = 0;
  199. $allAllData['agent_recharge_benefit'] = 0;
  200. }
  201. }
  202. $this->assign('guide_pvuv',$this->getGuidePvUv($this->authId,$this->groupId,$today));
  203. $this->assign('today',$todayAllData);
  204. $this->assign('yesterday',$yesterdayAllData);
  205. $this->assign('month',$monthAllData);
  206. $this->assign('all',$allAllData);
  207. }
  208. if ($this->request->isAjax()) {
  209. $beginDate = date('Ymd', strtotime('-31 days'));
  210. $endDate = date('Ymd', strtotime('-1 days'));
  211. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  212. $condition = [];
  213. $condition['type'] = 1;
  214. $condition['business_line'] = $business_line;
  215. if($this->groupId<3){ //管理员
  216. $condition['admin_id'] = 0;
  217. $condition['flag'] = 2;
  218. $beginDate = date('Ymd', strtotime('-62 days'));
  219. }elseif($this->groupId == 3){ //渠道商
  220. $condition['admin_id'] = $this->authId;
  221. $condition['flag'] = 3;
  222. }elseif($this->groupId == 4){ //代理商
  223. $condition['admin_id'] = $this->authId;
  224. $condition['flag'] = 1;
  225. }
  226. if($this->groupId != 3){
  227. $total = $this->model
  228. ->where($where)
  229. ->where('createdate','>=',$beginDate)
  230. ->where('createdate','<=',$endDate)
  231. ->where($condition)
  232. ->order($sort, $order)
  233. ->count();
  234. $list = $this->model
  235. ->where($where)
  236. ->where('createdate','>=',$beginDate)
  237. ->where('createdate','<=',$endDate)
  238. ->where($condition)
  239. ->order($sort, $order)
  240. ->limit($offset, $limit)
  241. ->select();
  242. $guide_pv_uv = $this->getGuidePvUv($this->authId,$this->groupId,[$beginDate,$endDate]);
  243. foreach ($list as $key=>$val) {
  244. $list[$key]['guide_pv'] = $guide_pv_uv[$val['createdate']]['pv'] ?? 0;
  245. $list[$key]['guide_uv'] = $guide_pv_uv[$val['createdate']]['uv'] ?? 0;
  246. }
  247. $result = array("total" => $total, "rows" => $list);
  248. }else{ //渠道商数据统计
  249. $total = $this->model
  250. ->where($where)
  251. ->where('createdate','>=',$beginDate)
  252. ->where('createdate','<=',$endDate)
  253. ->where($condition)
  254. ->order($sort, $order)
  255. ->count();
  256. $list = $this->model
  257. ->where($where)
  258. ->where('createdate','>=',$beginDate)
  259. ->where('createdate','<=',$endDate)
  260. ->where($condition)
  261. ->order($sort, $order)
  262. ->limit($offset, $limit)
  263. ->select();
  264. //代理商数据
  265. $condition_agent = [
  266. 'business_line'=>$business_line
  267. ];
  268. $condition_agent['type'] = 1;
  269. $condition_agent['admin_id'] = $this->authId;
  270. $condition_agent['flag'] = 2;
  271. $agentArr = [];
  272. $list_agent = $this->model
  273. ->where($where)
  274. ->where('createdate','>=',$beginDate)
  275. ->where('createdate','<=',$endDate)
  276. ->where($condition_agent)
  277. ->order($sort, $order)
  278. ->select();
  279. if($list_agent){
  280. foreach ($list_agent as $key=>$val) {
  281. $agentArr[$val['createdate']] = $val;
  282. }
  283. }
  284. //渠道商flag=1 渠道商自身数据
  285. $condition_channel = [
  286. 'business_line'=>$business_line
  287. ];
  288. $condition_channel['type'] = 1;
  289. $condition_channel['admin_id'] = $this->authId;
  290. $condition_channel['flag'] = 1;
  291. $channelArr = [];
  292. $list_channel = $this->model
  293. ->where($where)
  294. ->where('createdate','>=',$beginDate)
  295. ->where('createdate','<=',$endDate)
  296. ->where($condition_channel)
  297. ->order($sort, $order)
  298. ->select();
  299. if($list_channel){
  300. foreach($list_channel as $key=>$val){
  301. $channelArr[$val['createdate']] = $val;
  302. }
  303. }
  304. $guide_pv_uv = $this->getGuidePvUv($this->authId,$this->groupId,[$beginDate,$endDate]);
  305. foreach ($list as $key=>$val) {
  306. if(array_key_exists($val['createdate'],$channelArr)){
  307. $list[$key]['self_recharge'] = $channelArr[$val['createdate']]['recharge_money'];
  308. $list[$key]['self_recharge_benefit'] = $channelArr[$val['createdate']]['recharge_money_benefit'];
  309. //total_benefit 渠道商自身的分成收益+代理商充值-代理商分成收益(之前晓红顶的算法)
  310. if(array_key_exists($val['createdate'],$agentArr)) { //当天有代理商数据
  311. //$list[$key]['total_benefit'] = $channelArr[$val['createdate']]['recharge_money_benefit'] + $agentArr[$val['createdate']]['recharge_money'] - $agentArr[$val['createdate']]['recharge_money_benefit'];
  312. $list[$key]['total_benefit'] = $val['recharge_money_benefit'] - $agentArr[$val['createdate']]['recharge_money_benefit'];
  313. }else{ //代理商没有当天的数据;
  314. $list[$key]['total_benefit'] = $val['recharge_money_benefit'];
  315. }
  316. }else{
  317. //$list[$key]['total_benefit'] = $val['recharge_money_benefit'];
  318. if(array_key_exists($val['createdate'],$agentArr)){
  319. //$list[$key]['total_benefit'] = $agentArr[$val['createdate']]['recharge_money'] - $agentArr[$val['createdate']]['recharge_money_benefit'];
  320. $list[$key]['total_benefit'] = $val['recharge_money_benefit'] - $agentArr[$val['createdate']]['recharge_money_benefit'];
  321. }else{
  322. $list[$key]['total_benefit'] = 0;
  323. }
  324. $list[$key]['self_recharge'] = 0;
  325. $list[$key]['self_recharge_benefit'] = 0;
  326. }
  327. if(array_key_exists($val['createdate'],$agentArr)){
  328. $list[$key]['agent_benefit'] =$agentArr[$val['createdate']]['recharge_money_benefit'];
  329. $list[$key]['agent_recharge'] =$agentArr[$val['createdate']]['recharge_money'];
  330. }else{
  331. $list[$key]['agent_benefit'] = 0;
  332. $list[$key]['agent_recharge'] = 0;
  333. }
  334. $list[$key]['guide_pv'] = $guide_pv_uv[$val['createdate']]['pv'] ?? 0;
  335. $list[$key]['guide_uv'] = $guide_pv_uv[$val['createdate']]['uv'] ?? 0;
  336. }
  337. unset($channelArr);
  338. unset($agentArr);
  339. $result = array("total" => $total, "rows" => $list);
  340. }
  341. return json($result);
  342. }
  343. return $this->view->fetch();
  344. }
  345. //今日数据统计异步请求
  346. public function ajaxToday(){
  347. $today = date('Ymd', time());
  348. $groupId = $this->request->post('groupId');
  349. $authId = $this->request->post('authId');
  350. $business_line = $this->request->param('business_line');
  351. if (!$business_line) {
  352. $business_line = PayConstants::BUSINESS_WECHAT;
  353. }
  354. if ($this->request->isGet() || !$groupId || !$authId) {
  355. LogService::notice("invalid request for ajaxToday");
  356. return json(['code' => 'request deny']);
  357. }
  358. if($groupId < 3){ //管理员
  359. $todayData = Db::table('orders_collect')->where(['type'=>'1','flag'=>'2','admin_id'=>0,'createdate'=>$today, 'business_line'=>$business_line])->find(); //今天的数据
  360. if($todayData){
  361. $todayData['normal_percent'] = 0;
  362. if ($todayData['kandian_recharge_orders_count']) {
  363. $todayData['normal_percent'] = floor($todayData['kandian_recharge_orders']/$todayData['kandian_recharge_orders_count']*100);
  364. } else if ($todayData['normal_recharge_orders_count']) {
  365. $todayData['normal_percent'] = floor($todayData['normal_recharge_orders']/$todayData['normal_recharge_orders_count']*100);
  366. }
  367. $todayData['vip_percent'] = empty($todayData['vip_recharge_orders_count'])? 0 : floor($todayData['vip_recharge_orders']/$todayData['vip_recharge_orders_count']*100);
  368. }else{
  369. $todayData['recharge_money'] = 0;
  370. $todayData['recharge_money_benefit'] = 0;
  371. $todayData['agent_recharge'] = 0;
  372. $todayData['agent_recharge_benefit'] = 0;
  373. $todayData['normal_percent'] = 0;
  374. $todayData['vip_percent'] = 0;
  375. $todayData['normal_recharge_orders'] = 0;
  376. $todayData['normal_recharge_orders_count'] = 0;
  377. $todayData['vip_recharge_orders'] = 0;
  378. $todayData['vip_recharge_orders_count'] = 0;
  379. $todayData['normal_recharge_money'] = 0;
  380. $todayData['vip_recharge_money'] = 0;
  381. }
  382. $todayData['guide_pv'] = 0;
  383. $todayData['guide_uv'] = 0;
  384. return json($todayData);
  385. }elseif($groupId==4){ //分销商
  386. $todayData = Db::table('orders_collect')->where(['type'=>'1','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$today, 'business_line'=>$business_line])->find(); //今天的数据
  387. if($todayData){
  388. $todayData['normal_percent'] = 0;
  389. if ($todayData['kandian_recharge_orders_count']) {
  390. $todayData['normal_percent'] = floor($todayData['kandian_recharge_orders']/$todayData['kandian_recharge_orders_count']*100);
  391. } else if ($todayData['normal_recharge_orders_count']) {
  392. $todayData['normal_percent'] = floor($todayData['normal_recharge_orders']/$todayData['normal_recharge_orders_count']*100);
  393. }
  394. $todayData['vip_percent'] = empty($todayData['vip_recharge_orders_count'])? 0 : floor($todayData['vip_recharge_orders']/$todayData['vip_recharge_orders_count']*100) ;
  395. }else{
  396. $todayData['recharge_money'] = 0;
  397. $todayData['recharge_money_benefit'] = 0;
  398. $todayData['agent_recharge'] = 0;
  399. $todayData['agent_recharge_benefit'] = 0;
  400. $todayData['normal_percent'] = 0;
  401. $todayData['vip_percent'] = 0;
  402. $todayData['normal_recharge_orders'] = 0;
  403. $todayData['normal_recharge_orders_count'] = 0;
  404. $todayData['vip_recharge_orders'] = 0;
  405. $todayData['vip_recharge_orders_count'] = 0;
  406. $todayData['normal_recharge_money'] = 0;
  407. $todayData['vip_recharge_money'] = 0;
  408. }
  409. $twoHoursEarlier = time() - 7200;
  410. $pvuv = Db::table('admin_puv')
  411. ->where('admin_id', $authId)
  412. ->where('updatetime', '>', $twoHoursEarlier)
  413. ->find();
  414. if(!empty($pvuv)){
  415. $todayData['pv'] = $pvuv['pv'];
  416. $todayData['uv'] = $pvuv['uv'];
  417. }else{
  418. $todayData['pv'] = 0;
  419. $todayData['uv'] = 0;
  420. }
  421. $guide_collect_data = $this->getGuidePvUv($authId,$groupId,$today);
  422. $todayData['guide_pv'] = $guide_collect_data['pv'];
  423. $todayData['guide_uv'] = $guide_collect_data['uv'];
  424. return json($todayData);
  425. }else{ //渠道商
  426. $todayData = $this->model->where(['type'=>'1','flag'=>'1','admin_id'=>$this->authId,'createdate'=>$today, 'business_line'=>$business_line])->find(); //今天自己的数据
  427. $todayAgentData = $this->model->where(['type'=>'1','flag'=>'2','admin_id'=>$this->authId,'createdate'=>$today, 'business_line'=>$business_line])->find(); //今天代理商的数据
  428. $todayAllData = $this->model->where(['type'=>'1','flag'=>'3','admin_id'=>$this->authId,'createdate'=>$today, 'business_line'=>$business_line])->find(); //今天所有的数据
  429. $twoHoursEarlier = time() - 7200;
  430. $pvuv = Db::table('admin_puv')
  431. ->where('admin_id', $authId)
  432. ->where('updatetime', '>', $twoHoursEarlier)
  433. ->find();
  434. if($todayAllData){
  435. if($todayData){
  436. $todayAllData['recharge_money'] = $todayData->recharge_money;
  437. $todayAllData['recharge_money_benefit'] = $todayData->recharge_money_benefit;
  438. }else{
  439. $todayAllData['recharge_money'] = 0;
  440. $todayAllData['recharge_money_benefit'] = 0;
  441. }
  442. if($todayAgentData){
  443. $todayAllData['agent_recharge'] = $todayAgentData->recharge_money;
  444. $todayAllData['agent_recharge_benefit'] = $todayAgentData->recharge_money_benefit;
  445. }else{
  446. $todayAllData['agent_recharge'] = 0.00;
  447. $todayAllData['agent_recharge_benefit'] = 0.00;
  448. }
  449. $todayAllData['normal_percent'] = 0;
  450. if ($todayAllData['kandian_recharge_orders_count']) {
  451. $todayAllData['normal_percent'] = floor($todayAllData['kandian_recharge_orders']/$todayAllData['kandian_recharge_orders_count']*100);
  452. } else if ($todayAllData['normal_recharge_orders_count']) {
  453. $todayAllData['normal_percent'] = floor($todayAllData['normal_recharge_orders']/$todayAllData['normal_recharge_orders_count']*100);
  454. }
  455. $todayAllData['vip_percent'] = empty($todayAllData['vip_recharge_orders_count'])? 0 : floor($todayAllData['vip_recharge_orders']/$todayAllData['vip_recharge_orders_count']*100);
  456. }else{
  457. $todayAllData['recharge_money'] = 0;
  458. $todayAllData['recharge_money_benefit'] = 0;
  459. $todayAllData['agent_recharge'] = 0;
  460. $todayAllData['agent_recharge_benefit'] = 0;
  461. $todayAllData['normal_percent'] = 0;
  462. $todayAllData['vip_percent'] = 0;
  463. $todayAllData['normal_recharge_orders'] = 0;
  464. $todayAllData['normal_recharge_orders_count'] = 0;
  465. $todayAllData['vip_recharge_orders'] = 0;
  466. $todayAllData['vip_recharge_orders_count'] = 0;
  467. $todayAllData['normal_recharge_money'] = 0;
  468. $todayAllData['vip_recharge_money'] = 0;
  469. }
  470. if(!empty($pvuv)){
  471. $todayAllData['pv'] = $pvuv['pv'];
  472. $todayAllData['uv'] = $pvuv['uv'];
  473. }else{
  474. $todayAllData['pv'] = 0;
  475. $todayAllData['uv'] = 0;
  476. }
  477. $guide_collect_data = $this->getGuidePvUv($authId,$groupId,$today);
  478. $todayAllData['guide_pv'] = $guide_collect_data['pv'];
  479. $todayAllData['guide_uv'] = $guide_collect_data['uv'];
  480. return json($todayAllData);
  481. }
  482. }
  483. /**
  484. * 获取前置域名PV UV
  485. * @param $admin_id
  486. * @param $group_id
  487. * @param $date
  488. * @return array
  489. */
  490. public function getGuidePvUv($admin_id,$group_id,$date){
  491. if(!is_array($date)){
  492. $result = ['pv'=>0,'uv'=>0];
  493. //渠道商
  494. if(intval($group_id) == AdminConstants::ADMIN_GROUP_ID_CHANNEL){
  495. $agent_ids = model('AdminExtend')->where('create_by',$admin_id)->column('admin_id');
  496. array_push($agent_ids,$admin_id);
  497. $map = ['admin_id'=>['in',implode(',',$agent_ids)],'createdate'=>$date,'type'=>'1'];
  498. $result_collect = model('GuideCollect')->where($map)->field('sum(pv) as pv,sum(uv) as uv')->find();
  499. $result['pv'] = $result_collect['pv'] ?? 0;
  500. $result['uv'] = $result_collect['uv'] ?? 0;
  501. }
  502. //代理商
  503. if(intval($group_id) == AdminConstants::ADMIN_GROUP_ID_AGENT){
  504. $map = ['admin_id'=>$admin_id,'createdate'=>$date,'type'=>'1'];
  505. $result_collect = model('GuideCollect')->where($map)->find();
  506. $result['pv'] = $result_collect['pv'] ?? 0;
  507. $result['uv'] = $result_collect['uv'] ?? 0;
  508. }
  509. return $result;
  510. }else{
  511. list($begin,$end) = $date;
  512. $result_collect = null;
  513. //渠道商
  514. if(intval($group_id) == AdminConstants::ADMIN_GROUP_ID_CHANNEL){
  515. $agent_ids = model('AdminExtend')->where('create_by',$admin_id)->column('admin_id');
  516. array_push($agent_ids,$admin_id);
  517. $map = ['admin_id'=>['in',implode(',',$agent_ids)],'type'=>'1'];
  518. $result_collect = model('GuideCollect')
  519. ->where($map)
  520. ->where('createdate','>=',$begin)
  521. ->where('createdate','<=',$end)
  522. ->field('createdate,sum(pv) as pv,sum(uv) as uv')
  523. ->group('createdate')
  524. ->select();
  525. }
  526. //代理商
  527. if(intval($group_id) == AdminConstants::ADMIN_GROUP_ID_AGENT){
  528. $map = ['admin_id'=>$admin_id,'type'=>'1'];
  529. $result_collect = model('GuideCollect')
  530. ->where($map)
  531. ->where('createdate','>=',$begin)
  532. ->where('createdate','<=',$end)
  533. ->field('createdate,sum(pv) as pv ,sum(uv) as uv')
  534. ->group('createdate')
  535. ->select();
  536. }
  537. if($result_collect){
  538. $result_collect = collection($result_collect)->toArray();
  539. $temp_key = array_column($result_collect,'createdate');
  540. $result_collect = array_combine($temp_key,$result_collect);
  541. }
  542. return $result_collect;
  543. }
  544. }
  545. public function kl()
  546. {
  547. }
  548. }