123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\model;
- use app\common\model\store\shop\Order as ShopOrder;
- use app\common\service\Order as OrderService;
- use app\common\service\order\Complete as OrderCompleteService;
- use app\common\enum\OrderType as OrderTypeEnum;
- use app\common\enum\order\PayStatus as PayStatusEnum;
- use app\common\enum\order\OrderStatus as OrderStatusEnum;
- use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
- use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
- use app\common\library\helper;
- use app\console\model\Setting as SettingModel;
- use think\facade\Db;
- /**
- * 订单模型
- * Class Order
- * @package app\common\model
- */
- class OrderTj extends BaseModel
- {
- // 定义表名
- protected $name = 'order_tj';
- // 定义表名(外部引用)
- public static $tableName = 'order_tj';
- // 定义主键
- protected $pk = 'id';
- // // 定义别名
- // protected $alias = 'id';
- protected $ftype = [1=>'day',2=>'week',3=>'month'];
- //(商品总价 - 优惠券 - 分销员)+ 运费 = 微信支付+米卡抵扣+米卡抵扣运费(total_price-coupon_money-distrbutor_total_money)+express_price = pay_price+rice_card_money+rice_card_express_money
- public function day($dif = 1){
- //应付总金额(含运费)
- //未扣减优惠金额、米卡抵扣金额前的订单总金额
- $start = strtotime(Date("Y-m-d",time()-86400*$dif)." 00:00:00");
- $end = strtotime(Date("Y-m-d",time()-86400*$dif)." 23:59:59");
- $remark = Date("Y-m-d",time()-86400*$dif);
- $data = $this->search($start,$end);
- $data['remark'] = $remark;
- $data['ftype'] = 1;
- $data['tj_time'] = Date("Y-m-d H:i:s",$start + 10);
- $this->add($data);
- // var_dump(Date("Y-m-d H:i:s",$data['tj_time']));
- return true;
- }
- public function month($dif = 1){
- $d = date("d",time());
- if($d!='01'){
- return false;
- }
- $standardTime = date('Y-m-1');
- $_lastMonthStart = date('Y-m-01 00:00:00', strtotime("-1 month", strtotime($standardTime)));
- $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
- $start = strtotime($_lastMonthStart);
- $end = strtotime($_lastMonthEnd);
- $remark = substr($_lastMonthStart,0,7);
- $data = $this->search($start,$end);
- $data['ftype'] =3;
- $data['remark'] = $remark;
- $data['tj_time'] = Date("Y-m-d H:i:s",$start + 10);
- // var_dump(Date("Y-m-d H:i:s",$data['tj_time']));
- $this->add($data);
- return true;
- }
- public function week($dif = 1){
- //上周一
- $w = date("w",time());
- if($w!=1){
- return false;;
- }
- $t = $w==1?1:2; //如果当前时间是周一,则-1,如果已经是周二 要-2
- $d1 = date('Y-m-d 00:00:00',strtotime('-'.$t.' monday',time())); //无论今天几号,-1 monday为上一个有效周未
- //上周日
- $d7 = date('Y-m-d 23:59:59',strtotime('-1 sunday', time())); //
- //上一个有效周日,同样适用于其它星期
- $remark = substr($d1,0,10).'--'.substr($d7,0,10);
- $start = strtotime($d1);
- $end = strtotime($d7);
- $data = $this->search($start,$end);
- $data['ftype'] =2;
- $data['remark'] = $remark;
- $data['tj_time'] = Date("Y-m-d H:i:s",$start + 10);
- // var_dump(Date("Y-m-d H:i:s",$data['tj_time']));
- $this->add($data);
- return true;
- }
- //查找时间段
- public function search($start,$end){
- $sql = "SELECT sum(total_price+express_price) as t1,
- sum(total_price) as t2,sum(pay_price+rice_card_money) as t3,sum(express_price) as t4,
- sum(coupon_money) as t5,count(distinct(user_id)) as t6,count(*) as t7
- FROM yoshop_order where pay_status=? and create_time>? and create_time<?";
- $param = [20,$start,$end];
- $result = Db::query($sql, $param);
- $t1 = $result[0]['t1']??0;//应付总金额(含运费)
- $t2 = $result[0]['t2']??0;//应付商品总金额
- $t3 = $result[0]['t3']??0;//实付金额(含运费)
- $t4 = $result[0]['t4']??0;//运费
- $t5 = $result[0]['t5']??0;//平台优惠券抵扣
- $t6 = $result[0]['t6']??0;//成交用户数
- $t7 = $result[0]['t7']??0;//订单数
- if($t6>0){
- $t8 = $t3/$t6; //客单价
- }else{
- $t8 =0;
- }
-
- $data = ['total_price'=>$t1,
- 'total_goods_price'=>$t2,
- 'total_pay_price'=>$t3,
- 'total_express_price'=>$t4,
- 'total_coupon_price'=>$t5,
- 'total_users'=>$t6,
- 'total_orders'=>$t7,
- 'pcts'=>$t8];
- return $data;
- }
- //添加
- public function add($data){
- $m = new OrderTj;
- $one = $m->where('remark',$data['remark'])->where("ftype",$data['ftype'])->find();
- if(empty($one)){
- $m->save($data);
- }else{
- // var_dump($data);
- $one->where('id',$one->id)->update($data);
- }
- }
- public function getList($params){
- // 检查查询条件
- $filter = [];
- isset($params['ftype'])&&$params['ftype']>-1 && $filter[] = ['ftype', '=', (int)$params['ftype']];
- // 申请时间
- if (!empty($params['betweenTime'])) {
- $times = between_time($params['betweenTime']);
- $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
- $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
- }
- return $this->where($filter)->order(['create_time' => 'desc'])->paginate(15);
- }
- /**
- * 导出列表
- * @param array $param
- * @author: zq
- * @Time: 2021/10/15 13:51
- */
- public function listExport(array $params)
- {
- //$ids = $param['goods_id'];
- $data['header'] = ['时间', '应付总金额(含运费)', '应付商品总金额', '实付金额(含运费)', '运费','平台优惠券抵扣', '成交用户数','订单数','客单价'];
- $data['filename'] = '销售分析';
- $data['data'] = [];
- $filter = [];
- isset($params['ftype'])&&$params['ftype']>-1 && $filter[] = ['ftype', '=', (int)$params['ftype']];
- // 申请时间
- if (!empty($params['betweenTime'])) {
- $times = between_time($params['betweenTime']);
- $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
- $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
- }
- $res = $this->where($filter)->order(['create_time' => 'desc'])->select();
- /*
- $data = ['total_price'=>$t1,
- 'total_goods_price'=>$t2,
- 'total_pay_price'=>$t3,
- 'total_express_price'=>$t4,
- 'total_coupon_price'=>$t5,
- 'total_users'=>$t6,
- 'total_orders'=>$t7,
- 'pcts'=>$t8];
- */
- $newdata = [];
- foreach($res as $row){
- $newrow['remark'] = $row['remark'];
- $newrow['total_price'] = $row['total_price'];
- $newrow['total_goods_price'] = $row['total_goods_price'];
- $newrow['total_pay_price'] = $row['total_pay_price'];
- $newrow['total_express_price'] = $row['total_express_price'];
- $newrow['total_coupon_price'] = $row['total_coupon_price'];
- $newrow['total_users'] = $row['total_users'];
- $newrow['total_orders'] = $row['total_orders'];
- $newrow['pcts'] = $row['pcts'];
- $newdata[] = $newrow;
- }
- $data['data'] = $newdata;
- return $data;
- }
- }
|