截流自动化的商城平台
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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\shop\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($shop_id)
  33. {
  34. //更新时间
  35. $time = date('Y-m-d H:i:s', time());
  36. //头部数据统计
  37. $data = $where = [];
  38. $where[] = ['pay_status', '>', PayEnum::UNPAID];
  39. $where[] = ['shop_id','=',$shop_id];
  40. //成交笔数
  41. $order_num_all = Db::name('order')
  42. ->where($where)
  43. ->count('id');
  44. $order_num_yesterday = Db::name('order')
  45. ->where($where)
  46. ->whereTime('create_time', 'yesterday')
  47. ->count('id');
  48. $order_num_today = Db::name('order')
  49. ->where($where)
  50. ->whereTime('create_time', 'today')
  51. ->count('id');
  52. $order_num_change_red = 0;
  53. $order_num_change_add = $order_num_today - $order_num_yesterday;
  54. if($order_num_change_add < 0){
  55. $order_num_change_red = abs($order_num_change_add);
  56. }
  57. //销售金额
  58. $order_price_all = Db::name('order')
  59. ->where($where)
  60. ->sum('order_amount') ?? 0;
  61. $order_price_yesterday = Db::name('order')
  62. ->where($where)
  63. ->whereTime('create_time', 'yesterday')
  64. ->sum('order_amount') ?? 0;
  65. $order_price_today = Db::name('order')
  66. ->where($where)
  67. ->whereTime('create_time', 'today')
  68. ->sum('order_amount') ?? 0;
  69. $order_price_change_red = 0;
  70. $order_price_change_add = $order_price_today - $order_price_yesterday;
  71. if($order_price_change_add < 0){
  72. $order_price_change_red = abs($order_price_change_add);
  73. }
  74. $where = [];
  75. $where[] = ['shop_id','=',$shop_id];
  76. //进店人数
  77. $add_user_all = Db::name('shop_stat')
  78. ->where($where)
  79. ->group(['ip'])
  80. ->count('id');
  81. $add_user_yesterday = Db::name('shop_stat')
  82. ->where($where)
  83. ->whereTime('create_time', 'yesterday')
  84. ->group(['ip'])
  85. ->count('id');
  86. $add_user_today = Db::name('shop_stat')
  87. ->where($where)
  88. ->whereTime('create_time', 'today')
  89. ->group(['ip'])
  90. ->count('id');
  91. $add_user_change_red = 0;
  92. $add_user_change_add = $add_user_today - $add_user_yesterday;
  93. if($add_user_change_add < 0){
  94. $add_user_change_red = abs($add_user_change_add);
  95. }
  96. //商品浏览人数
  97. $visit_user_all = Db::name('goods_click')
  98. ->where($where)
  99. ->group(['user_id'])
  100. ->count('id');
  101. $visit_user_yesterday = Db::name('goods_click')
  102. ->where($where)
  103. ->whereTime('create_time', 'yesterday')
  104. ->group(['user_id'])
  105. ->count('id');
  106. $visit_user_today = Db::name('goods_click')
  107. ->where($where)
  108. ->whereTime('create_time', 'today')
  109. ->group(['user_id'])
  110. ->count('id');
  111. $visit_user_change_red = 0;
  112. $visit_user_change_add = $visit_user_today - $visit_user_yesterday;
  113. if($visit_user_change_add < 0){
  114. $visit_user_change_red = abs($visit_user_change_add);
  115. }
  116. $data = [
  117. 'order_num' => [
  118. 'yesterday' => $order_num_yesterday,
  119. 'today' => $order_num_today,
  120. 'change_add' => $order_num_change_add,
  121. 'change_red' => $order_num_change_red,
  122. 'all_num' => $order_num_all
  123. ],
  124. 'order_price' => [
  125. 'yesterday' => number_format($order_price_yesterday,2),
  126. 'today' => number_format($order_price_today,2),
  127. 'change_add' => number_format($order_price_change_add,2),
  128. 'change_red' => number_format($order_price_change_red,2),
  129. 'all_price' => number_format($order_price_all,2)
  130. ],
  131. 'add_user_num' => [
  132. 'yesterday' => $add_user_yesterday,
  133. 'today' => $add_user_today,
  134. 'change_add' => $add_user_change_add,
  135. 'change_red' => $add_user_change_red,
  136. 'all_num' => $add_user_all
  137. ],
  138. 'visit_user_num' => [
  139. 'yesterday' => $visit_user_yesterday,
  140. 'today' => $visit_user_today,
  141. 'change_add' => $visit_user_change_add,
  142. 'change_red' => $visit_user_change_red,
  143. 'all_num' => $visit_user_all
  144. ],
  145. ];
  146. return [
  147. 'time' => $time,
  148. 'data' => $data,
  149. ];
  150. }
  151. //图标数据 TODO
  152. public static function graphData($shop_id)
  153. {
  154. //当前时间戳
  155. $start_t = time();
  156. //echarts图表数据
  157. $echarts_order_amount = [];
  158. $echarts_user_pv = [];
  159. $dates = [];
  160. for ($i = 15; $i >= 1; $i--) {
  161. $where_start = strtotime("- ".$i."day", $start_t);
  162. $dates[] = date('m-d',$where_start);
  163. $start_now = strtotime(date('Y-m-d',$where_start));
  164. $end_now = strtotime(date('Y-m-d 23:59:59',$where_start));
  165. $amount = Db::name('order')
  166. ->where([['shop_id','=',$shop_id],['create_time','between',[$start_now, $end_now]],['pay_status','>',PayEnum::UNPAID]])
  167. ->sum('order_amount');
  168. $pv = Db::name('shop_stat')
  169. ->where([['shop_id','=',$shop_id],['create_time','between',[$start_now, $end_now]]])
  170. ->group('ip')
  171. ->count('id');
  172. $echarts_order_amount[] = sprintf("%.2f",substr(sprintf("%.3f", $amount), 0, -2));
  173. $echarts_user_pv[] = $pv;
  174. }
  175. return [
  176. 'echarts_order_amount' => $echarts_order_amount,
  177. 'echarts_user_visit' => $echarts_user_pv,
  178. 'dates' => $dates,
  179. ];
  180. }
  181. // 工作台商品数据
  182. public static function goodsLists($get,$shop_id)
  183. {
  184. $goods_list = [];
  185. // 销冠商品、人气商品的商品列表
  186. if($get['type'] == 1){
  187. // 销冠商品
  188. $goods_list = Db::name('order')->alias('o')
  189. ->join('order_goods og','og.order_id = o.id')
  190. ->where([['o.shop_id','=',$shop_id],['o.pay_status','>',PayEnum::UNPAID]])
  191. ->group('og.goods_id')
  192. ->limit(5)
  193. ->order('order_amount desc')
  194. ->column('sum(o.order_amount) as order_amount, og.goods_id');
  195. foreach($goods_list as $k => $item){
  196. $goods_list[$k]['number'] = $k+1;
  197. $goods_list[$k]['order_amount'] = '¥'.number_format($item['order_amount'],2);
  198. $goods_list[$k]['image'] = '';
  199. $goods_list[$k]['name'] = '';
  200. $goods_info = Db::name('goods')
  201. ->where(['id'=>$item['goods_id']])
  202. ->field('name,image')
  203. ->find();
  204. if($goods_info){
  205. $goods_list[$k]['image'] = UrlServer::getFileUrl($goods_info['image']);
  206. $goods_list[$k]['name'] = $goods_info['name'];
  207. }
  208. }
  209. }else{
  210. // 人气商品
  211. $goods_list = Db::name('goods_click')
  212. ->where([['shop_id','=',$shop_id]])
  213. ->group(['goods_id'])
  214. ->field('goods_id,count(DISTINCT user_id) as visited_num')
  215. ->order('visited_num desc')
  216. ->limit(5)
  217. ->select()
  218. ->toArray();
  219. $num = 0;
  220. foreach($goods_list as $k => $item){
  221. if($item['visited_num']){
  222. $num++;
  223. $goods_list[$k]['number'] = $num;
  224. $goods_list[$k]['logo'] = $goods_list[$k]['name'] = '';
  225. $goods = Db::name('goods')
  226. ->where(['id'=>$item['goods_id']])
  227. ->field('name,image')
  228. ->find();
  229. if($goods){
  230. $goods_list[$k]['image'] = UrlServer::getFileUrl($goods['image']);
  231. $goods_list[$k]['name'] = $goods['name'];
  232. }
  233. }else{
  234. unset($goods_list[$k]);
  235. }
  236. }
  237. }
  238. return ['count'=>0,'lists'=>$goods_list];
  239. }
  240. }