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