OrderTj.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\common\model;
  13. use app\common\model\store\shop\Order as ShopOrder;
  14. use app\common\service\Order as OrderService;
  15. use app\common\service\order\Complete as OrderCompleteService;
  16. use app\common\enum\OrderType as OrderTypeEnum;
  17. use app\common\enum\order\PayStatus as PayStatusEnum;
  18. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  19. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  20. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  21. use app\common\library\helper;
  22. use app\console\model\Setting as SettingModel;
  23. use think\facade\Db;
  24. /**
  25. * 订单模型
  26. * Class Order
  27. * @package app\common\model
  28. */
  29. class OrderTj extends BaseModel
  30. {
  31. // 定义表名
  32. protected $name = 'order_tj';
  33. // 定义表名(外部引用)
  34. public static $tableName = 'order_tj';
  35. // 定义主键
  36. protected $pk = 'id';
  37. // // 定义别名
  38. // protected $alias = 'id';
  39. protected $ftype = [1=>'day',2=>'week',3=>'month'];
  40. //(商品总价 - 优惠券 - 分销员)+ 运费 = 微信支付+米卡抵扣+米卡抵扣运费(total_price-coupon_money-distrbutor_total_money)+express_price = pay_price+rice_card_money+rice_card_express_money
  41. public function day($dif = 1){
  42. //应付总金额(含运费)
  43. //未扣减优惠金额、米卡抵扣金额前的订单总金额
  44. $start = strtotime(Date("Y-m-d",time()-86400*$dif)." 00:00:00");
  45. $end = strtotime(Date("Y-m-d",time()-86400*$dif)." 23:59:59");
  46. $remark = Date("Y-m-d",time()-86400*$dif);
  47. $data = $this->search($start,$end);
  48. $data['remark'] = $remark;
  49. $data['ftype'] = 1;
  50. $data['tj_time'] = Date("Y-m-d H:i:s",$start + 10);
  51. $this->add($data);
  52. // var_dump(Date("Y-m-d H:i:s",$data['tj_time']));
  53. return true;
  54. }
  55. public function month($dif = 1){
  56. $d = date("d",time());
  57. if($d!='01'){
  58. return false;
  59. }
  60. $standardTime = date('Y-m-1');
  61. $_lastMonthStart = date('Y-m-01 00:00:00', strtotime("-1 month", strtotime($standardTime)));
  62. $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
  63. $start = strtotime($_lastMonthStart);
  64. $end = strtotime($_lastMonthEnd);
  65. $remark = substr($_lastMonthStart,0,7);
  66. $data = $this->search($start,$end);
  67. $data['ftype'] =3;
  68. $data['remark'] = $remark;
  69. $data['tj_time'] = Date("Y-m-d H:i:s",$start + 10);
  70. // var_dump(Date("Y-m-d H:i:s",$data['tj_time']));
  71. $this->add($data);
  72. return true;
  73. }
  74. public function week($dif = 1){
  75. //上周一
  76. $w = date("w",time());
  77. if($w!=1){
  78. return false;;
  79. }
  80. $t = $w==1?1:2; //如果当前时间是周一,则-1,如果已经是周二 要-2
  81. $d1 = date('Y-m-d 00:00:00',strtotime('-'.$t.' monday',time())); //无论今天几号,-1 monday为上一个有效周未
  82. //上周日
  83. $d7 = date('Y-m-d 23:59:59',strtotime('-1 sunday', time())); //
  84. //上一个有效周日,同样适用于其它星期
  85. $remark = substr($d1,0,10).'--'.substr($d7,0,10);
  86. $start = strtotime($d1);
  87. $end = strtotime($d7);
  88. $data = $this->search($start,$end);
  89. $data['ftype'] =2;
  90. $data['remark'] = $remark;
  91. $data['tj_time'] = Date("Y-m-d H:i:s",$start + 10);
  92. // var_dump(Date("Y-m-d H:i:s",$data['tj_time']));
  93. $this->add($data);
  94. return true;
  95. }
  96. //查找时间段
  97. public function search($start,$end){
  98. $sql = "SELECT sum(total_price+express_price) as t1,
  99. sum(total_price) as t2,sum(pay_price+rice_card_money) as t3,sum(express_price) as t4,
  100. sum(coupon_money) as t5,count(distinct(user_id)) as t6,count(*) as t7
  101. FROM yoshop_order where pay_status=? and create_time>? and create_time<?";
  102. $param = [20,$start,$end];
  103. $result = Db::query($sql, $param);
  104. $t1 = $result[0]['t1']??0;//应付总金额(含运费)
  105. $t2 = $result[0]['t2']??0;//应付商品总金额
  106. $t3 = $result[0]['t3']??0;//实付金额(含运费)
  107. $t4 = $result[0]['t4']??0;//运费
  108. $t5 = $result[0]['t5']??0;//平台优惠券抵扣
  109. $t6 = $result[0]['t6']??0;//成交用户数
  110. $t7 = $result[0]['t7']??0;//订单数
  111. if($t6>0){
  112. $t8 = $t3/$t6; //客单价
  113. }else{
  114. $t8 =0;
  115. }
  116. $data = ['total_price'=>$t1,
  117. 'total_goods_price'=>$t2,
  118. 'total_pay_price'=>$t3,
  119. 'total_express_price'=>$t4,
  120. 'total_coupon_price'=>$t5,
  121. 'total_users'=>$t6,
  122. 'total_orders'=>$t7,
  123. 'pcts'=>$t8];
  124. return $data;
  125. }
  126. //添加
  127. public function add($data){
  128. $m = new OrderTj;
  129. $one = $m->where('remark',$data['remark'])->where("ftype",$data['ftype'])->find();
  130. if(empty($one)){
  131. $m->save($data);
  132. }else{
  133. // var_dump($data);
  134. $one->where('id',$one->id)->update($data);
  135. }
  136. }
  137. public function getList($params){
  138. // 检查查询条件
  139. $filter = [];
  140. isset($params['ftype'])&&$params['ftype']>-1 && $filter[] = ['ftype', '=', (int)$params['ftype']];
  141. // 申请时间
  142. if (!empty($params['betweenTime'])) {
  143. $times = between_time($params['betweenTime']);
  144. $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  145. $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
  146. }
  147. return $this->where($filter)->order(['create_time' => 'desc'])->paginate(15);
  148. }
  149. /**
  150. * 导出列表
  151. * @param array $param
  152. * @author: zq
  153. * @Time: 2021/10/15 13:51
  154. */
  155. public function listExport(array $params)
  156. {
  157. //$ids = $param['goods_id'];
  158. $data['header'] = ['时间', '应付总金额(含运费)', '应付商品总金额', '实付金额(含运费)', '运费','平台优惠券抵扣', '成交用户数','订单数','客单价'];
  159. $data['filename'] = '销售分析';
  160. $data['data'] = [];
  161. $filter = [];
  162. isset($params['ftype'])&&$params['ftype']>-1 && $filter[] = ['ftype', '=', (int)$params['ftype']];
  163. // 申请时间
  164. if (!empty($params['betweenTime'])) {
  165. $times = between_time($params['betweenTime']);
  166. $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  167. $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
  168. }
  169. $res = $this->where($filter)->order(['create_time' => 'desc'])->select();
  170. /*
  171. $data = ['total_price'=>$t1,
  172. 'total_goods_price'=>$t2,
  173. 'total_pay_price'=>$t3,
  174. 'total_express_price'=>$t4,
  175. 'total_coupon_price'=>$t5,
  176. 'total_users'=>$t6,
  177. 'total_orders'=>$t7,
  178. 'pcts'=>$t8];
  179. */
  180. $newdata = [];
  181. foreach($res as $row){
  182. $newrow['remark'] = $row['remark'];
  183. $newrow['total_price'] = $row['total_price'];
  184. $newrow['total_goods_price'] = $row['total_goods_price'];
  185. $newrow['total_pay_price'] = $row['total_pay_price'];
  186. $newrow['total_express_price'] = $row['total_express_price'];
  187. $newrow['total_coupon_price'] = $row['total_coupon_price'];
  188. $newrow['total_users'] = $row['total_users'];
  189. $newrow['total_orders'] = $row['total_orders'];
  190. $newrow['pcts'] = $row['pcts'];
  191. $newdata[] = $newrow;
  192. }
  193. $data['data'] = $newdata;
  194. return $data;
  195. }
  196. }