CensusMallPurchasingPower.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace common\models\census;
  3. use common\enums\RedisKeyEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_census_mall_purchasing_power".
  9. *
  10. * @property int $id ID
  11. * @property int $mall_id 商城ID
  12. * @property int $purchasing_power 购买力
  13. * @property int $numbers 数量
  14. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 上次更新数据时间
  17. */
  18. class CensusMallPurchasingPower extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%census_mall_purchasing_power}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'numbers', 'purchasing_power', 'status', 'created_at', 'updated_at'], 'integer'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => '商城ID',
  44. 'purchasing_power' => '购买力',
  45. 'numbers' => '数量',
  46. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  47. 'created_at' => '创建时间',
  48. 'updated_at' => '上次更新数据时间',
  49. ];
  50. }
  51. public static function getConfigList($key = null)
  52. {
  53. $config_list = [
  54. 1 => ['min' => 0, 'max' => 100,'desc'=>'0_100'],
  55. 2 => ['min' => 100, 'max' => 1000,'desc'=>'100_1000'],
  56. 3 => ['min' => 1000, 'max' => 10000,'desc'=>'1000_10000'],
  57. 4 => ['min' => 10000, 'max' => 100000,'desc'=>'10000_100000'],
  58. 5 => ['min' => 100000, 'max' => 1000000,'desc'=>'100000_1000000'],
  59. 6 => ['min' => 1000000, 'max' => 99999999999,'desc'=>'1000000+'],
  60. ];
  61. if ($key !== null && isset($config_list[$key])) {
  62. return $config_list[$key];
  63. }
  64. return $config_list;
  65. }
  66. public static function setData($params)
  67. {
  68. try {
  69. if (empty($params['mall_id']) && empty($params['purchasing_power'])) {
  70. return false;
  71. }
  72. $model = self::findOne(['mall_id' => $params['mall_id'], 'purchasing_power' => $params['purchasing_power'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  73. if (empty($model)) {
  74. $model = new self();
  75. $model->mall_id = $params['mall_id'];
  76. $model->purchasing_power = $params['purchasing_power'];
  77. $model->loadDefaultValues();
  78. }
  79. if (!empty($params['numbers'])){
  80. $model->numbers += $params['numbers'];
  81. if($model->numbers<0) $model->numbers = 0;
  82. }
  83. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  84. return $model;
  85. } catch (\Exception $e) {
  86. self::$static_error = $e->getMessage();
  87. return false;
  88. }
  89. }
  90. public static function getMallUserPower($mall_id){
  91. $redis = \Yii::$app->redis;
  92. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_BUY_POWER, '', true) . ':' . $mall_id;
  93. $result = $redis->hgetall($redis_key);
  94. $data = [];
  95. if($result){
  96. foreach ($result as $k=>$v){
  97. if($k%2==0){
  98. $exist_keys_arr[]=$v;
  99. $arr = explode(":",$v);
  100. $data[$k]['name'] = $arr[1];
  101. }
  102. if($k%2==1)$data[$k-1]['num'] = $v;
  103. }
  104. }else{
  105. $list = CensusMallPurchasingPower::lists(['where'=>[['mall_id'=>$mall_id]]]);
  106. foreach ($list as $item){
  107. $data [] =[
  108. 'name'=>$item['purchasing_power'],
  109. 'num'=>$item['numbers'],
  110. ];
  111. }
  112. }
  113. $config_list = self::getConfigList();
  114. $list = [];
  115. $data = array_column($data,'num','name');
  116. foreach ($config_list as $key=>$item){
  117. if(isset($data[$key])){
  118. $list[] = [
  119. 'name'=>$item['desc'],
  120. 'num'=>$data[$key],
  121. ];
  122. }else{
  123. $list[] = [
  124. 'name'=>$item['desc'],
  125. 'num'=>0,
  126. ];
  127. }
  128. }
  129. return $list;
  130. }
  131. }