Vipindex.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\OrdersCollect;
  5. use think\Controller;
  6. use think\Db;
  7. use think\Log;
  8. /**
  9. * 数据统计-订单汇总
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Vipindex extends Backend
  14. {
  15. /**
  16. * @var OrdersCollect
  17. */
  18. protected $model = null;
  19. protected $groupId;
  20. protected $authId;
  21. /**
  22. * @var VipAdminBind VipAdminBind模型对象
  23. */
  24. protected $vipAdminBindModel = null;
  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. $this->vipAdminBindModel = model('VipAdminBind');
  33. $truegroupId = $group->group_id;
  34. // if(isset($_GET['channel_id']) && intval($_GET['channel_id'])>0){
  35. // $groupObj = model('AuthGroupAccess')->where('uid',$_GET['channel_id'])->find();
  36. // if($groupObj){
  37. // $thisGroup = $groupObj->group_id;
  38. // if($thisGroup > 2){ //判断传过来的不是管理员的id
  39. // if($thisGroup==3){ //如果是渠道商
  40. // if($truegroupId <3){ //只有管理员才有此操作
  41. // $this->authId = $_GET['channel_id'];
  42. // $thisChannel = model('Admin')->where('id',$this->authId)->find();
  43. // $this->groupId = 3;
  44. // $this->assign('channelTitle','当前是渠道商('.$thisChannel->nickname.')的数据');
  45. // $this->assignconfig('channelstr','?channel_id='.$this->authId);
  46. // }
  47. // }else{
  48. // if($truegroupId <4){ //管理员或渠道商才有此操作
  49. // if($truegroupId == 3){ //如果是从渠道商查来的,判断当前代理商是不是渠道商下属
  50. // $isCh = model('AdminExtend')->where(['admin_id'=>$_GET['channel_id'],'create_by'=>$group->uid])->find();
  51. // if($isCh){ //如果是当前渠道商的下属
  52. // $this->authId = $_GET['channel_id'];
  53. // $thisChannel = model('Admin')->where('id',$this->authId)->find();
  54. // $this->groupId = 4;
  55. // $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
  56. // $this->assignconfig('channelstr','?channel_id='.$this->authId);
  57. // }
  58. // }else{ //管理员查过来的
  59. // $this->authId = $_GET['channel_id'];
  60. // $thisChannel = model('Admin')->where('id',$this->authId)->find();
  61. // $this->groupId = 4;
  62. // $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
  63. // $this->assignconfig('channelstr','?channel_id='.$this->authId);
  64. // }
  65. //
  66. // }
  67. // }
  68. // }
  69. // }
  70. //
  71. // }else{
  72. // $this->authId = $this->auth->id;
  73. // $this->groupId = $truegroupId;
  74. // $this->assignconfig('channelstr','?channel_id=0');
  75. // }
  76. $this->view->assign("groupId",$this->groupId);
  77. $this->assignconfig('groupId',$this->groupId);
  78. $this->assignconfig('authId',$this->authId);
  79. }
  80. public function index()
  81. {
  82. $vipadminIds = model("VipAdminBind")
  83. ->where("admin_id_master",$this->auth->id)
  84. ->where("flag = 1")
  85. ->column("admin_id_slave");
  86. $zeroKey = array_search(0, $vipadminIds);
  87. if ($zeroKey !== false) {
  88. array_splice($vipadminIds, $zeroKey, 1);
  89. }
  90. //今天
  91. $today = date('Ymd', time());
  92. //昨天
  93. $yesterday = date('Ymd', strtotime('-1 days'));
  94. //本月
  95. $month = date('Ym', time()).'01';
  96. $fields = "sum(recharge_money) recharge_money,sum(recharge_money_benefit) recharge_money_benefit,sum(normal_recharge_orders_count) normal_recharge_orders_count,sum(normal_recharge_orders) normal_recharge_orders,sum(normal_recharge_money) normal_recharge_money,sum(normal_recharge_money_benefit) normal_recharge_money_benefit,sum(normal_recharge_user_suc_count) normal_recharge_user_suc_count,sum(vip_recharge_orders_count) vip_recharge_orders_count,sum(vip_recharge_orders) vip_recharge_orders,sum(vip_recharge_money) vip_recharge_money,sum(pv) pv,sum(uv) uv";
  97. if ($vipadminIds) {
  98. $todayData = $this->model
  99. ->field($fields)
  100. ->where(['type' => '1', 'flag' => '1', 'createdate' => $today])
  101. ->where("admin_id", "in", $vipadminIds)
  102. ->find(); //今天自己的数据
  103. $todayAgentData = $this->model
  104. ->field($fields)
  105. ->where(['type' => '1', 'flag' => '2', 'createdate' => $today])
  106. ->where("admin_id", "in", $vipadminIds)
  107. ->find(); //今天代理商的数据
  108. $todayAllData = $this->model
  109. ->field($fields)
  110. ->where(['type' => '1', 'flag' => '3', 'createdate' => $today])
  111. ->where("admin_id", "in", $vipadminIds)
  112. ->find(); //今天所有的数据
  113. $pvuv = Db::table('admin_puv')
  114. ->field("sum(pv) pv,sum(uv) uv")
  115. ->where('admin_id', "in", $vipadminIds)
  116. ->where("updatetime", "egt", strtotime("today"))
  117. ->find();
  118. } else {
  119. $todayData = $todayAgentData = $todayAllData = false;
  120. $pvuv = ['pv' => 0, 'uv' => 0];
  121. }
  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. if ($vipadminIds) {
  140. $yesterdayData = $this->model
  141. ->where(['type' => '1', 'flag' => '1', 'createdate' => $yesterday])
  142. ->where("admin_id", "in", $vipadminIds)
  143. ->field($fields)
  144. ->find(); //昨天自己的数据
  145. $yesterdayAgentData = $this->model
  146. ->where(['type' => '1', 'flag' => '2', 'createdate' => $yesterday])
  147. ->where("admin_id", "in", $vipadminIds)
  148. ->field($fields)
  149. ->find(); //昨天代理商的数据
  150. $yesterdayAllData = $this->model
  151. ->where(['type' => '1', 'flag' => '3', 'createdate' => $yesterday])
  152. ->where("admin_id", "in", $vipadminIds)
  153. ->field($fields)
  154. ->find(); //昨天所有的数据
  155. } else {
  156. $yesterdayData = $yesterdayAgentData = $yesterdayAllData = false;
  157. }
  158. if(!empty($yesterdayAllData)){
  159. if($yesterdayData){
  160. $yesterdayAllData['recharge_money'] = $yesterdayData->recharge_money;
  161. $yesterdayAllData['recharge_money_benefit'] = $yesterdayData->recharge_money_benefit;
  162. }else{
  163. $yesterdayAllData['recharge_money'] = 0;
  164. $yesterdayAllData['recharge_money_benefit'] = 0;
  165. }
  166. if($yesterdayAgentData){
  167. $yesterdayAllData['agent_recharge'] = $yesterdayAgentData->recharge_money;
  168. $yesterdayAllData['agent_recharge_benefit'] = $yesterdayAgentData->recharge_money_benefit;
  169. }else{
  170. $yesterdayAllData['agent_recharge'] = 0;
  171. $yesterdayAllData['agent_recharge_benefit'] = 0;
  172. }
  173. }
  174. $monthData = $this->model
  175. ->sumCollect($vipadminIds,1,1,$month,$yesterday); //当月自己的数据
  176. $monthAgentData = $this->model
  177. ->sumCollect($vipadminIds,1,2,$month,$yesterday); //当月代理商的数据
  178. $monthAllData = $this->model
  179. ->sumCollect($vipadminIds,1,3,$month,$yesterday); //当月所有的数据
  180. if(!empty($monthAllData)){
  181. if($monthData){
  182. $monthAllData['recharge_money'] = $monthData->recharge_money;
  183. $monthAllData['recharge_money_benefit'] = $monthData->recharge_money_benefit;
  184. }else{
  185. $monthAllData['recharge_money'] = 0;
  186. $monthAllData['recharge_money_benefit'] = 0;
  187. }
  188. if($monthAgentData){
  189. $monthAllData['agent_recharge'] = $monthAgentData->recharge_money;
  190. $monthAllData['agent_recharge_benefit'] = $monthAgentData->recharge_money_benefit;
  191. }else{
  192. $monthAllData['agent_recharge'] = 0;
  193. $monthAllData['agent_recharge_benefit'] = 0;
  194. }
  195. }
  196. $allData = $this->model
  197. ->sumCollect($vipadminIds,1,1); //汇总自己的数据
  198. $allAgentData = $this->model
  199. ->sumCollect($vipadminIds,1,2); //汇总代理商的数据
  200. $allAllData = $this->model
  201. ->sumCollect($vipadminIds,1,3); //汇总所有的数据
  202. if($allAllData){
  203. if($allData){
  204. $allAllData['recharge_money'] = $allData->recharge_money;
  205. $allAllData['recharge_money_benefit'] = $allData->recharge_money_benefit;
  206. }else{
  207. $allAllData['recharge_money'] = 0;
  208. $allAllData['recharge_money_benefit'] = 0;
  209. }
  210. if($allAgentData){
  211. $allAllData['agent_recharge'] = $allAgentData->recharge_money;
  212. $allAllData['agent_recharge_benefit'] = $allAgentData->recharge_money_benefit;
  213. }else{
  214. $allAllData['agent_recharge'] = 0;
  215. $allAllData['agent_recharge_benefit'] = 0;
  216. }
  217. }
  218. $this->assign('today',$todayAllData);
  219. $this->assign('yesterday',$yesterdayAllData);
  220. $this->assign('month',$monthAllData);
  221. $this->assign('all',$allAllData);
  222. if ($this->request->isAjax()) {
  223. $beginDate = date('Ymd', strtotime('-31 days'));
  224. $endDate = date('Ymd', strtotime('-1 days'));
  225. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  226. $condition = [];
  227. $condition['type'] = 1;
  228. $condition['flag'] = 3;
  229. // $total = $this->model
  230. // ->where($where)
  231. // ->where($condition)
  232. // ->where("createdate",$today)
  233. // ->where("admin_id","in",$vipadminIds)
  234. // ->order($sort, $order)
  235. // ->count();
  236. if ($vipadminIds) {
  237. $total = model("Admin")
  238. ->where("id","in",$vipadminIds)
  239. ->count();
  240. $list = $this->model
  241. ->alias("a")
  242. // ->where($condition)
  243. ->join("admin_config ac","a.admin_id = ac.admin_id AND a.type = 1 AND a.flag = 3 AND a.createdate = {$today}","right")
  244. // ->where("a.createdate",$today)
  245. ->where("ac.admin_id","in",$vipadminIds)
  246. ->field("a.type,a.flag,a.createdate,a.recharge_money,a.normal_recharge_money,a.normal_recharge_orders,a.vip_recharge_money
  247. ,a.vip_recharge_orders,ac.json,ac.admin_id")
  248. ->order("a.recharge_money", "desc")
  249. // ->order($sort, $order)
  250. ->limit($offset, $limit)
  251. ->select();
  252. foreach ($list as &$v){
  253. $v['name'] = $v['json']?json_decode($v['json'])->authorizer_info->nick_name:'';
  254. unset($v['json']);
  255. }
  256. $result = array("total" => $total, "rows" => $list);
  257. }else{
  258. $result = array("total" => [], "rows" => 0);
  259. }
  260. return json($result);
  261. }
  262. return $this->view->fetch();
  263. }
  264. /**
  265. * 近30天汇总,不包括今天
  266. * @return \think\response\Json
  267. */
  268. public function orderThirtyStatistics(){
  269. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  270. $vipadminIds = model("VipAdminBind")
  271. ->where("admin_id_master",$this->auth->id)
  272. ->where("flag = 1")
  273. ->column("admin_id_slave");
  274. $beginDate = date('Ymd', strtotime('-30 days'));
  275. $endDate = date('Ymd', strtotime('-1 days'));
  276. $total = $this->model
  277. ->where("type", 1)
  278. ->where("flag", 3)
  279. ->where("createdate", ">=", $beginDate)
  280. ->where("createdate", "<=", $endDate)
  281. ->where("admin_id", "in", $vipadminIds)
  282. ->group("createdate,type,flag")
  283. ->count();
  284. $list = $this->model
  285. ->where("type", 1)
  286. ->where("flag", 3)
  287. ->where("createdate", ">=", $beginDate)
  288. ->where("createdate", "<=", $endDate)
  289. ->where("admin_id", "in", $vipadminIds)
  290. ->field([
  291. "DATE_FORMAT(createdate, '%Y-%m-%d')" => "f_date",
  292. "SUM(recharge_money)" => "recharge_money_total",
  293. "SUM(normal_recharge_orders)" => "normal_recharge_orders_total",
  294. "SUM(normal_recharge_user_suc_count)" => "normal_user_num_total",
  295. "SUM(vip_recharge_money)" => "vip_recharge_money_total",
  296. "SUM(vip_recharge_orders)" => "vip_recharge_orders_total",
  297. "SUM(vip_recharge_user_suc_count)" => "vip_user_num_total",
  298. "type","flag"
  299. ])
  300. ->group("createdate,type,flag")
  301. ->limit($offset,$limit)
  302. ->order('createdate', 'desc')
  303. ->select();
  304. foreach ($list as $key => &$value){
  305. $list[$key]['money_total'] = $value['recharge_money_total'];
  306. $list[$key]['orders_total'] = $value['normal_recharge_orders_total'] + $value['vip_recharge_orders_total'];
  307. $list[$key]['user_num_total'] = $value['normal_user_num_total'] + $value['vip_user_num_total'];
  308. }
  309. $result = array("total" => $total, "rows" => $list);
  310. return json($result);
  311. }
  312. }