FinanceLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\adminapi\logic\distribution\DistributionDataLogic;
  21. use app\common\enum\AfterSaleEnum;
  22. use app\common\enum\PayEnum;
  23. use app\common\enum\YesNoEnum;
  24. use app\common\model\AfterSale;
  25. use app\common\model\Goods;
  26. use app\common\model\IndexVisit;
  27. use app\common\model\Order;
  28. use app\common\model\OrderGoods;
  29. use app\common\model\User;
  30. /**
  31. * 财务控制器类
  32. * Class FinanceController
  33. * @package app\businessapi\controller
  34. */
  35. class FinanceLogic
  36. {
  37. /**
  38. * @notes 财务概况
  39. * @return array
  40. * @author cjhao
  41. * @date 2023/2/16 14:35
  42. */
  43. public function dataCenter()
  44. {
  45. // 订单总支付金额
  46. $orderSum = Order::where('pay_status', PayEnum::ISPAID)->sum('order_amount');
  47. // 订单总支付笔数
  48. $orderNum = Order::where('pay_status', PayEnum::ISPAID)->count('id');
  49. // 售后退款成功总金额
  50. $afterSaleSum = AfterSale::where('sub_status', AfterSaleEnum::SUB_STATUS_SELLER_REFUND_SUCCESS)->sum('refund_total_amount');
  51. // 用户总余额(不可提现)
  52. $userMoneySum = User::sum('user_money');
  53. // 用户总收入(可提现)
  54. $userEarningsSum = User::sum('user_earnings');
  55. // 用户总资产
  56. $userTotalAssets = round($userMoneySum + $userEarningsSum, 2);
  57. //分销订单收益
  58. $distributionData = DistributionDataLogic::earningsData();
  59. $data = [
  60. 'order_sum' => $orderSum,
  61. 'order_num' => $orderNum,
  62. 'after_sale_sum' => $afterSaleSum,
  63. 'user_money_sum' => $userMoneySum,
  64. 'user_earnings_sum' => $userEarningsSum,
  65. 'user_total_assets' => $userTotalAssets,
  66. 'distribution_data' => $distributionData
  67. ];
  68. return $data;
  69. }
  70. public function dealCenter()
  71. {
  72. $today = new \DateTime();
  73. $todayStr = $today->format('Y-m-d') . ' 23:59:59';
  74. $todayDec15 = $today->add(\DateInterval::createFromDateString('-4day'));
  75. $todayDec15Str = $todayDec15->format('Y-m-d');
  76. // 订单总支付金额
  77. $orderSum = Order::where('pay_status', PayEnum::ISPAID)
  78. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  79. ->sum('order_amount');
  80. // 订单总支付笔数
  81. $orderNum = Order::where('pay_status', PayEnum::ISPAID)
  82. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  83. ->count('id');
  84. $field = [
  85. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  86. "sum(order_amount) as today_amount,count(id) as today_num"
  87. ];
  88. $lists = Order::field($field)
  89. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  90. ->where('pay_status', YesNoEnum::YES)
  91. ->group('date')
  92. ->select()
  93. ->toArray();
  94. $lists = array_column($lists, null, 'date');
  95. $amountData = [];
  96. $date = [];
  97. for($i = 6; $i >= 0; $i --) {
  98. $today = new \DateTime();
  99. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  100. $targetDayTime = $targetDay->format('Ymd');
  101. $targetDay = $targetDay->format('d');
  102. $date[] = $targetDay;
  103. $amountData[] = $lists[$targetDayTime]['today_amount'] ?? 0;
  104. $numData[] = $lists[$targetDayTime]['today_num'] ?? 0;
  105. }
  106. return [
  107. 'order_sum' => $orderSum,
  108. 'order_num' => $orderNum,
  109. 'date' => $date,
  110. 'list' => [
  111. ['name' => '成交数量', 'data' => $numData],
  112. ['name' => '营业额', 'data' => $amountData],
  113. ]
  114. ];
  115. }
  116. /**
  117. * @notes 商品中心
  118. * @return Goods[]|array|\think\Collection
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @author cjhao
  123. * @date 2023/2/17 16:58
  124. */
  125. public function goodsCenter()
  126. {
  127. $field = [
  128. 'g.id',
  129. 'g.name',
  130. 'g.image',
  131. 'g.min_price',
  132. 'g.max_price',
  133. 'sum(og.goods_num)' => 'total_num',
  134. 'sum(og.total_pay_price)' => 'total_pay_price',
  135. ];
  136. $lists = OrderGoods::alias('og')
  137. ->leftJoin('order o', 'o.id = og.order_id')
  138. ->leftJoin('goods g', 'g.id = og.goods_id')
  139. ->field($field)
  140. ->where('o.pay_status', YesNoEnum::YES)
  141. ->group('g.id,g.name,g.image')
  142. ->order('total_pay_price', 'desc')
  143. ->limit(20)
  144. ->select()
  145. ->toArray();
  146. foreach ($lists as $key => $goods){
  147. //商品价格
  148. $list[$key]['price'] = '¥' . $goods['min_price'];
  149. if ($goods['min_price'] != $goods['max_price']) {
  150. $lists[$key]['price'] = '¥' . $goods['min_price'] . '~' . '¥' . $goods['max_price'];
  151. }
  152. }
  153. return $lists;
  154. }
  155. /**
  156. * @notes 访客统计
  157. * @return array
  158. * @throws \Exception
  159. * @author cjhao
  160. * @date 2023/2/17 17:01
  161. */
  162. public function visitCenter()
  163. {
  164. $today = new \DateTime();
  165. $todayStr = $today->format('Y-m-d') . ' 23:59:59';
  166. $todayDec15 = $today->add(\DateInterval::createFromDateString('-5day'));
  167. $todayDec15Str = $todayDec15->format('Y-m-d');
  168. $field = [
  169. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  170. "ip"
  171. ];
  172. $count = IndexVisit::distinct(true)->count();
  173. $lists = IndexVisit::field($field)
  174. ->distinct(true)
  175. ->whereTime('create_time', 'between', [$todayDec15Str,$todayStr])
  176. ->select()
  177. ->toArray();
  178. // 集合一天的IP
  179. $temp1 = [];
  180. foreach ($lists as $item) {
  181. $temp1[$item['date']][] = $item['ip'];
  182. }
  183. // 统计数量
  184. $temp2 = [];
  185. foreach ($temp1 as $k => $v) {
  186. $temp2[$k] = count($v);
  187. }
  188. $userData = [];
  189. $date = [];
  190. for($i = 6; $i >= 0; $i --) {
  191. $today = new \DateTime();
  192. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  193. $targetDayTime = $targetDay->format('Ymd');
  194. $targetDay = $targetDay->format('d');
  195. $date[] = $targetDay;
  196. $userData[] = $temp2[$targetDayTime] ?? 0;
  197. }
  198. return [
  199. 'count' => $count,
  200. 'date' => $date,
  201. 'list' => [
  202. ['name' => '访客数', 'data' => $userData]
  203. ]
  204. ];
  205. }
  206. }