BusinessCardStatistics.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\user\User;
  7. use Exception;
  8. use Yii;
  9. /**
  10. * This is the model class for table "qimall_addons_business_card_statistics".
  11. *
  12. * @property int $id
  13. * @property int $mall_id
  14. * @property int $user_id
  15. * @property int $card_num 名片数量
  16. * @property int $clue_num 线索数量
  17. * @property int $browse_num 浏览量(名片)
  18. * @property int|null $share_num 分享次数
  19. * @property int $like_num 点赞数
  20. * @property int $order_num 订单数量
  21. * @property float $order_money 支付金额
  22. * @property int $status 状态[1启用 0禁用 -1删除]
  23. * @property int $created_at 创建时间
  24. * @property int $updated_at 修改时间
  25. */
  26. class BusinessCardStatistics extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return 'qimall_addons_business_card_statistics';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'user_id', 'card_num', 'clue_num', 'browse_num', 'share_num', 'like_num', 'order_num', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['order_money'], 'number'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => 'Mall ID',
  53. 'user_id' => 'User ID',
  54. 'card_num' => 'Card Num',
  55. 'clue_num' => 'Clue Num',
  56. 'browse_num' => 'Browse Num',
  57. 'share_num' => 'Share Num',
  58. 'like_num' => 'Like Num',
  59. 'order_num' => 'Order Num',
  60. 'order_money' => 'Order Money',
  61. 'status' => 'Status',
  62. 'created_at' => 'Created At',
  63. 'updated_at' => 'Updated At',
  64. ];
  65. }
  66. public function getUser()
  67. {
  68. return $this->hasOne(User::class, ['id' => 'user_id']);
  69. }
  70. /**
  71. * @author hpei
  72. * @param array $params
  73. * @param array $select
  74. * @param string|null $group
  75. * @return array
  76. */
  77. public static function getList($where, $select = '*', $group = '') {
  78. $where[] = ['<>','status',StatusEnum::DELETE];
  79. $model = self::find();
  80. $params['where'] = $where;
  81. $params['select'] = $select;
  82. if ($group !== '') $params['group'] = $group;
  83. self::paramPack($params, $model);
  84. return $model->asArray()->all();
  85. }
  86. /**
  87. * 获取排行榜
  88. * @author hpei
  89. * @param int mall_id
  90. * @param string type money|num
  91. * @return array [user_id=>num]
  92. */
  93. public static function rank($param) {
  94. $key = $param['type'] == 'money' ? RedisKeyEnum::CARD_ORDER_MONEY_STATISTICS : RedisKeyEnum::CARD_ORDER_NUM_STATISTICS;
  95. $cacheKey = RedisKeyEnum::suffix($key, 'mall_id_'. $param['mall_id']);
  96. $redis = Yii::$app->redis;
  97. $rank = $redis->zrevrange($cacheKey, 0, 9, 'WITHSCORES');
  98. if (empty($rank)) {
  99. $model = self::find()
  100. ->where(['mall_id' => $param['mall_id']])
  101. ->orderBy('count desc')
  102. ->groupBy('user_id')
  103. ->indexBy('user_id')
  104. ->limit(10);
  105. if ($param['type'] == 'money') {
  106. $model->select(['sum(order_money) as count', 'user_id']);
  107. } else {
  108. $model->select(['sum(order_num) as count', 'user_id']);
  109. }
  110. $list = $model->asArray()->all();
  111. if (!empty($list)) {
  112. $options = [];
  113. foreach ($list as $val) {
  114. array_push($options, $val['count'], 'user_id:' . $val['user_id']);
  115. }
  116. $redis->zadd($cacheKey, ...$options);
  117. }
  118. } else {
  119. for ($i = 0; $i < count($rank); $i++) {
  120. if ($i %2 == 0) {
  121. $user_id = explode(':', $rank[$i])[1];
  122. $list[$i] = ['user_id' => $user_id, 'count' => 0];
  123. } else {
  124. $list[$i - 1]['count'] = $rank[$i];
  125. }
  126. }
  127. $list = array_values($list);
  128. }
  129. return $list;
  130. }
  131. /**
  132. * 保存数据
  133. * @author hpei
  134. * @return bool|Exception
  135. */
  136. public static function setData($params) {
  137. try {
  138. $model = self::find()
  139. ->where(['user_id' => $params['user_id'], 'mall_id' => $params['mall_id']])
  140. ->andWhere(['>=', 'created_at', strtotime('today 0:0:0')])
  141. ->andWhere(['<=', 'created_at', strtotime('today 23:59:59')])
  142. ->one();
  143. if (empty($model)) {
  144. $model = new self();
  145. $model->user_id = $params['user_id'];
  146. $model->mall_id = $params['mall_id'];
  147. }
  148. unset($params['user_id'], $params['mall_id']);
  149. foreach ($model as $key => $val) {
  150. if (isset($params[$key]) && !empty($params[$key])) $model->$key += $params[$key];
  151. }
  152. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  153. return $model->id;
  154. } catch (\Exception $e) {
  155. self::$static_error = $e->getMessage();
  156. return false;
  157. }
  158. }
  159. /**
  160. * 概况数据
  161. * @author hpei
  162. * @param int date
  163. * @param array|int ids
  164. * @return array
  165. */
  166. public static function survey($date, $ids) {
  167. if(!empty($date)) {
  168. $start_time = strtotime("-$date day 0:0:0");
  169. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  170. $where[] = ['>', 'created_at', $start_time];
  171. $where[] = ['<=', 'created_at', $end_time];
  172. }
  173. $where[] = ['in', 'user_id', $ids];
  174. $data = [];
  175. //订单数,付款金额
  176. $statisticsMDL = BusinessCardStatistics::find();
  177. $params = [
  178. 'where' => $where,
  179. 'select' => ['sum(order_num) order_num', 'sum(order_money) order_money']
  180. ];
  181. BusinessCardStatistics::paramPack($params, $statisticsMDL);
  182. $statistics = $statisticsMDL->asArray()->one();
  183. $data['order_num'] = isset($statistics['order_num']) ? $statistics['order_num'] : 0;
  184. $data['order_money'] = isset($statistics['order_money']) ? $statistics['order_money'] : 0;
  185. //客户类型
  186. $user_menu = [CardEnums::CUSTOMER_TYPE_INTENTION, CardEnums::CUSTOMER_TYPE_COMPARE, CardEnums::CUSTOMER_TYPE_ORDER];
  187. $user_where = $where;
  188. $user_where[] = ['in', 'customer_type', $user_menu];
  189. $userMDL = BusinessCardUser::find();
  190. $params = [
  191. 'where' => $user_where,
  192. 'select' => ['count(*) count', 'customer_type'],
  193. 'group' => 'customer_type',
  194. 'index' => 'customer_type'
  195. ];
  196. BusinessCardUser::paramPack($params, $userMDL);
  197. $statistics = $userMDL->asArray()->all();
  198. $data['intention_customer'] = isset($statistics[CardEnums::CUSTOMER_TYPE_INTENTION]) ? $statistics[CardEnums::CUSTOMER_TYPE_INTENTION]['count'] : 0;//意向
  199. $data['compare_customer'] = isset($statistics[CardEnums::CUSTOMER_TYPE_COMPARE]) ? $statistics[CardEnums::CUSTOMER_TYPE_COMPARE]['count'] : 0;//比较
  200. $data['order_customer'] = isset($statistics[CardEnums::CUSTOMER_TYPE_ORDER]) ? $statistics[CardEnums::CUSTOMER_TYPE_ORDER]['count'] : 0;//待成交
  201. //新增客户,浏览量,线索,下单人数
  202. $log_menu = [CardEnums::LOG_TYPE_USER, CardEnums::LOG_TYPE_CLUE, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_ORDER];
  203. $log_where = $where;
  204. $log_where = [['in', 'log_type', $log_menu]];
  205. $params = [
  206. 'where' => $log_where,
  207. 'group' => 'log_type',
  208. 'index' => 'log_type',
  209. 'select' => ['count(*) count', 'log_type']
  210. ];
  211. $logMDL = BusinessCardCustomerLog::find();
  212. BusinessCardCustomerLog::paramPack($params, $logMDL);
  213. $statistics = $logMDL->asArray()->all();
  214. $data['add_user'] = isset($statistics[CardEnums::LOG_TYPE_USER]) ? $statistics[CardEnums::LOG_TYPE_USER]['count'] : 0;//新增用户
  215. $data['browse_num'] = isset($statistics[CardEnums::LOG_TYPE_CARD]) ? $statistics[CardEnums::LOG_TYPE_CARD]['count'] : 0;//卡片浏览量
  216. $data['add_clue'] = isset($statistics[CardEnums::LOG_TYPE_CLUE]) ? $statistics[CardEnums::LOG_TYPE_CLUE]['count'] : 0;//新增线索
  217. $data['order_people_num'] = isset($statistics[CardEnums::LOG_TYPE_ORDER]) ? $statistics[CardEnums::LOG_TYPE_ORDER]['count'] : 0;//下单人数
  218. return $data;
  219. }
  220. }