UserStatistics.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. namespace common\models\statistics;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\enums\RedisKeyEnum;
  6. use common\models\census\CensusMallSingleDay;
  7. use common\models\census\CensusMallUser;
  8. use Yii;
  9. use yii\redis\Connection;
  10. use common\models\common\Download;
  11. use common\enums\DownloadEnum;
  12. use common\components\export\CsvTool;
  13. use common\models\user\User;
  14. use common\enums\OrderStatusEnum;
  15. use common\models\order\Order;
  16. use yii\db\Expression;
  17. /**
  18. *
  19. * This is the model class for table "{{%user_statistics}}".
  20. * @property int $id 记录ID
  21. * @property int $mall_id 商城ID
  22. * @property int $user_id 用户ID
  23. * @property int $order_num 下单数
  24. * @property int $pay_num 支付数
  25. * @property float $pay_money 支付金额
  26. * @property float $average_money 客单价
  27. * @property int $finish_order_num 已完成订单数
  28. * @property float $finish_order_money 已完成订单金额
  29. * @property int $refund_order_num 退款订单数
  30. * @property float $refund_order_money 退款订单金额
  31. * @property float $recharge_money 充值金额
  32. * @property float $pay_selling_price 已支付的订单售价总额(商品价格)
  33. * @property float $finish_selling_price 已完成的订单售价总额(商品价格)
  34. * @property float $user_goods_price_total 支付金额不包含运费
  35. * @property float $user_order_num 订单总数
  36. * @property int $date 日期,年月日
  37. * @property int $status 状态【1启用 0禁用 -1删除】
  38. * @property int $created_at 创建时间
  39. * @property int $updated_at 更新事件
  40. */
  41. class UserStatistics extends BaseActiveRecord
  42. {
  43. const ORDER_NUM = 'order_num';//订单数量
  44. const PAY_NUM = 'pay_num';//支付数量
  45. const TOTAL_MONEY = 'total_money';//总金额
  46. const TODAY_MONEY = 'today_money';//今日收益
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public static function tableName() {
  51. return '{{%user_statistics}}';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function rules()
  57. {
  58. return [
  59. [['mall_id', 'user_id'], 'required'],
  60. [['mall_id', 'user_id', 'order_num', 'pay_num', 'date', 'status', 'finish_order_num', 'refund_order_num', 'pay_goods_num', 'cancel_order_num', 'finish_goods_num', 'cancel_goods_num', 'refund_goods_num'], 'integer'],
  61. [['finish_order_money', 'refund_order_money', 'pay_money', 'average_money', 'recharge_money', 'original_order_money', 'order_money', 'balance_pay', 'wechat_pay', 'ali_pay', 'cancel_order_money',
  62. 'pay_selling_price', 'finish_selling_price'],'number']
  63. ];
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function attributeLabels()
  69. {
  70. return [
  71. 'id' => 'ID',
  72. 'mall_id' => '商城id',
  73. 'user_id' => '用户id',
  74. 'pay_num' => '支付量',
  75. 'order_num' => '订单量',
  76. 'pay_money' => '支付金额',
  77. 'average_money' => '客单价',
  78. 'pay_goods_num' => '支付商品件数',
  79. 'date' => '日期',
  80. 'order_money' => '下单金额',
  81. 'cancel_order_num' => '已取消订单数',
  82. 'cancel_order_money' => '已取消订单金额',
  83. 'cancel_goods_num' => '已取消商品数',
  84. 'original_order_money' => '支付订单原金额',
  85. 'finish_order_num' => '已完成订单数',
  86. 'finish_order_money' => '已完成订单金额',
  87. 'finish_goods_num' => '已完成商品数',
  88. 'refund_order_num' => '退款订单数',
  89. 'refund_order_money' => '退款订单金额',
  90. 'refund_goods_num' => '退款商品数',
  91. 'recharge_money' => '充值金额',
  92. 'balance_pay' => '余额支付金额',
  93. 'wechat_pay' => '微信支付金额',
  94. 'ali_pay' => '支付宝支付金额',
  95. 'pay_selling_price' => '已支付的订单售价总额(商品价格)',
  96. 'finish_selling_price' => '已完成的订单售价总额(商品价格)',
  97. 'user_goods_price_total' => '支付金额不包含运费',
  98. 'user_order_num' => '订单总数',
  99. 'status' => '状态'
  100. ];
  101. }
  102. /**
  103. * 商城首页订单统计
  104. *
  105. * @param int $mall_id
  106. * @author hpei
  107. * @return array
  108. */
  109. public static function getUserStatistics($mall_id) {
  110. try {
  111. $redis = \Yii::$app->redis;
  112. //获取会员来源统计
  113. $cacheKeySource = RedisKeyEnum::suffix(RedisKeyEnum::SOURCE_STATISTICS, 'mall_id_' . $mall_id);
  114. $sourceStatistics = $redis->get($cacheKeySource);
  115. if (empty($sourceStatistics)) {
  116. $sourceStatistics = self::staticsUserSource($mall_id);
  117. self::setUserSourceStatisticsCache($mall_id, $sourceStatistics);
  118. }
  119. //用户购买力统计 staticsUserBuy
  120. $cacheKeyBuy = RedisKeyEnum::suffix(RedisKeyEnum::BUY_STATISTICS, 'mall_id_' . $mall_id);
  121. $buyStatistics = $redis->get($cacheKeyBuy);
  122. if (empty($buyStatistics)) {
  123. $buyStatistics = self::staticsUserBuy($mall_id);
  124. self::setUserBuyStatisticsCache($mall_id, $buyStatistics);
  125. }
  126. //会员订单统计
  127. $cacheKey = RedisKeyEnum::suffix(RedisKeyEnum::ORDER_STATISTICS, 'mall_id_' . $mall_id);
  128. $orderStatistics = $redis->hgetall($cacheKey);
  129. if (empty($orderStatistics)) {
  130. $orderStatistics = self::createCacheData($mall_id);
  131. self::setUserStatisticsCache($mall_id, $orderStatistics);
  132. } else {
  133. $num = 0;
  134. $fields = [];
  135. $values = [];
  136. foreach ($orderStatistics as $val) {
  137. if ($num % 2 === 0) {
  138. $fields[] = $val;
  139. } else {
  140. $values[] = $val;
  141. }
  142. $num++;
  143. }
  144. $orderStatistics = array_combine($fields, $values);
  145. }
  146. if(!empty($sourceStatistics)) $orderStatistics['source'] = json_decode($sourceStatistics);
  147. if(!empty($buyStatistics)) $orderStatistics['buy'] = json_decode($buyStatistics);
  148. return $orderStatistics;
  149. } catch (\Exception $e) {
  150. Yii::error('读取订单统计缓存异常:' . $e->getMessage());
  151. return self::getDefaultData();
  152. }
  153. }
  154. /**
  155. * 设置数据
  156. *
  157. * @param int $mall_id
  158. * @param int $user_id
  159. * @param array $data [order_num=>$order_num,pay_num=>$pay_num,pay_money=>$pay_money]
  160. * @author hpei
  161. * @return bool
  162. */
  163. public static function setUserStatistics($mall_id, $user_id, $data) {
  164. try {
  165. $date = date('Ymd');
  166. if (isset($data['ymd'])) {
  167. $date = $data['ymd'];
  168. unset($data['ymd']);
  169. }
  170. $orderMdl = self::find()->where(['mall_id' => $mall_id, 'date' => $date, 'user_id' => $user_id])->indexBy('date')->one();
  171. if (empty($orderMdl)) {
  172. $orderMdl = new self();
  173. $orderMdl->mall_id = $mall_id;
  174. $orderMdl->date = $date;
  175. $orderMdl->user_id = $user_id;
  176. }
  177. // 循环赋值
  178. foreach ($orderMdl as $key => $value) {
  179. if (isset($data[$key]) && !empty($data[$key]) && $key != 'goods_num_data') $orderMdl->$key += $data[$key];
  180. }
  181. // 客单价处理
  182. !empty($data['pay_money']) && $orderMdl->average_money = bcdiv($orderMdl->pay_money, $orderMdl->pay_num, 2);
  183. $chcheData = [
  184. 'pay_money' => isset($data['pay_money']) ? $data['pay_money'] : 0.00,
  185. 'pay_num' => isset($data['pay_num']) ? $data['pay_num'] : 0,
  186. 'order_num' => isset($data['order_num']) ? $data['order_num'] : 0,
  187. ];
  188. if (!$orderMdl->save()) {
  189. self::$static_error = $orderMdl->getErrorMessage();
  190. return false;
  191. }
  192. if ($chcheData['pay_money'] > 0 || $chcheData['pay_num'] > 0 || $chcheData['order_num'] > 0) {
  193. self::setUserStatisticsCache($mall_id, $chcheData, true);
  194. }
  195. return true;
  196. } catch (\Exception $e) {
  197. self::$static_error = $e->getMessage();
  198. return false;
  199. }
  200. }
  201. /**
  202. * 设置缓存
  203. *
  204. * @param int $mall_id
  205. * @param array $data
  206. * @author hpei
  207. * @return void
  208. */
  209. public static function setUserStatisticsCache($mall_id, $data, $is_edit = false) {
  210. $config = \Yii::$app->getComponents()['redis'];
  211. unset($config['class']);
  212. $redis = new Connection($config);
  213. $cacheKey = RedisKeyEnum::suffix(RedisKeyEnum::ORDER_STATISTICS, 'mall_id_' . $mall_id);
  214. if ($is_edit) {
  215. if (isset($data['pay_money']) && !empty($data['pay_money'])) {
  216. $redis->hincrbyfloat($cacheKey, self::TOTAL_MONEY, $data['pay_money']);
  217. $redis->hincrbyfloat($cacheKey, self::TODAY_MONEY, $data['pay_money']);
  218. }
  219. if (isset($data['pay_num']) && !empty($data['pay_num'])) $redis->hincrby($cacheKey, self::PAY_NUM, $data['pay_num']);
  220. if (isset($data['order_num']) && !empty($data['order_num'])) $redis->hincrby($cacheKey, self::ORDER_NUM, $data['order_num']);
  221. } else {
  222. $redis->hmset($cacheKey,
  223. self::ORDER_NUM, $data[self::ORDER_NUM],
  224. self::PAY_NUM, $data[self::PAY_NUM],
  225. self::TOTAL_MONEY, $data[self::TOTAL_MONEY],
  226. self::TODAY_MONEY, $data[self::TODAY_MONEY]
  227. );
  228. }
  229. //$redis->expire($cacheKey, strtotime("+1 day 0:0:0") - time());
  230. $redis->close();
  231. }
  232. /**
  233. * 缓存用户来源数据
  234. * @Author: ltm
  235. * @DateTime: 2022/3/16 0016 14:35
  236. * @Copyright: copyright (c) 2021 广东七件事集团
  237. * @param $data
  238. * @return string
  239. */
  240. public static function setUserSourceStatisticsCache($mall_id, $data) {
  241. $config = \Yii::$app->getComponents()['redis'];
  242. unset($config['class']);
  243. $redis = new Connection($config);
  244. $cacheKey = RedisKeyEnum::suffix(RedisKeyEnum::SOURCE_STATISTICS, 'mall_id_' . $mall_id);
  245. $redis->set($cacheKey,$data);
  246. $redis->expire($cacheKey, strtotime("+1 day 0:0:0") - time());
  247. $redis->close();
  248. }
  249. /**
  250. * 缓存用户购买力数据
  251. * @Author: ltm
  252. * @DateTime: 2022/3/16 0016 14:35
  253. * @Copyright: copyright (c) 2021 广东七件事集团
  254. * @param $data
  255. * @return string
  256. */
  257. public static function setUserBuyStatisticsCache($mall_id, $data) {
  258. $config = \Yii::$app->getComponents()['redis'];
  259. unset($config['class']);
  260. $redis = new Connection($config);
  261. $cacheKey = RedisKeyEnum::suffix(RedisKeyEnum::BUY_STATISTICS, 'mall_id_' . $mall_id);
  262. $redis->set($cacheKey,$data);
  263. $redis->expire($cacheKey, strtotime("+1 day 0:0:0") - time());
  264. $redis->close();
  265. }
  266. /**
  267. * 生成缓存数据
  268. */
  269. public static function createCacheData($mall_id) {
  270. try {
  271. $orderStatistics = self::find()
  272. ->select(["sum(pay_num) as pay_num", 'sum(order_num) as order_num', 'sum(pay_money) as total_money'])
  273. ->asArray()
  274. ->where(['mall_id' => $mall_id])
  275. ->one();
  276. if (empty($orderStatistics['total_money'])) $orderStatistics['total_money'] = 0;
  277. if (empty($orderStatistics['pay_num'])) $orderStatistics['pay_num'] = 0;
  278. if (empty($orderStatistics['order_num'])) $orderStatistics['order_num'] = 0;
  279. $todayMoney = self::find()->select('pay_money')->where(['mall_id' => $mall_id, 'date' => date('Ymd')])->column();
  280. $orderStatistics['today_money'] = empty($todayMoney) ? 0 : $todayMoney[0];
  281. return $orderStatistics;
  282. } catch (\Exception $e) {
  283. throw new \Exception('用户缓存。生成缓存数据' . $e->getMessage() . ',行数:' . $e->getLine());
  284. }
  285. }
  286. /**
  287. * 设置默认展示数据
  288. *
  289. * @author hpei
  290. * @return array
  291. */
  292. public static function getDefaultData() {
  293. return [self::ORDER_NUM => 0, self::PAY_NUM => 0, self::TOTAL_MONEY => 0, self::TODAY_MONEY => 0];
  294. }
  295. /**
  296. * 资产概况统计
  297. *
  298. * @param array $params
  299. * @author hpei
  300. * @return array
  301. */
  302. public static function userOrderStatistics($params, $mall_id) {
  303. $params['group'] = 'mall_id';
  304. $params['select'] = [
  305. 'sum(order_money) as order_money', //下单总金额
  306. 'sum(original_order_money) as original_order_money', //优惠前订单总金额(已支付)
  307. 'sum(pay_money) as pay_money', //支付金额
  308. 'sum(refund_order_money) as refund_order_money', //已完成订单金额
  309. 'sum(recharge_money) as recharge_money', //充值金额
  310. 'sum(balance_pay) as balance', //余额支付金额
  311. 'sum(wechat_pay) as wechat', //微信支付金额
  312. 'sum(ali_pay) as alipay' //支付宝支付金额
  313. ];
  314. $model = self::find();
  315. self::paramPack($params, $model);
  316. $list = $model->asArray()->one();
  317. $list['deduction_money'] = $list['original_order_money'] - $list['pay_money'];//抵扣金额
  318. $orderWhere = [];
  319. foreach ($params['where'] as $key => $where) {
  320. if ($where[1] == 'created_at' && $where[0] == '>=') {
  321. // $orderWhere['where'][] = ['>=', 'created_at', strtotime(substr($where[2], 0, 4) . '-' . substr($where[2], 4, 2) . '-' . substr($where[2], 6, 2).'00:00:00')];
  322. $orderWhere['where'][] = ['>=', 'created_at', $where[2]];
  323. }
  324. if ($where[1] == 'created_at' && $where[0] == '<=') {
  325. $orderWhere['where'][] = ['<=', 'created_at', $where[2]];
  326. }
  327. }
  328. $orderWhere['where'][] = ['=', 'order_status', OrderStatusEnum::NOT_PAY];
  329. $orderWhere['where'][] = ['=', 'mall_id', $mall_id];
  330. $orderMdl = Order::find();
  331. Order::paramPack($orderWhere, $orderMdl);
  332. $list['not_pay_money'] = $orderMdl->sum('pay_money');
  333. array_walk($list, function(&$v) {
  334. $v = round($v, 2);
  335. });
  336. return $list;
  337. }
  338. public static function getOrderStatistics($params, array $select = [], $group = false, $limit = 0, $order = '') {
  339. if ($group != false) $params['group'] = $group;
  340. if (!empty($select)) $params['select'] = $select;
  341. if (!empty($order)) $params['order'] = $order;
  342. $model = self::find();
  343. if ($limit > 0) {
  344. $model->limit($limit);
  345. }
  346. self::paramPack($params, $model);
  347. $list = $model->asArray()->all();
  348. return $list ?? [];
  349. }
  350. /**
  351. * 导出排行榜
  352. */
  353. public static function exportRankHandleOld(array $data) {
  354. $download_id = $data['download_id'] ?? 0;
  355. $download = Download::findOne(['id' => $download_id]);
  356. if($download) {
  357. try {
  358. $params = json_decode($download->params,true);
  359. $filename = $download->name;
  360. $type = $download->type;
  361. $csv = new CsvTool();
  362. $where['where'][] = ['=', 'mall_id', $params['mall_id']];
  363. $list = self::getOrderStatistics($where, ['user_id', new Expression('0 as nickname'), 'sum(pay_money) as pay_money', 'sum(pay_num) as pay_num', 'sum(pay_goods_num) as pay_goods_num'], 'user_id', 100, 'pay_money desc');
  364. if (!empty($list)) {
  365. $user_ids = array_column($list, 'user_id');
  366. $userList = User::find()->where(['id' => $user_ids])->select(['avatar_url','nickname','id'])->indexBy('id')->asArray()->all();
  367. foreach ($list as &$goods) {
  368. if (isset($userList[$goods['user_id']]['nickname'])) {
  369. $goods['nickname'] = $userList[$goods['user_id']]['nickname'];
  370. } else {
  371. $goods['nickname'] = '';
  372. }
  373. $goods['pay_money'] = $goods['pay_money'] ?? 0;
  374. $goods['pay_money'] = $goods['pay_money'] ?? 0;
  375. $goods['pay_money'] = $goods['pay_money'] ?? 0;
  376. }
  377. $csv->filename = $filename;
  378. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  379. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  380. $csv->header = ['用户ID', '用户昵称' , '销售额', '销量','支付件数'];
  381. $csv->data = $list;
  382. $csv->export();
  383. $csv->updateDown($download, $params['mall_id']);
  384. }
  385. return true;
  386. } catch(\Exception $e){
  387. $download->status = 3;
  388. $res = $download->save();
  389. self::setStaticError($e->getMessage());
  390. return false;
  391. }
  392. }
  393. }
  394. public static function exportRankHandle(array $data) {
  395. $download_id = $data['download_id'] ?? 0;
  396. $download = Download::findOne(['id' => $download_id]);
  397. if($download) {
  398. try {
  399. $params = json_decode($download->params,true);
  400. $filename = $download->name;
  401. $type = $download->type;
  402. $csv = new CsvTool();
  403. $mall_id = $params['mall_id'];
  404. $where = [];
  405. $where[]=['mall_id'=>$mall_id];
  406. $params['where'] = $where;
  407. $params['limit'] = 100;
  408. $params['order'] = 'total_pay_money desc';
  409. $list = CensusMallUser::lists($params);
  410. if(!empty($list)){
  411. $rows = [];
  412. $user_ids = array_column($list,'user_id');
  413. $user_arr = User::find()->where(['id' => $user_ids])->select(['avatar_url','nickname','id'])->indexBy('id')->asArray()->all();
  414. foreach ($list as &$item){
  415. $row['user_id'] = $item['user_id'];
  416. $row['nickname'] = '';
  417. $row['pay_money'] = $item['total_pay_money'];
  418. $row['pay_num'] = $item['add_orders'];
  419. $row['pay_goods_num'] = $item['add_pay_nums'];
  420. if(isset($user_arr[$item['user_id']])){
  421. $row['nickname'] = $user_arr[$item['user_id']]['nickname'];
  422. }
  423. $rows[]=$row;
  424. }
  425. $csv->filename = $filename;
  426. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  427. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  428. $csv->header = ['用户ID', '用户昵称' , '销售额', '销量','支付件数'];
  429. $csv->data = $rows;
  430. $csv->export();
  431. $csv->updateDown($download, $params['mall_id']);
  432. return true;
  433. }
  434. return true;
  435. } catch(\Exception $e){
  436. $download->status = 3;
  437. $res = $download->save();
  438. self::setStaticError($e->getMessage());
  439. return false;
  440. }
  441. }
  442. }
  443. /**
  444. * 导出销量统计
  445. */
  446. public static function exportSaleStatisticsHandleOld(array $data) {
  447. $download_id = $data['download_id'] ?? 0;
  448. $download = Download::findOne(['id' => $download_id]);
  449. if($download) {
  450. try {
  451. $params = json_decode($download->params,true);
  452. $where['where'][] = ['=', 'mall_id', $params['mall_id']];
  453. $model = self::find()
  454. ->where(['mall_id' => $params['mall_id']])
  455. ->select(['date', 'count(user_id) user_num', 'sum(pay_num) pay_num', 'sum(pay_goods_num) pay_goods_num', 'sum(pay_money) pay_money'])
  456. ->groupBy('date')
  457. ->orderBy('date desc')
  458. ->asArray();
  459. $list = [];
  460. foreach ($model->each(500) as $l) {
  461. $l['date'] = substr($l['date'], 0, 4) . '-' . substr($l['date'], 4, 2) . '-' . substr($l['date'], 6, 2);
  462. $l['user_num'] = $l['user_num'] ?? 0;
  463. $l['pay_num'] = $l['pay_num'] ?? 0;
  464. $l['pay_goods_num'] = $l['pay_goods_num'] ?? 0;
  465. $l['pay_money'] = $l['pay_money'] ?? 0;
  466. $list[] = $l;
  467. }
  468. if (!empty($list)) {
  469. $csv = new CsvTool();
  470. $filename = $download->name;
  471. $type = $download->type;
  472. $csv->filename = $filename;
  473. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  474. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  475. $csv->header = ['日期', '付款人数', '付款订单数','付款件数','付款金额'];
  476. $csv->data = $list;
  477. $csv->export();
  478. $csv->updateDown($download, $params['mall_id']);
  479. }
  480. return true;
  481. } catch (\Exception $e) {
  482. $download->status = 3;
  483. $res = $download->save();
  484. self::setStaticError($e->getMessage());
  485. return $e->getMessage();
  486. }
  487. }
  488. }
  489. public static function exportSaleStatisticsHandle(array $data) {
  490. $download_id = $data['download_id'] ?? 0;
  491. $download = Download::findOne(['id' => $download_id]);
  492. if($download) {
  493. try {
  494. $params = json_decode($download->params,true);
  495. $mallId = $params['mall_id'];
  496. $where[] = ['=', 'mall_id', $params['mall_id']];
  497. $params['where'] = $where;
  498. $select = 'date,add_pay_people_nums as user_num,add_pay_orders as pay_num,add_pay_nums as pay_goods_num,total_pay_money as pay_money';
  499. $params = array( 'where' => $where, 'select'=>$select,'order'=>'created_at desc', 'limit' => 10000);
  500. $list = CensusMallSingleDay::lists($params);
  501. $rows = [];
  502. foreach ($list as $l) {
  503. $row['date'] = $l['date'];
  504. $row['user_num'] = $l['user_num'];
  505. $row['pay_num'] = $l['pay_num'];
  506. $row['pay_goods_num'] = $l['pay_goods_num'];
  507. $row['pay_money'] = $l['pay_money'];
  508. $rows[] = $row;
  509. }
  510. if (!empty($rows)) {
  511. $csv = new CsvTool();
  512. $filename = $download->name;
  513. $type = $download->type;
  514. $csv->filename = $filename;
  515. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  516. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  517. $csv->header = ['日期', '付款人数', '付款订单数','付款件数','付款金额'];
  518. $csv->data = $rows;
  519. $csv->export();
  520. $csv->updateDown($download, $mallId);
  521. }
  522. return true;
  523. } catch (\Exception $e) {
  524. $download->status = 3;
  525. $res = $download->save();
  526. self::setStaticError($e->getMessage());
  527. return $e->getMessage();
  528. }
  529. }
  530. }
  531. /**
  532. * 重新生成缓存
  533. */
  534. public static function resetCache($mall_id) {
  535. try {
  536. $orderStatistics = self::createCacheData($mall_id);
  537. self::setUserStatisticsCache($mall_id, $orderStatistics);
  538. } catch (\Exception $e) {
  539. throw new \Exception($e->getMessage());
  540. }
  541. }
  542. /**
  543. *
  544. * @Author: ltm
  545. * @DateTime: 2022/3/16 0016 14:17
  546. * @Copyright: copyright (c) 2021 广东七件事集团
  547. * @param $data
  548. * @return string
  549. */
  550. public static function staticsUserSource($mall_id){
  551. try {
  552. $where[] = ['=','mall_id',$mall_id];
  553. $where[] = ['=','status',StatusEnum::ENABLED];
  554. $select = 'source,count(*) as source_num';
  555. $group = "source";
  556. $params = ['where' => $where,'select' =>$select,'group' => $group];
  557. $source = User::lists($params,true);
  558. return json_encode(array_column($source,"source_num","source"),true);
  559. } catch (\Exception $e) {
  560. throw new \Exception($e->getMessage());
  561. }
  562. }
  563. /**
  564. * 购买力分析
  565. * @Author: ltm
  566. * @DateTime: 2022/3/16 0016 15:41
  567. * @Copyright: copyright (c) 2021 广东七件事集团
  568. * @param $data
  569. * @return string
  570. */
  571. public static function staticsUserBuy($mall_id){
  572. try {
  573. $num = 6;
  574. $number = [0,100,1000,10000,100000,1000000,10000000];
  575. $user_buy = [];
  576. for ($i=0; $i<$num;$i++){
  577. $buy_item = [];
  578. $where = [];
  579. $where[] = ['=','mall_id',$mall_id];
  580. $where[] = ['=','status',StatusEnum::ENABLED];
  581. if($i==5){
  582. $where[] = ['>','pay_money',$number[($i+1)]];
  583. $buy_item['name'] = $number[$i]."+";
  584. }else{
  585. $where[] = ['between','pay_money',$number[$i],$number[($i+1)]];
  586. $buy_item['name'] = $number[$i]."-".$number[($i+1)];
  587. }
  588. $select = 'count(*) as pay_money_num';
  589. $buy_count = UserStatisticsTotal::getOne($where,true,'','',$select);
  590. $buy_item['num'] = $buy_count['pay_money_num']??0;
  591. $user_buy[] = $buy_item;
  592. }
  593. return json_encode($user_buy,true);
  594. } catch (\Exception $e) {
  595. throw new \Exception($e->getMessage());
  596. }
  597. }
  598. }