WorkbenchLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\adminapi\logic;
  20. use app\adminapi\logic\distribution\DistributionDataLogic;
  21. use app\common\enum\AfterSaleEnum;
  22. use app\common\enum\DistributionOrderGoodsEnum;
  23. use app\common\enum\OrderEnum;
  24. use app\common\enum\UserTerminalEnum;
  25. use app\common\enum\YesNoEnum;
  26. use app\common\logic\BaseLogic;
  27. use app\common\model\AfterSale;
  28. use app\common\model\DistributionOrderGoods;
  29. use app\common\model\Goods;
  30. use app\common\model\GoodsComment;
  31. use app\common\model\IndexVisit;
  32. use app\common\model\Order;
  33. use app\common\model\OrderGoods;
  34. use app\common\model\User;
  35. use app\common\service\ConfigService;
  36. use app\common\service\FileService;
  37. /**
  38. * 工作台
  39. * Class WorkbenchLogic
  40. * @package app\adminapi\logic
  41. */
  42. class WorkbenchLogic extends BaseLogic
  43. {
  44. /**
  45. * @notes 工作台
  46. * @return int[]
  47. * @author Tab
  48. * @date 2021/9/10 14:12
  49. */
  50. public static function index($adminInfo)
  51. {
  52. // 基本信息
  53. $shopInfo = self::shopInfo($adminInfo);
  54. // 今日数据
  55. $today = self::today();
  56. // 待办事项
  57. $pending = self::pending();
  58. // 近15日营业额
  59. $business15 = self::business15();
  60. // 近15日访客数
  61. $visitor15 = self::visitor15();
  62. return [
  63. 'shop_info' => $shopInfo,
  64. 'today' => $today,
  65. 'pending' => $pending,
  66. 'business15' => $business15,
  67. 'visitor15' => $visitor15,
  68. ];
  69. }
  70. public static function earningsData()
  71. {
  72. }
  73. /**
  74. * @notes 商城信息
  75. * @param $adminInfo
  76. * @return array
  77. * @author Tab
  78. * @date 2021/9/10 15:02
  79. */
  80. public static function shopInfo($adminInfo)
  81. {
  82. // 商城名称
  83. $name = ConfigService::get('shop', 'name');
  84. // 商城logo
  85. $logo = ConfigService::get('shop', 'logo');
  86. $logo = FileService::getFileUrl($logo);
  87. // 管理员名称
  88. $adminName = $adminInfo['name'];
  89. return [
  90. 'name' => $name,
  91. 'logo' => $logo,
  92. 'admin_name' => $adminName,
  93. ];
  94. }
  95. /**
  96. * @notes 今日数据
  97. * @return array
  98. * @author Tab
  99. * @date 2021/9/10 17:41
  100. */
  101. public static function today()
  102. {
  103. // 营业额
  104. $todayOrderAmount = Order::where('pay_status', YesNoEnum::YES)
  105. ->whereDay('create_time')
  106. ->sum('order_amount');
  107. // 成交订单数
  108. $todayOrderNum = Order::where('pay_status', YesNoEnum::YES)
  109. ->whereDay('create_time')
  110. ->count();
  111. // 访客数
  112. $visitor = IndexVisit::whereDay('create_time')->column('ip');
  113. $todayVisitor = count(array_unique($visitor));
  114. // 新增用户
  115. $todayNewUser = User::whereDay('create_time')->count();
  116. return [
  117. 'today_order_amount' => $todayOrderAmount,
  118. 'today_order_num' => $todayOrderNum,
  119. 'today_visitor' => $todayVisitor,
  120. 'today_new_user' => $todayNewUser,
  121. ];
  122. }
  123. /**
  124. * @notes 待办事项
  125. * @return array
  126. * @author Tab
  127. * @date 2021/9/10 17:57
  128. */
  129. public static function pending()
  130. {
  131. // 待发货订单数
  132. $waitShipped = Order::where('order_status', OrderEnum::STATUS_WAIT_DELIVERY)->count();
  133. // 待审核售后申请
  134. $waitAudit = AfterSale::where('sub_status', AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE)->count();
  135. // 待审核评价
  136. $waitProcess = GoodsComment::where(['status'=>YesNoEnum::NO])->count();
  137. // 售罄商品
  138. $noStockGoods = Goods::where('total_stock', 0)->count();
  139. return [
  140. 'wait_shipped' => $waitShipped,
  141. 'wait_audit' => $waitAudit,
  142. 'wait_process' => $waitProcess,
  143. 'no_stock_goods' => $noStockGoods
  144. ];
  145. }
  146. /**
  147. * @notes 近15天营业额
  148. * @return array
  149. * @author Tab
  150. * @date 2021/9/10 18:06
  151. */
  152. public static function business15()
  153. {
  154. $today = new \DateTime();
  155. $todayStr = $today->format('Y-m-d') . ' 23:59:59';
  156. $todayDec15 = $today->add(\DateInterval::createFromDateString('-14day'));
  157. $todayDec15Str = $todayDec15->format('Y-m-d');
  158. $field = [
  159. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  160. "sum(order_amount) as today_amount"
  161. ];
  162. $lists = Order::field($field)
  163. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  164. ->where('pay_status', YesNoEnum::YES)
  165. ->group('date')
  166. ->select()
  167. ->toArray();
  168. $lists = array_column($lists, 'today_amount', 'date');
  169. $amountData = [];
  170. $date = [];
  171. for($i = 0; $i < 15; $i ++) {
  172. $today = new \DateTime();
  173. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  174. $targetDay = $targetDay->format('Ymd');
  175. $date[] = $targetDay;
  176. $amountData[] = $lists[$targetDay] ?? 0;
  177. }
  178. return [
  179. 'date' => $date,
  180. 'list' => [
  181. ['name' => '营业额', 'data' => $amountData]
  182. ]
  183. ];
  184. }
  185. /**
  186. * @notes 近15天访客数
  187. * @return mixed
  188. * @author Tab
  189. * @date 2021/9/10 18:51
  190. */
  191. public static function visitor15()
  192. {
  193. $today = new \DateTime();
  194. $todayStr = $today->format('Y-m-d') . ' 23:59:59';
  195. $todayDec15 = $today->add(\DateInterval::createFromDateString('-14day'));
  196. $todayDec15Str = $todayDec15->format('Y-m-d');
  197. $field = [
  198. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  199. "ip"
  200. ];
  201. $lists = IndexVisit::field($field)
  202. ->distinct(true)
  203. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  204. ->select()
  205. ->toArray();
  206. // 集合一天的IP
  207. $temp1 = [];
  208. foreach ($lists as $item) {
  209. $temp1[$item['date']][] = $item['ip'];
  210. }
  211. // 统计数量
  212. $temp2 = [];
  213. foreach ($temp1 as $k => $v) {
  214. $temp2[$k] = count($v);
  215. }
  216. $userData = [];
  217. $date = [];
  218. for($i = 0; $i < 15; $i ++) {
  219. $today = new \DateTime();
  220. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  221. $targetDay = $targetDay->format('Ymd');
  222. $date[] = $targetDay;
  223. $userData[] = $temp2[$targetDay] ?? 0;
  224. }
  225. return [
  226. 'date' => $date,
  227. 'list' => [
  228. ['name' => '访客数', 'data' => $userData]
  229. ]
  230. ];
  231. }
  232. /**
  233. * @notes 商品排行榜 - 按销售额排序
  234. * @return mixed
  235. * @author Tab
  236. * @date 2021/9/10 19:40
  237. */
  238. public static function topGoods50($params)
  239. {
  240. $field = [
  241. 'g.id',
  242. 'g.name',
  243. 'g.image',
  244. 'sum(og.goods_num)' => 'total_num',
  245. 'sum(og.total_pay_price)' => 'total_pay_price',
  246. ];
  247. $lists = OrderGoods::alias('og')
  248. ->leftJoin('order o', 'o.id = og.order_id')
  249. ->leftJoin('goods g', 'g.id = og.goods_id')
  250. ->field($field)
  251. ->where('o.pay_status', YesNoEnum::YES)
  252. ->group('g.id,g.name,g.image')
  253. ->order('total_pay_price', 'desc')
  254. ->limit(50)
  255. ->page($params['page_no'], $params['page_size'])
  256. ->select()
  257. ->toArray();
  258. $count = OrderGoods::alias('og')
  259. ->leftJoin('order o', 'o.id = og.order_id')
  260. ->leftJoin('goods g', 'g.id = og.goods_id')
  261. ->field($field)
  262. ->where('o.pay_status', YesNoEnum::YES)
  263. ->group('g.id,g.name,g.image')
  264. ->order('total_pay_price', 'desc')
  265. ->limit(50)
  266. ->count();
  267. return [
  268. 'lists' => $lists,
  269. 'count' => $count,
  270. 'page_no' => $params['page_no'],
  271. 'page_size' => $params['page_size'],
  272. ];
  273. }
  274. /**
  275. * @notes 用户排行榜 - 按用户累计购买金额
  276. * @return mixed
  277. * @author Tab
  278. * @date 2021/9/10 19:50
  279. */
  280. public static function topUser50($params)
  281. {
  282. $field = [
  283. 'u.id',
  284. 'u.nickname',
  285. 'u.avatar',
  286. 'count(o.id)' => 'total_num',
  287. 'sum(o.order_amount)' => 'total_order_amount',
  288. ];
  289. $lists = Order::alias('o')
  290. ->leftJoin('user u', 'u.id = o.user_id')
  291. ->field($field)
  292. ->where('o.pay_status', YesNoEnum::YES)
  293. ->group('u.id,u.nickname,u.avatar')
  294. ->order('total_order_amount', 'desc')
  295. ->limit(50)
  296. ->page($params['page_no'], $params['page_size'])
  297. ->select()
  298. ->toArray();
  299. $count = Order::alias('o')
  300. ->leftJoin('user u', 'u.id = o.user_id')
  301. ->field($field)
  302. ->where('o.pay_status', YesNoEnum::YES)
  303. ->group('u.id,u.nickname,u.avatar')
  304. ->order('total_order_amount', 'desc')
  305. ->limit(50)
  306. ->count();
  307. return [
  308. 'lists' => $lists,
  309. 'count' => $count,
  310. 'page_no' => $params['page_no'],
  311. 'page_size' => $params['page_size'],
  312. ];
  313. }
  314. }