Stat.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\system;
  13. use app\model\BaseModel;
  14. use Carbon\Carbon;
  15. use think\facade\Db;
  16. /**
  17. * 统计
  18. * @author Administrator
  19. *
  20. */
  21. class Stat extends BaseModel
  22. {
  23. /**
  24. * 添加店铺统计(按照天统计)
  25. * @param array $data
  26. */
  27. public function addShopStat($data)
  28. {
  29. $carbon = Carbon::now();
  30. $condition = [
  31. 'site_id' => $data['site_id'],
  32. 'year' => $carbon->year,
  33. 'month' => $carbon->month,
  34. 'day' => $carbon->day
  35. ];
  36. $info = model("stat_shop")->getInfo($condition, 'id');
  37. if(empty($info))
  38. {
  39. $res = model("stat_shop")->add(
  40. [
  41. 'site_id' => $data['site_id'],
  42. 'year' => $carbon->year,
  43. 'month' => $carbon->month,
  44. 'day' => $carbon->day,
  45. 'day_time' => Carbon::today()->timestamp,
  46. "order_total" => isset($data['order_total']) ? $data['order_total'] : 0,
  47. "shipping_total" => isset($data['shipping_total']) ? $data['shipping_total'] : 0,
  48. "refund_total" => isset($data['refund_total']) ? $data['refund_total'] : 0,
  49. "order_pay_count" => isset($data['order_pay_count']) ? $data['order_pay_count'] : 0,
  50. "goods_pay_count" => isset($data['goods_pay_count']) ? $data['goods_pay_count'] : 0,
  51. "shop_money" => isset($data['shop_money']) ? $data['shop_money'] : 0,
  52. "platform_money" => isset($data['platform_money']) ? $data['platform_money'] : 0,
  53. "collect_shop" => isset($data['collect_shop']) ? $data['collect_shop'] : 0,
  54. "collect_goods" => isset($data['collect_goods']) ? $data['collect_goods'] : 0,
  55. "visit_count" => isset($data['visit_count']) ? $data['visit_count'] : 0,
  56. "order_count" => isset($data['order_count']) ? $data['order_count'] : 0,
  57. "goods_count" => isset($data['goods_count']) ? $data['goods_count'] : 0,
  58. "add_goods_count" => isset($data['add_goods_count']) ? $data['add_goods_count'] : 0,
  59. "member_count" => isset($data['member_count']) ? $data['member_count'] : 0,
  60. 'create_time' => time()
  61. ]);
  62. }else{
  63. $res = Db::name('stat_shop')->where($condition)
  64. ->update(
  65. [
  66. "order_total" => isset($data['order_total']) ? Db::raw('order_total+'.$data['order_total']) : Db::raw('order_total'),
  67. "shipping_total" => isset($data['shipping_total']) ? Db::raw('shipping_total+'.$data['shipping_total']) : Db::raw('shipping_total'),
  68. "refund_total" => isset($data['refund_total']) ? Db::raw('refund_total+'.$data['refund_total']) : Db::raw('refund_total'),
  69. "order_pay_count" => isset($data['order_pay_count']) ? Db::raw('order_pay_count+'.$data['order_pay_count']) : Db::raw('order_pay_count'),
  70. "goods_pay_count" => isset($data['goods_pay_count']) ? Db::raw('goods_pay_count+'.$data['goods_pay_count']) : Db::raw('goods_pay_count'),
  71. "shop_money" => isset($data['shop_money']) ? Db::raw('shop_money+'.$data['shop_money']) : Db::raw('shop_money'),
  72. "platform_money" => isset($data['platform_money']) ? Db::raw('platform_money+'.$data['platform_money']) : Db::raw('platform_money'),
  73. "collect_shop" => isset($data['collect_shop']) ? Db::raw('collect_shop+'.$data['collect_shop']) : Db::raw('collect_shop'),
  74. "collect_goods" => isset($data['collect_goods']) ? Db::raw('collect_goods+'.$data['collect_goods']) : Db::raw('collect_goods'),
  75. "visit_count" => isset($data['visit_count']) ? Db::raw('visit_count+'.$data['visit_count']) : Db::raw('visit_count'),
  76. "order_count" => isset($data['order_count']) ? Db::raw('order_count+'.$data['order_count']) : Db::raw('order_count'),
  77. "goods_count" => isset($data['goods_count']) ? Db::raw('goods_count+'.$data['goods_count']) : Db::raw('goods_count'),
  78. "add_goods_count" => isset($data['add_goods_count']) ? Db::raw('add_goods_count+'.$data['add_goods_count']) : Db::raw('add_goods_count'),
  79. "member_count" => isset($data['member_count']) ? Db::raw('member_count+'.$data['member_count']) : Db::raw('member_count'),
  80. 'modify_time' => time()
  81. ]);
  82. }
  83. if($data['site_id'] != 0)
  84. {
  85. $data['site_id'] = 0;
  86. $this->addShopStat($data);
  87. }
  88. return $this->success($res);
  89. }
  90. /**
  91. * 获取店铺统计(按照天查询)
  92. * @param unknown $site_id 0表示平台
  93. * @param unknown $year
  94. * @param unknown $month
  95. * @param unknown $day
  96. */
  97. public function getStatShop($site_id, $year, $month, $day)
  98. {
  99. $condition = [
  100. 'site_id' => $site_id,
  101. 'year' => $year,
  102. 'month' => $month,
  103. 'day' => $day
  104. ];
  105. $info = model("stat_shop")->getInfo($condition,
  106. 'id, site_id, year, month, day, order_total, shipping_total, refund_total, order_pay_count, goods_pay_count, shop_money, platform_money, create_time, modify_time, collect_shop, collect_goods, visit_count, order_count, goods_count, add_goods_count, day_time, member_count');
  107. if(empty($info))
  108. {
  109. $condition['day_time'] = Carbon::today()->timestamp;
  110. model("stat_shop")->add($condition);
  111. $info = model("stat_shop")->getInfo($condition,
  112. 'id, site_id, year, month, day, order_total, shipping_total, refund_total, order_pay_count, goods_pay_count, shop_money, platform_money, create_time, modify_time, collect_shop, collect_goods, visit_count, order_count, goods_count, add_goods_count, day_time, member_count');
  113. }
  114. return $this->success($info);
  115. }
  116. /**
  117. * 获取店铺统计信息
  118. * @param unknown $site_id
  119. * @param unknown $start_time
  120. */
  121. public function getShopStatSum($site_id, $start_time = 0)
  122. {
  123. $condition = [
  124. ['site_id', '=', $site_id]
  125. ];
  126. if (!empty($start_time)) {
  127. $condition[] = ['day_time', '>=', $start_time];
  128. }
  129. $info = model("stat_shop")->getInfo($condition,
  130. 'SUM(order_total) as order_total,SUM(shipping_total) as shipping_total,SUM(refund_total) as refund_total,SUM(order_pay_count) as order_pay_count,SUM(goods_pay_count) as goods_pay_count,SUM(shop_money) as shop_money,SUM(platform_money) as platform_money,SUM(collect_shop) as collect_shop,SUM(collect_goods) as collect_goods,SUM(visit_count) as visit_count,SUM(order_count) as order_count,SUM(goods_count) as goods_count,SUM(add_goods_count) as add_goods_count,SUM(member_count) as member_count');
  131. if($info['order_total'] == null)
  132. {
  133. $info = [
  134. "order_total" => 0,
  135. "shipping_total" => 0,
  136. "refund_total" => 0,
  137. "order_pay_count" => 0,
  138. "goods_pay_count" => 0,
  139. "shop_money" => 0,
  140. "platform_money" => 0,
  141. "collect_shop" => 0,
  142. "collect_goods" => 0,
  143. "visit_count" => 0,
  144. "order_count" => 0,
  145. "goods_count" => 0,
  146. "add_goods_count" => 0,
  147. "member_count" => 0
  148. ];
  149. }
  150. return $this->success($info);
  151. }
  152. /**
  153. * 获取店铺统计列表
  154. * @param unknown $site_id
  155. * @param unknown $start_time
  156. */
  157. public function getShopStatList($site_id, $start_time)
  158. {
  159. $condition = [
  160. ['site_id', '=', $site_id],
  161. ['day_time', '>=', $start_time],
  162. ];
  163. $info = model("stat_shop")->getList($condition,
  164. 'id, site_id, year, month, day, order_total, shipping_total, refund_total, order_pay_count, goods_pay_count, shop_money, platform_money, create_time, modify_time, collect_shop, collect_goods, visit_count, order_count, goods_count, add_goods_count, day_time, member_count');
  165. return $this->success($info);
  166. }
  167. }