CensusMall.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace common\models\census;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\user\User;
  8. use Yii;
  9. use common\models\order\Order;
  10. /**
  11. * This is the model class for table "qimall_census_mall".
  12. *
  13. * @property int $id ID
  14. * @property int $mall_id 商城ID
  15. * @property int $total_users 总会员数
  16. * @property int $total_orders 总订单量
  17. * @property int $total_pay_orders 支付订单量
  18. * @property int $total_pay_people 支付人数
  19. * @property int $number_of_orders 下单人数
  20. * @property int $repurchase_people 复购人数
  21. * @property int $share_people 分享人数
  22. * @property int $total_visitors 总访客数
  23. * @property int $total_views 总浏览量
  24. * @property int $new_customers 新客户,未支付过
  25. * @property int $regular_customers 老客户,已支付过
  26. * @property int $goods_listings 商品上架量
  27. * @property int $goods_total_visitors 商品访客数
  28. * @property int $goods_total_views 商品浏览量
  29. * @property int $new_customers_pay_peoples 新客户成交人数
  30. * @property int $regular_customers_pay_peoples 老客户成交人数
  31. * @property float $total_order_money 下单金额
  32. * @property float $total_pay_money 支付金额
  33. * @property float $repurchase_money 复购金额
  34. * @property float $new_customers_pay_money 新客户成交金额
  35. * @property float $regular_customers_pay_money 老客户成交金额
  36. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  37. * @property int|null $total_follow_num 关注
  38. * @property int $created_at 创建时间
  39. * @property int $updated_at 上次更新数据时间
  40. * @property float $can_use_commission 可使用佣金
  41. * @property float $can_use_balance 可使用余额
  42. * @property float $can_use_score 可使用积分
  43. */
  44. class CensusMall extends BaseActiveRecord
  45. {
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public static function tableName()
  50. {
  51. return '{{%census_mall}}';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function rules()
  57. {
  58. return [
  59. [['mall_id', 'total_users', 'total_orders', 'total_pay_orders', 'total_pay_people', 'number_of_orders', 'repurchase_people', 'share_people', 'total_visitors', 'total_views', 'new_customers', 'regular_customers', 'goods_listings', 'goods_total_visitors','goods_total_views', 'new_customers_pay_peoples',
  60. 'regular_customers_pay_peoples','total_follow_num', 'status', 'created_at', 'updated_at','add_pay_nums','add_goods'], 'integer'],
  61. [['total_order_money', 'total_pay_money', 'repurchase_money', 'new_customers_pay_money', 'regular_customers_pay_money',
  62. 'can_use_commission','can_use_balance','can_use_score'], 'number'],
  63. ];
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function attributeLabels()
  69. {
  70. return [
  71. 'id' => 'ID',
  72. 'mall_id' => '商城ID',
  73. 'total_users' => '总会员数',
  74. 'total_orders' => '总订单量',
  75. 'total_pay_orders' => '支付订单量',
  76. 'total_pay_people' => '支付人数',
  77. 'number_of_orders' => '下单人数',
  78. 'repurchase_people' => '复购人数',
  79. 'share_people' => '分享人数',
  80. 'total_visitors' => '总访客数',
  81. 'total_views' => '总浏览量',
  82. 'new_customers' => '新客户,未支付过',
  83. 'regular_customers' => '老客户,已支付过',
  84. 'goods_listings' => '商品上架量',
  85. 'goods_total_visitors' => '商品访客数',
  86. 'goods_total_views' => '商品浏览量',
  87. 'new_customers_pay_peoples' => '新客户成交人数',
  88. 'regular_customers_pay_peoples' => '老客户成交人数',
  89. 'total_order_money' => '下单金额',
  90. 'total_pay_money' => '支付金额',
  91. 'repurchase_money' => '复购金额',
  92. 'new_customers_pay_money' => '新客户成交金额',
  93. 'regular_customers_pay_money' => '老客户成交金额',
  94. 'total_follow_num' => '关注',
  95. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  96. 'created_at' => '创建时间',
  97. 'updated_at' => '上次更新数据时间',
  98. ];
  99. }
  100. public static function setData($params)
  101. {
  102. try {
  103. if (empty($params['mall_id'])) {
  104. return false;
  105. }
  106. $model = self::findOne(['mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  107. if (empty($model)) {
  108. $model = new self();
  109. $model->mall_id = $params['mall_id'];
  110. $model->loadDefaultValues();
  111. }
  112. if (!empty($params['users'])) $model->total_users += $params['users'];
  113. if (!empty($params['create_counts'])) $model->total_orders += $params['create_counts'];
  114. if (!empty($params['number_of_orders'])) $model->number_of_orders += $params['number_of_orders'];
  115. if (!empty($params['order_money'])) $model->total_order_money += $params['order_money'];
  116. if (!empty($params['pay_counts'])) $model->total_pay_orders += $params['pay_counts'];
  117. if (!empty($params['pay_people'])) $model->total_pay_people += $params['pay_people'];
  118. if (!empty($params['pay_money'])) $model->total_pay_money += $params['pay_money'];
  119. if (!empty($params['repurchase_people'])) $model->repurchase_people += $params['repurchase_people'];
  120. if (!empty($params['repurchase_money'])) $model->repurchase_money += $params['repurchase_money'];
  121. if (!empty($params['regular_customers_pay_money'])) $model->regular_customers_pay_money += $params['regular_customers_pay_money'];
  122. if (!empty($params['regular_customers_pay_peoples'])) $model->regular_customers_pay_peoples += $params['regular_customers_pay_peoples'];
  123. if (!empty($params['new_customers'])) $model->new_customers += $params['new_customers'];
  124. if (!empty($params['share_people'])) $model->share_people += $params['share_people'];
  125. if (!empty($params['regular_customers'])) $model->regular_customers += $params['regular_customers'];
  126. if (!empty($params['new_customers_pay_peoples'])) $model->new_customers_pay_peoples += $params['new_customers_pay_peoples'];
  127. if (!empty($params['new_customers_pay_money'])) $model->new_customers_pay_money += $params['new_customers_pay_money'];
  128. if (!empty($params['views'])) $model->total_views += $params['views'];
  129. if (!empty($params['goods_listings'])) $model->goods_listings += $params['goods_listings'];
  130. if (!empty($params['goods_total_visitors'])) $model->goods_total_visitors += $params['goods_total_visitors'];
  131. if (!empty($params['goods_total_views'])) $model->goods_total_views += $params['goods_total_views'];
  132. if (!empty($params['visitors'])) $model->total_visitors = $params['visitors'];
  133. if (!empty($params['total_follow_num'])) $model->total_follow_num += $params['total_follow_num'];
  134. if (!empty($params['add_pay_nums'])) $model->add_pay_nums += $params['add_pay_nums'];
  135. if (!empty($params['add_goods'])) $model->add_goods += $params['add_goods'];
  136. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  137. return $model;
  138. } catch (\Exception $e) {
  139. self::$static_error = $e->getMessage();
  140. return false;
  141. }
  142. }
  143. public static function setCensusMallCache($mall_id, $data, $type = 'views', $fields = [])
  144. {
  145. $redis = \Yii::$app->redis;
  146. $key = RedisKeyEnum::CENSUS_MALL;
  147. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  148. switch ($type) {
  149. case 'views':
  150. self::getViews($redis, $redis_key, $data);
  151. break;
  152. case 'share':
  153. $result = $redis->hget($redis_key, $fields);
  154. $data['share_people'] = $data['share_people'] ?? 1;
  155. !empty($result) ? $result += $data['share_people'] : $result = $data['share_people'];
  156. $redis->hset($redis_key, $fields, $result);
  157. break;
  158. case 'goods_listings':
  159. $result = $redis->hget($redis_key, $fields);
  160. $data['goods_listings'] = $data['goods_listings'] ?? 1;
  161. !empty($result) ? $result += $data['goods_listings'] : $result = $data['goods_listings'] ? $data['goods_listings'] : 0;
  162. $redis->hset($redis_key, $fields, $result);
  163. break;
  164. case 'visitors':
  165. $counts = User::find()->andWhere(['mall_id'=>$mall_id])->andWhere(['>','last_login_at',0])->count();
  166. $data['visitors'] = $counts ?? 0;
  167. $redis->hset($redis_key, $fields, $data['visitors'] );
  168. break;
  169. case 'pay':
  170. self::getPayOrder($redis, $redis_key, $data);
  171. break;
  172. case 'create_order':
  173. self::getCreateOrder($redis, $redis_key, $data);
  174. break;
  175. case 'user_register':
  176. self::getUserRegister($redis, $redis_key, $data);
  177. break;
  178. case 'follow':
  179. $result = $redis->hget($redis_key, $fields);
  180. $data['total_follow_num'] = $data['total_follow_num'] ?? 1;
  181. !empty($result) ? $result += $data['total_follow_num'] : $result = $data['total_follow_num'];
  182. $redis->hset($redis_key, $fields, $result);
  183. break;
  184. case 'add_goods':
  185. $result = $redis->hget($redis_key, $fields);
  186. $data['add_goods'] = $data['add_goods'] ?? 1;
  187. !empty($result) ? $result += $data['add_goods'] : $result = $data['add_goods'] ? $data['add_goods'] : 0;
  188. $redis->hset($redis_key, $fields, $result);
  189. break;
  190. default:
  191. return;
  192. }
  193. $data['mall_id'] = $mall_id;
  194. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK_CENSUS, RabbitMqEnum::CENSUS_QUEUE, $data, self::class, 'setData');
  195. }
  196. //添加订单
  197. public static function getCreateOrder($redis, $redis_key, $data)
  198. {
  199. $fields = ['total_orders', 'total_order_money'];
  200. if(!Order::userIsCreated($data['user_id'])){
  201. $fields[] = 'number_of_orders';
  202. }
  203. $arr = array_flip($fields);
  204. $result = $redis->hmget($redis_key, ...$fields);
  205. if(isset($arr['number_of_orders'])){
  206. isset($result[$arr['number_of_orders']]) ? $result[$arr['number_of_orders']] += $data['number_of_orders'] : $result[$arr['number_of_orders']] = $data['number_of_orders'];
  207. $set_data[] = 'number_of_orders';
  208. $set_data[] = $result[$arr['number_of_orders']];
  209. }
  210. isset($result[$arr['total_orders']]) ? $result[$arr['total_orders']] += $data['create_counts'] : $result[$arr['total_orders']] = $data['create_counts'];
  211. isset($result[$arr['total_order_money']]) ? $result[$arr['total_order_money']] += $data['order_money'] : $result[$arr['total_order_money']] = $data['order_money'];
  212. $set_data[] = 'total_orders';
  213. $set_data[] = $result[$arr['total_orders']];
  214. $set_data[] = 'total_order_money';
  215. $set_data[] = $result[$arr['total_order_money']];
  216. $redis->hmset($redis_key, ...$set_data
  217. );
  218. }
  219. //支付
  220. public static function getPayOrder($redis, $redis_key, $data)
  221. {
  222. $fields = ['total_pay_orders', 'total_pay_money'];
  223. if (!Order::userIsPay($data['user_id'])) {
  224. $fields[] = 'total_pay_people';
  225. }
  226. if (isset($data['repurchase_people'])) $fields[] = 'repurchase_people';
  227. if (isset($data['repurchase_money'])) $fields[] = 'repurchase_money';
  228. if (isset($data['new_customers_pay_money'])) $fields[] = 'new_customers_pay_money';
  229. if (isset($data['new_customers_pay_peoples'])) $fields[] = 'new_customers_pay_peoples';
  230. if (isset($data['regular_customers_pay_money'])) $fields[] = 'regular_customers_pay_money';
  231. if (isset($data['regular_customers_pay_peoples'])) $fields[] = 'regular_customers_pay_peoples';
  232. if (isset($data['regular_customers'])) {
  233. $fields[] = 'regular_customers';//老客户
  234. $fields[] = 'new_customers';//新客户
  235. }
  236. $arr = array_flip($fields);
  237. $result = $redis->hmget($redis_key, ...$fields);
  238. !empty($result[$arr['total_pay_orders']]) ? $result[$arr['total_pay_orders']] += $data['pay_counts'] : $result[$arr['total_pay_orders']] = $data['pay_counts'];
  239. !empty($result[$arr['total_pay_money']]) ? $result[$arr['total_pay_money']] += $data['pay_money'] : $result[$arr['total_pay_money']] = $data['pay_money'];
  240. $set_data = [
  241. 'total_pay_orders', $result[$arr['total_pay_orders']],
  242. 'total_pay_money', $result[$arr['total_pay_money']]
  243. ];
  244. if(isset($arr['total_pay_people'])){
  245. !empty($result[$arr['total_pay_people']]) ? $result[$arr['total_pay_people']] += $data['pay_people'] : $result[$arr['total_pay_people']] = $data['pay_people'];
  246. $set_data[] = 'total_pay_people';
  247. $set_data[] = $result[$arr['total_pay_people']];
  248. }
  249. //复购人数
  250. if (isset($arr['repurchase_people'])) {
  251. !empty($result[$arr['repurchase_people']]) ? $result[$arr['repurchase_people']] += $data['repurchase_people'] : $result[$arr['repurchase_people']] = $data['repurchase_people'];
  252. $set_data[] = 'repurchase_people';
  253. $set_data[] = $result[$arr['repurchase_people']];
  254. }
  255. //复购金额
  256. if (isset($arr['repurchase_money'])) {
  257. !empty($result[$arr['repurchase_money']]) ? $result[$arr['repurchase_money']] += $data['repurchase_money'] : $result[$arr['repurchase_money']] = $data['repurchase_money'];
  258. $set_data[] = 'repurchase_money';
  259. $set_data[] = $result[$arr['repurchase_money']];
  260. }
  261. //添加老客户
  262. if (isset($arr['regular_customers'])) {
  263. !empty($result[$arr['regular_customers']]) ? $result[$arr['regular_customers']] += $data['regular_customers'] : $result[$arr['regular_customers']] = $data['regular_customers'];
  264. if (!empty($result[$arr['new_customers']])) {
  265. $result[$arr['new_customers']]--;
  266. } else {
  267. $result[$arr['new_customers']] = 0;
  268. }
  269. $set_data[] = 'regular_customers';
  270. $set_data[] = $result[$arr['regular_customers']];
  271. $set_data[] = 'new_customers';
  272. $set_data[] = $result[$arr['new_customers']];
  273. }
  274. //新客户成交金额
  275. if (isset($arr['new_customers_pay_money'])) {
  276. !empty($result[$arr['new_customers_pay_money']]) ? $result[$arr['new_customers_pay_money']] += $data['new_customers_pay_money'] : $result[$arr['new_customers_pay_money']] = $data['new_customers_pay_money'];
  277. $set_data[] = 'new_customers_pay_money';
  278. $set_data[] = $result[$arr['new_customers_pay_money']];
  279. }
  280. //新客户成交人数
  281. if (isset($arr['new_customers_pay_peoples'])) {
  282. !empty($result[$arr['new_customers_pay_peoples']]) ? $result[$arr['new_customers_pay_peoples']] += $data['new_customers_pay_peoples'] : $result[$arr['new_customers_pay_peoples']] = $data['new_customers_pay_peoples'];
  283. $set_data[] = 'new_customers_pay_peoples';
  284. $set_data[] = $result[$arr['new_customers_pay_peoples']];
  285. }
  286. //老客户成交金额
  287. if (isset($arr['regular_customers_pay_money'])) {
  288. !empty($result[$arr['regular_customers_pay_money']]) ? $result[$arr['regular_customers_pay_money']] += $data['regular_customers_pay_money'] : $result[$arr['regular_customers_pay_money']] = $data['new_customers_pay_money'];
  289. $set_data[] = 'regular_customers_pay_money';
  290. $set_data[] = $result[$arr['regular_customers_pay_money']];
  291. }
  292. //老客户成交人数
  293. if (isset($arr['regular_customers_pay_peoples'])) {
  294. !empty($result[$arr['regular_customers_pay_peoples']]) ? $result[$arr['regular_customers_pay_peoples']] += $data['regular_customers_pay_peoples'] : $result[$arr['regular_customers_pay_peoples']] = $data['regular_customers_pay_peoples'];
  295. $set_data[] = 'regular_customers_pay_peoples';
  296. $set_data[] = $result[$arr['regular_customers_pay_peoples']];
  297. }
  298. $redis->hmset($redis_key, ...$set_data);
  299. }
  300. //注册
  301. public static function getUserRegister($redis, $redis_key, $data)
  302. {
  303. $fields = ['total_users', 'new_customers'];
  304. $arr = array_flip($fields);
  305. $result = $redis->hmget($redis_key, ...$fields);
  306. !empty($result[$arr['total_users']]) ? $result[$arr['total_users']] += $data['users'] : $result[$arr['total_users']] = $data['users'];
  307. !empty($result[$arr['new_customers']]) ? $result[$arr['new_customers']] += $data['new_customers'] : $result[$arr['new_customers']] = $data['new_customers'];
  308. $redis->hmset($redis_key, ...[
  309. 'total_users', $result[$arr['total_users']],
  310. 'new_customers', $result[$arr['new_customers']],
  311. ]
  312. );
  313. }
  314. public static function getViews($redis, $redis_key, $data)
  315. {
  316. $fields = ['total_views'];
  317. if (!empty($data['goods_id'])) {
  318. $fields[] = 'goods_total_visitors';
  319. }
  320. if (!empty($data['goods_total_views'])) {
  321. $fields[] = 'goods_total_views';
  322. }
  323. $arr = array_flip($fields);
  324. $result = $redis->hmget($redis_key, ...$fields);
  325. !empty($result[$arr['total_views']]) ? $result[$arr['total_views']] += $data['views'] : $result[$arr['total_views']] = $data['views'];
  326. $set_data = [
  327. 'total_views', $result[$arr['total_views']],
  328. ];
  329. if (isset($arr['goods_total_visitors'])&&!empty($data['user_id'])) {
  330. $key = RedisKeyEnum::CENSUS_MALL.'_is_goods_total_visitors:';
  331. $is_goods_total_visitors_redis_key = RedisKeyEnum::suffix($key, '', true);
  332. if($redis->setbit($is_goods_total_visitors_redis_key,$data['user_id'],1)==0){
  333. !empty($result[$arr['goods_total_visitors']]) ? $result[$arr['goods_total_visitors']] += $data['visitors'] : $result[$arr['goods_total_visitors']] = $data['visitors'];
  334. $set_data[] = 'goods_total_visitors';
  335. $set_data[] = $result[$arr['goods_total_visitors']];
  336. }
  337. }
  338. if (isset($arr['goods_total_views'])) {
  339. !empty($result[$arr['goods_total_views']]) ? $result[$arr['goods_total_views']] += $data['visitors'] : $result[$arr['goods_total_views']] = $data['visitors'];
  340. $set_data[] = 'goods_total_views';
  341. $set_data[] = $result[$arr['goods_total_views']];
  342. }
  343. $redis->hmset($redis_key, ...$set_data);
  344. CensusMall::setData($data);
  345. }
  346. public static function getMallData($mall_id,$fields=null)
  347. {
  348. $redis = \Yii::$app->redis;
  349. $key = RedisKeyEnum::CENSUS_MALL;
  350. $redis_key = RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  351. if($fields){
  352. if(is_string($fields)){
  353. $result = $redis->hget($redis_key,$fields);
  354. return [$fields=>$result??0];
  355. }else{
  356. $result = $redis->hmget($redis_key,...$fields);
  357. $data = [];
  358. foreach ($fields as $k=>$v){
  359. $data[$v]=$result[$k]??0;
  360. }
  361. return $data;
  362. }
  363. }else{
  364. $result = $redis->hgetall($redis_key);
  365. if($result){
  366. foreach ($result as $k=>$v){
  367. if($k%2==0){
  368. $data[$k]['name'] = $v;
  369. }
  370. if($k%2==1)$data[$k-1]['num'] = $v;
  371. }
  372. return array_column($data,'num','name');
  373. }else{
  374. return [];
  375. }
  376. }
  377. }
  378. }