| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace common\models\census;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- * This is the model class for table "qimall_census_mall_purchasing_power".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $purchasing_power 购买力
- * @property int $numbers 数量
- * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- */
- class CensusMallPurchasingPower extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%census_mall_purchasing_power}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'numbers', 'purchasing_power', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'purchasing_power' => '购买力',
- 'numbers' => '数量',
- 'status' => '状态[-1:删除;0:禁用;1启用;]',
- 'created_at' => '创建时间',
- 'updated_at' => '上次更新数据时间',
- ];
- }
- public static function getConfigList($key = null)
- {
- $config_list = [
- 1 => ['min' => 0, 'max' => 100,'desc'=>'0_100'],
- 2 => ['min' => 100, 'max' => 1000,'desc'=>'100_1000'],
- 3 => ['min' => 1000, 'max' => 10000,'desc'=>'1000_10000'],
- 4 => ['min' => 10000, 'max' => 100000,'desc'=>'10000_100000'],
- 5 => ['min' => 100000, 'max' => 1000000,'desc'=>'100000_1000000'],
- 6 => ['min' => 1000000, 'max' => 99999999999,'desc'=>'1000000+'],
- ];
- if ($key !== null && isset($config_list[$key])) {
- return $config_list[$key];
- }
- return $config_list;
- }
- public static function setData($params)
- {
- try {
- if (empty($params['mall_id']) && empty($params['purchasing_power'])) {
- return false;
- }
- $model = self::findOne(['mall_id' => $params['mall_id'], 'purchasing_power' => $params['purchasing_power'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->purchasing_power = $params['purchasing_power'];
- $model->loadDefaultValues();
- }
- if (!empty($params['numbers'])){
- $model->numbers += $params['numbers'];
- if($model->numbers<0) $model->numbers = 0;
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getMallUserPower($mall_id){
- $redis = \Yii::$app->redis;
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_BUY_POWER, '', true) . ':' . $mall_id;
- $result = $redis->hgetall($redis_key);
- $data = [];
- if($result){
- foreach ($result as $k=>$v){
- if($k%2==0){
- $exist_keys_arr[]=$v;
- $arr = explode(":",$v);
- $data[$k]['name'] = $arr[1];
- }
- if($k%2==1)$data[$k-1]['num'] = $v;
- }
- }else{
- $list = CensusMallPurchasingPower::lists(['where'=>[['mall_id'=>$mall_id]]]);
- foreach ($list as $item){
- $data [] =[
- 'name'=>$item['purchasing_power'],
- 'num'=>$item['numbers'],
- ];
- }
- }
- $config_list = self::getConfigList();
- $list = [];
- $data = array_column($data,'num','name');
- foreach ($config_list as $key=>$item){
- if(isset($data[$key])){
- $list[] = [
- 'name'=>$item['desc'],
- 'num'=>$data[$key],
- ];
- }else{
- $list[] = [
- 'name'=>$item['desc'],
- 'num'=>0,
- ];
- }
- }
- return $list;
- }
- }
|