Browse Source

Merge branch 'store_new_user_report_1029'

master
liangyuyan 5 years ago
parent
commit
a2544fa93f
  1. 15
      app/Admin/Common/Type.php
  2. 57
      app/Admin/Controllers/StoreUserReportController.php
  3. 121
      app/Admin/Repositories/StoreUserReport.php
  4. 2
      app/Admin/routes.php
  5. 2
      app/Models/FinancialRecord0.php
  6. 2
      app/Models/FinancialRecord1.php
  7. 2
      app/Models/FinancialRecord2.php
  8. 2
      app/Models/FinancialRecord3.php
  9. 2
      app/Models/FinancialRecord4.php
  10. 3
      app/Models/StoreAccount.php
  11. 34
      app/Models/v3/Store.php
  12. 3
      resources/lang/zh-CN/store-user-report.php

15
app/Admin/Common/Type.php

@ -7,6 +7,7 @@ namespace App\Admin\Common;
class Type
{
/**
* lanzu_financial_record
* 流水类型,大的分类,<100是奖励分账等收入项 >=100是提现消费等支出项
*/
const MONEY_TYPE_CS_PLAT_NEW_USER = 1; // 社区服务点新用户奖励(线上订单完成)
@ -29,6 +30,20 @@ class Type
const MONEY_TYPE_MM = 103; //市场经理提现类型
const MONEY_TYPE_CS = 104; //社区提现类型
// 账户类型
const USER_TYPE_LEDGER = -1; // 虚拟账户
const USER_TYPE_USER = 1; // user用户
const USER_TYPE_MP = 2; // 服务商 mp用户
const USER_TYPE_MM = 3; // 市场经理 mm用户
const USER_TYPE_CS = 4; // 社区代理点 cs用户
const USER_TYPE_STORE = 5; // 商户 store用户
// 流水关联id类型
const SOURCE_TYPE_NONE = 0; // 无类型
const SOURCE_TYPE_ORDER = 1; // 订单,关联主订单
const SOURCE_TYPE_STORE_WITHDRAW = 2; // 商户提现,关联商户提现表
/**
* lanzu_user_balance 提现类型
*/

57
app/Admin/Controllers/StoreUserReportController.php

@ -2,12 +2,15 @@
namespace App\Admin\Controllers;
use App\Admin\Actions\Grid\v3\DataReportOption;
use App\Admin\Repositories\StoreUserReport;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use App\Models\ImsCjdcMarket as marketModel;
use App\Models\v3\Market as MarketModel;
use App\Models\StoreAccount as StoreAccountModel;
use App\Models\v3\Store as StoreModel;
class StoreUserReportController extends AdminController
{
@ -24,37 +27,37 @@ class StoreUserReportController extends AdminController
$grid->disableEditButton();
$grid->disableQuickEditButton();
$grid->disableViewButton();
$grid->disableActions();
$grid->disableRowSelector();
$marketList = MarketModel::getMarketArray();
$storeList = StoreModel::getStoreArray();
$grid->store_id;
$grid->market_id->display(function ($markrtId) {
$market = marketModel::select('id', 'name')->find($markrtId);
if (!$market) {
return '<span style="color: red">数据错误</span>';
}
return $market->name;
$grid->market_id->display(function ($marketId) use($marketList){
return $marketList[$marketId] ?? '';
});
$grid->store_name;
$grid->new_user_total;
$grid->store_name->width('25%');
$grid->new_total;
$grid->filter(function (Grid\Filter $filter) {
$marketList = [];
$list = marketModel::select('id', 'name')->get()->toArray();
foreach ($list as $value) {
$marketList[$value['id']] = $value['name'];
}
$filter->in('market_id')->multipleSelect($marketList);
$filter->like('store.name', '店铺名称');
$filter->whereBetween('time', function ($q) {
$start = $this->input['start'] ?? null;
$end = $this->input['end'] ?? null;
if ($start !== null) {
$q->whereRaw("ims_cjdc_store_account.time >= ?", $start);
}
if ($end !== null) {
$q->whereRaw("ims_cjdc_store_account.time <= ?", $end);
}
})->datetime();
$grid->filter(function (Grid\Filter $filter) use($marketList,$storeList){
$filter->panel();
$filter->equal('start_time','开始时间')->date()->width(2);
$filter->equal('end_time','结束时间')->date()->width(2);
$filter->equal('market_id')->select($marketList)->width(3);
$filter->in('store_id','店铺名称')->multipleSelect($storeList)->width(4);
});
$grid->tools([
new DataReportOption('today','store_new_user','今日'),
new DataReportOption('yesterday','store_new_user','昨日'),
new DataReportOption('this_week','store_new_user','本周'),
new DataReportOption('last_week','store_new_user','上周'),
new DataReportOption('this_month','store_new_user','本月'),
new DataReportOption('last_month','store_new_user','上月')
]);
// 每页10条
$grid->paginate(10);
});
}

121
app/Admin/Repositories/StoreUserReport.php

@ -6,7 +6,14 @@ use App\Models\v3\Store as Model;
use Dcat\Admin\Grid\Model as GridModel;
use Dcat\Admin\Repositories\EloquentRepository;
use App\Models\StoreAccount as StoreAccountModel;
use App\Models\FinancialRecord0 as FinancialRecordModel0;
use App\Models\FinancialRecord1 as FinancialRecordModel1;
use App\Models\FinancialRecord2 as FinancialRecordModel2;
use App\Models\FinancialRecord3 as FinancialRecordModel3;
use App\Models\FinancialRecord4 as FinancialRecordModel4;
use Dcat\Admin\Layout\Row;
use Illuminate\Support\Facades\DB;
use App\Admin\Common\Type;
class StoreUserReport extends EloquentRepository
{
@ -19,30 +26,112 @@ class StoreUserReport extends EloquentRepository
/**
* 获取统计列表数据
* 旧表流水数据未洗到新表,所以统计需要查询旧表和新表数据
*/
public function get(GridModel $model)
{
$this->setSort($model);
$this->setPaginate($model);
// 获取每页显示行数
$perPage = $model->getPerPage();
$startTime = $params['start_time'] ?? request()->input('start_time','');
$endTime = $params['end_time'] ?? request()->input('end_time','');
$marketId = $params['market_id'] ?? request()->input('market_id',0);
$storeIds = $params['store_id'] ?? request()->input('store_id',[]);
/* 根据流水查询 2020-08-18 目前用全匹配文字方式查询新用户 */
$storeAccountModel = new StoreAccountModel();
$query = $storeAccountModel::join('ims_cjdc_store as store','ims_cjdc_store_account.store_id','store.id')
->select('store_id','store.market_id','store.name as store_name',DB::raw("count(distinct ims_cjdc_store_account.id) as new_user_total"))
/* 根据流水查询 2020-08-18 用全匹配文字方式查询新用户 旧表 */
$storeAccount = StoreAccountModel::join(Model::$tableName.' as store',StoreAccountModel::$tableName.'.store_id','store.id')
->select('store_id','store.name as store_name','store.market_id',DB::raw("count(distinct ".StoreAccountModel::$tableName.".id) as new_user_total"))
->whereRaw("note = '新用户下单成功,平台奖励'")
->groupBy('store_id')
->orderBy('store.market_id','desc')
->orderBY('new_user_total','desc')
->orderBY('store_id','desc');
$model->getQueries()->unique()->each(function ($value) use (&$query) {
if ($value['method'] == 'paginate') {
$value['arguments'][1] = $this->getGridColumns();
} elseif ($value['method'] == 'get') {
$value['arguments'] = [$this->getGridColumns()];
->groupBy('store_id');
// dd($storeAccount->get()->toArray());
/* 根据流水查询 2020-10-29 用类型字段查询新用户 新表 */
$financial0 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f0.id) as new_user_total'))
->join(FinancialRecordModel0::$tableName.' as f0',Model::$tableName.'.user_id','=','f0.user_id','left')
->whereRaw(Model::$tableName.'.user_id MOD 5 = 0')
->whereRaw('user_type ='.Type::USER_TYPE_STORE)
// ->where('source_type',Type::SOURCE_TYPE_ORDER)
// ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
->whereRaw('f0.created_at > 1600444800')
->groupBy('store_id','store_name','market_id');
$financial1 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f1.id) as new_user_total'))
->join(FinancialRecordModel1::$tableName.' as f1',Model::$tableName.'.user_id','=','f1.user_id','left')
->whereRaw(Model::$tableName.'.user_id MOD 5 = 1')
->whereRaw('user_type ='.Type::USER_TYPE_STORE)
// ->where('source_type',Type::SOURCE_TYPE_ORDER)
// ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
->whereRaw('f1.created_at > 1600444800')
->groupBy('store_id','store_name','market_id');
$financial2 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f2.id) as new_user_total'))
->join(FinancialRecordModel2::$tableName.' as f2',Model::$tableName.'.user_id','=','f2.user_id','left')
->whereRaw(Model::$tableName.'.user_id MOD 5 = 2')
->whereRaw('user_type ='.Type::USER_TYPE_STORE)
// ->where('source_type',Type::SOURCE_TYPE_ORDER)
// ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
->whereRaw('f2.created_at > 1600444800')
->groupBy('store_id','store_name','market_id');
$financial3 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f3.id) as new_user_total'))
->join(FinancialRecordModel3::$tableName.' as f3',Model::$tableName.'.user_id','=','f3.user_id','left')
->whereRaw(Model::$tableName.'.user_id MOD 5 = 3')
->whereRaw('user_type ='.Type::USER_TYPE_STORE)
// ->where('source_type',Type::SOURCE_TYPE_ORDER)
// ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
->whereRaw('f3.created_at > 1600444800')
->groupBy('store_id','store_name','market_id');
$financial4 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f4.id) as new_user_total'))
->join(FinancialRecordModel4::$tableName.' as f4',Model::$tableName.'.user_id','=','f4.user_id','left')
->whereRaw(Model::$tableName.'.user_id MOD 5 = 4')
->whereRaw('user_type ='.Type::USER_TYPE_STORE)
// ->where('source_type',Type::SOURCE_TYPE_ORDER)
// ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
->whereRaw('f4.created_at > 1600444800')
->groupBy('store_id','store_name','market_id');
if($startTime){
$startTime = $startTime.' 00:00:00';
$storeAccount->whereRaw(StoreAccountModel::$tableName.".time >= '".$startTime."'");
$financial0->whereRaw('f0.created_at >='.strtotime($startTime));
$financial1->whereRaw('f1.created_at >='.strtotime($startTime));
$financial2->whereRaw('f2.created_at >='.strtotime($startTime));
$financial3->whereRaw('f3.created_at >='.strtotime($startTime));
$financial4->whereRaw('f4.created_at >='.strtotime($startTime));
}
if($endTime){
$endTime = $endTime.' 23:59:59';
$storeAccount->whereRaw(StoreAccountModel::$tableName.".time <='".$endTime."'");
$financial0->whereRaw('f0.created_at <='.strtotime($endTime));
$financial1->whereRaw('f1.created_at <='.strtotime($endTime));
$financial2->whereRaw('f2.created_at <='.strtotime($endTime));
$financial3->whereRaw('f3.created_at <='.strtotime($endTime));
$financial4->whereRaw('f4.created_at <='.strtotime($endTime));
}
if($marketId){
$storeAccount->whereRaw('store.market_id ='.$marketId);
$financial0->whereRaw(Model::$tableName.'.market_id ='.$marketId);
$financial1->whereRaw(Model::$tableName.'.market_id ='.$marketId);
$financial2->whereRaw(Model::$tableName.'.market_id ='.$marketId);
$financial3->whereRaw(Model::$tableName.'.market_id ='.$marketId);
$financial4->whereRaw(Model::$tableName.'.market_id ='.$marketId);
}
if($storeIds){
$storeStr = implode(',',$storeIds);//dd($storeArr);
$storeAccount->whereRaw('store.id in('. $storeStr.')');
$financial0->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
$financial1->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
$financial2->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
$financial3->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
$financial4->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
}
$query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
});
$unionAll = $storeAccount->union($financial0)->union($financial1)->union($financial2)->union($financial3)->union($financial4);
$query = DB::table(Db::raw("({$unionAll->toSql()}) as a1"))
->select('a1.store_id','a1.store_name','a1.market_id',DB::raw('SUM(a1.new_user_total) as new_total'))
->groupBy('market_id','store_id','store_name')
->orderBy('new_total','desc')->paginate($perPage);
$query = $query->toArray();

2
app/Admin/routes.php

@ -46,7 +46,7 @@ Route::group([
//根据市场id,商户名查询商户
$router->any('/api/stores', 'LanzuServiceSpeakerController@getStores');
//统计店铺新增用户
$router->resource('/storeUserReport', 'StoreUserReportController');
$router->resource('/store_new_user', 'StoreUserReportController');
$router->any('/sentsms', 'Test@sentSms');
// 分类

2
app/Models/FinancialRecord0.php

@ -8,4 +8,6 @@ class FinancialRecord0 extends FinancialRecord
{
protected $table = 'lanzu_financial_record_0';
public static $tableName = 'lanzu_financial_record_0';
}

2
app/Models/FinancialRecord1.php

@ -7,4 +7,6 @@ use Illuminate\Database\Eloquent\Model;
class FinancialRecord1 extends FinancialRecord
{
protected $table = 'lanzu_financial_record_1';
public static $tableName = 'lanzu_financial_record_1';
}

2
app/Models/FinancialRecord2.php

@ -7,4 +7,6 @@ use Illuminate\Database\Eloquent\Model;
class FinancialRecord2 extends FinancialRecord
{
protected $table = 'lanzu_financial_record_2';
public static $tableName = 'lanzu_financial_record_2';
}

2
app/Models/FinancialRecord3.php

@ -7,4 +7,6 @@ use Illuminate\Database\Eloquent\Model;
class FinancialRecord3 extends FinancialRecord
{
protected $table = 'lanzu_financial_record_3';
public static $tableName = 'lanzu_financial_record_3';
}

2
app/Models/FinancialRecord4.php

@ -7,4 +7,6 @@ use Illuminate\Database\Eloquent\Model;
class FinancialRecord4 extends FinancialRecord
{
protected $table = 'lanzu_financial_record_4';
public static $tableName = 'lanzu_financial_record_4';
}

3
app/Models/StoreAccount.php

@ -14,6 +14,9 @@ class StoreAccount extends Model
{
use HasDateTimeFormatter;
protected $table = 'ims_cjdc_store_account';
public static $tableName = 'ims_cjdc_store_account';
protected $dateFormat = 'U';
public $timestamps = false;

34
app/Models/v3/Store.php

@ -11,6 +11,8 @@ class Store extends Model
use HasDateTimeFormatter;
use SoftDeletes;
public static $tableName = 'lanzu_store';
protected $table = 'lanzu_store';
protected $dateFormat = 'U';
@ -103,4 +105,36 @@ class Store extends Model
return $value;
}
}
// 关联旧流水
public function storeAccount()
{
return $this->belongsTo('App\Models\v3\StoreAccount','store_id','id','light');
}
// 关联流水
public function financialRecord0()
{
return $this->belongsTo('App\Models\FinancialRecord0','user_id','user_id','light');
}
// 关联流水
public function financialRecord1()
{
return $this->belongsTo('App\Models\FinancialRecord1','user_id','user_id','light');
}
// 关联流水
public function financialRecord2()
{
return $this->belongsTo('App\Models\FinancialRecord2','user_id','user_id','light');
}
// 关联流水
public function financialRecord3()
{
return $this->belongsTo('App\Models\FinancialRecord3','user_id','user_id','light');
}
// 关联流水
public function financialRecord4()
{
return $this->belongsTo('App\Models\FinancialRecord4','user_id','user_id','light');
}
}

3
resources/lang/zh-CN/store-user-report.php

@ -2,13 +2,14 @@
return [
'labels' => [
'StoreUserReport' => '店铺新用户报表',
'storeUserReport' => '店铺新用户报表',
'store_new_user' => '店铺新用户报表',
],
'fields' => [
'store_id' => '店铺ID',
'market_id' => '所属市场',
'store_name' => '商家名称',
'new_user_total' => '新增用户总数',
'new_total' => '新增用户总数',
'mm_user_id' => '所属市场经理',
'time' => '时间',
],

Loading…
Cancel
Save