| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace common\models\census;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\Region;
- use Yii;
- /**
- * This is the model class for table "qimall_census_mall_region".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $province_id 省份id
- * @property int $total_orders 订单量
- * @property float $total_pay_money 交易金额
- * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- */
- class CensusMallRegion extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%census_mall_region}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'province_id', 'total_orders', 'status', 'created_at', 'updated_at'], 'integer'],
- [['total_pay_money'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'province_id' => '省份id',
- 'total_orders' => '订单量',
- 'total_pay_money' => '交易金额',
- 'status' => '状态[-1:删除;0:禁用;1启用;]',
- 'created_at' => '创建时间',
- 'updated_at' => '上次更新数据时间',
- ];
- }
- public static function setData($params)
- {
- try {
- if (empty($params['mall_id'])&&empty($params['province_id'])) {
- return false;
- }
- $model = self::findOne(['mall_id' => $params['mall_id'],'province_id'=>$params['province_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->province_id = $params['province_id'];
- $model->loadDefaultValues();
- }
- if (!empty($params['total_orders'])) $model->total_orders += $params['total_orders'];
- if (!empty($params['total_pay_money'])) $model->total_pay_money += $params['total_pay_money'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getMallRegion($mall_id){
- $redis = \Yii::$app->redis;
- $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_REGION, '', true) . ':' . $mall_id;
- $region_list = Region::lists(['where'=>[['parent_id'=>1]],'select'=>'id,name','index'=>'id']);
- $field_arr = [];
- foreach ($region_list as $item){
- $field_arr[]='total_pay_money_province_id_'.$item['id'];
- }
- $result = $redis->hmget($redis_key,...$field_arr);
- $list = [];
- foreach ($field_arr as $k=>$item){
- $arr = explode('_',$item);
- $province_id = array_pop($arr);
- $list[] = [
- 'parent_id'=>1,
- 'level'=>'province',
- 'name'=>$region_list[$province_id]['name']??'',
- 'province_id'=>$province_id,
- 'order_money'=>0,
- 'order_num'=>0,
- 'pay_num'=>0,
- 'pay_money'=>!empty($result[$k])?$result[$k]:0,
- ];
- }
- return $list;
- }
- }
|