ChannelSaleVolumeTj.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model;
  13. use app\common\model\ChannelSaleVolumeTj as ChannelStModel;
  14. use app\common\model\user\CommissionsDetail;
  15. /**
  16. * 门店销售数据每日统计模型
  17. * Class ShopsDailySalesSt
  18. * @package app\common\model
  19. */
  20. class ChannelSaleVolumeTj extends ChannelStModel
  21. {
  22. protected $ftype = [1=>'day',2=>'week',3=>'month'];
  23. protected $channelType = [1,2,3];//1:门店销售渠道,2:推荐官渠道,3:自然成交
  24. public function day($baseTimestamp,$dif = 1){
  25. $start = strtotime(Date("Y-m-d",$baseTimestamp-86400*$dif)." 00:00:00");
  26. $end = strtotime(Date("Y-m-d",$baseTimestamp-86400*$dif)." 23:59:59");
  27. $remark = Date("Y-m-d",$baseTimestamp-86400*$dif);
  28. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  29. foreach ($this->channelType as $channel_type){
  30. $data = $this->search($start,$end,1,$remark,$channel_type,$tj_time);
  31. $this->add($data);
  32. }
  33. return true;
  34. }
  35. public function month($baseTimestamp){
  36. //如果是1号,重新统计上月的数据
  37. if(intval(date('d',$baseTimestamp)) == 1){
  38. $standardTime = date('Y-m-1',$baseTimestamp);
  39. $_lastMonthStart = date('Y-m-01 00:00:00', strtotime("-1 month", strtotime($standardTime)));
  40. $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
  41. $start = strtotime($_lastMonthStart);
  42. $end = strtotime($_lastMonthEnd);
  43. $remark = substr($_lastMonthStart,0,7);
  44. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  45. foreach ($this->channelType as $channel_type){
  46. $data = $this->search($start,$end,3,$remark,$channel_type,$tj_time);
  47. $this->add($data);
  48. }
  49. }
  50. $standardTime = date('Y-m-01 00:00:00',$baseTimestamp);
  51. $_lastMonthStart = date('Y-m-01 00:00:00', strtotime($standardTime));
  52. $start = strtotime($standardTime);
  53. $end = $baseTimestamp;
  54. $remark = substr($_lastMonthStart,0,7);
  55. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  56. foreach ($this->channelType as $channel_type){
  57. $data = $this->search($start,$end,3,$remark,$channel_type,$tj_time);
  58. $this->add($data);
  59. }
  60. return true;
  61. }
  62. public function week($baseTimestamp){
  63. $w = date("w",$baseTimestamp);
  64. //如果是周一,统计上周的数据
  65. if($w==1){
  66. $t = $w==1?1:2; //如果当前时间是周一,则-1,如果已经是周二 要-2
  67. $d1 = date('Y-m-d 00:00:00',strtotime('-'.$t.' monday',$baseTimestamp)); //无论今天几号,-1 monday为上一个有效周未
  68. //上周日
  69. $d7 = date('Y-m-d 23:59:59',strtotime('-1 sunday', $baseTimestamp)); //
  70. //上一个有效周日,同样适用于其它星期
  71. $remark = substr($d1,0,10).'--'.substr($d7,0,10);
  72. $start = strtotime($d1);
  73. $end = strtotime($d7);
  74. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  75. foreach ($this->channelType as $channel_type){
  76. $data = $this->search($start,$end,2,$remark,$channel_type,$tj_time);
  77. $this->add($data);
  78. }
  79. }
  80. $start = mktime(0,0,0,intval(date("m",$baseTimestamp)),date("d",$baseTimestamp)-date("N",$baseTimestamp)+1,intval(date("y",$baseTimestamp)));
  81. $end = $start + 7*86400 - 1;
  82. $d1 = date('Y-m-d 00:00:00',$start);
  83. $d7 = date('Y-m-d H:i:s',$end);
  84. //上一个有效周日,同样适用于其它星期
  85. $remark = substr($d1,0,10).'--'.substr($d7,0,10);
  86. $start = strtotime($d1);
  87. $end = strtotime($d7);
  88. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  89. foreach ($this->channelType as $channel_type){
  90. $data = $this->search($start,$end,2,$remark,$channel_type,$tj_time);
  91. $this->add($data);
  92. }
  93. return true;
  94. }
  95. //查找时间段
  96. public function search($start,$end,$ftype,$remark,$channel_type,$tj_time): array
  97. {
  98. $data['ftype'] =$ftype;
  99. $data['remark'] = $remark;
  100. $data['channel_type'] =$channel_type;
  101. $data['tj_time'] = $tj_time;
  102. $where = [['commission_level','=',1],
  103. ['role','=',$channel_type==2?User::COMMISSION_USER:User::SHOP_SELLER],
  104. ['order_create_time','>=',$start],
  105. ['order_create_time','<=',$end]
  106. ];
  107. if($channel_type<3){
  108. $data['sale_volume'] = CommissionsDetail::sumOrderSaleVolume($where);
  109. }else{
  110. //自然成交额
  111. $where = [ ['create_time','>=',$start],
  112. ['create_time','<=',$end],
  113. ['staff_user_id', '=', 0],
  114. ['staff_shop_id' ,'=', 0],
  115. ['is_delete','=',0],
  116. ['pay_status', '=', 20]
  117. ];
  118. $order = new Order();
  119. $data['sale_volume'] = $order->sumNatureSaleVolume($where);
  120. }
  121. return $data;
  122. }
  123. //添加
  124. public function add($data){
  125. $m = new ChannelSaleVolumeTj;
  126. $one = $m->where('remark',$data['remark'])
  127. ->where("ftype",$data['ftype'])
  128. ->where("channel_type",$data['channel_type'])
  129. ->find();
  130. if(empty($one)){
  131. $m->save($data);
  132. }else{
  133. $one->where('id',$one->id)->update($data);
  134. }
  135. }
  136. public function getList($ftype,$times){
  137. // 检查查询条件
  138. $filter = [];
  139. $ftype>-1 && $filter[] = ['ftype', '=', (int)$ftype];
  140. $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  141. $filter[] = ['tj_time', '<=', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
  142. return $this->where($filter)->field('remark,tj_time,sum(sale_volume) as sale_volume')
  143. ->group('remark')
  144. ->order(['tj_time' => 'asc'])->select();
  145. }
  146. public function getPieList($times){
  147. // 检查查询条件
  148. $filter = [];
  149. $filter[] = ['ftype','=',1];
  150. $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  151. $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'])];
  152. return $this->where($filter)->field('sum(sale_volume) as sale_volume,channel_type')
  153. ->group('channel_type')
  154. ->select()->toArray();
  155. }
  156. }