| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193 |
- <?php
- namespace console\controllers;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\census\CensusMall;
- use common\models\census\CensusMallAnalysis;
- use common\models\census\CensusMallCapital;
- use common\models\census\CensusMallGoods;
- use common\models\census\CensusMallPaymentSingleMonth;
- use common\models\census\CensusMallPurchasingPower;
- use common\models\census\CensusMallRegion;
- use common\models\census\CensusMallRfAnalysis;
- use common\models\census\CensusMallRmAnalysis;
- use common\models\census\CensusMallSingleDay;
- use common\models\census\CensusMallSingleHour;
- use common\models\census\CensusMallSingleMonth;
- use common\models\census\CensusMallUser;
- use common\models\census\CensusMallUserLevel;
- use common\models\census\CensusMallUserLevelSingleDay;
- use common\models\census\CensusMallUserSource;
- use common\models\common\CapitalLog;
- use common\models\common\CommissionLog;
- use common\models\goods\Goods;
- use common\models\mall\Mall;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use common\models\user\UserWallet;
- use yii\console\Controller;
- use yii\db\Expression;
- class CensusController extends Controller
- {
- public $single_day_fields = ['mall_id', 'total_users', 'add_users', 'total_visitors',
- 'total_actives', 'total_views', 'total_order_money', 'total_pay_money',
- 'date', 'status', 'created_at', 'updated_at', 'add_goods', 'add_orders', 'add_pay_orders', 'add_pay_nums',
- 'add_pay_people_nums', 'add_refund_orders', 'settlement_commission', 'expenditure_commission'
- ];
- //初始化商城统计数据,不包含会员
- public function actionInit($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->initM($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->initM($redis, $mall);
- }
- }
- }
- //初始化会员数据
- public function actionInitUser($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->initUser($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->initUser($redis, $mall);
- }
- }
- }
- public function initUser($redis, $mall)
- {
- $config_list = CensusMallPurchasingPower::getConfigList();
- $mall_id = $mall['id'];
- $page_size = 1000;
- $new_customers = 0;//新客户数量
- $regular_customers = 0;//老客户数量
- $repurchase_people = 0;//复购人数
- $repurchase_money = 0;//复购金额
- $regular_customers_pay_money = 0;//老客户成交金额
- $regular_customers_pay_peoples = 0;//老客户成交人数
- $new_customers_pay_peoples = 0;//新客户成交人数
- $new_customers_pay_money = 0;//新客户成交金额
- $buy_power_arr = [];
- $mall_user_redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_USER, '', true) . ':' . $mall_id;;
- $i = 1;
- while ($user_list = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- $st = time();
- $user_ids = array_column($user_list, 'id');
- $order_list = Order::lists([
- 'where' => [
- ['user_id' => $user_ids],
- ['store_id'=>0],
- ['mch_id'=>0],
- ],
- 'select' => 'user_id,sum(pay_money) as pay_money,count(*) counts,pay_status',
- 'group' => 'user_id,pay_status',
- ]);
- $user_arr = [];
- $have_pay_user_ids = [];
- foreach ($order_list as $val) {
- if (isset($user_arr[$val['user_id']])) {
- $user_arr[$val['user_id']]['add_orders'] += $val['counts'];
- $user_arr[$val['user_id']]['add_order_money'] += $val['pay_money'];
- } else {
- $user_arr[$val['user_id']]['add_orders'] = $val['counts'];
- $user_arr[$val['user_id']]['add_order_money'] = $val['pay_money'];
- }
- if ($val['pay_status'] == 1) {
- $have_pay_user_ids[] = $val['user_id'];
- $user_arr[$val['user_id']]['total_pay_money'] = $val['pay_money'];
- $user_arr[$val['user_id']]['add_pay_orders'] = $val['counts'];
- $redis->hset($mall_user_redis_key, 'total_pay_money:' . $val['user_id'], $val['pay_money']);
- if ($val['counts'] > 1) {
- $repurchase_money += $val['pay_money'];
- $regular_customers_pay_money += $val['pay_money'];
- $regular_customers++;
- $repurchase_people++;
- $regular_customers_pay_peoples++;
- } elseif ($val['counts'] == 1) {
- $new_customers_pay_money += $val['pay_money'];
- $regular_customers++;
- $new_customers_pay_peoples++;
- }
- $add_key = '';
- foreach ($config_list as $key => $config) {
- if ($val['pay_money'] >= $config['min'] && $val['pay_money'] < $config['max']) {
- $add_key = $key;
- break;
- }
- }
- if ($add_key) {
- if (isset($buy_power_arr[$add_key])) {
- $buy_power_arr[$add_key]++;
- } else {
- $buy_power_arr[$add_key] = 1;
- }
- }
- }
- }
- $diff_user_ids = array_diff($user_ids, $have_pay_user_ids);
- $no_pay_counts = count($diff_user_ids);
- $new_customers += $no_pay_counts;
- if (isset($buy_power_arr[1])) {
- $buy_power_arr[1] += $no_pay_counts;
- } else {
- $buy_power_arr[1] = $no_pay_counts;
- }
- $sale_order_list = Order::lists([
- 'where' => [
- ['user_id' => $user_ids],
- ['store_id'=>0],
- ['mch_id'=>0],
- ['<>', 'finish_time', 0]
- ],
- 'select' => 'user_id,sum(pay_money) as pay_money,count(*) counts',
- 'group' => 'user_id',
- 'index' => 'user_id'
- ]);
- $pay_order_nums_list = OrderDetail::lists([
- 'where' => [
- ['user_id' => $user_ids],
- ['in', 'order_status', [1, 2, 3, 4, -5]]
- ],
- 'select' => 'user_id,sum(num) as num',
- 'group' => 'user_id',
- 'index' => 'user_id'
- ]);
- foreach ($user_list as $item) {
- $obj = CensusMallUser::findOne(['mall_id' => $item['mall_id'], 'user_id' => $item['id']]);
- if (empty($obj)) {
- $obj = new CensusMallUser();
- $obj->loadDefaultValues();
- $obj->mall_id = $item['mall_id'];
- $obj->user_id = $item['id'];
- }
- $obj->total_pay_money = $user_arr[$item['id']]['total_pay_money'] ?? 0;
- $obj->add_pay_orders = $user_arr[$item['id']]['add_pay_orders'] ?? 0;
- $obj->add_orders = $user_arr[$item['id']]['add_orders'] ?? 0;
- $obj->add_order_money = $user_arr[$item['id']]['add_order_money'] ?? 0;
- $obj->add_pay_nums = $pay_order_nums_list[$item['id']]['num'] ?? 0;
- $obj->add_sale_orders = $sale_order_list[$item['id']]['counts'] ?? 0;
- $obj->add_sale_money = $sale_order_list[$item['id']]['pay_money'] ?? 0;
- $obj->save();
- }
- $t = time() - $st;
- $page = ($i - 1) * $page_size;
- var_dump($page . '耗时:' . $t);
- $i++;
- }
- $this->setBuyPower($redis, $mall_id, $buy_power_arr);
- $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
- if (empty($census_mall_obj)) {
- $census_mall_obj = new CensusMall();
- $census_mall_obj->loadDefaultValues();
- $census_mall_obj->mall_id = $mall['id'];
- }
- $census_mall_obj->new_customers = $new_customers ?? 0;
- $census_mall_obj->regular_customers = $regular_customers ?? 0;
- $census_mall_obj->repurchase_people = $repurchase_people ?? 0;
- $census_mall_obj->repurchase_money = $repurchase_money ?? 0;
- $census_mall_obj->new_customers_pay_money = $new_customers_pay_money ?? 0;
- $census_mall_obj->new_customers_pay_peoples = $new_customers_pay_peoples ?? 0;
- $census_mall_obj->regular_customers_pay_money = $regular_customers_pay_money ?? 0;
- $census_mall_obj->regular_customers_pay_peoples = $regular_customers_pay_peoples ?? 0;
- $census_mall_obj->save();
- $key = RedisKeyEnum::CENSUS_MALL;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
- $set_data[] = 'new_customers';
- $set_data[] = $new_customers;
- $set_data[] = 'regular_customers';
- $set_data[] = $regular_customers;
- $set_data[] = 'repurchase_people';
- $set_data[] = $repurchase_people;
- $set_data[] = 'repurchase_money';
- $set_data[] = $repurchase_money;
- $set_data[] = 'new_customers_pay_money';
- $set_data[] = $new_customers_pay_money;
- $set_data[] = 'new_customers_pay_peoples';
- $set_data[] = $new_customers_pay_peoples;
- $set_data[] = 'regular_customers_pay_money';
- $set_data[] = $regular_customers_pay_money;
- $set_data[] = 'regular_customers_pay_peoples';
- $set_data[] = $regular_customers_pay_peoples;
- $redis->hmset($redis_key, ...$set_data);
- }
- public function initM($redis, $mall)
- {
- $this->initMall($redis, $mall);
- $this->updateCensusMallRegion($redis, $mall);
- $this->updateCensusMallGoods($redis, $mall);
- $this->updateCensusMallUserLevel($redis, $mall);
- $this->updateCensusMallUserSource($redis, $mall);
- $this->initAna($mall);
- $this->rfRm($mall);
- $date = date('Y-m-d', time());
- $this->dateOrder($redis, $mall, $date);
- $this->dateUser($redis, $mall, $date);
- $this->hourOrder($redis, $mall);
- $this->month($redis, $mall);
- $this->paymentMonth($redis, $mall);
- $this->fixUserWallet($mall['id']);
- $this->uLevel($mall['id']);
- $this->fixCommissionDate($mall['id']);
- $this->capitalBalance($mall['id']);
- $this->capitalScore($mall['id']);
- $this->capitalCommission($mall['id']);
- $this->fixGoodsAdd($mall['id']);
- $this->fixOrdersAdd($mall['id']);
- $this->fixPayOrdersAdd($mall['id']);
- $this->fixPayOrdersNum($mall['id']);
- $this->fixAddPayPeopleNums($mall['id']);
- $this->fixOrderRefund($mall['id']);
- $date = date('Y-m-d', strtotime('-1 days'));
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time);
- $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time);
- $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time);
- }
- public function initMall($redis, $mall)
- {
- $t = time();
- $mall_id = $mall['id'];
- $total_users = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
- $total_orders_users = Order::find()->where(['mall_id' => $mall['id'], 'store_id'=>0,'mch_id'=>0,'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->select('user_id')->groupBy('user_id')->count();
- $total_pay_people = Order::find()->where(['mall_id' => $mall['id'], 'store_id'=>0,'mch_id'=>0,'pay_status' => 1])->select('user_id')->groupBy('user_id')->count();
- $order_list = Order::lists(
- [
- 'where' => [
- ['mall_id' => $mall_id],
- ['store_id' => 0],
- ['mch_id' => 0],
- ],
- 'select' => 'sum(pay_money) as pay_money,pay_status,count(*) counts',
- 'group' => 'pay_status'
- ]
- );
- $order_arr = ['total_order_money' => 0, 'total_orders' => 0, 'total_pay_money' => 0, 'total_pay_orders' => 0];
- foreach ($order_list as $val) {
- $order_arr['total_order_money'] += $val['pay_money'];
- $order_arr['total_orders'] += $val['counts'];
- if ($val['pay_status'] == 1) {
- $order_arr['total_pay_money'] += $val['pay_money'];
- $order_arr['total_pay_orders'] += $val['counts'];
- }
- }
- $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
- if (empty($census_mall_obj)) {
- $census_mall_obj = new CensusMall();
- $census_mall_obj->loadDefaultValues();
- $census_mall_obj->mall_id = $mall['id'];
- }
- $census_mall_obj->total_order_money = $order_arr['total_order_money'];
- $census_mall_obj->total_orders = $order_arr['total_orders'];
- $census_mall_obj->total_pay_money = $order_arr['total_pay_money'];
- $census_mall_obj->total_pay_orders = $order_arr['total_pay_orders'];
- $census_mall_obj->total_users = $total_users ?? 0;
- $census_mall_obj->number_of_orders = $total_orders_users ?? 0;
- $census_mall_obj->total_pay_people = $total_pay_people ?? 0;
- $census_mall_obj->total_visitors = $total_users;
- $census_mall_obj->total_views = $total_users;
- $census_mall_obj->save();
- $key = RedisKeyEnum::CENSUS_MALL;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
- $set_data[] = 'total_pay_money';
- $set_data[] = $order_arr['total_pay_money'];
- $set_data[] = 'total_orders';
- $set_data[] = $order_arr['total_orders'];
- $set_data[] = 'total_users';
- $set_data[] = $total_users;
- $set_data[] = 'total_pay_orders';
- $set_data[] = $order_arr['total_pay_orders'];
- $set_data[] = 'total_pay_people';
- $set_data[] = $total_pay_people;
- $set_data[] = 'number_of_orders';
- $set_data[] = $total_orders_users;
- $set_data[] = 'total_order_money';
- $set_data[] = $order_arr['total_order_money'];
- $set_data[] = 'total_visitors';
- $set_data[] = $total_users;
- $set_data[] = 'total_views';
- $set_data[] = $total_users;
- $redis->hmset($redis_key, ...$set_data);
- $date = date('Y-m-d', time());
- $start_time = strtotime($date . ' 00:00:00');
- $today_total_users = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['>=', 'created_at', $start_time])
- ->count();
- $today_order_list = Order::lists(
- [
- 'where' => [
- ['mall_id' => $mall_id],
- ['store_id' => 0],
- ['mch_id' => 0],
- ['>=', 'created_at', $start_time]
- ],
- 'select' => 'sum(pay_money) as pay_money,pay_status,count(*) counts',
- 'group' => 'pay_status'
- ]
- );
- $today_order_arr = ['total_order_money' => 0, 'total_pay_money' => 0];
- foreach ($today_order_list as $val) {
- $today_order_arr['total_order_money']+=$val['pay_money'];
- if($val['pay_status']==1) $today_order_arr['total_pay_money']+=$val['pay_money'];
- }
- $census_mall_single_day_obj = CensusMallSingleDay::findOne(['mall_id' => $mall['id'], 'date' => $date]);
- if (empty($census_mall_single_day_obj)) {
- $census_mall_single_day_obj = new CensusMallSingleDay();
- $census_mall_single_day_obj->loadDefaultValues();
- $census_mall_single_day_obj->mall_id = $mall['id'];
- $census_mall_single_day_obj->date = $date;
- }
- $census_mall_single_day_obj->total_users = $total_users ?? 0;
- $census_mall_single_day_obj->add_users = $today_total_users ?? 0;
- $census_mall_single_day_obj->total_pay_money = $today_order_arr['total_pay_money'];
- $census_mall_single_day_obj->total_order_money = $today_order_arr['total_order_money'];
- $census_mall_single_day_obj->save();
- $date = date('Y-m-d', time());
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
- $set_data = [];
- $set_data[] = 'total_order_money';
- $set_data[] = $today_order_arr['total_order_money'];
- $set_data[] = 'total_pay_money';
- $set_data[] = $today_order_arr['total_pay_money'];
- $set_data[] = 'add_users';
- $set_data[] = $today_total_users;
- $set_data[] = 'total_users';
- $set_data[] = $total_users;
- $redis->hmset($redis_key, ...$set_data);
- $redis->expire($redis_key, 24*3600);
- $t = time()-$t;
- var_dump('initMall执行耗时'.$t);
- }
- //复购,新客户,老客户
- public function actionFugou($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fuGou($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fuGou($redis, $mall);
- }
- }
- }
- public function fuGou($redis, $mall)
- {
- $fu_gou_list = Order::lists(['where' => [
- ['mall_id' => $mall['id']],
- ['pay_status' => 1],
- ['store_id' => 0],
- ['mch_id' => 0],
- ],
- 'select' => 'count(*) as count,user_id',
- 'group' => 'user_id'
- ]);
- $repurchase_people = 0;
- $repurchase_people_user_id_arr = [];
- $new_customers_pay_peoples = 0;
- $new_customers_pay_peoples_user_id_arr = [];
- foreach ($fu_gou_list as $item) {
- if ($item['count'] > 1) {
- $repurchase_people++;
- $repurchase_people_user_id_arr[] = $item['user_id'];
- }
- if ($item['count'] == 1) {
- $new_customers_pay_peoples++;
- $new_customers_pay_peoples_user_id_arr[] = $item['user_id'];
- }
- }
- $repurchase_money = Order::find()->where(
- [
- 'mall_id' => $mall['id'],
- 'store_id'=>0,
- 'mch_id'=>0,
- 'user_id' => $repurchase_people_user_id_arr,
- 'pay_status' => 1
- ])->sum('pay_money');
- $new_customers_pay_money = Order::find()->where([
- 'mall_id' => $mall['id'],
- 'store_id'=>0,
- 'mch_id'=>0,
- 'user_id' => $new_customers_pay_peoples_user_id_arr,
- 'pay_status' => 1
- ])->sum('pay_money');
- $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
- if (empty($census_mall_obj)) {
- $census_mall_obj = new CensusMall();
- $census_mall_obj->loadDefaultValues();
- $census_mall_obj->mall_id = $mall['id'];
- }
- $census_mall_obj->repurchase_people = $repurchase_people ?? 0;
- $census_mall_obj->regular_customers_pay_peoples = $repurchase_people ?? 0;
- $census_mall_obj->repurchase_money = $repurchase_money ?? 0;
- $census_mall_obj->regular_customers_pay_money = $repurchase_money ?? 0;
- $census_mall_obj->new_customers_pay_peoples = $new_customers_pay_peoples ?? 0;
- $census_mall_obj->new_customers_pay_money = $new_customers_pay_money ?? 0;
- $regular_customers = Order::find()->where([
- 'mall_id' => $mall['id'],
- 'store_id'=>0,
- 'mch_id'=>0,
- 'pay_status' => 1])->select('user_id')->groupBy('user_id')->count();
- $new_customers = $census_mall_obj->total_users - $regular_customers;
- if ($new_customers < 0) $new_customers = 0;
- $census_mall_obj->regular_customers = $regular_customers ?? 0;
- $census_mall_obj->new_customers = $new_customers ?? 0;
- $census_mall_obj->save();
- $key = RedisKeyEnum::CENSUS_MALL;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
- $set_data[] = 'repurchase_people';
- $set_data[] = $repurchase_people;
- $set_data[] = 'regular_customers_pay_peoples';
- $set_data[] = $repurchase_people;
- $set_data[] = 'repurchase_money';
- $set_data[] = $repurchase_money;
- $set_data[] = 'regular_customers_pay_money';
- $set_data[] = $repurchase_money;
- $set_data[] = 'new_customers_pay_peoples';
- $set_data[] = $new_customers_pay_peoples;
- $set_data[] = 'new_customers_pay_money';
- $set_data[] = $new_customers_pay_money;
- $set_data[] = 'regular_customers';//老客户
- $set_data[] = $regular_customers;//老客户
- $set_data[] = 'new_customers';//新客户
- $set_data[] = $new_customers;//新客户
- $redis->hmset($redis_key, ...$set_data);
- var_dump('fuGou执行结束');
- }
- //地区
- public function actionRegion($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->updateCensusMallRegion($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->updateCensusMallRegion($redis, $mall);
- }
- }
- }
- public function updateCensusMallRegion($redis, $mall)
- {
- $t = time();
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_REGION, '', true) . ':' . $mall['id'];
- $region_order_list = Order::lists(['where' =>
- [
- ['mall_id' => $mall['id']],
- ['store_id'=>0],
- ['mch_id'=>0],
- ],
- 'select' => 'count(*) as count,province',
- 'group' => 'province'
- ]
- );
- foreach ($region_order_list as $item) {
- if ($item['province']) {
- $obj = CensusMallRegion::findOne(['mall_id' => $mall['id'], 'province_id' => $item['province']]);
- if (empty($obj)) {
- $obj = new CensusMallRegion();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall['id'];
- $obj->province_id = $item['province'];
- }
- $obj->total_orders = $item['count'] ?? 0;
- $obj->save();
- $field = 'total_orders_province_id_' . $item['province'];
- $redis->hset($redis_key, $field, $obj->total_orders);
- }
- }
- $region_pay_pay_money_list = Order::lists(['where' =>
- [
- ['mall_id' => $mall['id']],
- ['pay_status' => 1],
- ['store_id'=>0],
- ['mch_id'=>0],
- ],
- 'select' => 'sum(pay_money) as pay_money,province',
- 'group' => 'province'
- ]
- );
- foreach ($region_pay_pay_money_list as $item) {
- if ($item['province']) {
- $obj = CensusMallRegion::findOne(['mall_id' => $mall['id'], 'province_id' => $item['province']]);
- if (empty($obj)) {
- $obj = new CensusMallRegion();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall['id'];
- $obj->province_id = $item['province'];
- }
- $obj->total_pay_money = $item['pay_money'] ?? 0;
- $obj->save();
- $field = 'total_pay_money_province_id_' . $obj->province_id;
- $redis->hset($redis_key, $field, $obj->total_pay_money);
- }
- }
- $t = time()-$t;
- var_dump('updateCensusMallRegion耗时'.$t);
- }
- //商品
- public function actionGoods($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->updateCensusMallGoods($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->updateCensusMallGoods($redis, $mall);
- }
- }
- }
- public function updateCensusMallGoods($redis, $mall)
- {
- $mall_id = $mall['id'];
- $t = time();
- $i = 1;
- $page_size = 1000;
- $goods_listings = 0;
- $add_goods = 0;
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, '', true) . ':' . $mall['id'];
- $pay_money_redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_PAY_MONEY, '', true) . ':' . $mall['id'];
- $redis->del($redis_key);
- $redis->del($pay_money_redis_key);
- while ($goods_list = Goods::find()->where(['mall_id' => $mall_id,'mch_id'=>0, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->select('is_on_sale,sales_num,id')->limit($page_size)->all()) {
- $goods_ids = [];
- foreach ($goods_list as $goods) {
- $goods_ids[]=$goods['id'];
- $add_goods++;
- if (!empty($goods['is_on_sale'])) $goods_listings++;
- }
- //1待发货 2 已发货 3已收货 4已结束
- $goods_price_arr = OrderDetail::lists([
- 'where'=>[
- ['goods_id' => $goods_ids],
- ['order_status' => [1, 2, 3, 4, -5]]
- ],
- 'select'=>'sum(goods_price) as goods_price,goods_id',
- 'group'=>'goods_id',
- 'index'=>'goods_id'
- ]);
- foreach ($goods_list as $goods) {
- $obj = CensusMallGoods::findOne(['mall_id' => $mall['id'], 'goods_id' => $goods['id']]);
- if (empty($obj)) {
- $obj = new CensusMallGoods();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall['id'];
- $obj->goods_id = $goods['id'];
- }
- $value = 'goods_id:' . $goods['id'];
- if ($goods['sales_num'] > 0) $redis->zadd($redis_key, $goods['sales_num'], $value);
- $total_pay_money = $goods_price_arr[$goods['id']]['goods_price']??0;
- if ($total_pay_money > 0) $redis->zadd($pay_money_redis_key, $total_pay_money, $value);
- $obj->total_pay_money = $total_pay_money;
- $obj->total_sales_volume = $goods['sales_num'];
- $obj->save();
- }
- $i++;
- }
- $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
- if (empty($census_mall_obj)) {
- $census_mall_obj = new CensusMall();
- $census_mall_obj->loadDefaultValues();
- $census_mall_obj->mall_id = $mall['id'];
- }
- $census_mall_obj->goods_listings = $goods_listings ?? 0;
- $key = RedisKeyEnum::CENSUS_MALL;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
- $set_data[] = 'goods_listings';
- $set_data[] = $goods_listings;
- $set_data[] = 'add_goods';
- $set_data[] = $add_goods;
- $redis->hmset($redis_key, ...$set_data);
- $t = time()-$t;
- var_dump("商城{$mall_id}updateCensusMallGoods执行耗时".$t);
- }
- //会员等级分布
- public function actionUserLevel($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->updateCensusMallUserLevel($redis, $mall);
- $this->updateCensusMallUserSource($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->updateCensusMallUserLevel($redis, $mall);
- $this->updateCensusMallUserSource($redis, $mall);
- }
- }
- }
- public function updateCensusMallUserLevel($redis, $mall)
- {
- $t = time();
- $user_level = UserLevel::lists(['where' => [['mall_id' => $mall['id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]], 'select' => 'level']);
- $user_level_arr = array_column($user_level, 'level');
- $user_level_arr[] = 0;
- $user_list = User::lists([
- 'where' => [
- ['mall_id' => $mall['id']],
- ['level' => $user_level_arr],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- 'select' => 'count(*) as count,level',
- 'group' => 'level'
- ]);
- $key = RedisKeyEnum::CENSUS_MALL_USER_LEVEL;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
- foreach ($user_list as $item) {
- $redis->hset($redis_key, 'level_' . $item['level'], $item['count']);
- $obj = CensusMallUserLevel::findOne(['mall_id' => $mall['id'], 'level' => $item['level']]);
- if (empty($obj)) {
- $obj = new CensusMallUserLevel();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall['id'];
- $obj->level = $item['level'];
- }
- $obj->numbers = $item['count'];
- $obj->save();
- }
- $t= time()-$t;
- var_dump("商城".$mall['id']."updateCensusMallUserLevel执行耗时".$t);
- }
- public function updateCensusMallUserSource($redis, $mall)
- {
- $t = time();
- $user_list = User::lists([
- 'where' => [
- ['mall_id' => $mall['id']],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- 'select' => 'count(*) as count,platform',
- 'group' => 'platform'
- ]);
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, '', true) . ':' . $mall['id'];
- foreach ($user_list as $item) {
- if (empty($item['platform'])) $item['platform'] = 'manual';
- $redis->hset($redis_key, $item['platform'], $item['count']);
- $obj = CensusMallUserSource::findOne(['mall_id' => $mall['id'], 'source' => $item['platform']]);
- if (empty($obj)) {
- $obj = new CensusMallUserSource();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall['id'];
- $obj->source = $item['platform'];
- }
- $obj->numbers = $item['count'];
- $obj->save();
- }
- $t = time()-$t;
- var_dump('商城'.$mall['id'].'updateCensusMallUserSource执行'.$t);
- }
- //当天交易情况
- public function actionToday($mall_id = null, $date = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->dateOrder($redis, $mall, $date);
- $this->dateUser($redis, $mall, $date);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->dateOrder($redis, $mall, $date);
- $this->dateUser($redis, $mall, $date);
- }
- }
- }
- public function dateOrder($redis, $mall, $date)
- {
- $mall_id = $mall['id'];
- $i = 1;
- $page_size = 1000;
- $date_order_money = [];
- $date_pay_money = [];
- if (empty($date)) $date = date('Y-m-d', time());
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- while ($order_list = Order::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($order_list as $item) {
- $created_at = $item['created_at'];
- $date = date('Y-m-d', $created_at);
- if (isset($date_arr[$date])) {
- $date_order_money[$date] += $item['pay_money'];
- } else {
- $date_order_money[$date] = $item['pay_money'];
- }
- if ($item['pay_status']) {
- $pay_time = $item['pay_time'];
- $date_pay = date('Y-m-d', $pay_time);
- if (isset($date_pay_money[$date_pay])) {
- $date_pay_money[$date_pay] += $item['pay_money'];
- } else {
- $date_pay_money[$date_pay] = $item['pay_money'];
- }
- }
- }
- $i++;
- }
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
- foreach ($date_order_money as $date => $money) {
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
- $redis->hset($redis_key, 'total_order_money', $money);
- $redis->expire($redis_key, 24 * 3600);
- $data = [
- 'mall_id' => $mall_id,
- 'total_order_money' => $money,
- 'date' => $date,
- ];
- CensusMallSingleDay::setData($data);
- }
- foreach ($date_pay_money as $date => $money) {
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
- $redis->hset($redis_key, 'total_order_money', $money);
- $redis->expire($redis_key, 24 * 3600);
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $date;
- }
- $obj->total_pay_money = $money;
- $obj->save();
- }
- var_dump('dateOrder执行结束');
- }
- public function dateUserOld($redis, $mall, $date)
- {
- $date_arr = [];
- $i = 1;
- $page_size = 1000;
- $mall_id = $mall['id'];
- if (empty($date)) $date = date('Y-m-d', time());
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- while ($user_list = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($user_list as $item) {
- $created_at = $item['created_at'];
- $date = date('Y-m-d', $created_at);
- if (isset($date_arr[$date])) {
- $date_arr[$date]++;
- } else {
- $date_arr[$date] = 1;
- }
- }
- $i++;
- }
- if ($date_arr) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
- $total_nums = 0;
- foreach ($date_arr as $date => $nums) {
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
- $total_nums += $nums;
- $redis->hset($redis_key, 'total_users', $total_nums);
- $redis->hset($redis_key, 'add_users', $nums);
- $redis->expire($redis_key, 24 * 3600);
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $date;
- }
- $obj->add_users = $nums;
- $obj->total_users = $total_nums;
- $obj->save();
- }
- }
- }
- //单日会员数据
- public function dateUser($redis, $mall, $date)
- {
- $t = time();
- $mall_id = $mall['id'];
- $query = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (!empty($date)) {
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- $query->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]);
- }
- $list = $query->select(new Expression("mall_id,count(*) as counts,FROM_UNIXTIME(created_at,'%Y-%m-%d') as date"))
- ->groupBy('date')->asArray()->all();
- if ($list) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
- $total_nums = 0;
- foreach ($list as $item) {
- $nums = $item['counts'];
- $date = $item['date'];
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
- $total_nums += $nums;
- $redis->hset($redis_key, 'total_users', $total_nums);
- $redis->hset($redis_key, 'add_users', $nums);
- $redis->expire($redis_key, 24 * 3600);
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $date;
- }
- $obj->add_users = $nums;
- $obj->total_users = $total_nums;
- $res = $obj->save();
- var_dump($res);
- }
- }
- $t = time()-$t;
- var_dump('dateUser执行结束'.$t);
- }
- //当天每小时的交易情况
- public function actionHour($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->hourOrder($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->hourOrder($redis, $mall);
- }
- }
- }
- public function hourOrder($redis, $mall)
- {
- $t = time();
- $mall_id = $mall['id'];
- $i = 1;
- $page_size = 1000;
- $hour_pay_money = [];
- $date = date('Y-m-d', time());
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- while ($order_list = Order::find()->where(['mall_id' => $mall_id,'store_id'=>0,'mch_id'=>0,
- 'pay_status' => 1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
- ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($order_list as $item) {
- if ($item['pay_status']) {
- $pay_time = $item['pay_time'];
- $hour = date('H', $pay_time);
- if (isset($hour_pay_money[$hour])) {
- $hour_pay_money[$hour] += $item['pay_money'];
- } else {
- $hour_pay_money[$hour] = $item['pay_money'];
- }
- }
- }
- $i++;
- }
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR . ':' . $mall_id . ':' . $date;
- foreach ($hour_pay_money as $hour => $money) {
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $field = 'hour_' . $hour;
- $redis->hset($redis_key, $field, $money);
- $redis->expire($redis_key, 24 * 3600);
- $obj = CensusMallSingleHour::findOne(['mall_id' => $mall_id, 'hour' => $hour, 'date' => $date]);
- if (empty($obj)) {
- $obj = new CensusMallSingleHour();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->hour = $hour;
- $obj->date = $date;
- }
- $obj->total_pay_money = $money;
- $obj->save();
- }
- $t = time()-$t;
- var_dump('hourOrder执行结束'.$t);
- }
- public function setBuyPower($redis, $mall_id, $buy_power_arr)
- {
- $set_data = [];
- foreach ($buy_power_arr as $add_key => $nums) {
- $redis_add_key = RedisKeyEnum::CENSUS_MALL_BUY_POWER . 'add_' . $add_key;
- $redis_add_key = RedisKeyEnum::suffix($redis_add_key, '', true);
- $redis->setbit($redis_add_key, $add_key, 1);
- $obj = CensusMallPurchasingPower::findOne(['mall_id' => $mall_id, 'purchasing_power' => $add_key]);
- if (empty($obj)) {
- $obj = new CensusMallPurchasingPower();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->purchasing_power = $add_key;
- }
- $obj->numbers = $nums;
- $obj->save();
- $field = 'buy_power:' . $add_key;
- $set_data[] = $field;
- $set_data[] = $nums;
- }
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_BUY_POWER, '', true) . ':' . $mall_id;
- if ($set_data) {
- $redis->hmset($redis_key, ...$set_data);
- }
- }
- public function userVisitors($redis, $mall)
- {
- $counts = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
- $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
- $census_mall_obj->total_visitors = $counts;
- $census_mall_obj->save();
- $set_data[] = 'total_visitors';
- $set_data[] = $counts;
- $set_data[] = 'total_views';
- $set_data[] = $counts;
- $key = RedisKeyEnum::CENSUS_MALL;
- $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
- $redis->hmset($redis_key, ...$set_data);
- var_dump('userVisitors执行结束');
- }
- public function actionMonth($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->month($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->month($redis, $mall);
- }
- }
- }
- public function month($redis, $mall)
- {
- $t = time();
- $mall_id = $mall['id'];
- //下单金额
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money FROM
- qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 GROUP BY month;
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $set_data = [];
- $set_data[] = 'total_order_money';
- $set_data[] = $item['total_pay_money'];
- $redis->hmset($redis_key, ...$set_data);
- $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleMonth();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->month = $item['month'];
- }
- $obj->total_order_money = $item['total_pay_money'];
- $res = $obj->save();
- }
- //支付金额
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money FROM
- qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and pay_status=1 GROUP BY month;
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $set_data = [];
- $set_data[] = 'total_pay_money';
- $set_data[] = $item['total_pay_money'];
- $redis->hmset($redis_key, ...$set_data);
- $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleMonth();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->month = $item['month'];
- }
- $obj->total_pay_money = $item['total_pay_money'];
- $res = $obj->save();
- }
- //抵扣金额
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money
- ,sum((original_goods_price+shipping_money)) as original_goods_price
- FROM
- qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 GROUP BY month
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $total_deduction_money = bcsub($item['original_goods_price'], $item['total_pay_money'], 2);
- if ($total_deduction_money < 0) $total_deduction_money = 0;
- $set_data = [];
- $set_data[] = 'total_deduction_money';
- $set_data[] = $total_deduction_money;
- $redis->hmset($redis_key, ...$set_data);
- $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleMonth();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->month = $item['month'];
- }
- $obj->total_deduction_money = $total_deduction_money;
- $res = $obj->save();
- }
- //退款金额
- $sql = "
- SELECT FROM_UNIXTIME(refund_at,'%Y-%m') as month ,sum(reality_refund_price) as total_refund_money FROM
- qimall_order_refund where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and is_refund = 1 GROUP BY month
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $set_data = [];
- $set_data[] = 'total_refund_money';
- $set_data[] = $item['total_refund_money'];
- $redis->hmset($redis_key, ...$set_data);
- $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleMonth();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->month = $item['month'];
- }
- $obj->total_refund_money = $item['total_refund_money'];
- $res = $obj->save();
- }
- //充值金额
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(change_num) as total_recharge_money FROM
- qimall_balance_log where
- mall_id = {$mall_id} and change_type = 'add' and from_type = 'recharge' and status = 1 GROUP BY month
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $set_data = [];
- $set_data[] = 'total_recharge_money';
- $set_data[] = $item['total_recharge_money'];
- $redis->hmset($redis_key, ...$set_data);
- $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleMonth();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->month = $item['month'];
- }
- $obj->total_recharge_money = $item['total_recharge_money'];
- $res = $obj->save();
- }
- $t = time()-$t;
- var_dump('month执行结束'.$t);
- }
- public function actionPaymentMonth($mall_id = null)
- {
- $redis = \Yii::$app->redis;
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->paymentMonth($redis, $mall);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->paymentMonth($redis, $mall);
- }
- }
- }
- public function paymentMonth($redis, $mall)
- {
- $t = time();
- $mall_id = $mall['id'];
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money,payment_type FROM
- qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and pay_status=1 and payment_type>0 GROUP BY month,payment_type
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $key = RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $set_data = [];
- $set_data[] = 'pay_type:' . $item['payment_type'];
- $set_data[] = $item['total_pay_money'];
- $redis->hmset($redis_key, ...$set_data);
- $obj = CensusMallPaymentSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month'], 'pay_type' => $item['payment_type']]);
- if (empty($obj)) {
- $obj = new CensusMallPaymentSingleMonth();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->month = $item['month'];
- $obj->pay_type = $item['payment_type'];
- }
- $obj->total_pay_money = $item['total_pay_money'];
- $res = $obj->save();
- }
- $t = time()-$t;
- var_dump('paymentMonth执行结束'.$t);
- }
- public function actionFixUserWallet($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixUserWallet($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixUserWallet($mall['id']);
- }
- }
- }
- public function fixUserWallet($mall_id)
- {
- $t = time();
- $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('sum(score) as can_use_score,sum(balance) as can_use_balance,sum(commission) as can_use_commission')
- ->asArray()
- ->one();
- $obj = CensusMall::findOne(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($obj)) {
- $obj = new CensusMall();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->status = StatusEnum::ENABLED;
- }
- $obj->can_use_score = $result['can_use_score'] ?? 0;
- $obj->can_use_balance = $result['can_use_balance'] ?? 0;
- $obj->can_use_commission = $result['can_use_commission'] ?? 0;
- $res = $obj->save();
- $t = time()-$t;
- var_dump('fixUserWallet执行结束'.$t);
- }
- //会员等级情况
- public function actionULevel($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->uLevel($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->uLevel($mall['id']);
- }
- }
- }
- public function uLevel($mall_id)
- {
- $t = time();
- $list = User::lists(
- [
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ],
- 'select' => 'count(*) as counts,level',
- 'group' => 'level'
- ]
- );
- foreach ($list as $item) {
- $date = date('Y-m-d', $t);
- $model = CensusMallUserLevelSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date, 'level' => $item['level']]);
- if (empty($model)) {
- $model = new CensusMallUserLevelSingleDay();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->level = $item['level'];
- $model->date = $date;
- }
- $model->numbers = $item['counts'];
- $res = $model->save();
- }
- $t = time()-$t;
- var_dump('uLevel执行结束');
- }
- //商城资金统计
- public function actionCapital($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->capitalBalance($mall['id']);
- $this->capitalScore($mall['id']);
- $this->capitalCommission($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->capitalBalance($mall['id']);
- $this->capitalScore($mall['id']);
- $this->capitalCommission($mall['id']);
- }
- }
- }
- public function capitalBalance($mall_id)
- {
- $type = 'balance';
- $total_nums = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->sum('total_balance');
- CapitalLog::$capitalType = $type;
- $use_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'change_type' => 'sub', 'status' => StatusEnum::ENABLED])->sum('change_num');
- $surplus_nums = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->sum('balance');
- CapitalLog::$capitalType = $type;
- $withdrawal_nums = CapitalLog::find()->where(['mall_id' => $mall_id,
- 'change_type' => 'sub',
- 'from_type' => 'withdrawal',
- 'status' => StatusEnum::ENABLED])->sum('change_num');
- $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]);
- if (empty($model)) {
- $model = new CensusMallCapital();
- $model->mall_id = $mall_id;
- $model->type = $type;
- }
- $model->total_nums = $total_nums ?? 0;
- $model->use_nums = $use_nums ?? 0;
- $model->surplus_nums = $surplus_nums ?? 0;
- $model->withdrawal_nums = $withdrawal_nums ?? 0;
- $res = $model->save();
- var_dump('capitalBalance执行结束');
- }
- public function capitalScore($mall_id)
- {
- $type = 'score';
- $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('sum(total_score) as total_score,sum(total_use_score) as total_use_score,sum(score) as score')
- ->asArray()
- ->one();
- $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]);
- if (empty($model)) {
- $model = new CensusMallCapital();
- $model->mall_id = $mall_id;
- $model->type = $type;
- }
- $model->total_nums = $result['total_score'] ?? 0;
- $model->use_nums = $result['total_use_score'] ?? 0;
- $model->surplus_nums = $result['score'] ?? 0;
- $res = $model->save();
- var_dump('capitalScore执行结束');
- }
- public function capitalCommission($mall_id)
- {
- $type = 'commission';
- $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('sum(total_commission) as total_commission,sum(frozen_commission) as frozen_commission,sum(commission) as commission')
- ->asArray()
- ->one();
- CapitalLog::$capitalType = $type . '_frozen';
- $cancel_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::DELETE])->sum('change_num');
- $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]);
- if (empty($model)) {
- $model = new CensusMallCapital();
- $model->mall_id = $mall_id;
- $model->type = $type;
- }
- $model->total_nums = $result['total_commission'] ?? 0;
- $model->unsettled = $result['frozen_commission'] ?? 0;
- $model->surplus_nums = $result['commission'] ?? 0;
- $model->cancel_nums = $cancel_nums ?? 0;
- $res = $model->save();
- var_dump('capitalCommission执行结束');
- }
- //商城单日数据
- public function actionMallSingleData($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixGoodsAdd($mall['id']);
- $this->fixOrdersAdd($mall['id']);
- $this->fixPayOrdersAdd($mall['id']);
- $this->fixPayOrdersNum($mall['id']);
- $this->fixAddPayPeopleNums($mall['id']);
- $this->fixOrderRefund($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixGoodsAdd($mall['id']);
- $this->fixOrdersAdd($mall['id']);
- $this->fixPayOrdersAdd($mall['id']);
- $this->fixPayOrdersNum($mall['id']);
- $this->fixAddPayPeopleNums($mall['id']);
- $this->fixOrderRefund($mall['id']);
- }
- }
- }
- public function actionFixGoods($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixGoodsAdd($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixGoodsAdd($mall['id']);
- }
- }
- }
- public function fixGoodsAdd($mall_id, $time = null)
- {
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM
- qimall_goods where mall_id = {$mall_id} and mch_id = 0 and status in (0,1) GROUP BY date
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- $time = strtotime($item['date'] . ' 00:00:02');
- if (empty($obj)) {
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, $item['counts'], 0, 0, 0, 0, 0, 0, 0
- ];
- $values_arr[] = $info;
- } else {
- $res = CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_goods' => $item['counts']], ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- $counts = Goods::find()->where(['mall_id' => $mall_id, 'mch_id' => 0,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
- $model = CensusMall::findOne(['mall_id' => $mall_id]);
- if (empty($model)) {
- $model = new CensusMall();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- }
- $model->add_goods = $counts;
- $res = $model->save();
- var_dump('fixGoodsAdd执行结束');
- }
- public function actionFixOrders($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixGoodsAdd($mall['id']);
- $this->fixOrdersAdd($mall['id']);
- $this->fixPayOrdersAdd($mall['id']);
- $this->fixPayOrdersNum($mall['id']);
- $this->fixAddPayPeopleNums($mall['id']);
- $this->fixOrderRefund($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixGoodsAdd($mall['id']);
- $this->fixOrdersAdd($mall['id']);
- $this->fixPayOrdersAdd($mall['id']);
- $this->fixPayOrdersNum($mall['id']);
- $this->fixAddPayPeopleNums($mall['id']);
- $this->fixOrderRefund($mall['id']);
- }
- }
- }
- public function fixOrdersAdd($mall_id)
- {
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM
- qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 GROUP BY date
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $time = strtotime($item['date'] . ' 00:00:02');
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- if (empty($obj)) {
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, $item['counts'], 0, 0, 0, 0, 0, 0
- ];
- $values_arr[] = $info;
- } else {
- $time = strtotime($obj['date'] . ' 00:00:02');
- CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_orders' => $item['counts']], ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- }
- public function fixPayOrdersAdd($mall_id)
- {
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,sum(pay_money) as pay_money, mall_id FROM
- qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status=1 GROUP BY date
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $time = strtotime($item['date'] . ' 00:00:02');
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $item['date'];
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, $item['pay_money'], $item['date'], 1, $time, $time, 0, 0, $item['counts'], 0, 0, 0, 0, 0
- ];
- $values_arr[] = $info;
- } else {
- $time = strtotime($obj['date'] . ' 00:00:02');
- CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'total_pay_money' => $item['pay_money'], 'add_pay_orders' => $item['counts']], ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- var_dump('fixPayOrdersAdd执行结束');
- }
- public function fixAddPayPeopleNums($mall_id)
- {
- $sql = "
- SELECT t.date,t.mall_id,count(t.user_id) as counts FROM (
- SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,mall_id,user_id FROM
- qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status=1 GROUP BY date,user_id) t GROUP BY t.date
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $time = strtotime($item['date'] . ' 00:00:02');
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $item['date'];
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, $item['counts'], 0, 0, 0, 0
- ];
- $values_arr[] = $info;
- } else {
- $time = strtotime($obj['date'] . ' 00:00:02');
- CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_pay_people_nums' => $item['counts']], ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- var_dump('fixAddPayPeopleNums执行结束');
- }
- public function fixPayOrdersNum($mall_id)
- {
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,sum(num) as counts,mall_id FROM
- qimall_order_detail where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and order_status !=0 GROUP BY date
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $time = strtotime($item['date'] . ' 00:00:02');
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $item['date'];
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, $item['counts'], 0, 0, 0
- ];
- $values_arr[] = $info;
- } else {
- $time = strtotime($obj['date'] . ' 00:00:02');
- CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_pay_nums' => $item['counts']], ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- var_dump('fixPayOrdersNum执行结束');
- }
- public function actionFixOrderRefund($mall_id = null)
- {
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixOrderRefund($mall['id']);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixOrderRefund($mall['id']);
- }
- }
- }
- public function fixOrderRefund($mall_id)
- {
- $sql = "
- SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM
- qimall_order_refund where mall_id = $mall_id and mch_id = 0 and store_id=0 GROUP BY date
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $time = strtotime($item['date'] . ' 00:00:02');
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $item['date'];
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, $item['counts'], 0, 0
- ];
- $values_arr[] = $info;
- } else {
- $time = strtotime($obj['date'] . ' 00:00:02');
- CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_refund_orders' => $item['counts']], ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- var_dump('fixOrderRefund执行结束');
- }
- public function fixCommissionDate($mall_id)
- {
- $query = CommissionLog::find()->where(['mall_id' => $mall_id]);
- if (!empty($date)) {
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- $query->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]);
- }
- $list = $query->select(new Expression("mall_id,
- sum(change_num) as change_num,FROM_UNIXTIME(created_at,'%Y-%m-%d') as date,change_type"))
- ->groupBy('date,change_type')->asArray()->all();
- foreach ($list as $item) {
- $time = strtotime($item['date'] . ' 00:00:02');
- $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
- if (empty($obj)) {
- $obj = new CensusMallSingleDay();
- $obj->loadDefaultValues();
- $obj->mall_id = $mall_id;
- $obj->date = $item['date'];
- if ($item['change_type'] == 'add') {
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, 0, $item['change_num'], 0
- ];
- } else {
- $info = [
- $mall_id,
- 0,
- 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, 0, 0, $item['change_num']
- ];
- }
- $values_arr[] = $info;
- } else {
- $time = strtotime($obj['date'] . ' 00:00:02');
- $field = ['created_at' => $time, 'updated_at' => $time];
- if ($item['change_type'] == 'add') {
- $field['settlement_commission'] = $item['change_num'];
- } else {
- $field['expenditure_commission'] = $item['change_num'];
- }
- CensusMallSingleDay::updateAll($field, ['id' => $obj['id']]);
- }
- }
- if (!empty($values_arr)) {
- \Yii::$app->db->createCommand()->batchInsert(
- CensusMallSingleDay::tableName(),
- $this->single_day_fields,
- $values_arr
- )->execute();
- }
- var_dump($mall_id . 'fixCommissionDate执行结束');
- }
- public function actionMallSingleHourData($mall_id = null)
- {
- // $date = date('Y-m-d', strtotime('-1 days'));
- $dates = [];
- for ($i=0;$i<=7;$i++){
- $dates[] = date('Y-m-d',strtotime("-$i day"));
- }
- foreach ($dates as $date){
- $start_time = strtotime($date . ' 00:00:00');
- $end_time = strtotime($date . ' 23:59:59');
- if (!empty($mall_id)) {
- $mall = Mall::findOne(['id' => $mall_id]);
- if ($mall) {
- $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time);
- $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time);
- $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time);
- }
- } else {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time);
- $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time);
- $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time);
- }
- }
- }
- }
- public function fixPayOrdersNumByHour($mall_id, $date, $start_time, $end_time)
- {
- $sql = "
- SELECT
- FROM_UNIXTIME(od.created_at,'%H') as hour,
- sum(od.num) as counts,od.mall_id FROM
- qimall_order_detail as od LEFT JOIN qimall_order as o on od.order_id = o.id where od.mall_id = {$mall_id} and od.mch_id = 0 and od.store_id=0 and o.pay_status =1
- and od.created_at between {$start_time} and {$end_time}
- GROUP BY hour
- ;
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- $rows = [];
- foreach ($list as $item) {
- $rows[$item['hour']] = $item;
- }
- for ($i = 0; $i < 24; $i++) {
- $temp = $i;
- if ($i <= 9) $temp = '0' . $i;
- $params = [
- 'mall_id' => $mall_id,
- 'date' => $date,
- 'hour' => (string)$temp,
- 'add_pay_nums' => 0,
- ];
- if (isset($rows[$temp])) {
- $params['add_pay_nums'] = $rows[$temp]['counts'];
- }
- $res = CensusMallSingleHour::addData($params);
- }
- var_dump('fixPayOrdersNumByHour执行结束');
- }
- public function fixAddPayOrdersByHour($mall_id, $date, $start_time, $end_time)
- {
- $sql = "
- SELECT
- FROM_UNIXTIME(created_at,'%H') as hour,
- count(*) as counts,sum(pay_money) as total_pay_money,mall_id FROM
- qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1
- and created_at between {$start_time} and {$end_time}
- GROUP BY hour
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- $rows = [];
- foreach ($list as $item) {
- $rows[$item['hour']] = $item;
- }
- for ($i = 0; $i < 24; $i++) {
- $temp = $i;
- if ($i <= 9) $temp = '0' . $i;
- $params = [
- 'mall_id' => $mall_id,
- 'date' => $date,
- 'hour' => (string)$temp,
- 'add_pay_orders' => 0,
- 'total_pay_money' => 0,
- ];
- if (isset($rows[$temp])) {
- $params['add_pay_orders'] = $rows[$temp]['counts'];
- $params['total_pay_money'] = $rows[$temp]['total_pay_money'];
- }
- $res = CensusMallSingleHour::addData($params);
- }
- var_dump('fixAddPayOrdersByHour执行结束');
- }
- public function fixAddPayPeopleNumsByHour($mall_id, $date, $start_time, $end_time)
- {
- $sql = "
- SELECT t.`hour`,t.mall_id,count(t.user_id) as counts FROM (
- SELECT
- FROM_UNIXTIME(created_at,'%H') as hour,
- user_id,mall_id FROM
- qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1
- and created_at between {$start_time} and {$end_time}
- GROUP BY hour,user_id
- )t GROUP BY t.`hour`
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- $rows = [];
- foreach ($list as $item) {
- $rows[$item['hour']] = $item;
- }
- for ($i = 0; $i < 24; $i++) {
- $temp = $i;
- if ($i <= 9) $temp = '0' . $i;
- $params = [
- 'mall_id' => $mall_id,
- 'date' => $date,
- 'hour' => (string)$temp,
- 'add_pay_people_nums' => 0,
- ];
- if (isset($rows[$temp])) {
- $params['add_pay_people_nums'] = $rows[$temp]['counts'];
- }
- $res = CensusMallSingleHour::addData($params);
- }
- $sql = "
- SELECT
- FROM_UNIXTIME(created_at,'%H') as hour,
- user_id,mall_id FROM
- qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1
- and created_at between {$start_time} and {$end_time}
- GROUP BY hour,user_id
- ";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- $date = date('Y-m-d', $start_time);
- foreach ($list as $val){
- $hour = $date.' '.$val['hour'].':00:00';
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR .':'. $mall_id.':' .strtotime($hour);
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $redis = \Yii::$app->redis;
- $redis->setbit($redis_key, $val['user_id'], 1);
- }
- var_dump('fixAddPayPeopleNumsByHour执行结束');
- }
- //初始化基础分析表
- public function actionInitAna($mall_id = null)
- {
- $mall_list = Mall::getEnableMallList(true);
- if ($mall_id) {
- $mall = Mall::findOne(['id' => $mall_id]);
- $this->initAna($mall);
- } else {
- foreach ($mall_list as $mall) {
- $this->initAna($mall);
- }
- }
- }
- protected function initAna($mall)
- {
- $t = time();
- $mall_id = $mall['id'];
- $sql = "
- SELECT t.mall_id, t.user_id,t.date,sum(t.pay_money) as total_pay_money,count(*) as buy_numbers from (
- SELECT FROM_UNIXTIME(pay_time, '%Y-%m-%d') as date,user_id,mall_id, pay_money FROM qimall_order where mall_id = $mall_id and pay_status = 1
- ) t GROUP BY t.user_id,t.date
- ";
- $list = \Yii::$app->getDb()->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- $model = CensusMallAnalysis::findOne(['mall_id' => $mall['id'], 'user_id' => $item['user_id'], 'date' => $item['date'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new CensusMallAnalysis();
- $model->date = $item['date'];
- $model->user_id = $item['user_id'];
- $model->mall_id = $mall['id'];
- $model->loadDefaultValues();
- }
- $model->buy_numbers = $item['buy_numbers'];
- $model->total_pay_money = $item['total_pay_money'];
- $model->save();
- $item['created_at'] = strtotime($item['date'] . ' 00:20:00');
- $res = CensusMallAnalysis::updateAll(['created_at' => strtotime($item['date'] . ' 00:20:00')],
- ['mall_id' => $item['mall_id'], 'user_id' => $item['user_id'], 'date' => $item['date']]
- );
- }
- $t = time()-$t;
- var_dump("商城{$mall_id}initAna执行".$t);
- }
- //初始化rf,rm表
- public function actionInitRfm()
- {
- $mall_list = Mall::getEnableMallList(true);
- $time = strtotime(date('Y-m-d', time()) . ' 00:00:00');
- $config_list = CensusMallAnalysis::getConfigList();
- foreach ($mall_list as $mall) {
- $rf_arr = [];
- $rm_arr = [];
- foreach ($config_list as $key => $item) {
- list($rf, $rm) = $this->initRfRm($mall, $time, $item['min'], $item['max'], $key);
- if (!empty($rf)) $rf_arr[] = $rf;
- if (!empty($rm)) $rm_arr[] = $rm;
- }
- if (!empty($rf_arr)) {
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, '', true) . ':' . $mall['id'];
- $redis = \Yii::$app->redis;
- $redis->set($redis_key, json_encode($rf_arr));
- foreach ($rf_arr as $item) {
- CensusMallRfAnalysis::setData($item);
- }
- }
- if (!empty($rm_arr)) {
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, '', true) . ':' . $mall['id'];
- $redis = \Yii::$app->redis;
- $redis->set($redis_key, json_encode($rm_arr));
- foreach ($rm_arr as $item) {
- CensusMallRmAnalysis::setData($item);
- }
- }
- }
- }
- protected function rfRm($mall)
- {
- $t = time();
- $time = strtotime(date('Y-m-d', time()) . ' 00:00:00');
- $config_list = CensusMallAnalysis::getConfigList();
- $rf_arr = [];
- $rm_arr = [];
- foreach ($config_list as $key => $item) {
- list($rf, $rm) = $this->initRfRm($mall, $time, $item['min'], $item['max'], $key);
- if (!empty($rf)) $rf_arr[] = $rf;
- if (!empty($rm)) $rm_arr[] = $rm;
- }
- if (!empty($rf_arr)) {
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, '', true) . ':' . $mall['id'];
- $redis = \Yii::$app->redis;
- $redis->set($redis_key, json_encode($rf_arr));
- foreach ($rf_arr as $item) {
- CensusMallRfAnalysis::setData($item);
- }
- }
- if (!empty($rm_arr)) {
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, '', true) . ':' . $mall['id'];
- $redis = \Yii::$app->redis;
- $redis->set($redis_key, json_encode($rm_arr));
- foreach ($rm_arr as $item) {
- CensusMallRmAnalysis::setData($item);
- }
- }
- $t = time()-$t;
- var_dump('商城'.$mall['id'].'rfRm执行结束'.$t);
- }
- protected function initRfRm($mall, $time, $start, $end, $purchase_interval = 'r_1_30')
- {
- $start_time = strtotime(date('Y-m-d', $time - $start * 24 * 3600) . ' 23:59:59');
- if ($end) {
- $end_time = strtotime(date('Y-m-d', $time - $end * 24 * 3600) . ' 23:59:59');
- }
- $where[] = ['mall_id' => $mall['id']];
- $where[] = ['<', 'created_at', $start_time];
- if (isset($end_time)) $where[] = ['>=', 'created_at', $end_time];
- $list = CensusMallAnalysis::lists([
- 'where' => $where,
- 'group' => 'user_id',
- 'select' => 'mall_id,user_id,sum(total_pay_money) as total_pay_money,sum(buy_numbers) as buy_numbers,'
- ]);
- $rf_row = ['purchase_interval' => $purchase_interval,
- 'mall_id' => $mall['id'],
- 'f_users1' => 0, 'f_users2' => 0, 'f_users3' => 0, 'f_users4' => 0, 'f_users5' => 0,
- 'f_total_pay_money1' => 0, 'f_total_pay_money2' => 0, 'f_total_pay_money3' => 0, 'f_total_pay_money4' => 0, 'f_total_pay_money5' => 0,
- ];
- foreach ($list as $item) {
- switch ($item['buy_numbers']) {
- case 0:
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- $rf_row['f_users' . $item['buy_numbers']]++;
- $rf_row['f_total_pay_money' . $item['buy_numbers']] += $item['total_pay_money'];
- $rf_row['f_total_pay_money' . $item['buy_numbers']] = bcmul($rf_row['f_total_pay_money' . $item['buy_numbers']] ,1,2);
- break;
- default:
- $rf_row['f_users5']++;
- $rf_row['f_total_pay_money5'] += $item['total_pay_money'];
- $rf_row['f_total_pay_money5'] = bcmul($rf_row['f_total_pay_money5'], 1,2);
- }
- }
- $rm_row = ['purchase_interval' => $purchase_interval,
- 'mall_id' => $mall['id'],
- 'm_users1' => 0, 'm_users2' => 0, 'm_users3' => 0, 'm_users4' => 0, 'm_users5' => 0,
- 'm_total_pay_money1' => 0, 'm_total_pay_money2' => 0, 'm_total_pay_money3' => 0, 'm_total_pay_money4' => 0, 'm_total_pay_money5' => 0,
- ];
- foreach ($list as $item) {
- if ($item['total_pay_money'] >= 2000) {
- $rm_row['m_users5']++;
- $rm_row['m_total_pay_money5'] += $item['total_pay_money'];
- $rm_row['m_total_pay_money5'] = intval($rm_row['m_total_pay_money5'] * 100) / 100;
- } elseif ($item['total_pay_money'] >= 1000 && $item['total_pay_money'] < 2000) {
- $rm_row['m_users4']++;
- $rm_row['m_total_pay_money4'] += $item['total_pay_money'];
- $rm_row['m_total_pay_money4'] = intval($rm_row['m_total_pay_money4'] * 100) / 100;
- } elseif ($item['total_pay_money'] >= 500 && $item['total_pay_money'] < 1000) {
- $rm_row['m_users3']++;
- $rm_row['m_total_pay_money3'] += $item['total_pay_money'];
- $rm_row['m_total_pay_money3'] = intval($rm_row['m_total_pay_money3'] * 100) / 100;
- } elseif ($item['total_pay_money'] >= 200 && $item['total_pay_money'] < 500) {
- $rm_row['m_users2']++;
- $rm_row['m_total_pay_money2'] += $item['total_pay_money'];
- $rm_row['m_total_pay_money2'] = intval($rm_row['m_total_pay_money2'] * 100) / 100;
- } else {
- $rm_row['m_users1']++;
- $rm_row['m_total_pay_money1'] += $item['total_pay_money'];
- $rm_row['m_total_pay_money1'] = intval($rm_row['m_total_pay_money1'] * 100) / 100;
- }
- }
- return [$rf_row, $rm_row];
- }
- /**
- * 清除小时交易额缓存
- * @return void
- */
- public function actionClearSingleHour()
- {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $mall_id = $mall['id'];
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR . ':' . $mall_id . ':';
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $res = \Yii::$app->redis->keys($redis_key . '*');
- if (empty($res)) {
- continue;
- }
- foreach ($res as $key) {
- $arr = explode(':', $key);
- $time = array_pop($arr);
- if (strpos($time, '-')) {
- continue;
- }
- if (time() - $time > 3600) {
- \Yii::$app->redis->del($key);
- }
- }
- }
- }
- /**
- * 清除单日数据缓存
- * @return void
- */
- public function actionClearSingleDate()
- {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- $mall_id = $mall['id'];
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE . ':' . $mall_id . ':';
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $res = \Yii::$app->redis->keys($redis_key . '*');
- if (empty($res)) {
- continue;
- }
- foreach ($res as $key) {
- $arr = explode(':', $key);
- $time = array_pop($arr);
- if (strpos($time, '-')) {
- continue;
- }
- if (time() - $time > 24 * 3600) {
- \Yii::$app->redis->del($key);
- }
- }
- }
- }
- }
|