|
|
<?php
namespace App\Admin\Repositories\v3;
use App\Models\v3\Store as Model;use Dcat\Admin\Grid\Model as GridModel;use Dcat\Admin\Repositories\EloquentRepository;use Illuminate\Support\Facades\DB;use App\Models\ImsCjdcOrder as orderModel;use App\Models\ImsCjdcOrderMain as orderMainModel;use App\Models\v3\Market as MarketModel;use App\Models\StoreAccount as StoreAccountModel;use App\Models\v3\Store as StoreModel;use App\Models\v3\User as UserModel;
class OrderReport extends EloquentRepository{ /** * Model. * * @var string */ protected $eloquentClass = Model::class;
/** * 获取统计列表数据 * 订单 用户 * 订单总金额 现存用户总数 * 新用户补贴金额 新增用户总数 * 商户首单补贴金额 */ public function get(GridModel $model) { $this->setSort($model); $this->setPaginate($model);
$marketId = request()->input('market_id'); $startTime = request()->input('start_time'); $endTime = request()->input('end_time'); $orderMain = orderMainModel::select(DB::raw('COUNT(id) AS total_num, SUM(money) AS total_money'))->whereIn('state',[4,5,10]); $storeNewUser = StoreAccountModel::select(DB::raw("SUM( CASE WHEN note = '新用户下单成功,平台奖励' THEN 1 ELSE 0 END ) AS new_add_total, SUM( CASE WHEN note = '新用户下单成功,平台奖励' THEN money ELSE 0 END ) AS new_user_total,SUM( CASE WHEN note = '用户下单成功,平台奖励' THEN money ELSE 0 END ) AS store_total")); $newUser = UserModel::select(DB::raw('count(id) AS total'));
if($marketId){ $orderMain->where('market_id',$marketId);
// 查询这个市场下的店铺id
$storeIds = StoreModel::where('market_id',$marketId)->pluck('id'); $storeNewUser->whereIn('store_id',$storeIds); } if($startTime){ $orderMain->where([['created_at','>=',strtotime($startTime)]]); $storeNewUser->where([['time','>=',$startTime]]); $newUser->where([['created_at','>=',strtotime($startTime)]]); } if($endTime){ $orderMain->where([['created_at','<=',strtotime($endTime)]]); $storeNewUser->where([['time','<=',$endTime]]); $newUser->where([['created_at','<=',strtotime($endTime)]]); } if(empty($startTime) && empty($endTime)){ $time = date('Y-m-d',time()); $todayStart = $time.'00:00:00'; $todayEnd = $time.'23:59:59'; $orderMain->where([['created_at','>=',strtotime($todayStart)]]); $storeNewUser->where([['time','>=',$todayStart]]); $newUser->where([['created_at','>=',strtotime($todayStart)]]);
$orderMain->where([['created_at','<=',strtotime($todayEnd)]]); $storeNewUser->where([['time','<=',$todayEnd]]); $newUser->where([['created_at','<=',strtotime($todayEnd)]]); } // 订单总额
$orderReport = $orderMain->get()->toArray();//dd($orderReport);
// 总补贴金额
$storeNewUser = $storeNewUser->get(); // 总用户
$userTotal = $newUser->get();
$query = [ 'order_total_num' => $orderReport['total_num'], 'order_total_money' => $orderReport['total_money'],
'new_user_total_money' => $storeNewUser['new_user_total'], 'store_user_total_money' => $storeNewUser['store_total'], 'new_add_user' => $userTotal['new_add_total'],
'user_total' => $newUser['total'], ]; return $query; }}
|