GoodsStat.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\stat;
  11. use app\model\BaseModel;
  12. use app\model\system\Stat;
  13. use Carbon\Carbon;
  14. /**
  15. * 统计
  16. * @author Administrator
  17. *
  18. */
  19. class GoodsStat extends BaseModel
  20. {
  21. /**
  22. * 用于订单(同与订单支付后调用)
  23. * @param $params
  24. */
  25. public function addGoodsStat($params)
  26. {
  27. $stat_model = new Stat();
  28. $result = $stat_model->addShopStat($params);
  29. return $result;
  30. }
  31. /**
  32. * 商品增加收藏量
  33. * @param $params
  34. */
  35. public function addGoodsCollectStat($params)
  36. {
  37. $stat_model = new Stat();
  38. $result = $stat_model->addShopStat($params);
  39. return $result;
  40. }
  41. /**
  42. * 增加访问数
  43. * @param $params
  44. */
  45. public function addGoodsVisit($params)
  46. {
  47. $member_id = $params[ 'member_id' ] ?? 0;
  48. $goods_id = $params[ 'goods_id' ] ?? 0;
  49. $data = array (
  50. 'site_id' => $params[ 'site_id' ],
  51. 'goods_visit_count' => 1
  52. );
  53. $stat_model = new Stat();
  54. $time_region = getDayStartAndEndTime();
  55. $today_start_time = $time_region[ 'start_time' ];
  56. $today_end_time = $time_region[ 'end_time' ];
  57. if ($goods_id > 0) {
  58. $goods_browse_condition = array (
  59. [ 'goods_id', '=', $goods_id ],
  60. [ 'browse_time', 'between', [ $today_start_time, $today_end_time ] ],
  61. );
  62. $info = model('goods_browse')->getInfo($goods_browse_condition);
  63. if (empty($info)) {
  64. $data[ 'goods_visited_type_count' ] = 1;
  65. }
  66. }
  67. if ($member_id > 0) {
  68. $member_browse_condition = array (
  69. [ 'member_id', '=', $member_id ],
  70. [ 'browse_time', 'between', [ $today_start_time, $today_end_time ] ],
  71. );
  72. $info = model('goods_browse')->getInfo($member_browse_condition);
  73. if (empty($info)) {
  74. $data[ 'goods_visit_member_count' ] = 1;
  75. }
  76. }
  77. $result = $stat_model->addShopStat($data);
  78. return $result;
  79. }
  80. /**
  81. * 商品上架商品统计
  82. * @param $params
  83. */
  84. public function addGoodsOnStat($params)
  85. {
  86. $site_id = $params[ 'site_id' ];
  87. //查询当前的已上架商品
  88. $goods_on_condition = array (
  89. [ 'goods_state', '=', 1 ],
  90. [ 'is_delete', '=', 0 ],
  91. [ 'site_id', '=', $site_id ]
  92. );
  93. $count = model('goods')->getCount($goods_on_condition);
  94. $carbon = Carbon::now();
  95. $condition = [
  96. [ 'site_id', '=', $site_id ],
  97. [ 'year', '=', $carbon->year ],
  98. [ 'month', '=', $carbon->month ],
  99. [ 'day', '=', $carbon->day ],
  100. [ 'goods_on_type_count', '>', 0 ]
  101. ];
  102. $info = model('stat_shop')->getInfo($condition, 'goods_on_type_count') ?? [];
  103. $o_count = $info[ 'goods_on_type_count' ] ?? 0;
  104. $data = [
  105. 'site_id' => $site_id,
  106. ];
  107. $diff_count = $count - $o_count;
  108. if ($diff_count != 0) {
  109. $data[ 'goods_on_type_count' ] = $diff_count;
  110. $stat_model = new Stat();
  111. $result = $stat_model->addShopStat($data);
  112. return $result;
  113. }
  114. }
  115. /**
  116. * 销售额统计
  117. * @param $params
  118. * @return array
  119. */
  120. public function getGoodsOrderMoneyStat($params)
  121. {
  122. $start_time = $params[ 'start_time' ];
  123. $end_time = $params[ 'end_time' ];
  124. $condition = array (
  125. [ 'o.create_time', 'between', [ $start_time, $end_time ] ],
  126. [ 'o.pay_status', '=', 1 ]
  127. );
  128. $site_id = $params[ 'site_id' ] ?? 0;
  129. if ($site_id > 0) {
  130. $condition[] = [ 'o.site_id', '=', $site_id ];
  131. }
  132. $alias = 'og';
  133. $join = [
  134. [
  135. 'order o',
  136. 'o.order_id = og.order_id',
  137. 'inner'
  138. ],
  139. ];
  140. $list = model('order_goods')->getList($condition, 'sum(og.goods_money) as total_money, og.sku_name, og.goods_id, og.sku_id, og.sku_image', 'total_money desc', $alias, $join, 'og.goods_id', $params[ 'limit' ]);
  141. return $this->success($list);
  142. }
  143. /**
  144. * 销售量统计
  145. * @param $params
  146. * @return array
  147. */
  148. public function getGoodsOrderNumStat($params)
  149. {
  150. $start_time = $params[ 'start_time' ];
  151. $end_time = $params[ 'end_time' ];
  152. $condition = array (
  153. [ 'o.create_time', 'between', [ $start_time, $end_time ] ],
  154. [ 'o.pay_status', '=', 1 ]
  155. );
  156. $site_id = $params[ 'site_id' ] ?? 0;
  157. if ($site_id > 0) {
  158. $condition[] = [ 'o.site_id', '=', $site_id ];
  159. }
  160. $alias = 'og';
  161. $join = [
  162. [
  163. 'order o',
  164. 'o.order_id = og.order_id',
  165. 'inner'
  166. ],
  167. ];
  168. $list = model('order_goods')->getList($condition, 'sum(og.num) as total_num, og.sku_name, og.goods_id, og.sku_id, og.sku_image', 'total_num desc', $alias, $join, 'og.goods_id', $params[ 'limit' ]);
  169. return $this->success($list);
  170. }
  171. }