CensusController.php 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  1. <?php
  2. namespace console\controllers;
  3. use common\enums\RedisKeyEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\census\CensusMall;
  6. use common\models\census\CensusMallAnalysis;
  7. use common\models\census\CensusMallCapital;
  8. use common\models\census\CensusMallGoods;
  9. use common\models\census\CensusMallPaymentSingleMonth;
  10. use common\models\census\CensusMallPurchasingPower;
  11. use common\models\census\CensusMallRegion;
  12. use common\models\census\CensusMallRfAnalysis;
  13. use common\models\census\CensusMallRmAnalysis;
  14. use common\models\census\CensusMallSingleDay;
  15. use common\models\census\CensusMallSingleHour;
  16. use common\models\census\CensusMallSingleMonth;
  17. use common\models\census\CensusMallUser;
  18. use common\models\census\CensusMallUserLevel;
  19. use common\models\census\CensusMallUserLevelSingleDay;
  20. use common\models\census\CensusMallUserSource;
  21. use common\models\common\CapitalLog;
  22. use common\models\common\CommissionLog;
  23. use common\models\goods\Goods;
  24. use common\models\mall\Mall;
  25. use common\models\order\Order;
  26. use common\models\order\OrderDetail;
  27. use common\models\user\User;
  28. use common\models\user\UserLevel;
  29. use common\models\user\UserWallet;
  30. use yii\console\Controller;
  31. use yii\db\Expression;
  32. class CensusController extends Controller
  33. {
  34. public $single_day_fields = ['mall_id', 'total_users', 'add_users', 'total_visitors',
  35. 'total_actives', 'total_views', 'total_order_money', 'total_pay_money',
  36. 'date', 'status', 'created_at', 'updated_at', 'add_goods', 'add_orders', 'add_pay_orders', 'add_pay_nums',
  37. 'add_pay_people_nums', 'add_refund_orders', 'settlement_commission', 'expenditure_commission'
  38. ];
  39. //初始化商城统计数据,不包含会员
  40. public function actionInit($mall_id = null)
  41. {
  42. $redis = \Yii::$app->redis;
  43. if (!empty($mall_id)) {
  44. $mall = Mall::findOne(['id' => $mall_id]);
  45. if ($mall) {
  46. $this->initM($redis, $mall);
  47. }
  48. } else {
  49. $mall_list = Mall::getEnableMallList(true);
  50. foreach ($mall_list as $mall) {
  51. $this->initM($redis, $mall);
  52. }
  53. }
  54. }
  55. //初始化会员数据
  56. public function actionInitUser($mall_id = null)
  57. {
  58. $redis = \Yii::$app->redis;
  59. if (!empty($mall_id)) {
  60. $mall = Mall::findOne(['id' => $mall_id]);
  61. if ($mall) {
  62. $this->initUser($redis, $mall);
  63. }
  64. } else {
  65. $mall_list = Mall::getEnableMallList(true);
  66. foreach ($mall_list as $mall) {
  67. $this->initUser($redis, $mall);
  68. }
  69. }
  70. }
  71. public function initUser($redis, $mall)
  72. {
  73. $config_list = CensusMallPurchasingPower::getConfigList();
  74. $mall_id = $mall['id'];
  75. $page_size = 1000;
  76. $new_customers = 0;//新客户数量
  77. $regular_customers = 0;//老客户数量
  78. $repurchase_people = 0;//复购人数
  79. $repurchase_money = 0;//复购金额
  80. $regular_customers_pay_money = 0;//老客户成交金额
  81. $regular_customers_pay_peoples = 0;//老客户成交人数
  82. $new_customers_pay_peoples = 0;//新客户成交人数
  83. $new_customers_pay_money = 0;//新客户成交金额
  84. $buy_power_arr = [];
  85. $mall_user_redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_USER, '', true) . ':' . $mall_id;;
  86. $i = 1;
  87. while ($user_list = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  88. ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  89. $st = time();
  90. $user_ids = array_column($user_list, 'id');
  91. $order_list = Order::lists([
  92. 'where' => [
  93. ['user_id' => $user_ids],
  94. ['store_id'=>0],
  95. ['mch_id'=>0],
  96. ],
  97. 'select' => 'user_id,sum(pay_money) as pay_money,count(*) counts,pay_status',
  98. 'group' => 'user_id,pay_status',
  99. ]);
  100. $user_arr = [];
  101. $have_pay_user_ids = [];
  102. foreach ($order_list as $val) {
  103. if (isset($user_arr[$val['user_id']])) {
  104. $user_arr[$val['user_id']]['add_orders'] += $val['counts'];
  105. $user_arr[$val['user_id']]['add_order_money'] += $val['pay_money'];
  106. } else {
  107. $user_arr[$val['user_id']]['add_orders'] = $val['counts'];
  108. $user_arr[$val['user_id']]['add_order_money'] = $val['pay_money'];
  109. }
  110. if ($val['pay_status'] == 1) {
  111. $have_pay_user_ids[] = $val['user_id'];
  112. $user_arr[$val['user_id']]['total_pay_money'] = $val['pay_money'];
  113. $user_arr[$val['user_id']]['add_pay_orders'] = $val['counts'];
  114. $redis->hset($mall_user_redis_key, 'total_pay_money:' . $val['user_id'], $val['pay_money']);
  115. if ($val['counts'] > 1) {
  116. $repurchase_money += $val['pay_money'];
  117. $regular_customers_pay_money += $val['pay_money'];
  118. $regular_customers++;
  119. $repurchase_people++;
  120. $regular_customers_pay_peoples++;
  121. } elseif ($val['counts'] == 1) {
  122. $new_customers_pay_money += $val['pay_money'];
  123. $regular_customers++;
  124. $new_customers_pay_peoples++;
  125. }
  126. $add_key = '';
  127. foreach ($config_list as $key => $config) {
  128. if ($val['pay_money'] >= $config['min'] && $val['pay_money'] < $config['max']) {
  129. $add_key = $key;
  130. break;
  131. }
  132. }
  133. if ($add_key) {
  134. if (isset($buy_power_arr[$add_key])) {
  135. $buy_power_arr[$add_key]++;
  136. } else {
  137. $buy_power_arr[$add_key] = 1;
  138. }
  139. }
  140. }
  141. }
  142. $diff_user_ids = array_diff($user_ids, $have_pay_user_ids);
  143. $no_pay_counts = count($diff_user_ids);
  144. $new_customers += $no_pay_counts;
  145. if (isset($buy_power_arr[1])) {
  146. $buy_power_arr[1] += $no_pay_counts;
  147. } else {
  148. $buy_power_arr[1] = $no_pay_counts;
  149. }
  150. $sale_order_list = Order::lists([
  151. 'where' => [
  152. ['user_id' => $user_ids],
  153. ['store_id'=>0],
  154. ['mch_id'=>0],
  155. ['<>', 'finish_time', 0]
  156. ],
  157. 'select' => 'user_id,sum(pay_money) as pay_money,count(*) counts',
  158. 'group' => 'user_id',
  159. 'index' => 'user_id'
  160. ]);
  161. $pay_order_nums_list = OrderDetail::lists([
  162. 'where' => [
  163. ['user_id' => $user_ids],
  164. ['in', 'order_status', [1, 2, 3, 4, -5]]
  165. ],
  166. 'select' => 'user_id,sum(num) as num',
  167. 'group' => 'user_id',
  168. 'index' => 'user_id'
  169. ]);
  170. foreach ($user_list as $item) {
  171. $obj = CensusMallUser::findOne(['mall_id' => $item['mall_id'], 'user_id' => $item['id']]);
  172. if (empty($obj)) {
  173. $obj = new CensusMallUser();
  174. $obj->loadDefaultValues();
  175. $obj->mall_id = $item['mall_id'];
  176. $obj->user_id = $item['id'];
  177. }
  178. $obj->total_pay_money = $user_arr[$item['id']]['total_pay_money'] ?? 0;
  179. $obj->add_pay_orders = $user_arr[$item['id']]['add_pay_orders'] ?? 0;
  180. $obj->add_orders = $user_arr[$item['id']]['add_orders'] ?? 0;
  181. $obj->add_order_money = $user_arr[$item['id']]['add_order_money'] ?? 0;
  182. $obj->add_pay_nums = $pay_order_nums_list[$item['id']]['num'] ?? 0;
  183. $obj->add_sale_orders = $sale_order_list[$item['id']]['counts'] ?? 0;
  184. $obj->add_sale_money = $sale_order_list[$item['id']]['pay_money'] ?? 0;
  185. $obj->save();
  186. }
  187. $t = time() - $st;
  188. $page = ($i - 1) * $page_size;
  189. var_dump($page . '耗时:' . $t);
  190. $i++;
  191. }
  192. $this->setBuyPower($redis, $mall_id, $buy_power_arr);
  193. $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
  194. if (empty($census_mall_obj)) {
  195. $census_mall_obj = new CensusMall();
  196. $census_mall_obj->loadDefaultValues();
  197. $census_mall_obj->mall_id = $mall['id'];
  198. }
  199. $census_mall_obj->new_customers = $new_customers ?? 0;
  200. $census_mall_obj->regular_customers = $regular_customers ?? 0;
  201. $census_mall_obj->repurchase_people = $repurchase_people ?? 0;
  202. $census_mall_obj->repurchase_money = $repurchase_money ?? 0;
  203. $census_mall_obj->new_customers_pay_money = $new_customers_pay_money ?? 0;
  204. $census_mall_obj->new_customers_pay_peoples = $new_customers_pay_peoples ?? 0;
  205. $census_mall_obj->regular_customers_pay_money = $regular_customers_pay_money ?? 0;
  206. $census_mall_obj->regular_customers_pay_peoples = $regular_customers_pay_peoples ?? 0;
  207. $census_mall_obj->save();
  208. $key = RedisKeyEnum::CENSUS_MALL;
  209. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  210. $set_data[] = 'new_customers';
  211. $set_data[] = $new_customers;
  212. $set_data[] = 'regular_customers';
  213. $set_data[] = $regular_customers;
  214. $set_data[] = 'repurchase_people';
  215. $set_data[] = $repurchase_people;
  216. $set_data[] = 'repurchase_money';
  217. $set_data[] = $repurchase_money;
  218. $set_data[] = 'new_customers_pay_money';
  219. $set_data[] = $new_customers_pay_money;
  220. $set_data[] = 'new_customers_pay_peoples';
  221. $set_data[] = $new_customers_pay_peoples;
  222. $set_data[] = 'regular_customers_pay_money';
  223. $set_data[] = $regular_customers_pay_money;
  224. $set_data[] = 'regular_customers_pay_peoples';
  225. $set_data[] = $regular_customers_pay_peoples;
  226. $redis->hmset($redis_key, ...$set_data);
  227. }
  228. public function initM($redis, $mall)
  229. {
  230. $this->initMall($redis, $mall);
  231. $this->updateCensusMallRegion($redis, $mall);
  232. $this->updateCensusMallGoods($redis, $mall);
  233. $this->updateCensusMallUserLevel($redis, $mall);
  234. $this->updateCensusMallUserSource($redis, $mall);
  235. $this->initAna($mall);
  236. $this->rfRm($mall);
  237. $date = date('Y-m-d', time());
  238. $this->dateOrder($redis, $mall, $date);
  239. $this->dateUser($redis, $mall, $date);
  240. $this->hourOrder($redis, $mall);
  241. $this->month($redis, $mall);
  242. $this->paymentMonth($redis, $mall);
  243. $this->fixUserWallet($mall['id']);
  244. $this->uLevel($mall['id']);
  245. $this->fixCommissionDate($mall['id']);
  246. $this->capitalBalance($mall['id']);
  247. $this->capitalScore($mall['id']);
  248. $this->capitalCommission($mall['id']);
  249. $this->fixGoodsAdd($mall['id']);
  250. $this->fixOrdersAdd($mall['id']);
  251. $this->fixPayOrdersAdd($mall['id']);
  252. $this->fixPayOrdersNum($mall['id']);
  253. $this->fixAddPayPeopleNums($mall['id']);
  254. $this->fixOrderRefund($mall['id']);
  255. $date = date('Y-m-d', strtotime('-1 days'));
  256. $start_time = strtotime($date . ' 00:00:00');
  257. $end_time = strtotime($date . ' 23:59:59');
  258. $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time);
  259. $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time);
  260. $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time);
  261. }
  262. public function initMall($redis, $mall)
  263. {
  264. $t = time();
  265. $mall_id = $mall['id'];
  266. $total_users = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
  267. $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();
  268. $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();
  269. $order_list = Order::lists(
  270. [
  271. 'where' => [
  272. ['mall_id' => $mall_id],
  273. ['store_id' => 0],
  274. ['mch_id' => 0],
  275. ],
  276. 'select' => 'sum(pay_money) as pay_money,pay_status,count(*) counts',
  277. 'group' => 'pay_status'
  278. ]
  279. );
  280. $order_arr = ['total_order_money' => 0, 'total_orders' => 0, 'total_pay_money' => 0, 'total_pay_orders' => 0];
  281. foreach ($order_list as $val) {
  282. $order_arr['total_order_money'] += $val['pay_money'];
  283. $order_arr['total_orders'] += $val['counts'];
  284. if ($val['pay_status'] == 1) {
  285. $order_arr['total_pay_money'] += $val['pay_money'];
  286. $order_arr['total_pay_orders'] += $val['counts'];
  287. }
  288. }
  289. $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
  290. if (empty($census_mall_obj)) {
  291. $census_mall_obj = new CensusMall();
  292. $census_mall_obj->loadDefaultValues();
  293. $census_mall_obj->mall_id = $mall['id'];
  294. }
  295. $census_mall_obj->total_order_money = $order_arr['total_order_money'];
  296. $census_mall_obj->total_orders = $order_arr['total_orders'];
  297. $census_mall_obj->total_pay_money = $order_arr['total_pay_money'];
  298. $census_mall_obj->total_pay_orders = $order_arr['total_pay_orders'];
  299. $census_mall_obj->total_users = $total_users ?? 0;
  300. $census_mall_obj->number_of_orders = $total_orders_users ?? 0;
  301. $census_mall_obj->total_pay_people = $total_pay_people ?? 0;
  302. $census_mall_obj->total_visitors = $total_users;
  303. $census_mall_obj->total_views = $total_users;
  304. $census_mall_obj->save();
  305. $key = RedisKeyEnum::CENSUS_MALL;
  306. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  307. $set_data[] = 'total_pay_money';
  308. $set_data[] = $order_arr['total_pay_money'];
  309. $set_data[] = 'total_orders';
  310. $set_data[] = $order_arr['total_orders'];
  311. $set_data[] = 'total_users';
  312. $set_data[] = $total_users;
  313. $set_data[] = 'total_pay_orders';
  314. $set_data[] = $order_arr['total_pay_orders'];
  315. $set_data[] = 'total_pay_people';
  316. $set_data[] = $total_pay_people;
  317. $set_data[] = 'number_of_orders';
  318. $set_data[] = $total_orders_users;
  319. $set_data[] = 'total_order_money';
  320. $set_data[] = $order_arr['total_order_money'];
  321. $set_data[] = 'total_visitors';
  322. $set_data[] = $total_users;
  323. $set_data[] = 'total_views';
  324. $set_data[] = $total_users;
  325. $redis->hmset($redis_key, ...$set_data);
  326. $date = date('Y-m-d', time());
  327. $start_time = strtotime($date . ' 00:00:00');
  328. $today_total_users = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  329. ->andWhere(['>=', 'created_at', $start_time])
  330. ->count();
  331. $today_order_list = Order::lists(
  332. [
  333. 'where' => [
  334. ['mall_id' => $mall_id],
  335. ['store_id' => 0],
  336. ['mch_id' => 0],
  337. ['>=', 'created_at', $start_time]
  338. ],
  339. 'select' => 'sum(pay_money) as pay_money,pay_status,count(*) counts',
  340. 'group' => 'pay_status'
  341. ]
  342. );
  343. $today_order_arr = ['total_order_money' => 0, 'total_pay_money' => 0];
  344. foreach ($today_order_list as $val) {
  345. $today_order_arr['total_order_money']+=$val['pay_money'];
  346. if($val['pay_status']==1) $today_order_arr['total_pay_money']+=$val['pay_money'];
  347. }
  348. $census_mall_single_day_obj = CensusMallSingleDay::findOne(['mall_id' => $mall['id'], 'date' => $date]);
  349. if (empty($census_mall_single_day_obj)) {
  350. $census_mall_single_day_obj = new CensusMallSingleDay();
  351. $census_mall_single_day_obj->loadDefaultValues();
  352. $census_mall_single_day_obj->mall_id = $mall['id'];
  353. $census_mall_single_day_obj->date = $date;
  354. }
  355. $census_mall_single_day_obj->total_users = $total_users ?? 0;
  356. $census_mall_single_day_obj->add_users = $today_total_users ?? 0;
  357. $census_mall_single_day_obj->total_pay_money = $today_order_arr['total_pay_money'];
  358. $census_mall_single_day_obj->total_order_money = $today_order_arr['total_order_money'];
  359. $census_mall_single_day_obj->save();
  360. $date = date('Y-m-d', time());
  361. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
  362. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
  363. $set_data = [];
  364. $set_data[] = 'total_order_money';
  365. $set_data[] = $today_order_arr['total_order_money'];
  366. $set_data[] = 'total_pay_money';
  367. $set_data[] = $today_order_arr['total_pay_money'];
  368. $set_data[] = 'add_users';
  369. $set_data[] = $today_total_users;
  370. $set_data[] = 'total_users';
  371. $set_data[] = $total_users;
  372. $redis->hmset($redis_key, ...$set_data);
  373. $redis->expire($redis_key, 24*3600);
  374. $t = time()-$t;
  375. var_dump('initMall执行耗时'.$t);
  376. }
  377. //复购,新客户,老客户
  378. public function actionFugou($mall_id = null)
  379. {
  380. $redis = \Yii::$app->redis;
  381. if (!empty($mall_id)) {
  382. $mall = Mall::findOne(['id' => $mall_id]);
  383. if ($mall) {
  384. $this->fuGou($redis, $mall);
  385. }
  386. } else {
  387. $mall_list = Mall::getEnableMallList(true);
  388. foreach ($mall_list as $mall) {
  389. $this->fuGou($redis, $mall);
  390. }
  391. }
  392. }
  393. public function fuGou($redis, $mall)
  394. {
  395. $fu_gou_list = Order::lists(['where' => [
  396. ['mall_id' => $mall['id']],
  397. ['pay_status' => 1],
  398. ['store_id' => 0],
  399. ['mch_id' => 0],
  400. ],
  401. 'select' => 'count(*) as count,user_id',
  402. 'group' => 'user_id'
  403. ]);
  404. $repurchase_people = 0;
  405. $repurchase_people_user_id_arr = [];
  406. $new_customers_pay_peoples = 0;
  407. $new_customers_pay_peoples_user_id_arr = [];
  408. foreach ($fu_gou_list as $item) {
  409. if ($item['count'] > 1) {
  410. $repurchase_people++;
  411. $repurchase_people_user_id_arr[] = $item['user_id'];
  412. }
  413. if ($item['count'] == 1) {
  414. $new_customers_pay_peoples++;
  415. $new_customers_pay_peoples_user_id_arr[] = $item['user_id'];
  416. }
  417. }
  418. $repurchase_money = Order::find()->where(
  419. [
  420. 'mall_id' => $mall['id'],
  421. 'store_id'=>0,
  422. 'mch_id'=>0,
  423. 'user_id' => $repurchase_people_user_id_arr,
  424. 'pay_status' => 1
  425. ])->sum('pay_money');
  426. $new_customers_pay_money = Order::find()->where([
  427. 'mall_id' => $mall['id'],
  428. 'store_id'=>0,
  429. 'mch_id'=>0,
  430. 'user_id' => $new_customers_pay_peoples_user_id_arr,
  431. 'pay_status' => 1
  432. ])->sum('pay_money');
  433. $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
  434. if (empty($census_mall_obj)) {
  435. $census_mall_obj = new CensusMall();
  436. $census_mall_obj->loadDefaultValues();
  437. $census_mall_obj->mall_id = $mall['id'];
  438. }
  439. $census_mall_obj->repurchase_people = $repurchase_people ?? 0;
  440. $census_mall_obj->regular_customers_pay_peoples = $repurchase_people ?? 0;
  441. $census_mall_obj->repurchase_money = $repurchase_money ?? 0;
  442. $census_mall_obj->regular_customers_pay_money = $repurchase_money ?? 0;
  443. $census_mall_obj->new_customers_pay_peoples = $new_customers_pay_peoples ?? 0;
  444. $census_mall_obj->new_customers_pay_money = $new_customers_pay_money ?? 0;
  445. $regular_customers = Order::find()->where([
  446. 'mall_id' => $mall['id'],
  447. 'store_id'=>0,
  448. 'mch_id'=>0,
  449. 'pay_status' => 1])->select('user_id')->groupBy('user_id')->count();
  450. $new_customers = $census_mall_obj->total_users - $regular_customers;
  451. if ($new_customers < 0) $new_customers = 0;
  452. $census_mall_obj->regular_customers = $regular_customers ?? 0;
  453. $census_mall_obj->new_customers = $new_customers ?? 0;
  454. $census_mall_obj->save();
  455. $key = RedisKeyEnum::CENSUS_MALL;
  456. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
  457. $set_data[] = 'repurchase_people';
  458. $set_data[] = $repurchase_people;
  459. $set_data[] = 'regular_customers_pay_peoples';
  460. $set_data[] = $repurchase_people;
  461. $set_data[] = 'repurchase_money';
  462. $set_data[] = $repurchase_money;
  463. $set_data[] = 'regular_customers_pay_money';
  464. $set_data[] = $repurchase_money;
  465. $set_data[] = 'new_customers_pay_peoples';
  466. $set_data[] = $new_customers_pay_peoples;
  467. $set_data[] = 'new_customers_pay_money';
  468. $set_data[] = $new_customers_pay_money;
  469. $set_data[] = 'regular_customers';//老客户
  470. $set_data[] = $regular_customers;//老客户
  471. $set_data[] = 'new_customers';//新客户
  472. $set_data[] = $new_customers;//新客户
  473. $redis->hmset($redis_key, ...$set_data);
  474. var_dump('fuGou执行结束');
  475. }
  476. //地区
  477. public function actionRegion($mall_id = null)
  478. {
  479. $redis = \Yii::$app->redis;
  480. if (!empty($mall_id)) {
  481. $mall = Mall::findOne(['id' => $mall_id]);
  482. if ($mall) {
  483. $this->updateCensusMallRegion($redis, $mall);
  484. }
  485. } else {
  486. $mall_list = Mall::getEnableMallList(true);
  487. foreach ($mall_list as $mall) {
  488. $this->updateCensusMallRegion($redis, $mall);
  489. }
  490. }
  491. }
  492. public function updateCensusMallRegion($redis, $mall)
  493. {
  494. $t = time();
  495. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_REGION, '', true) . ':' . $mall['id'];
  496. $region_order_list = Order::lists(['where' =>
  497. [
  498. ['mall_id' => $mall['id']],
  499. ['store_id'=>0],
  500. ['mch_id'=>0],
  501. ],
  502. 'select' => 'count(*) as count,province',
  503. 'group' => 'province'
  504. ]
  505. );
  506. foreach ($region_order_list as $item) {
  507. if ($item['province']) {
  508. $obj = CensusMallRegion::findOne(['mall_id' => $mall['id'], 'province_id' => $item['province']]);
  509. if (empty($obj)) {
  510. $obj = new CensusMallRegion();
  511. $obj->loadDefaultValues();
  512. $obj->mall_id = $mall['id'];
  513. $obj->province_id = $item['province'];
  514. }
  515. $obj->total_orders = $item['count'] ?? 0;
  516. $obj->save();
  517. $field = 'total_orders_province_id_' . $item['province'];
  518. $redis->hset($redis_key, $field, $obj->total_orders);
  519. }
  520. }
  521. $region_pay_pay_money_list = Order::lists(['where' =>
  522. [
  523. ['mall_id' => $mall['id']],
  524. ['pay_status' => 1],
  525. ['store_id'=>0],
  526. ['mch_id'=>0],
  527. ],
  528. 'select' => 'sum(pay_money) as pay_money,province',
  529. 'group' => 'province'
  530. ]
  531. );
  532. foreach ($region_pay_pay_money_list as $item) {
  533. if ($item['province']) {
  534. $obj = CensusMallRegion::findOne(['mall_id' => $mall['id'], 'province_id' => $item['province']]);
  535. if (empty($obj)) {
  536. $obj = new CensusMallRegion();
  537. $obj->loadDefaultValues();
  538. $obj->mall_id = $mall['id'];
  539. $obj->province_id = $item['province'];
  540. }
  541. $obj->total_pay_money = $item['pay_money'] ?? 0;
  542. $obj->save();
  543. $field = 'total_pay_money_province_id_' . $obj->province_id;
  544. $redis->hset($redis_key, $field, $obj->total_pay_money);
  545. }
  546. }
  547. $t = time()-$t;
  548. var_dump('updateCensusMallRegion耗时'.$t);
  549. }
  550. //商品
  551. public function actionGoods($mall_id = null)
  552. {
  553. $redis = \Yii::$app->redis;
  554. if (!empty($mall_id)) {
  555. $mall = Mall::findOne(['id' => $mall_id]);
  556. if ($mall) {
  557. $this->updateCensusMallGoods($redis, $mall);
  558. }
  559. } else {
  560. $mall_list = Mall::getEnableMallList(true);
  561. foreach ($mall_list as $mall) {
  562. $this->updateCensusMallGoods($redis, $mall);
  563. }
  564. }
  565. }
  566. public function updateCensusMallGoods($redis, $mall)
  567. {
  568. $mall_id = $mall['id'];
  569. $t = time();
  570. $i = 1;
  571. $page_size = 1000;
  572. $goods_listings = 0;
  573. $add_goods = 0;
  574. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, '', true) . ':' . $mall['id'];
  575. $pay_money_redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_GOODS_PAY_MONEY, '', true) . ':' . $mall['id'];
  576. $redis->del($redis_key);
  577. $redis->del($pay_money_redis_key);
  578. while ($goods_list = Goods::find()->where(['mall_id' => $mall_id,'mch_id'=>0, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  579. ->orderBy('id asc')->offset(($i - 1) * $page_size)->select('is_on_sale,sales_num,id')->limit($page_size)->all()) {
  580. $goods_ids = [];
  581. foreach ($goods_list as $goods) {
  582. $goods_ids[]=$goods['id'];
  583. $add_goods++;
  584. if (!empty($goods['is_on_sale'])) $goods_listings++;
  585. }
  586. //1待发货 2 已发货 3已收货 4已结束
  587. $goods_price_arr = OrderDetail::lists([
  588. 'where'=>[
  589. ['goods_id' => $goods_ids],
  590. ['order_status' => [1, 2, 3, 4, -5]]
  591. ],
  592. 'select'=>'sum(goods_price) as goods_price,goods_id',
  593. 'group'=>'goods_id',
  594. 'index'=>'goods_id'
  595. ]);
  596. foreach ($goods_list as $goods) {
  597. $obj = CensusMallGoods::findOne(['mall_id' => $mall['id'], 'goods_id' => $goods['id']]);
  598. if (empty($obj)) {
  599. $obj = new CensusMallGoods();
  600. $obj->loadDefaultValues();
  601. $obj->mall_id = $mall['id'];
  602. $obj->goods_id = $goods['id'];
  603. }
  604. $value = 'goods_id:' . $goods['id'];
  605. if ($goods['sales_num'] > 0) $redis->zadd($redis_key, $goods['sales_num'], $value);
  606. $total_pay_money = $goods_price_arr[$goods['id']]['goods_price']??0;
  607. if ($total_pay_money > 0) $redis->zadd($pay_money_redis_key, $total_pay_money, $value);
  608. $obj->total_pay_money = $total_pay_money;
  609. $obj->total_sales_volume = $goods['sales_num'];
  610. $obj->save();
  611. }
  612. $i++;
  613. }
  614. $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
  615. if (empty($census_mall_obj)) {
  616. $census_mall_obj = new CensusMall();
  617. $census_mall_obj->loadDefaultValues();
  618. $census_mall_obj->mall_id = $mall['id'];
  619. }
  620. $census_mall_obj->goods_listings = $goods_listings ?? 0;
  621. $key = RedisKeyEnum::CENSUS_MALL;
  622. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
  623. $set_data[] = 'goods_listings';
  624. $set_data[] = $goods_listings;
  625. $set_data[] = 'add_goods';
  626. $set_data[] = $add_goods;
  627. $redis->hmset($redis_key, ...$set_data);
  628. $t = time()-$t;
  629. var_dump("商城{$mall_id}updateCensusMallGoods执行耗时".$t);
  630. }
  631. //会员等级分布
  632. public function actionUserLevel($mall_id = null)
  633. {
  634. $redis = \Yii::$app->redis;
  635. if (!empty($mall_id)) {
  636. $mall = Mall::findOne(['id' => $mall_id]);
  637. if ($mall) {
  638. $this->updateCensusMallUserLevel($redis, $mall);
  639. $this->updateCensusMallUserSource($redis, $mall);
  640. }
  641. } else {
  642. $mall_list = Mall::getEnableMallList(true);
  643. foreach ($mall_list as $mall) {
  644. $this->updateCensusMallUserLevel($redis, $mall);
  645. $this->updateCensusMallUserSource($redis, $mall);
  646. }
  647. }
  648. }
  649. public function updateCensusMallUserLevel($redis, $mall)
  650. {
  651. $t = time();
  652. $user_level = UserLevel::lists(['where' => [['mall_id' => $mall['id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]], 'select' => 'level']);
  653. $user_level_arr = array_column($user_level, 'level');
  654. $user_level_arr[] = 0;
  655. $user_list = User::lists([
  656. 'where' => [
  657. ['mall_id' => $mall['id']],
  658. ['level' => $user_level_arr],
  659. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  660. ],
  661. 'select' => 'count(*) as count,level',
  662. 'group' => 'level'
  663. ]);
  664. $key = RedisKeyEnum::CENSUS_MALL_USER_LEVEL;
  665. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
  666. foreach ($user_list as $item) {
  667. $redis->hset($redis_key, 'level_' . $item['level'], $item['count']);
  668. $obj = CensusMallUserLevel::findOne(['mall_id' => $mall['id'], 'level' => $item['level']]);
  669. if (empty($obj)) {
  670. $obj = new CensusMallUserLevel();
  671. $obj->loadDefaultValues();
  672. $obj->mall_id = $mall['id'];
  673. $obj->level = $item['level'];
  674. }
  675. $obj->numbers = $item['count'];
  676. $obj->save();
  677. }
  678. $t= time()-$t;
  679. var_dump("商城".$mall['id']."updateCensusMallUserLevel执行耗时".$t);
  680. }
  681. public function updateCensusMallUserSource($redis, $mall)
  682. {
  683. $t = time();
  684. $user_list = User::lists([
  685. 'where' => [
  686. ['mall_id' => $mall['id']],
  687. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  688. ],
  689. 'select' => 'count(*) as count,platform',
  690. 'group' => 'platform'
  691. ]);
  692. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, '', true) . ':' . $mall['id'];
  693. foreach ($user_list as $item) {
  694. if (empty($item['platform'])) $item['platform'] = 'manual';
  695. $redis->hset($redis_key, $item['platform'], $item['count']);
  696. $obj = CensusMallUserSource::findOne(['mall_id' => $mall['id'], 'source' => $item['platform']]);
  697. if (empty($obj)) {
  698. $obj = new CensusMallUserSource();
  699. $obj->loadDefaultValues();
  700. $obj->mall_id = $mall['id'];
  701. $obj->source = $item['platform'];
  702. }
  703. $obj->numbers = $item['count'];
  704. $obj->save();
  705. }
  706. $t = time()-$t;
  707. var_dump('商城'.$mall['id'].'updateCensusMallUserSource执行'.$t);
  708. }
  709. //当天交易情况
  710. public function actionToday($mall_id = null, $date = null)
  711. {
  712. $redis = \Yii::$app->redis;
  713. if (!empty($mall_id)) {
  714. $mall = Mall::findOne(['id' => $mall_id]);
  715. if ($mall) {
  716. $this->dateOrder($redis, $mall, $date);
  717. $this->dateUser($redis, $mall, $date);
  718. }
  719. } else {
  720. $mall_list = Mall::getEnableMallList(true);
  721. foreach ($mall_list as $mall) {
  722. $this->dateOrder($redis, $mall, $date);
  723. $this->dateUser($redis, $mall, $date);
  724. }
  725. }
  726. }
  727. public function dateOrder($redis, $mall, $date)
  728. {
  729. $mall_id = $mall['id'];
  730. $i = 1;
  731. $page_size = 1000;
  732. $date_order_money = [];
  733. $date_pay_money = [];
  734. if (empty($date)) $date = date('Y-m-d', time());
  735. $start_time = strtotime($date . ' 00:00:00');
  736. $end_time = strtotime($date . ' 23:59:59');
  737. while ($order_list = Order::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  738. ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
  739. ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  740. foreach ($order_list as $item) {
  741. $created_at = $item['created_at'];
  742. $date = date('Y-m-d', $created_at);
  743. if (isset($date_arr[$date])) {
  744. $date_order_money[$date] += $item['pay_money'];
  745. } else {
  746. $date_order_money[$date] = $item['pay_money'];
  747. }
  748. if ($item['pay_status']) {
  749. $pay_time = $item['pay_time'];
  750. $date_pay = date('Y-m-d', $pay_time);
  751. if (isset($date_pay_money[$date_pay])) {
  752. $date_pay_money[$date_pay] += $item['pay_money'];
  753. } else {
  754. $date_pay_money[$date_pay] = $item['pay_money'];
  755. }
  756. }
  757. }
  758. $i++;
  759. }
  760. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
  761. foreach ($date_order_money as $date => $money) {
  762. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
  763. $redis->hset($redis_key, 'total_order_money', $money);
  764. $redis->expire($redis_key, 24 * 3600);
  765. $data = [
  766. 'mall_id' => $mall_id,
  767. 'total_order_money' => $money,
  768. 'date' => $date,
  769. ];
  770. CensusMallSingleDay::setData($data);
  771. }
  772. foreach ($date_pay_money as $date => $money) {
  773. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
  774. $redis->hset($redis_key, 'total_order_money', $money);
  775. $redis->expire($redis_key, 24 * 3600);
  776. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]);
  777. if (empty($obj)) {
  778. $obj = new CensusMallSingleDay();
  779. $obj->loadDefaultValues();
  780. $obj->mall_id = $mall_id;
  781. $obj->date = $date;
  782. }
  783. $obj->total_pay_money = $money;
  784. $obj->save();
  785. }
  786. var_dump('dateOrder执行结束');
  787. }
  788. public function dateUserOld($redis, $mall, $date)
  789. {
  790. $date_arr = [];
  791. $i = 1;
  792. $page_size = 1000;
  793. $mall_id = $mall['id'];
  794. if (empty($date)) $date = date('Y-m-d', time());
  795. $start_time = strtotime($date . ' 00:00:00');
  796. $end_time = strtotime($date . ' 23:59:59');
  797. while ($user_list = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  798. ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
  799. ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  800. foreach ($user_list as $item) {
  801. $created_at = $item['created_at'];
  802. $date = date('Y-m-d', $created_at);
  803. if (isset($date_arr[$date])) {
  804. $date_arr[$date]++;
  805. } else {
  806. $date_arr[$date] = 1;
  807. }
  808. }
  809. $i++;
  810. }
  811. if ($date_arr) {
  812. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
  813. $total_nums = 0;
  814. foreach ($date_arr as $date => $nums) {
  815. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
  816. $total_nums += $nums;
  817. $redis->hset($redis_key, 'total_users', $total_nums);
  818. $redis->hset($redis_key, 'add_users', $nums);
  819. $redis->expire($redis_key, 24 * 3600);
  820. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]);
  821. if (empty($obj)) {
  822. $obj = new CensusMallSingleDay();
  823. $obj->loadDefaultValues();
  824. $obj->mall_id = $mall_id;
  825. $obj->date = $date;
  826. }
  827. $obj->add_users = $nums;
  828. $obj->total_users = $total_nums;
  829. $obj->save();
  830. }
  831. }
  832. }
  833. //单日会员数据
  834. public function dateUser($redis, $mall, $date)
  835. {
  836. $t = time();
  837. $mall_id = $mall['id'];
  838. $query = User::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  839. if (!empty($date)) {
  840. $start_time = strtotime($date . ' 00:00:00');
  841. $end_time = strtotime($date . ' 23:59:59');
  842. $query->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]);
  843. }
  844. $list = $query->select(new Expression("mall_id,count(*) as counts,FROM_UNIXTIME(created_at,'%Y-%m-%d') as date"))
  845. ->groupBy('date')->asArray()->all();
  846. if ($list) {
  847. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE;
  848. $total_nums = 0;
  849. foreach ($list as $item) {
  850. $nums = $item['counts'];
  851. $date = $item['date'];
  852. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id . ':' . $date;
  853. $total_nums += $nums;
  854. $redis->hset($redis_key, 'total_users', $total_nums);
  855. $redis->hset($redis_key, 'add_users', $nums);
  856. $redis->expire($redis_key, 24 * 3600);
  857. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date]);
  858. if (empty($obj)) {
  859. $obj = new CensusMallSingleDay();
  860. $obj->loadDefaultValues();
  861. $obj->mall_id = $mall_id;
  862. $obj->date = $date;
  863. }
  864. $obj->add_users = $nums;
  865. $obj->total_users = $total_nums;
  866. $res = $obj->save();
  867. var_dump($res);
  868. }
  869. }
  870. $t = time()-$t;
  871. var_dump('dateUser执行结束'.$t);
  872. }
  873. //当天每小时的交易情况
  874. public function actionHour($mall_id = null)
  875. {
  876. $redis = \Yii::$app->redis;
  877. if (!empty($mall_id)) {
  878. $mall = Mall::findOne(['id' => $mall_id]);
  879. if ($mall) {
  880. $this->hourOrder($redis, $mall);
  881. }
  882. } else {
  883. $mall_list = Mall::getEnableMallList(true);
  884. foreach ($mall_list as $mall) {
  885. $this->hourOrder($redis, $mall);
  886. }
  887. }
  888. }
  889. public function hourOrder($redis, $mall)
  890. {
  891. $t = time();
  892. $mall_id = $mall['id'];
  893. $i = 1;
  894. $page_size = 1000;
  895. $hour_pay_money = [];
  896. $date = date('Y-m-d', time());
  897. $start_time = strtotime($date . ' 00:00:00');
  898. $end_time = strtotime($date . ' 23:59:59');
  899. while ($order_list = Order::find()->where(['mall_id' => $mall_id,'store_id'=>0,'mch_id'=>0,
  900. 'pay_status' => 1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  901. ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
  902. ->orderBy('id asc')->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
  903. foreach ($order_list as $item) {
  904. if ($item['pay_status']) {
  905. $pay_time = $item['pay_time'];
  906. $hour = date('H', $pay_time);
  907. if (isset($hour_pay_money[$hour])) {
  908. $hour_pay_money[$hour] += $item['pay_money'];
  909. } else {
  910. $hour_pay_money[$hour] = $item['pay_money'];
  911. }
  912. }
  913. }
  914. $i++;
  915. }
  916. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR . ':' . $mall_id . ':' . $date;
  917. foreach ($hour_pay_money as $hour => $money) {
  918. $redis_key = RedisKeyEnum::suffix($key, '', true);
  919. $field = 'hour_' . $hour;
  920. $redis->hset($redis_key, $field, $money);
  921. $redis->expire($redis_key, 24 * 3600);
  922. $obj = CensusMallSingleHour::findOne(['mall_id' => $mall_id, 'hour' => $hour, 'date' => $date]);
  923. if (empty($obj)) {
  924. $obj = new CensusMallSingleHour();
  925. $obj->loadDefaultValues();
  926. $obj->mall_id = $mall_id;
  927. $obj->hour = $hour;
  928. $obj->date = $date;
  929. }
  930. $obj->total_pay_money = $money;
  931. $obj->save();
  932. }
  933. $t = time()-$t;
  934. var_dump('hourOrder执行结束'.$t);
  935. }
  936. public function setBuyPower($redis, $mall_id, $buy_power_arr)
  937. {
  938. $set_data = [];
  939. foreach ($buy_power_arr as $add_key => $nums) {
  940. $redis_add_key = RedisKeyEnum::CENSUS_MALL_BUY_POWER . 'add_' . $add_key;
  941. $redis_add_key = RedisKeyEnum::suffix($redis_add_key, '', true);
  942. $redis->setbit($redis_add_key, $add_key, 1);
  943. $obj = CensusMallPurchasingPower::findOne(['mall_id' => $mall_id, 'purchasing_power' => $add_key]);
  944. if (empty($obj)) {
  945. $obj = new CensusMallPurchasingPower();
  946. $obj->loadDefaultValues();
  947. $obj->mall_id = $mall_id;
  948. $obj->purchasing_power = $add_key;
  949. }
  950. $obj->numbers = $nums;
  951. $obj->save();
  952. $field = 'buy_power:' . $add_key;
  953. $set_data[] = $field;
  954. $set_data[] = $nums;
  955. }
  956. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_BUY_POWER, '', true) . ':' . $mall_id;
  957. if ($set_data) {
  958. $redis->hmset($redis_key, ...$set_data);
  959. }
  960. }
  961. public function userVisitors($redis, $mall)
  962. {
  963. $counts = User::find()->where(['mall_id' => $mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
  964. $census_mall_obj = CensusMall::findOne(['mall_id' => $mall['id']]);
  965. $census_mall_obj->total_visitors = $counts;
  966. $census_mall_obj->save();
  967. $set_data[] = 'total_visitors';
  968. $set_data[] = $counts;
  969. $set_data[] = 'total_views';
  970. $set_data[] = $counts;
  971. $key = RedisKeyEnum::CENSUS_MALL;
  972. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall['id'];
  973. $redis->hmset($redis_key, ...$set_data);
  974. var_dump('userVisitors执行结束');
  975. }
  976. public function actionMonth($mall_id = null)
  977. {
  978. $redis = \Yii::$app->redis;
  979. if (!empty($mall_id)) {
  980. $mall = Mall::findOne(['id' => $mall_id]);
  981. if ($mall) {
  982. $this->month($redis, $mall);
  983. }
  984. } else {
  985. $mall_list = Mall::getEnableMallList(true);
  986. foreach ($mall_list as $mall) {
  987. $this->month($redis, $mall);
  988. }
  989. }
  990. }
  991. public function month($redis, $mall)
  992. {
  993. $t = time();
  994. $mall_id = $mall['id'];
  995. //下单金额
  996. $sql = "
  997. SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money FROM
  998. qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 GROUP BY month;
  999. ";
  1000. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1001. foreach ($list as $item) {
  1002. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
  1003. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1004. $set_data = [];
  1005. $set_data[] = 'total_order_money';
  1006. $set_data[] = $item['total_pay_money'];
  1007. $redis->hmset($redis_key, ...$set_data);
  1008. $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
  1009. if (empty($obj)) {
  1010. $obj = new CensusMallSingleMonth();
  1011. $obj->loadDefaultValues();
  1012. $obj->mall_id = $mall_id;
  1013. $obj->month = $item['month'];
  1014. }
  1015. $obj->total_order_money = $item['total_pay_money'];
  1016. $res = $obj->save();
  1017. }
  1018. //支付金额
  1019. $sql = "
  1020. SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money FROM
  1021. qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and pay_status=1 GROUP BY month;
  1022. ";
  1023. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1024. foreach ($list as $item) {
  1025. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
  1026. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1027. $set_data = [];
  1028. $set_data[] = 'total_pay_money';
  1029. $set_data[] = $item['total_pay_money'];
  1030. $redis->hmset($redis_key, ...$set_data);
  1031. $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
  1032. if (empty($obj)) {
  1033. $obj = new CensusMallSingleMonth();
  1034. $obj->loadDefaultValues();
  1035. $obj->mall_id = $mall_id;
  1036. $obj->month = $item['month'];
  1037. }
  1038. $obj->total_pay_money = $item['total_pay_money'];
  1039. $res = $obj->save();
  1040. }
  1041. //抵扣金额
  1042. $sql = "
  1043. SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money
  1044. ,sum((original_goods_price+shipping_money)) as original_goods_price
  1045. FROM
  1046. qimall_order where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 GROUP BY month
  1047. ";
  1048. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1049. foreach ($list as $item) {
  1050. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
  1051. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1052. $total_deduction_money = bcsub($item['original_goods_price'], $item['total_pay_money'], 2);
  1053. if ($total_deduction_money < 0) $total_deduction_money = 0;
  1054. $set_data = [];
  1055. $set_data[] = 'total_deduction_money';
  1056. $set_data[] = $total_deduction_money;
  1057. $redis->hmset($redis_key, ...$set_data);
  1058. $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
  1059. if (empty($obj)) {
  1060. $obj = new CensusMallSingleMonth();
  1061. $obj->loadDefaultValues();
  1062. $obj->mall_id = $mall_id;
  1063. $obj->month = $item['month'];
  1064. }
  1065. $obj->total_deduction_money = $total_deduction_money;
  1066. $res = $obj->save();
  1067. }
  1068. //退款金额
  1069. $sql = "
  1070. SELECT FROM_UNIXTIME(refund_at,'%Y-%m') as month ,sum(reality_refund_price) as total_refund_money FROM
  1071. qimall_order_refund where mall_id = {$mall_id} and store_id = 0 and mch_id = 0 and is_refund = 1 GROUP BY month
  1072. ";
  1073. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1074. foreach ($list as $item) {
  1075. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
  1076. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1077. $set_data = [];
  1078. $set_data[] = 'total_refund_money';
  1079. $set_data[] = $item['total_refund_money'];
  1080. $redis->hmset($redis_key, ...$set_data);
  1081. $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
  1082. if (empty($obj)) {
  1083. $obj = new CensusMallSingleMonth();
  1084. $obj->loadDefaultValues();
  1085. $obj->mall_id = $mall_id;
  1086. $obj->month = $item['month'];
  1087. }
  1088. $obj->total_refund_money = $item['total_refund_money'];
  1089. $res = $obj->save();
  1090. }
  1091. //充值金额
  1092. $sql = "
  1093. SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(change_num) as total_recharge_money FROM
  1094. qimall_balance_log where
  1095. mall_id = {$mall_id} and change_type = 'add' and from_type = 'recharge' and status = 1 GROUP BY month
  1096. ";
  1097. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1098. foreach ($list as $item) {
  1099. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
  1100. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1101. $set_data = [];
  1102. $set_data[] = 'total_recharge_money';
  1103. $set_data[] = $item['total_recharge_money'];
  1104. $redis->hmset($redis_key, ...$set_data);
  1105. $obj = CensusMallSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month']]);
  1106. if (empty($obj)) {
  1107. $obj = new CensusMallSingleMonth();
  1108. $obj->loadDefaultValues();
  1109. $obj->mall_id = $mall_id;
  1110. $obj->month = $item['month'];
  1111. }
  1112. $obj->total_recharge_money = $item['total_recharge_money'];
  1113. $res = $obj->save();
  1114. }
  1115. $t = time()-$t;
  1116. var_dump('month执行结束'.$t);
  1117. }
  1118. public function actionPaymentMonth($mall_id = null)
  1119. {
  1120. $redis = \Yii::$app->redis;
  1121. if (!empty($mall_id)) {
  1122. $mall = Mall::findOne(['id' => $mall_id]);
  1123. if ($mall) {
  1124. $this->paymentMonth($redis, $mall);
  1125. }
  1126. } else {
  1127. $mall_list = Mall::getEnableMallList(true);
  1128. foreach ($mall_list as $mall) {
  1129. $this->paymentMonth($redis, $mall);
  1130. }
  1131. }
  1132. }
  1133. public function paymentMonth($redis, $mall)
  1134. {
  1135. $t = time();
  1136. $mall_id = $mall['id'];
  1137. $sql = "
  1138. SELECT FROM_UNIXTIME(created_at,'%Y-%m') as month ,sum(pay_money) as total_pay_money,payment_type FROM
  1139. 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
  1140. ";
  1141. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1142. foreach ($list as $item) {
  1143. $key = RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH . ':' . $mall_id . ':' . $item['month'];
  1144. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1145. $set_data = [];
  1146. $set_data[] = 'pay_type:' . $item['payment_type'];
  1147. $set_data[] = $item['total_pay_money'];
  1148. $redis->hmset($redis_key, ...$set_data);
  1149. $obj = CensusMallPaymentSingleMonth::findOne(['mall_id' => $mall_id, 'month' => $item['month'], 'pay_type' => $item['payment_type']]);
  1150. if (empty($obj)) {
  1151. $obj = new CensusMallPaymentSingleMonth();
  1152. $obj->loadDefaultValues();
  1153. $obj->mall_id = $mall_id;
  1154. $obj->month = $item['month'];
  1155. $obj->pay_type = $item['payment_type'];
  1156. }
  1157. $obj->total_pay_money = $item['total_pay_money'];
  1158. $res = $obj->save();
  1159. }
  1160. $t = time()-$t;
  1161. var_dump('paymentMonth执行结束'.$t);
  1162. }
  1163. public function actionFixUserWallet($mall_id = null)
  1164. {
  1165. if (!empty($mall_id)) {
  1166. $mall = Mall::findOne(['id' => $mall_id]);
  1167. if ($mall) {
  1168. $this->fixUserWallet($mall['id']);
  1169. }
  1170. } else {
  1171. $mall_list = Mall::getEnableMallList(true);
  1172. foreach ($mall_list as $mall) {
  1173. $this->fixUserWallet($mall['id']);
  1174. }
  1175. }
  1176. }
  1177. public function fixUserWallet($mall_id)
  1178. {
  1179. $t = time();
  1180. $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  1181. ->select('sum(score) as can_use_score,sum(balance) as can_use_balance,sum(commission) as can_use_commission')
  1182. ->asArray()
  1183. ->one();
  1184. $obj = CensusMall::findOne(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  1185. if (empty($obj)) {
  1186. $obj = new CensusMall();
  1187. $obj->loadDefaultValues();
  1188. $obj->mall_id = $mall_id;
  1189. $obj->status = StatusEnum::ENABLED;
  1190. }
  1191. $obj->can_use_score = $result['can_use_score'] ?? 0;
  1192. $obj->can_use_balance = $result['can_use_balance'] ?? 0;
  1193. $obj->can_use_commission = $result['can_use_commission'] ?? 0;
  1194. $res = $obj->save();
  1195. $t = time()-$t;
  1196. var_dump('fixUserWallet执行结束'.$t);
  1197. }
  1198. //会员等级情况
  1199. public function actionULevel($mall_id = null)
  1200. {
  1201. if (!empty($mall_id)) {
  1202. $mall = Mall::findOne(['id' => $mall_id]);
  1203. if ($mall) {
  1204. $this->uLevel($mall['id']);
  1205. }
  1206. } else {
  1207. $mall_list = Mall::getEnableMallList(true);
  1208. foreach ($mall_list as $mall) {
  1209. $this->uLevel($mall['id']);
  1210. }
  1211. }
  1212. }
  1213. public function uLevel($mall_id)
  1214. {
  1215. $t = time();
  1216. $list = User::lists(
  1217. [
  1218. 'where' => [
  1219. ['mall_id' => $mall_id],
  1220. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  1221. ],
  1222. 'select' => 'count(*) as counts,level',
  1223. 'group' => 'level'
  1224. ]
  1225. );
  1226. foreach ($list as $item) {
  1227. $date = date('Y-m-d', $t);
  1228. $model = CensusMallUserLevelSingleDay::findOne(['mall_id' => $mall_id, 'date' => $date, 'level' => $item['level']]);
  1229. if (empty($model)) {
  1230. $model = new CensusMallUserLevelSingleDay();
  1231. $model->loadDefaultValues();
  1232. $model->mall_id = $mall_id;
  1233. $model->level = $item['level'];
  1234. $model->date = $date;
  1235. }
  1236. $model->numbers = $item['counts'];
  1237. $res = $model->save();
  1238. }
  1239. $t = time()-$t;
  1240. var_dump('uLevel执行结束');
  1241. }
  1242. //商城资金统计
  1243. public function actionCapital($mall_id = null)
  1244. {
  1245. if (!empty($mall_id)) {
  1246. $mall = Mall::findOne(['id' => $mall_id]);
  1247. if ($mall) {
  1248. $this->capitalBalance($mall['id']);
  1249. $this->capitalScore($mall['id']);
  1250. $this->capitalCommission($mall['id']);
  1251. }
  1252. } else {
  1253. $mall_list = Mall::getEnableMallList(true);
  1254. foreach ($mall_list as $mall) {
  1255. $this->capitalBalance($mall['id']);
  1256. $this->capitalScore($mall['id']);
  1257. $this->capitalCommission($mall['id']);
  1258. }
  1259. }
  1260. }
  1261. public function capitalBalance($mall_id)
  1262. {
  1263. $type = 'balance';
  1264. $total_nums = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->sum('total_balance');
  1265. CapitalLog::$capitalType = $type;
  1266. $use_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'change_type' => 'sub', 'status' => StatusEnum::ENABLED])->sum('change_num');
  1267. $surplus_nums = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->sum('balance');
  1268. CapitalLog::$capitalType = $type;
  1269. $withdrawal_nums = CapitalLog::find()->where(['mall_id' => $mall_id,
  1270. 'change_type' => 'sub',
  1271. 'from_type' => 'withdrawal',
  1272. 'status' => StatusEnum::ENABLED])->sum('change_num');
  1273. $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]);
  1274. if (empty($model)) {
  1275. $model = new CensusMallCapital();
  1276. $model->mall_id = $mall_id;
  1277. $model->type = $type;
  1278. }
  1279. $model->total_nums = $total_nums ?? 0;
  1280. $model->use_nums = $use_nums ?? 0;
  1281. $model->surplus_nums = $surplus_nums ?? 0;
  1282. $model->withdrawal_nums = $withdrawal_nums ?? 0;
  1283. $res = $model->save();
  1284. var_dump('capitalBalance执行结束');
  1285. }
  1286. public function capitalScore($mall_id)
  1287. {
  1288. $type = 'score';
  1289. $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  1290. ->select('sum(total_score) as total_score,sum(total_use_score) as total_use_score,sum(score) as score')
  1291. ->asArray()
  1292. ->one();
  1293. $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]);
  1294. if (empty($model)) {
  1295. $model = new CensusMallCapital();
  1296. $model->mall_id = $mall_id;
  1297. $model->type = $type;
  1298. }
  1299. $model->total_nums = $result['total_score'] ?? 0;
  1300. $model->use_nums = $result['total_use_score'] ?? 0;
  1301. $model->surplus_nums = $result['score'] ?? 0;
  1302. $res = $model->save();
  1303. var_dump('capitalScore执行结束');
  1304. }
  1305. public function capitalCommission($mall_id)
  1306. {
  1307. $type = 'commission';
  1308. $result = UserWallet::find()->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  1309. ->select('sum(total_commission) as total_commission,sum(frozen_commission) as frozen_commission,sum(commission) as commission')
  1310. ->asArray()
  1311. ->one();
  1312. CapitalLog::$capitalType = $type . '_frozen';
  1313. $cancel_nums = CapitalLog::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::DELETE])->sum('change_num');
  1314. $model = CensusMallCapital::findOne(['mall_id' => $mall_id, 'type' => $type]);
  1315. if (empty($model)) {
  1316. $model = new CensusMallCapital();
  1317. $model->mall_id = $mall_id;
  1318. $model->type = $type;
  1319. }
  1320. $model->total_nums = $result['total_commission'] ?? 0;
  1321. $model->unsettled = $result['frozen_commission'] ?? 0;
  1322. $model->surplus_nums = $result['commission'] ?? 0;
  1323. $model->cancel_nums = $cancel_nums ?? 0;
  1324. $res = $model->save();
  1325. var_dump('capitalCommission执行结束');
  1326. }
  1327. //商城单日数据
  1328. public function actionMallSingleData($mall_id = null)
  1329. {
  1330. if (!empty($mall_id)) {
  1331. $mall = Mall::findOne(['id' => $mall_id]);
  1332. if ($mall) {
  1333. $this->fixGoodsAdd($mall['id']);
  1334. $this->fixOrdersAdd($mall['id']);
  1335. $this->fixPayOrdersAdd($mall['id']);
  1336. $this->fixPayOrdersNum($mall['id']);
  1337. $this->fixAddPayPeopleNums($mall['id']);
  1338. $this->fixOrderRefund($mall['id']);
  1339. }
  1340. } else {
  1341. $mall_list = Mall::getEnableMallList(true);
  1342. foreach ($mall_list as $mall) {
  1343. $this->fixGoodsAdd($mall['id']);
  1344. $this->fixOrdersAdd($mall['id']);
  1345. $this->fixPayOrdersAdd($mall['id']);
  1346. $this->fixPayOrdersNum($mall['id']);
  1347. $this->fixAddPayPeopleNums($mall['id']);
  1348. $this->fixOrderRefund($mall['id']);
  1349. }
  1350. }
  1351. }
  1352. public function actionFixGoods($mall_id = null)
  1353. {
  1354. if (!empty($mall_id)) {
  1355. $mall = Mall::findOne(['id' => $mall_id]);
  1356. if ($mall) {
  1357. $this->fixGoodsAdd($mall['id']);
  1358. }
  1359. } else {
  1360. $mall_list = Mall::getEnableMallList(true);
  1361. foreach ($mall_list as $mall) {
  1362. $this->fixGoodsAdd($mall['id']);
  1363. }
  1364. }
  1365. }
  1366. public function fixGoodsAdd($mall_id, $time = null)
  1367. {
  1368. $sql = "
  1369. SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM
  1370. qimall_goods where mall_id = {$mall_id} and mch_id = 0 and status in (0,1) GROUP BY date
  1371. ";
  1372. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1373. foreach ($list as $item) {
  1374. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1375. $time = strtotime($item['date'] . ' 00:00:02');
  1376. if (empty($obj)) {
  1377. $info = [
  1378. $mall_id,
  1379. 0,
  1380. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, $item['counts'], 0, 0, 0, 0, 0, 0, 0
  1381. ];
  1382. $values_arr[] = $info;
  1383. } else {
  1384. $res = CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_goods' => $item['counts']], ['id' => $obj['id']]);
  1385. }
  1386. }
  1387. if (!empty($values_arr)) {
  1388. \Yii::$app->db->createCommand()->batchInsert(
  1389. CensusMallSingleDay::tableName(),
  1390. $this->single_day_fields,
  1391. $values_arr
  1392. )->execute();
  1393. }
  1394. $counts = Goods::find()->where(['mall_id' => $mall_id, 'mch_id' => 0,
  1395. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->count();
  1396. $model = CensusMall::findOne(['mall_id' => $mall_id]);
  1397. if (empty($model)) {
  1398. $model = new CensusMall();
  1399. $model->loadDefaultValues();
  1400. $model->mall_id = $mall_id;
  1401. }
  1402. $model->add_goods = $counts;
  1403. $res = $model->save();
  1404. var_dump('fixGoodsAdd执行结束');
  1405. }
  1406. public function actionFixOrders($mall_id = null)
  1407. {
  1408. if (!empty($mall_id)) {
  1409. $mall = Mall::findOne(['id' => $mall_id]);
  1410. if ($mall) {
  1411. $this->fixGoodsAdd($mall['id']);
  1412. $this->fixOrdersAdd($mall['id']);
  1413. $this->fixPayOrdersAdd($mall['id']);
  1414. $this->fixPayOrdersNum($mall['id']);
  1415. $this->fixAddPayPeopleNums($mall['id']);
  1416. $this->fixOrderRefund($mall['id']);
  1417. }
  1418. } else {
  1419. $mall_list = Mall::getEnableMallList(true);
  1420. foreach ($mall_list as $mall) {
  1421. $this->fixGoodsAdd($mall['id']);
  1422. $this->fixOrdersAdd($mall['id']);
  1423. $this->fixPayOrdersAdd($mall['id']);
  1424. $this->fixPayOrdersNum($mall['id']);
  1425. $this->fixAddPayPeopleNums($mall['id']);
  1426. $this->fixOrderRefund($mall['id']);
  1427. }
  1428. }
  1429. }
  1430. public function fixOrdersAdd($mall_id)
  1431. {
  1432. $sql = "
  1433. SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM
  1434. qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 GROUP BY date
  1435. ";
  1436. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1437. foreach ($list as $item) {
  1438. $time = strtotime($item['date'] . ' 00:00:02');
  1439. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1440. if (empty($obj)) {
  1441. $info = [
  1442. $mall_id,
  1443. 0,
  1444. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, $item['counts'], 0, 0, 0, 0, 0, 0
  1445. ];
  1446. $values_arr[] = $info;
  1447. } else {
  1448. $time = strtotime($obj['date'] . ' 00:00:02');
  1449. CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_orders' => $item['counts']], ['id' => $obj['id']]);
  1450. }
  1451. }
  1452. if (!empty($values_arr)) {
  1453. \Yii::$app->db->createCommand()->batchInsert(
  1454. CensusMallSingleDay::tableName(),
  1455. $this->single_day_fields,
  1456. $values_arr
  1457. )->execute();
  1458. }
  1459. }
  1460. public function fixPayOrdersAdd($mall_id)
  1461. {
  1462. $sql = "
  1463. SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,sum(pay_money) as pay_money, mall_id FROM
  1464. qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status=1 GROUP BY date
  1465. ";
  1466. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1467. foreach ($list as $item) {
  1468. $time = strtotime($item['date'] . ' 00:00:02');
  1469. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1470. if (empty($obj)) {
  1471. $obj = new CensusMallSingleDay();
  1472. $obj->loadDefaultValues();
  1473. $obj->mall_id = $mall_id;
  1474. $obj->date = $item['date'];
  1475. $info = [
  1476. $mall_id,
  1477. 0,
  1478. 0, 0, 0, 0, 0, $item['pay_money'], $item['date'], 1, $time, $time, 0, 0, $item['counts'], 0, 0, 0, 0, 0
  1479. ];
  1480. $values_arr[] = $info;
  1481. } else {
  1482. $time = strtotime($obj['date'] . ' 00:00:02');
  1483. CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'total_pay_money' => $item['pay_money'], 'add_pay_orders' => $item['counts']], ['id' => $obj['id']]);
  1484. }
  1485. }
  1486. if (!empty($values_arr)) {
  1487. \Yii::$app->db->createCommand()->batchInsert(
  1488. CensusMallSingleDay::tableName(),
  1489. $this->single_day_fields,
  1490. $values_arr
  1491. )->execute();
  1492. }
  1493. var_dump('fixPayOrdersAdd执行结束');
  1494. }
  1495. public function fixAddPayPeopleNums($mall_id)
  1496. {
  1497. $sql = "
  1498. SELECT t.date,t.mall_id,count(t.user_id) as counts FROM (
  1499. SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,mall_id,user_id FROM
  1500. 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
  1501. ";
  1502. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1503. foreach ($list as $item) {
  1504. $time = strtotime($item['date'] . ' 00:00:02');
  1505. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1506. if (empty($obj)) {
  1507. $obj = new CensusMallSingleDay();
  1508. $obj->loadDefaultValues();
  1509. $obj->mall_id = $mall_id;
  1510. $obj->date = $item['date'];
  1511. $info = [
  1512. $mall_id,
  1513. 0,
  1514. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, $item['counts'], 0, 0, 0, 0
  1515. ];
  1516. $values_arr[] = $info;
  1517. } else {
  1518. $time = strtotime($obj['date'] . ' 00:00:02');
  1519. CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_pay_people_nums' => $item['counts']], ['id' => $obj['id']]);
  1520. }
  1521. }
  1522. if (!empty($values_arr)) {
  1523. \Yii::$app->db->createCommand()->batchInsert(
  1524. CensusMallSingleDay::tableName(),
  1525. $this->single_day_fields,
  1526. $values_arr
  1527. )->execute();
  1528. }
  1529. var_dump('fixAddPayPeopleNums执行结束');
  1530. }
  1531. public function fixPayOrdersNum($mall_id)
  1532. {
  1533. $sql = "
  1534. SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,sum(num) as counts,mall_id FROM
  1535. qimall_order_detail where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and order_status !=0 GROUP BY date
  1536. ";
  1537. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1538. foreach ($list as $item) {
  1539. $time = strtotime($item['date'] . ' 00:00:02');
  1540. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1541. if (empty($obj)) {
  1542. $obj = new CensusMallSingleDay();
  1543. $obj->loadDefaultValues();
  1544. $obj->mall_id = $mall_id;
  1545. $obj->date = $item['date'];
  1546. $info = [
  1547. $mall_id,
  1548. 0,
  1549. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, $item['counts'], 0, 0, 0
  1550. ];
  1551. $values_arr[] = $info;
  1552. } else {
  1553. $time = strtotime($obj['date'] . ' 00:00:02');
  1554. CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_pay_nums' => $item['counts']], ['id' => $obj['id']]);
  1555. }
  1556. }
  1557. if (!empty($values_arr)) {
  1558. \Yii::$app->db->createCommand()->batchInsert(
  1559. CensusMallSingleDay::tableName(),
  1560. $this->single_day_fields,
  1561. $values_arr
  1562. )->execute();
  1563. }
  1564. var_dump('fixPayOrdersNum执行结束');
  1565. }
  1566. public function actionFixOrderRefund($mall_id = null)
  1567. {
  1568. if (!empty($mall_id)) {
  1569. $mall = Mall::findOne(['id' => $mall_id]);
  1570. if ($mall) {
  1571. $this->fixOrderRefund($mall['id']);
  1572. }
  1573. } else {
  1574. $mall_list = Mall::getEnableMallList(true);
  1575. foreach ($mall_list as $mall) {
  1576. $this->fixOrderRefund($mall['id']);
  1577. }
  1578. }
  1579. }
  1580. public function fixOrderRefund($mall_id)
  1581. {
  1582. $sql = "
  1583. SELECT FROM_UNIXTIME(created_at,'%Y-%m-%d') as date ,count(*) as counts,mall_id FROM
  1584. qimall_order_refund where mall_id = $mall_id and mch_id = 0 and store_id=0 GROUP BY date
  1585. ";
  1586. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1587. foreach ($list as $item) {
  1588. $time = strtotime($item['date'] . ' 00:00:02');
  1589. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1590. if (empty($obj)) {
  1591. $obj = new CensusMallSingleDay();
  1592. $obj->loadDefaultValues();
  1593. $obj->mall_id = $mall_id;
  1594. $obj->date = $item['date'];
  1595. $info = [
  1596. $mall_id,
  1597. 0,
  1598. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, $item['counts'], 0, 0
  1599. ];
  1600. $values_arr[] = $info;
  1601. } else {
  1602. $time = strtotime($obj['date'] . ' 00:00:02');
  1603. CensusMallSingleDay::updateAll(['created_at' => $time, 'updated_at' => $time, 'add_refund_orders' => $item['counts']], ['id' => $obj['id']]);
  1604. }
  1605. }
  1606. if (!empty($values_arr)) {
  1607. \Yii::$app->db->createCommand()->batchInsert(
  1608. CensusMallSingleDay::tableName(),
  1609. $this->single_day_fields,
  1610. $values_arr
  1611. )->execute();
  1612. }
  1613. var_dump('fixOrderRefund执行结束');
  1614. }
  1615. public function fixCommissionDate($mall_id)
  1616. {
  1617. $query = CommissionLog::find()->where(['mall_id' => $mall_id]);
  1618. if (!empty($date)) {
  1619. $start_time = strtotime($date . ' 00:00:00');
  1620. $end_time = strtotime($date . ' 23:59:59');
  1621. $query->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time]);
  1622. }
  1623. $list = $query->select(new Expression("mall_id,
  1624. sum(change_num) as change_num,FROM_UNIXTIME(created_at,'%Y-%m-%d') as date,change_type"))
  1625. ->groupBy('date,change_type')->asArray()->all();
  1626. foreach ($list as $item) {
  1627. $time = strtotime($item['date'] . ' 00:00:02');
  1628. $obj = CensusMallSingleDay::findOne(['mall_id' => $mall_id, 'date' => $item['date']]);
  1629. if (empty($obj)) {
  1630. $obj = new CensusMallSingleDay();
  1631. $obj->loadDefaultValues();
  1632. $obj->mall_id = $mall_id;
  1633. $obj->date = $item['date'];
  1634. if ($item['change_type'] == 'add') {
  1635. $info = [
  1636. $mall_id,
  1637. 0,
  1638. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, 0, $item['change_num'], 0
  1639. ];
  1640. } else {
  1641. $info = [
  1642. $mall_id,
  1643. 0,
  1644. 0, 0, 0, 0, 0, 0, $item['date'], 1, $time, $time, 0, 0, 0, 0, 0, 0, 0, $item['change_num']
  1645. ];
  1646. }
  1647. $values_arr[] = $info;
  1648. } else {
  1649. $time = strtotime($obj['date'] . ' 00:00:02');
  1650. $field = ['created_at' => $time, 'updated_at' => $time];
  1651. if ($item['change_type'] == 'add') {
  1652. $field['settlement_commission'] = $item['change_num'];
  1653. } else {
  1654. $field['expenditure_commission'] = $item['change_num'];
  1655. }
  1656. CensusMallSingleDay::updateAll($field, ['id' => $obj['id']]);
  1657. }
  1658. }
  1659. if (!empty($values_arr)) {
  1660. \Yii::$app->db->createCommand()->batchInsert(
  1661. CensusMallSingleDay::tableName(),
  1662. $this->single_day_fields,
  1663. $values_arr
  1664. )->execute();
  1665. }
  1666. var_dump($mall_id . 'fixCommissionDate执行结束');
  1667. }
  1668. public function actionMallSingleHourData($mall_id = null)
  1669. {
  1670. // $date = date('Y-m-d', strtotime('-1 days'));
  1671. $dates = [];
  1672. for ($i=0;$i<=7;$i++){
  1673. $dates[] = date('Y-m-d',strtotime("-$i day"));
  1674. }
  1675. foreach ($dates as $date){
  1676. $start_time = strtotime($date . ' 00:00:00');
  1677. $end_time = strtotime($date . ' 23:59:59');
  1678. if (!empty($mall_id)) {
  1679. $mall = Mall::findOne(['id' => $mall_id]);
  1680. if ($mall) {
  1681. $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time);
  1682. $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time);
  1683. $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time);
  1684. }
  1685. } else {
  1686. $mall_list = Mall::getEnableMallList(true);
  1687. foreach ($mall_list as $mall) {
  1688. $this->fixPayOrdersNumByHour($mall['id'], $date, $start_time, $end_time);
  1689. $this->fixAddPayOrdersByHour($mall['id'], $date, $start_time, $end_time);
  1690. $this->fixAddPayPeopleNumsByHour($mall['id'], $date, $start_time, $end_time);
  1691. }
  1692. }
  1693. }
  1694. }
  1695. public function fixPayOrdersNumByHour($mall_id, $date, $start_time, $end_time)
  1696. {
  1697. $sql = "
  1698. SELECT
  1699. FROM_UNIXTIME(od.created_at,'%H') as hour,
  1700. sum(od.num) as counts,od.mall_id FROM
  1701. 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
  1702. and od.created_at between {$start_time} and {$end_time}
  1703. GROUP BY hour
  1704. ;
  1705. ";
  1706. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1707. $rows = [];
  1708. foreach ($list as $item) {
  1709. $rows[$item['hour']] = $item;
  1710. }
  1711. for ($i = 0; $i < 24; $i++) {
  1712. $temp = $i;
  1713. if ($i <= 9) $temp = '0' . $i;
  1714. $params = [
  1715. 'mall_id' => $mall_id,
  1716. 'date' => $date,
  1717. 'hour' => (string)$temp,
  1718. 'add_pay_nums' => 0,
  1719. ];
  1720. if (isset($rows[$temp])) {
  1721. $params['add_pay_nums'] = $rows[$temp]['counts'];
  1722. }
  1723. $res = CensusMallSingleHour::addData($params);
  1724. }
  1725. var_dump('fixPayOrdersNumByHour执行结束');
  1726. }
  1727. public function fixAddPayOrdersByHour($mall_id, $date, $start_time, $end_time)
  1728. {
  1729. $sql = "
  1730. SELECT
  1731. FROM_UNIXTIME(created_at,'%H') as hour,
  1732. count(*) as counts,sum(pay_money) as total_pay_money,mall_id FROM
  1733. qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1
  1734. and created_at between {$start_time} and {$end_time}
  1735. GROUP BY hour
  1736. ";
  1737. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1738. $rows = [];
  1739. foreach ($list as $item) {
  1740. $rows[$item['hour']] = $item;
  1741. }
  1742. for ($i = 0; $i < 24; $i++) {
  1743. $temp = $i;
  1744. if ($i <= 9) $temp = '0' . $i;
  1745. $params = [
  1746. 'mall_id' => $mall_id,
  1747. 'date' => $date,
  1748. 'hour' => (string)$temp,
  1749. 'add_pay_orders' => 0,
  1750. 'total_pay_money' => 0,
  1751. ];
  1752. if (isset($rows[$temp])) {
  1753. $params['add_pay_orders'] = $rows[$temp]['counts'];
  1754. $params['total_pay_money'] = $rows[$temp]['total_pay_money'];
  1755. }
  1756. $res = CensusMallSingleHour::addData($params);
  1757. }
  1758. var_dump('fixAddPayOrdersByHour执行结束');
  1759. }
  1760. public function fixAddPayPeopleNumsByHour($mall_id, $date, $start_time, $end_time)
  1761. {
  1762. $sql = "
  1763. SELECT t.`hour`,t.mall_id,count(t.user_id) as counts FROM (
  1764. SELECT
  1765. FROM_UNIXTIME(created_at,'%H') as hour,
  1766. user_id,mall_id FROM
  1767. qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1
  1768. and created_at between {$start_time} and {$end_time}
  1769. GROUP BY hour,user_id
  1770. )t GROUP BY t.`hour`
  1771. ";
  1772. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1773. $rows = [];
  1774. foreach ($list as $item) {
  1775. $rows[$item['hour']] = $item;
  1776. }
  1777. for ($i = 0; $i < 24; $i++) {
  1778. $temp = $i;
  1779. if ($i <= 9) $temp = '0' . $i;
  1780. $params = [
  1781. 'mall_id' => $mall_id,
  1782. 'date' => $date,
  1783. 'hour' => (string)$temp,
  1784. 'add_pay_people_nums' => 0,
  1785. ];
  1786. if (isset($rows[$temp])) {
  1787. $params['add_pay_people_nums'] = $rows[$temp]['counts'];
  1788. }
  1789. $res = CensusMallSingleHour::addData($params);
  1790. }
  1791. $sql = "
  1792. SELECT
  1793. FROM_UNIXTIME(created_at,'%H') as hour,
  1794. user_id,mall_id FROM
  1795. qimall_order where mall_id = {$mall_id} and mch_id = 0 and store_id=0 and pay_status =1
  1796. and created_at between {$start_time} and {$end_time}
  1797. GROUP BY hour,user_id
  1798. ";
  1799. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  1800. $date = date('Y-m-d', $start_time);
  1801. foreach ($list as $val){
  1802. $hour = $date.' '.$val['hour'].':00:00';
  1803. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR .':'. $mall_id.':' .strtotime($hour);
  1804. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1805. $redis = \Yii::$app->redis;
  1806. $redis->setbit($redis_key, $val['user_id'], 1);
  1807. }
  1808. var_dump('fixAddPayPeopleNumsByHour执行结束');
  1809. }
  1810. //初始化基础分析表
  1811. public function actionInitAna($mall_id = null)
  1812. {
  1813. $mall_list = Mall::getEnableMallList(true);
  1814. if ($mall_id) {
  1815. $mall = Mall::findOne(['id' => $mall_id]);
  1816. $this->initAna($mall);
  1817. } else {
  1818. foreach ($mall_list as $mall) {
  1819. $this->initAna($mall);
  1820. }
  1821. }
  1822. }
  1823. protected function initAna($mall)
  1824. {
  1825. $t = time();
  1826. $mall_id = $mall['id'];
  1827. $sql = "
  1828. SELECT t.mall_id, t.user_id,t.date,sum(t.pay_money) as total_pay_money,count(*) as buy_numbers from (
  1829. 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
  1830. ) t GROUP BY t.user_id,t.date
  1831. ";
  1832. $list = \Yii::$app->getDb()->createCommand($sql)->queryAll();
  1833. foreach ($list as $item) {
  1834. $model = CensusMallAnalysis::findOne(['mall_id' => $mall['id'], 'user_id' => $item['user_id'], 'date' => $item['date'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  1835. if (empty($model)) {
  1836. $model = new CensusMallAnalysis();
  1837. $model->date = $item['date'];
  1838. $model->user_id = $item['user_id'];
  1839. $model->mall_id = $mall['id'];
  1840. $model->loadDefaultValues();
  1841. }
  1842. $model->buy_numbers = $item['buy_numbers'];
  1843. $model->total_pay_money = $item['total_pay_money'];
  1844. $model->save();
  1845. $item['created_at'] = strtotime($item['date'] . ' 00:20:00');
  1846. $res = CensusMallAnalysis::updateAll(['created_at' => strtotime($item['date'] . ' 00:20:00')],
  1847. ['mall_id' => $item['mall_id'], 'user_id' => $item['user_id'], 'date' => $item['date']]
  1848. );
  1849. }
  1850. $t = time()-$t;
  1851. var_dump("商城{$mall_id}initAna执行".$t);
  1852. }
  1853. //初始化rf,rm表
  1854. public function actionInitRfm()
  1855. {
  1856. $mall_list = Mall::getEnableMallList(true);
  1857. $time = strtotime(date('Y-m-d', time()) . ' 00:00:00');
  1858. $config_list = CensusMallAnalysis::getConfigList();
  1859. foreach ($mall_list as $mall) {
  1860. $rf_arr = [];
  1861. $rm_arr = [];
  1862. foreach ($config_list as $key => $item) {
  1863. list($rf, $rm) = $this->initRfRm($mall, $time, $item['min'], $item['max'], $key);
  1864. if (!empty($rf)) $rf_arr[] = $rf;
  1865. if (!empty($rm)) $rm_arr[] = $rm;
  1866. }
  1867. if (!empty($rf_arr)) {
  1868. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, '', true) . ':' . $mall['id'];
  1869. $redis = \Yii::$app->redis;
  1870. $redis->set($redis_key, json_encode($rf_arr));
  1871. foreach ($rf_arr as $item) {
  1872. CensusMallRfAnalysis::setData($item);
  1873. }
  1874. }
  1875. if (!empty($rm_arr)) {
  1876. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, '', true) . ':' . $mall['id'];
  1877. $redis = \Yii::$app->redis;
  1878. $redis->set($redis_key, json_encode($rm_arr));
  1879. foreach ($rm_arr as $item) {
  1880. CensusMallRmAnalysis::setData($item);
  1881. }
  1882. }
  1883. }
  1884. }
  1885. protected function rfRm($mall)
  1886. {
  1887. $t = time();
  1888. $time = strtotime(date('Y-m-d', time()) . ' 00:00:00');
  1889. $config_list = CensusMallAnalysis::getConfigList();
  1890. $rf_arr = [];
  1891. $rm_arr = [];
  1892. foreach ($config_list as $key => $item) {
  1893. list($rf, $rm) = $this->initRfRm($mall, $time, $item['min'], $item['max'], $key);
  1894. if (!empty($rf)) $rf_arr[] = $rf;
  1895. if (!empty($rm)) $rm_arr[] = $rm;
  1896. }
  1897. if (!empty($rf_arr)) {
  1898. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, '', true) . ':' . $mall['id'];
  1899. $redis = \Yii::$app->redis;
  1900. $redis->set($redis_key, json_encode($rf_arr));
  1901. foreach ($rf_arr as $item) {
  1902. CensusMallRfAnalysis::setData($item);
  1903. }
  1904. }
  1905. if (!empty($rm_arr)) {
  1906. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, '', true) . ':' . $mall['id'];
  1907. $redis = \Yii::$app->redis;
  1908. $redis->set($redis_key, json_encode($rm_arr));
  1909. foreach ($rm_arr as $item) {
  1910. CensusMallRmAnalysis::setData($item);
  1911. }
  1912. }
  1913. $t = time()-$t;
  1914. var_dump('商城'.$mall['id'].'rfRm执行结束'.$t);
  1915. }
  1916. protected function initRfRm($mall, $time, $start, $end, $purchase_interval = 'r_1_30')
  1917. {
  1918. $start_time = strtotime(date('Y-m-d', $time - $start * 24 * 3600) . ' 23:59:59');
  1919. if ($end) {
  1920. $end_time = strtotime(date('Y-m-d', $time - $end * 24 * 3600) . ' 23:59:59');
  1921. }
  1922. $where[] = ['mall_id' => $mall['id']];
  1923. $where[] = ['<', 'created_at', $start_time];
  1924. if (isset($end_time)) $where[] = ['>=', 'created_at', $end_time];
  1925. $list = CensusMallAnalysis::lists([
  1926. 'where' => $where,
  1927. 'group' => 'user_id',
  1928. 'select' => 'mall_id,user_id,sum(total_pay_money) as total_pay_money,sum(buy_numbers) as buy_numbers,'
  1929. ]);
  1930. $rf_row = ['purchase_interval' => $purchase_interval,
  1931. 'mall_id' => $mall['id'],
  1932. 'f_users1' => 0, 'f_users2' => 0, 'f_users3' => 0, 'f_users4' => 0, 'f_users5' => 0,
  1933. '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,
  1934. ];
  1935. foreach ($list as $item) {
  1936. switch ($item['buy_numbers']) {
  1937. case 0:
  1938. break;
  1939. case 1:
  1940. case 2:
  1941. case 3:
  1942. case 4:
  1943. $rf_row['f_users' . $item['buy_numbers']]++;
  1944. $rf_row['f_total_pay_money' . $item['buy_numbers']] += $item['total_pay_money'];
  1945. $rf_row['f_total_pay_money' . $item['buy_numbers']] = bcmul($rf_row['f_total_pay_money' . $item['buy_numbers']] ,1,2);
  1946. break;
  1947. default:
  1948. $rf_row['f_users5']++;
  1949. $rf_row['f_total_pay_money5'] += $item['total_pay_money'];
  1950. $rf_row['f_total_pay_money5'] = bcmul($rf_row['f_total_pay_money5'], 1,2);
  1951. }
  1952. }
  1953. $rm_row = ['purchase_interval' => $purchase_interval,
  1954. 'mall_id' => $mall['id'],
  1955. 'm_users1' => 0, 'm_users2' => 0, 'm_users3' => 0, 'm_users4' => 0, 'm_users5' => 0,
  1956. '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,
  1957. ];
  1958. foreach ($list as $item) {
  1959. if ($item['total_pay_money'] >= 2000) {
  1960. $rm_row['m_users5']++;
  1961. $rm_row['m_total_pay_money5'] += $item['total_pay_money'];
  1962. $rm_row['m_total_pay_money5'] = intval($rm_row['m_total_pay_money5'] * 100) / 100;
  1963. } elseif ($item['total_pay_money'] >= 1000 && $item['total_pay_money'] < 2000) {
  1964. $rm_row['m_users4']++;
  1965. $rm_row['m_total_pay_money4'] += $item['total_pay_money'];
  1966. $rm_row['m_total_pay_money4'] = intval($rm_row['m_total_pay_money4'] * 100) / 100;
  1967. } elseif ($item['total_pay_money'] >= 500 && $item['total_pay_money'] < 1000) {
  1968. $rm_row['m_users3']++;
  1969. $rm_row['m_total_pay_money3'] += $item['total_pay_money'];
  1970. $rm_row['m_total_pay_money3'] = intval($rm_row['m_total_pay_money3'] * 100) / 100;
  1971. } elseif ($item['total_pay_money'] >= 200 && $item['total_pay_money'] < 500) {
  1972. $rm_row['m_users2']++;
  1973. $rm_row['m_total_pay_money2'] += $item['total_pay_money'];
  1974. $rm_row['m_total_pay_money2'] = intval($rm_row['m_total_pay_money2'] * 100) / 100;
  1975. } else {
  1976. $rm_row['m_users1']++;
  1977. $rm_row['m_total_pay_money1'] += $item['total_pay_money'];
  1978. $rm_row['m_total_pay_money1'] = intval($rm_row['m_total_pay_money1'] * 100) / 100;
  1979. }
  1980. }
  1981. return [$rf_row, $rm_row];
  1982. }
  1983. /**
  1984. * 清除小时交易额缓存
  1985. * @return void
  1986. */
  1987. public function actionClearSingleHour()
  1988. {
  1989. $mall_list = Mall::getEnableMallList(true);
  1990. foreach ($mall_list as $mall) {
  1991. $mall_id = $mall['id'];
  1992. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR . ':' . $mall_id . ':';
  1993. $redis_key = RedisKeyEnum::suffix($key, '', true);
  1994. $res = \Yii::$app->redis->keys($redis_key . '*');
  1995. if (empty($res)) {
  1996. continue;
  1997. }
  1998. foreach ($res as $key) {
  1999. $arr = explode(':', $key);
  2000. $time = array_pop($arr);
  2001. if (strpos($time, '-')) {
  2002. continue;
  2003. }
  2004. if (time() - $time > 3600) {
  2005. \Yii::$app->redis->del($key);
  2006. }
  2007. }
  2008. }
  2009. }
  2010. /**
  2011. * 清除单日数据缓存
  2012. * @return void
  2013. */
  2014. public function actionClearSingleDate()
  2015. {
  2016. $mall_list = Mall::getEnableMallList(true);
  2017. foreach ($mall_list as $mall) {
  2018. $mall_id = $mall['id'];
  2019. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_DATE . ':' . $mall_id . ':';
  2020. $redis_key = RedisKeyEnum::suffix($key, '', true);
  2021. $res = \Yii::$app->redis->keys($redis_key . '*');
  2022. if (empty($res)) {
  2023. continue;
  2024. }
  2025. foreach ($res as $key) {
  2026. $arr = explode(':', $key);
  2027. $time = array_pop($arr);
  2028. if (strpos($time, '-')) {
  2029. continue;
  2030. }
  2031. if (time() - $time > 24 * 3600) {
  2032. \Yii::$app->redis->del($key);
  2033. }
  2034. }
  2035. }
  2036. }
  2037. }