WorkbenchLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\businessapi\logic;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\OrderEnum;
  22. use app\common\enum\YesNoEnum;
  23. use app\common\model\AfterSale;
  24. use app\common\model\Goods;
  25. use app\common\model\GoodsComment;
  26. use app\common\model\IndexVisit;
  27. use app\common\model\Order;
  28. use app\common\model\User;
  29. use app\common\service\ConfigService;
  30. /**
  31. *
  32. * Class WorkbenchLogic
  33. * @package app\businessapi\logic
  34. */
  35. class WorkbenchLogic
  36. {
  37. public function index()
  38. {
  39. // 今日数据
  40. $today = self::today();
  41. // 待办事项
  42. $pending = self::pending();
  43. // 近5日营业额
  44. $business5 = self::business7();
  45. // 近5日访客数
  46. $visitor5 = self::visitor7();
  47. return [
  48. 'shop_name' => ConfigService::get('shop', 'name'),
  49. 'today' => $today,
  50. 'pending' => $pending,
  51. 'business5' => $business5,
  52. 'visitor5' => $visitor5,
  53. ];
  54. }
  55. /**
  56. * @notes 待办事项
  57. * @return array
  58. * @author Tab
  59. * @date 2021/9/10 17:57
  60. */
  61. public static function pending()
  62. {
  63. // 待发货订单数
  64. $waitShipped = Order::where('order_status', OrderEnum::STATUS_WAIT_DELIVERY)->count();
  65. // 待审核售后申请
  66. $waitAudit = AfterSale::where('sub_status', AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE)->count();
  67. // 待审核评价
  68. $waitProcess = GoodsComment::where(['status'=>YesNoEnum::NO])->count();
  69. // 售罄商品
  70. $noStockGoods = Goods::where('total_stock', 0)->count();
  71. return [
  72. 'wait_shipped' => $waitShipped,
  73. 'wait_audit' => $waitAudit,
  74. 'wait_process' => $waitProcess,
  75. 'no_stock_goods' => $noStockGoods
  76. ];
  77. }
  78. /**
  79. * @notes 今日数据
  80. * @return array
  81. * @author Tab
  82. * @date 2021/9/10 17:41
  83. */
  84. public static function today()
  85. {
  86. // 营业额
  87. $todayOrderAmount = Order::where('pay_status', YesNoEnum::YES)
  88. ->whereDay('create_time')
  89. ->sum('order_amount');
  90. // 成交订单数
  91. $todayOrderNum = Order::where('pay_status', YesNoEnum::YES)
  92. ->whereDay('create_time')
  93. ->count();
  94. // 访客数
  95. $visitor = IndexVisit::whereDay('create_time')->column('ip');
  96. $todayVisitor = count(array_unique($visitor));
  97. // 新增用户
  98. $todayNewUser = User::whereDay('create_time')->count();
  99. return [
  100. 'today_order_amount' => $todayOrderAmount,
  101. 'today_order_num' => $todayOrderNum,
  102. 'today_visitor' => $todayVisitor,
  103. 'today_new_user' => $todayNewUser,
  104. ];
  105. }
  106. /**
  107. * @notes 近15天营业额
  108. * @return array
  109. * @author Tab
  110. * @date 2021/9/10 18:06
  111. */
  112. public static function business7()
  113. {
  114. $today = new \DateTime();
  115. $todayStr = $today->format('Y-m-d') . ' 23:59:59';
  116. $todayDec15 = $today->add(\DateInterval::createFromDateString('-4day'));
  117. $todayDec15Str = $todayDec15->format('Y-m-d');
  118. $field = [
  119. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  120. "sum(order_amount) as today_amount"
  121. ];
  122. $lists = Order::field($field)
  123. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  124. ->where('pay_status', YesNoEnum::YES)
  125. ->group('date')
  126. ->select()
  127. ->toArray();
  128. $lists = array_column($lists, 'today_amount', 'date');
  129. $amountData = [];
  130. $date = [];
  131. for($i = 6; $i >= 0; $i --) {
  132. $today = new \DateTime();
  133. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  134. $targetDayTime = $targetDay->format('Ymd');
  135. $targetDays = $targetDay->format('d');
  136. $date[] = $targetDays;
  137. $amountData[] = $lists[$targetDayTime] ?? 0;
  138. }
  139. return [
  140. 'date' => $date,
  141. 'list' => [
  142. ['name' => '营业额', 'data' => $amountData]
  143. ]
  144. ];
  145. }
  146. /**
  147. * @notes 近15天访客数
  148. * @return mixed
  149. * @author Tab
  150. * @date 2021/9/10 18:51
  151. */
  152. public static function visitor7()
  153. {
  154. $today = new \DateTime();
  155. $todayStr = $today->format('Y-m-d') . ' 23:59:59';
  156. $todayDec15 = $today->add(\DateInterval::createFromDateString('-5day'));
  157. $todayDec15Str = $todayDec15->format('Y-m-d');
  158. $field = [
  159. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  160. "ip"
  161. ];
  162. $lists = IndexVisit::field($field)
  163. ->distinct(true)
  164. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  165. ->select()
  166. ->toArray();
  167. // 集合一天的IP
  168. $temp1 = [];
  169. foreach ($lists as $item) {
  170. $temp1[$item['date']][] = $item['ip'];
  171. }
  172. // 统计数量
  173. $temp2 = [];
  174. foreach ($temp1 as $k => $v) {
  175. $temp2[$k] = count($v);
  176. }
  177. $userData = [];
  178. $date = [];
  179. for($i = 6; $i >= 0; $i --) {
  180. $today = new \DateTime();
  181. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  182. $targetDayTime = $targetDay->format('Ymd');
  183. $targetDays = $targetDay->format('d');
  184. $date[] = $targetDays;
  185. $userData[] = $temp2[$targetDayTime] ?? 0;
  186. }
  187. return [
  188. 'date' => $date,
  189. 'list' => [
  190. ['name' => '访客数', 'data' => $userData]
  191. ]
  192. ];
  193. }
  194. }