Sale.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\statistics;
  10. use app\admin\service\Service;
  11. use app\common\models\users\Users as UsersModel;
  12. use app\admin\model\Order as OrderModel;
  13. use app\admin\model\order\OrderGoods as OrderGoodsModel;
  14. use mall\utils\Tool;
  15. class Sale extends Service {
  16. /**
  17. * 获取销售排行数据
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public static function getSalesRanking(){
  24. // 取得会员总数
  25. $users_count = UsersModel::count();
  26. // 购买过订单的会员数
  27. $order_count = OrderModel::field("COUNT(DISTINCT user_id) as count")->where("user_id > 0 AND status=5")->find();
  28. // 会员订单总数和订单总购物额
  29. $order = OrderModel::field("COUNT(*) AS order_num,SUM(order_amount) as order_amount_total")->where("user_id > 0 AND status=5")->find();
  30. // 注册后还未购买过商品的会员
  31. $users_goods_count = UsersModel::field("COUNT(DISTINCT o.user_id) as count")->alias("u")->join("order o","o.user_id=u.id","LEFT")->where("o.user_id > 0 AND o.status=5")->find();
  32. $not_goods_users_count = $users_count - $users_goods_count["count"];
  33. // 注册会员购买率
  34. $cart = sprintf("%0.2f", ($users_count > 0 ? $not_goods_users_count / $users_count : 0) * 100);
  35. // 订单排行
  36. $fields = "SUM(o.order_amount) as total,COUNT(o.id) as count,u.username";
  37. $order_hot = OrderModel::field($fields)
  38. ->alias("o")
  39. ->join("users u","o.user_id=u.id","LEFT")
  40. ->where("o.user_id > 0 AND o.status=5")
  41. ->group("o.user_id")->order("total DESC")
  42. ->limit(10)->select()->toArray();
  43. $i=1;
  44. foreach($order_hot as $k=>$v){
  45. $order_hot[$k]['p'] = $i++;
  46. }
  47. return [
  48. "a"=>$cart,
  49. "b"=>$users_count,
  50. "c"=>$not_goods_users_count,
  51. "d"=>$users_goods_count,
  52. "e"=>empty($order["order_amount_total"]) ? "0.00" : $order["order_amount_total"],
  53. "f"=>empty($order["order_amount_total"]) ? "0.00" : sprintf("%.2f",($order["order_amount_total"] > 0 && $users_goods_count["count"] > 0 ? $order["order_amount_total"]/$users_goods_count["count"] : 0)),
  54. "g"=>$order_hot
  55. ];
  56. }
  57. /**
  58. * 获取购买排行数据
  59. * @param $data
  60. * @return array
  61. * @throws \Exception
  62. */
  63. public static function getRankingData($data){
  64. $start_time = !empty($data["key"]["start_time"]) ? strtotime($data["key"]["start_time"]) : "";
  65. $end_time = !empty($data["key"]["end_time"]) ? strtotime($data["key"]["end_time"]) : "";
  66. if(!empty($start_time) && ($start_time > $end_time)){
  67. throw new \Exception("开始时间不能大于结束时间!",0);
  68. }
  69. $condition = "order.status=5";
  70. if(!empty($start_time)){
  71. $condition .= " AND (order.create_time >= '{$start_time}' AND order.create_time <= '{$end_time}')";
  72. }
  73. $fields = "SUM(order.order_amount) as total,COUNT(order.id) as count";
  74. $count = OrderModel::withJoin("users")->field($fields)->where($condition)->group("order.user_id")->order("total DESC")->count();
  75. $result = OrderModel::withJoin("users")->field($fields)->where($condition)->group("order.user_id")->order("total","DESC")->page($data["page"]??1,$data["limit"]??10)->select()->toArray();
  76. $list = array_map(function ($res){
  77. $res["username"] = getUserName($res);
  78. return $res;
  79. },$result);
  80. return [ "count"=>$count, "data"=>$list ];
  81. }
  82. /**
  83. * 获取销售明细数据
  84. * @param $data
  85. * @return array
  86. * @throws \Exception
  87. */
  88. public static function getSaleList($data){
  89. $start_time = !empty($data["key"]["start_time"]) ? strtotime($data["key"]["start_time"]) : "";
  90. $end_time = !empty($data["key"]["end_time"]) ? strtotime($data["key"]["end_time"]) : "";
  91. if(!empty($start_time) && ($start_time > $end_time)){
  92. throw new \Exception("开始时间不能大于结束时间!",1);
  93. }
  94. $condition = "order.status=5";
  95. if(!empty($start_time)){
  96. $condition .= " AND (order.create_time >= '{$start_time}' AND order.create_time <= '{$end_time}')";
  97. }
  98. $count = OrderGoodsModel::withJoin("order")->where($condition)->count();
  99. $data = OrderGoodsModel::withJoin("order")
  100. ->where($condition)->group("order.user_id")
  101. ->order("order_goods.goods_nums","DESC")->page($data["page"]??1,$data["limit"]??10)
  102. ->select()->toArray();
  103. $tableData = [];
  104. foreach($data as $key=>$value){
  105. $goods_array = json_decode($value["goods_array"],true);
  106. $tableData[$key] = [
  107. "goods_img"=>Tool::thumb($value["thumb_image"]),
  108. "goods_name"=>$goods_array["title"] . (!empty($goods_array["spec"]) ? '['.$goods_array["spec"].']' : ''),
  109. "order_no"=>$value["order_no"],
  110. "num"=>$value["goods_nums"],
  111. "price"=>$value["real_price"],
  112. "time"=>$value["create_time"]
  113. ];
  114. }
  115. return [ "count"=>$count, "data"=>$tableData ];
  116. }
  117. /**
  118. * 获取商品排行数据
  119. * @param $data
  120. * @return array
  121. * @throws \Exception
  122. */
  123. public static function getSaleOrder($data){
  124. $start_time = !empty($data["key"]["start_time"]) ? strtotime($data["key"]["start_time"]) : "";
  125. $end_time = !empty($data["key"]["end_time"]) ? strtotime($data["key"]["end_time"]) : "";
  126. if(!empty($start_time) && ($start_time > $end_time)){
  127. throw new \Exception("开始时间不能大于结束时间!",1);
  128. }
  129. $condition = "order.status=5";
  130. if(!empty($start_time)){
  131. $condition .= " AND (order.create_time >= '{$start_time}' AND order.create_time <= '{$end_time}')";
  132. }
  133. $count = OrderGoodsModel::withJoin("order")->where($condition)->group("order_goods.goods_id")->count();
  134. $data = OrderGoodsModel::withJoin("order")
  135. ->field("SUM(order_goods.goods_nums) as nums,SUM(order_goods.goods_nums * order_goods.real_price) as total")
  136. ->where($condition)->group("order.user_id")
  137. ->group("order_goods.goods_id")
  138. ->order("order_goods.goods_nums","DESC")->page($data["page"]??1,$data["limit"]??10)
  139. ->select()->toArray();
  140. $tableData = [];
  141. foreach($data as $key=>$value){
  142. $goods_array = json_decode($value["goods_array"],true);
  143. $tableData[$key] = [
  144. "goods_img"=>Tool::thumb($value["thumb_image"]),
  145. "goods_name"=>$goods_array["title"] . (!empty($goods_array["spec"]) ? '['.$goods_array["spec"].']' : ''),
  146. "goods_no"=>$value["goods_no"],
  147. "num"=>$value["nums"],
  148. "price"=>$value["total"],
  149. "average"=>number_format($value['nums'] ? $value['total'] / $value['nums'] : 0,2)
  150. ];
  151. }
  152. return [ "count"=>$count, "data"=>$tableData ];
  153. }
  154. }