Export.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace app\weixin\service;
  3. use app\weixin\model\Exports;
  4. use think\Db;
  5. class Export
  6. {
  7. /**
  8. * 更新数据
  9. * @return array
  10. * @throws \think\Exception
  11. * @throws \think\exception\PDOException
  12. */
  13. public static function updateData()
  14. {
  15. $ids = [];
  16. $counts = Export::getCounts(true);
  17. $date = date('Y-m-d');
  18. foreach ($counts as $k => $v) {
  19. if (Export::checkHasByType($k, $date)) {
  20. $ids[$k] = $v;
  21. Exports::where(['type' => $k, 'date' => $date])->update(['num' => $v]);
  22. } else if (Exports::insertGetId(['type' => $k, 'num' => $v, 'date' => $date, 'create_time' => date('Y-m-d H:i:s')])) {
  23. $ids[$k] = $v;
  24. }
  25. }
  26. // 清除4个月以前的统计数据
  27. Exports::where('create_time', '<', time() - 6 * 30 * 24 * 3600)->delete();
  28. return $ids;
  29. }
  30. /**
  31. * 导出上个月报表数据
  32. * @param string $month
  33. * @throws \PHPExcel_Exception
  34. * @throws \PHPExcel_Reader_Exception
  35. * @throws \PHPExcel_Writer_Exception
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. */
  40. public static function exportData($month='')
  41. {
  42. $month = $month? $month : date('Y-m-01');
  43. $monthText = date('Y年m月', strtotime($month));
  44. $PHPExcel = new \PHPExcel();
  45. $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象
  46. $PHPSheet->setTitle("{$monthText}统计报表数据"); /* 给当前sheet表设置名称及表头 */
  47. $PHPSheet->setCellValue('A1', '序号');
  48. $PHPSheet->setCellValue('B1', '统计类型');
  49. $PHPSheet->setCellValue('C1', '统计总数');
  50. $dataList = [];
  51. $date = Exports::where('date', 'like', "{$month}%")
  52. ->group('type')
  53. ->order('date','desc')
  54. ->value('date');
  55. if($date){
  56. $dataList = Exports::where('date', '=', $date)
  57. ->select();
  58. $dataList = $dataList ? $dataList->toArray() : [];
  59. }
  60. $accessDatas = Exports::whereIn('type', ['login', 'match', 'book', 'activity', 'home', 'center', 'heart'])
  61. ->where('date', 'like', "{$month}%")
  62. ->group('type')
  63. ->sum('num');
  64. $types = ['userCount' => '用户总数量', 'monthUserCount' => '本月新增注册用户', 'monthFans' => '本月新增粉丝', 'totalMarketCount' => '总推广合伙人', 'monthMarketCount' => '本月新增推广合伙人', 'totalMarketInviteCount' => '合伙人总推广用户数', 'totalMarketInviteCount1' => '合伙人总推广实际注册用户数', 'monthMarketInviteCount' => '本月合伙人新增推广用户', 'monthMarketInviteCount1' => '本月合伙人邀请注册用户', 'activityCount' => '活动数量', 'monthRecharge' => '本月充值人数', 'monthRechargeMoney' => '本月充值金额', 'totalRecharge' => '累计充值人数', 'totalRechargeMoney' => '累计充值金额', 'monthVipCount' => '本月开通VIP数量', 'monthVipMoney' => '本月开通VIP金额', 'totalVipCount' => '累计开通VIP人数', 'totalVipMoney' => '累计开通VIP金额', 'monthHandCount' => '本月牵线付费人数', 'monthHandMoney' => '本月牵线付费金额', 'totalHandCount' => '累计牵线付费人数', 'totalHandMoney' => '累计牵线付费金额', 'monthContactCount' => '本月申请微信次数', 'userlogout' => '累计注销人数', 'bookCount' => '累计报名人数', 'bookMoney' => '累计报名金额', 'bookCancelCount' => '累计报名退款次数', 'bookCancelMoney' => '累计报名退款金额', 'monthBookCount' => '本月总报名人数', 'monthBookMoney' => '本月总报名金额', 'login' => '本月登录人数', 'match' => '本月访问单身推荐', 'heart' => '本月访问怦然心动', 'center' => '本月访问用户中心', 'home' => "本月访问个人主页", 'activity' => '本月访问活动列表', 'book' => '本月想要报名','maxLogin'=>'本月最高登录人数'];
  65. $k = 1;
  66. foreach ($dataList as $key => $item) {
  67. $type = isset($types[$item['type']]) ? $types[$item['type']] : '';
  68. $num = isset($item['num'])? moneyFormat($item['num']) : 0;
  69. if ($type) {
  70. $k += 1;
  71. if(in_array($type, ['login', 'match', 'book', 'activity', 'home', 'center', 'heart'])){
  72. $num = isset($accessDatas[$type])? moneyFormat($accessDatas[$type]) : 0;
  73. }
  74. $num = intval($num) == $num? intval($num) : $num;
  75. $PHPSheet->setCellValue('A' . $k, $key);
  76. $PHPSheet->setCellValue('B' . $k, $type.' ');
  77. $PHPSheet->setCellValue('C' . $k, $num);
  78. }
  79. }
  80. ob_clean();
  81. $filename = "{$monthText}后台主页数据统计报表.xls";
  82. header("Pragma: public");
  83. header("Expires: 0");
  84. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  85. header("Content-Type:application/force-download");
  86. header("Content-Type:application/vnd.ms-execl");
  87. header("Content-Type:application/octet-stream");
  88. header("Content-Type:application/download");;
  89. header('Content-Disposition:attachment;filename=' . $filename . '');
  90. header("Content-Transfer-Encoding:binary");
  91. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');
  92. $PHPWriter->save('php://output');
  93. }
  94. /**
  95. * 验证师傅已经统计
  96. * @param $type
  97. * @return bool|mixed
  98. */
  99. public
  100. static function checkHasByType($type, $date = '')
  101. {
  102. $cacheKey = "counts:check:{$type}";
  103. if (PRedis::get($cacheKey)) {
  104. return true;
  105. }
  106. $date = $date ? $date : date("y-m-d");
  107. if ($id = Exports::where(['type' => $type, 'date' => $date])->value('id')) {
  108. PRedis::set($cacheKey, $id, rand(10, 20));
  109. }
  110. return $id;
  111. }
  112. public
  113. static function getCounts($reload = false)
  114. {
  115. // 统计数据
  116. $cacheKeys = "counts:all";
  117. $counts = PRedis::get($cacheKeys);
  118. if ($counts && !$reload) {
  119. return $counts;
  120. }
  121. $time = strtotime(date('Y-m-d', time()));
  122. $monthTime = strtotime(date('Y-m-01'));
  123. $counts['userCount'] = Db::name('user')
  124. ->where(['user_type' => 2, 'user_status' => 1, 'agent_type' => 0])
  125. ->where('openid', 'not in', '')
  126. ->count('id');
  127. $counts['realUserCount'] = Db::name('user')->alias('u')
  128. ->leftJoin('user_profile up', 'up.userid=u.id')
  129. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'up.idcard_check' => 2])
  130. ->where('u.openid', 'not in', '')
  131. ->count('u.id');
  132. $counts['todayUserCount'] = Db::name('user')->alias('u')
  133. ->leftJoin('user_profile up', 'up.userid=u.id')
  134. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'up.idcard_check' => 2])
  135. ->where('u.create_time', '>', $time)
  136. ->where('u.create_time', '<=', time())
  137. ->where('u.openid', 'not in', '')
  138. ->count('u.id');
  139. $counts['monthUserCount'] = Db::name('user')->alias('u')
  140. ->leftJoin('user_profile up', 'up.userid=u.id')
  141. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'up.idcard_check' => 2])
  142. ->where('u.create_time', '>=', $monthTime)
  143. ->where('u.create_time', '<=', time())
  144. ->where('u.openid', 'not in', '')
  145. ->count('u.id');
  146. $counts['monthFans'] = Db::name('user')
  147. ->where(['user_type' => 2, 'user_status' => 1, 'agent_type' => 0])
  148. ->where('create_time', '>', $monthTime)
  149. ->where('create_time', '<=', time())
  150. ->where('openid', 'not in', '')
  151. ->count('id');
  152. $counts['todayFans'] = Db::name('user')
  153. ->where(['user_type' => 2, 'user_status' => 1, 'agent_type' => 0])
  154. ->where('create_time', '>', $time)
  155. ->where('create_time', '<=', $time + 86400)
  156. ->where('openid', 'not in', '')
  157. ->count('id');
  158. $counts['marketCount'] = Db::name('user')
  159. ->where(['user_type' => 2, 'agent_status' => 1, 'agent_type' => 1])
  160. ->where('agent_create_time', '>', $time)
  161. ->where('agent_create_time', '<=', $time + 86400)
  162. ->count('id');
  163. $counts['monthMarketCount'] = Db::name('user')
  164. ->where(['user_type' => 2, 'agent_status' => 1, 'agent_type' => 1])
  165. ->where('agent_create_time', '>=', $monthTime)
  166. ->where('agent_create_time', '<=', $time + 86400)
  167. ->count('id');
  168. $counts['totalMarketCount'] = Db::name('user')
  169. ->where(['user_type' => 2, 'agent_status' => 1, 'agent_type' => 1])
  170. ->where('agent_create_time', '<=', $time + 86400)
  171. ->count('id');
  172. $counts['marketInviteCount'] = Db::name('user')
  173. ->alias('u')
  174. ->join('sg_user uu', 'uu.id=u.parent_id', 'left')
  175. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'uu.agent_type' => 1])
  176. ->where('u.create_time', '>=', $time)
  177. ->where('u.create_time', '<=', $time + 86400)
  178. ->count('u.id');
  179. $counts['marketInviteCount1'] = Db::name('user')
  180. ->alias('u')
  181. ->leftJoin('sg_user uu', 'uu.id=u.parent_id')
  182. ->leftJoin('sg_user_profile up', 'up.userid=u.id')
  183. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'uu.agent_type' => 1, 'up.idcard_check' => 2])
  184. ->where('u.create_time', '>=', $time)
  185. ->where('u.create_time', '<=', $time + 86400)
  186. ->count('u.id');
  187. $counts['monthMarketInviteCount'] = Db::name('user')
  188. ->alias('u')
  189. ->join('sg_user uu', 'uu.id=u.parent_id', 'left')
  190. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'uu.agent_type' => 1])
  191. ->where('u.create_time', '>=', $monthTime)
  192. ->where('u.create_time', '<=', $time + 86400)
  193. ->count('u.id');
  194. $counts['monthMarketInviteCount1'] = Db::name('user')
  195. ->alias('u')
  196. ->leftJoin('sg_user uu', 'uu.id=u.parent_id')
  197. ->leftJoin('sg_user_profile up', 'up.userid=u.id')
  198. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'uu.agent_type' => 1, 'up.idcard_check' => 2])
  199. ->where('u.create_time', '>=', $monthTime)
  200. ->where('u.create_time', '<=', $time + 86400)
  201. ->count('u.id');
  202. $counts['totalMarketInviteCount'] = Db::name('user')
  203. ->alias('u')
  204. ->join('sg_user uu', 'uu.id=u.parent_id', 'left')
  205. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'uu.agent_type' => 1])
  206. ->where('u.create_time', '<=', $time + 86400)
  207. ->count('u.id');
  208. $counts['totalMarketInviteCount1'] = Db::name('user')
  209. ->alias('u')
  210. ->leftJoin('sg_user uu', 'uu.id=u.parent_id')
  211. ->leftJoin('sg_user_profile up', 'up.userid=u.id')
  212. ->where(['u.user_type' => 2, 'u.user_status' => 1, 'u.agent_type' => 0, 'uu.agent_type' => 1, 'up.idcard_check' => 2])
  213. ->where('u.create_time', '<=', $time + 86400)
  214. ->count('u.id');
  215. $counts['activityCount'] = Db::name('activity')
  216. ->where(['status' => 1])
  217. ->count('id');
  218. $counts['recharge'] = Db::name('user_recharge_log')
  219. ->where(['type' => 1, 'status' => 2])
  220. ->where('pay_at', '>=', date('Y-m-d'))
  221. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  222. ->count('id');
  223. $counts['rechargeMoney'] = Db::name('user_recharge_log')
  224. ->where(['type' => 1, 'status' => 2])
  225. ->where('pay_at', '>=', date('Y-m-d'))
  226. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  227. ->sum('pay_money');
  228. $counts['monthRecharge'] = Db::name('user_recharge_log')
  229. ->where(['type' => 1, 'status' => 2])
  230. ->where('pay_at', '>=', date('Y-m-01'))
  231. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  232. ->count('id');
  233. $counts['monthRechargeMoney'] = Db::name('user_recharge_log')
  234. ->where(['type' => 1, 'status' => 2])
  235. ->where('pay_at', '>=', date('Y-m-01'))
  236. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  237. ->sum('pay_money');
  238. $counts['totalRecharge'] = Db::name('user_recharge_log')
  239. ->where(['type' => 1, 'status' => 2])
  240. ->count('id');
  241. $counts['totalRechargeMoney'] = Db::name('user_recharge_log')
  242. ->where(['type' => 1, 'status' => 2])
  243. ->sum('pay_money');
  244. $counts['vipMoney'] = Db::name('user_recharge_log')
  245. ->where(['type' => 4, 'status' => 2])
  246. ->where('pay_at', '>=', date('Y-m-d'))
  247. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  248. ->sum('pay_money');
  249. $counts['monthVipMoney'] = Db::name('user_recharge_log')
  250. ->where(['type' => 4, 'status' => 2])
  251. ->where('pay_at', '>=', date('Y-m-01'))
  252. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  253. ->sum('pay_money');
  254. $counts['vipCount'] = Db::name('user_recharge_log')
  255. ->where(['type' => 4, 'status' => 2])
  256. ->where('pay_at', '>=', date('Y-m-d'))
  257. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  258. ->count('id');
  259. $counts['monthVipCount'] = Db::name('user_recharge_log')
  260. ->where(['type' => 4, 'status' => 2])
  261. ->where('pay_at', '>=', date('Y-m-01'))
  262. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  263. ->count('id');
  264. $counts['totalVipCount'] = Db::name('user_recharge_log')
  265. ->where(['type' => 4, 'status' => 2])
  266. ->count('id');
  267. $counts['totalVipMoney'] = Db::name('user_recharge_log')
  268. ->where(['type' => 4, 'status' => 2])
  269. ->sum('pay_money');
  270. $counts['handMoney'] = Db::name('user_recharge_log')
  271. ->where(['type' => 5, 'status' => 2])
  272. ->where('pay_at', '>=', date('Y-m-d'))
  273. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  274. ->sum('pay_money');
  275. $counts['monthHandMoney'] = Db::name('user_recharge_log')
  276. ->where(['type' => 5, 'status' => 2])
  277. ->where('pay_at', '>=', date('Y-m-01'))
  278. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  279. ->sum('pay_money');
  280. $counts['handCount'] = Db::name('user_recharge_log')
  281. ->where(['type' => 5, 'status' => 2])
  282. ->where('pay_at', '>=', date('Y-m-d'))
  283. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  284. ->count('id');
  285. $counts['monthHandCount'] = Db::name('user_recharge_log')
  286. ->where(['type' => 5, 'status' => 2])
  287. ->where('pay_at', '>=', date('Y-m-01'))
  288. ->where('pay_at', '<=', date('Y-m-d', $time + 86400))
  289. ->count('id');
  290. $counts['totalHandCount'] = Db::name('user_recharge_log')
  291. ->where(['type' => 5, 'status' => 2])
  292. ->count('id');
  293. $counts['totalHandMoney'] = Db::name('user_recharge_log')
  294. ->where(['type' => 5, 'status' => 2])
  295. ->sum('pay_money');
  296. // 申请微信次数
  297. $counts['contactCount'] = Db::name('user_contact_logs')
  298. ->where(function ($query) {
  299. $query->whereIn('status', [1, 2])->whereOr(['status' => 3, 'is_read' => 1]);
  300. })
  301. ->where('created_at', '>=', date('Y-m-d'))
  302. ->where('created_at', '<=', date('Y-m-d', $time + 86400))
  303. ->count('id');
  304. $counts['monthContactCount'] = Db::name('user_contact_logs')
  305. ->where(function ($query) {
  306. $query->whereIn('status', [1, 2])->whereOr(['status' => 3, 'is_read' => 1]);
  307. })
  308. ->where('created_at', '>=', date('Y-m-01'))
  309. ->where('created_at', '<=', date('Y-m-d', $time + 86400))
  310. ->count('id');
  311. // 其他访问统计
  312. $counts['login'] = Db::name('user')
  313. ->where(['user_type' => 2, 'user_status' => 1])
  314. ->where('last_login_time', '>=', strtotime(date('Y-m-d', $time)))
  315. ->where('last_login_time', '<=', strtotime(date('Y-m-d', $time + 86400)))
  316. ->count('id');
  317. $counts['maxLogin'] = Exports::where(['type' => 'login'])
  318. ->where('date', 'like', "".date('Y-m')."%")
  319. ->max('num');
  320. if($counts['maxLogin'] < $counts['login']){
  321. $counts['maxLogin'] = $counts['login'];
  322. }
  323. //统计注销总人数
  324. $counts['userlogout'] = Db::name('user')
  325. ->where(['user_type' => 2, 'user_status' => -1])
  326. ->count('id');
  327. // 报名人数
  328. $counts['bookCount'] = Db::name('books')
  329. ->where('status', 'in', [2, 3])
  330. ->count('id');
  331. $counts['bookMoney'] = Db::name('books')
  332. ->where('status', 'in', [2, 3])
  333. ->sum('money');
  334. $counts['bookCancelCount'] = Db::name('books')
  335. ->where('status', '=', 5)
  336. ->count('id');
  337. $counts['bookCancelMoney'] = Db::name('books')
  338. ->where('status', '=', 5)
  339. ->sum('money');
  340. $counts['monthBookCount'] = Db::name('books')
  341. ->where('status', 'in', [2, 3])
  342. ->where('book_at', '>=', date('Y-m-01'))
  343. ->where('book_at', '<=', date('Y-m-d', $time + 86400))
  344. ->count('id');
  345. $counts['monthBookMoney'] = Db::name('books')
  346. ->where('status', 'in', [2, 3])
  347. ->where('book_at', '>=', date('Y-m-01'))
  348. ->where('book_at', '<=', date('Y-m-d', $time + 86400))
  349. ->sum('money');
  350. $cacheKey = 'counts:users:' . date('Ymd') . '_';
  351. $counts['activity'] = PRedis::get($cacheKey . 'activity'); // 活动列表
  352. $counts['activity'] = $counts['activity'] ? $counts['activity'] : 0;
  353. $counts['book'] = PRedis::get($cacheKey . 'book'); // 想要报名
  354. $counts['book'] = $counts['book'] ? $counts['book'] : 0;
  355. $counts['match'] = PRedis::get($cacheKey . 'match'); // 访问单身推荐
  356. $counts['match'] = $counts['match'] ? $counts['match'] : 0;
  357. $counts['heart'] = PRedis::get($cacheKey . 'heart'); // 访问怦然心动
  358. $counts['heart'] = $counts['heart'] ? $counts['heart'] : 0;
  359. $counts['center'] = PRedis::get($cacheKey . 'center'); // 访问个人中心
  360. $counts['center'] = $counts['center'] ? $counts['center'] : 0;
  361. $counts['home'] = PRedis::get($cacheKey . 'home'); // 访问个人主页
  362. $counts['home'] = $counts['home'] ? $counts['home'] : 0;
  363. PRedis::set($cacheKeys, $counts, rand(5, 10));
  364. // 后台调用更新
  365. $tempKey = "counts:update";
  366. if (!PRedis::get($tempKey) && !$reload) {
  367. $date = date('Y-m-d');
  368. foreach ($counts as $k => $v) {
  369. if (Export::checkHasByType($k, $date)) {
  370. $ids[$k] = $v;
  371. Exports::where(['type' => $k, 'date' => $date])->update(['num' => $v]);
  372. } else if (Exports::insertGetId(['type' => $k, 'num' => $v, 'date' => $date, 'create_time' => date('Y-m-d H:i:s')])) {
  373. $ids[$k] = $v;
  374. }
  375. }
  376. PRedis::set($tempKey, $ids, rand(10, 30));
  377. }
  378. return $counts;
  379. }
  380. }