截流自动化的商城平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

StatLogic.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\admin\logic\index;
  20. use app\common\basics\Logic;
  21. use app\common\server\UrlServer;
  22. use app\common\enum\PayEnum;
  23. use think\facade\Db;
  24. /**
  25. * 工作台统计
  26. * Class StatLogic
  27. * @package app\admin\logic\index
  28. */
  29. class StatLogic extends Logic
  30. {
  31. //工作台基本数据 TODO
  32. public static function stat()
  33. {
  34. //更新时间
  35. $time = date('Y-m-d H:i:s', time());
  36. //头部数据统计
  37. $data = $where = [];
  38. $where[] = ['pay_status', '>', PayEnum::UNPAID];
  39. //成交笔数
  40. $order_num_all = Db::name('order')
  41. ->where($where)
  42. ->count('id');
  43. $order_num_yesterday = Db::name('order')
  44. ->where($where)
  45. ->whereTime('create_time', 'yesterday')
  46. ->count('id');
  47. $order_num_today = Db::name('order')
  48. ->where($where)
  49. ->whereTime('create_time', 'today')
  50. ->count('id');
  51. $order_num_change_red = 0;
  52. $order_num_change_add = $order_num_today - $order_num_yesterday;
  53. if($order_num_change_add < 0){
  54. $order_num_change_red = abs($order_num_change_add);
  55. }
  56. //营业额
  57. $order_price_all = Db::name('order')
  58. ->where($where)
  59. ->sum('order_amount');
  60. $order_price_yesterday = Db::name('order')
  61. ->where($where)
  62. ->whereTime('create_time', 'yesterday')
  63. ->sum('order_amount');
  64. $order_price_today = Db::name('order')
  65. ->where($where)
  66. ->whereTime('create_time', 'today')
  67. ->sum('order_amount');
  68. $order_price_change_red = 0;
  69. $order_price_change_add = $order_price_today - $order_price_yesterday;
  70. if($order_price_change_add < 0){
  71. $order_price_change_red = abs($order_price_change_add);
  72. }
  73. //新增会员
  74. $add_user_all = Db::name('user')
  75. ->count('id');
  76. $add_user_yesterday = Db::name('user')
  77. ->whereTime('create_time', 'yesterday')
  78. ->count('id');
  79. $add_user_today = Db::name('user')
  80. ->whereTime('create_time', 'today')
  81. ->count('id');
  82. $add_user_change_red = 0;
  83. $add_user_change_add = $add_user_today - $add_user_yesterday;
  84. if($add_user_change_add < 0){
  85. $add_user_change_red = abs($add_user_change_add);
  86. }
  87. //用户访问量UV
  88. $visit_user_all = Db::name('stat')
  89. ->group('ip')
  90. ->count('id');
  91. $visit_user_yesterday = Db::name('stat')
  92. ->whereTime('create_time', 'yesterday')
  93. ->group('ip')
  94. ->count('id');
  95. $visit_user_today = Db::name('stat')
  96. ->whereTime('create_time', 'today')
  97. ->group('ip')
  98. ->count('id');
  99. $visit_user_change_red = 0;
  100. $visit_user_change_add = $visit_user_today - $visit_user_yesterday;
  101. if($visit_user_change_add < 0){
  102. $visit_user_change_red = abs($visit_user_change_add);
  103. }
  104. $data = [
  105. 'order_num' => [
  106. 'yesterday' => $order_num_yesterday,
  107. 'today' => $order_num_today,
  108. 'change_add' => $order_num_change_add,
  109. 'change_red' => $order_num_change_red,
  110. 'all_num' => $order_num_all
  111. ],
  112. 'order_price' => [
  113. 'yesterday' => number_format($order_price_yesterday,2),
  114. 'today' => number_format($order_price_today,2),
  115. 'change_add' => number_format($order_price_change_add,2),
  116. 'change_red' => number_format($order_price_change_red,2),
  117. 'all_price' => number_format($order_price_all,2)
  118. ],
  119. 'add_user_num' => [
  120. 'yesterday' => $add_user_yesterday,
  121. 'today' => $add_user_today,
  122. 'change_add' => $add_user_change_add,
  123. 'change_red' => $add_user_change_red,
  124. 'all_num' => $add_user_all
  125. ],
  126. 'visit_user_num' => [
  127. 'yesterday' => $visit_user_yesterday,
  128. 'today' => $visit_user_today,
  129. 'change_add' => $visit_user_change_add,
  130. 'change_red' => $visit_user_change_red,
  131. 'all_num' => $visit_user_all
  132. ],
  133. ];
  134. return [
  135. 'time' => $time,
  136. 'data' => $data,
  137. ];
  138. }
  139. //图标数据
  140. public static function graphData()
  141. {
  142. //当前时间戳
  143. $start_t = time();
  144. //echarts图表数据
  145. $echarts_order_amount = [];
  146. $echarts_user_pv = [];
  147. $dates = [];
  148. for ($i = 15; $i >= 1; $i--) {
  149. $where_start = strtotime("- ".$i."day", $start_t);
  150. $dates[] = date('m-d',$where_start);
  151. $start_now = strtotime(date('Y-m-d',$where_start));
  152. $end_now = strtotime(date('Y-m-d 23:59:59',$where_start));
  153. $amount = Db::name('order')
  154. ->where([['create_time','between',[$start_now, $end_now]],['pay_status', '>', PayEnum::UNPAID]])
  155. ->sum('order_amount');
  156. $pv = Db::name('stat')
  157. ->where([['create_time','between',[$start_now, $end_now]]])
  158. ->group('ip')
  159. ->count('id');
  160. $echarts_order_amount[] = sprintf("%.2f",substr(sprintf("%.3f", $amount), 0, -2));
  161. $echarts_user_pv[] = $pv;
  162. }
  163. return [
  164. 'echarts_order_amount' => $echarts_order_amount,
  165. 'echarts_user_visit' => $echarts_user_pv,
  166. 'dates' => $dates,
  167. ];
  168. }
  169. // 工作台商家数据
  170. public static function shopLists($get)
  171. {
  172. $shop_list = [];
  173. // 销冠商家、人气商家的商家列表
  174. if($get['type'] == 1){
  175. // 销冠商家
  176. $shop_list = Db::name('order')->alias('o')
  177. ->join('shop s','s.id = o.shop_id')
  178. ->where([['order_amount','>',0], ['pay_status', '>', PayEnum::UNPAID]])
  179. ->group('shop_id')
  180. ->limit(5)
  181. ->order('order_amount desc')
  182. ->column('o.shop_id,sum(o.order_amount) as order_amount,s.logo,s.name');
  183. foreach($shop_list as $k => $shop){
  184. $shop_list[$k]['number'] = $k+1;
  185. $shop_list[$k]['order_amount'] = '¥'.number_format($shop['order_amount'],2);
  186. $shop_list[$k]['logo'] = UrlServer::getFileUrl($shop['logo']);
  187. }
  188. }else{
  189. // 人气商家
  190. $shop_list = Db::name('shop')
  191. ->order('visited_num desc')
  192. ->where([['visited_num','>',0]])
  193. ->limit(5)
  194. ->column('id,logo,name,visited_num');
  195. foreach($shop_list as $k => $shop){
  196. $shop_list[$k]['number'] = $k+1;
  197. $shop_list[$k]['logo'] = UrlServer::getFileUrl($shop['logo']);
  198. }
  199. }
  200. return ['count'=>0,'lists'=>$shop_list];
  201. }
  202. }