CensusMallRegion.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 common\models\common\Region;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_census_mall_region".
  10. *
  11. * @property int $id ID
  12. * @property int $mall_id 商城ID
  13. * @property int $province_id 省份id
  14. * @property int $total_orders 订单量
  15. * @property float $total_pay_money 交易金额
  16. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  17. * @property int $created_at 创建时间
  18. * @property int $updated_at 上次更新数据时间
  19. */
  20. class CensusMallRegion extends BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%census_mall_region}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'province_id', 'total_orders', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['total_pay_money'], 'number'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城ID',
  47. 'province_id' => '省份id',
  48. 'total_orders' => '订单量',
  49. 'total_pay_money' => '交易金额',
  50. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => '上次更新数据时间',
  53. ];
  54. }
  55. public static function setData($params)
  56. {
  57. try {
  58. if (empty($params['mall_id'])&&empty($params['province_id'])) {
  59. return false;
  60. }
  61. $model = self::findOne(['mall_id' => $params['mall_id'],'province_id'=>$params['province_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  62. if (empty($model)) {
  63. $model = new self();
  64. $model->mall_id = $params['mall_id'];
  65. $model->province_id = $params['province_id'];
  66. $model->loadDefaultValues();
  67. }
  68. if (!empty($params['total_orders'])) $model->total_orders += $params['total_orders'];
  69. if (!empty($params['total_pay_money'])) $model->total_pay_money += $params['total_pay_money'];
  70. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  71. return $model;
  72. } catch (\Exception $e) {
  73. self::$static_error = $e->getMessage();
  74. return false;
  75. }
  76. }
  77. public static function getMallRegion($mall_id){
  78. $redis = \Yii::$app->redis;
  79. $redis_key = RedisKeyEnum::suffix(RedisKeyEnum::CENSUS_MALL_REGION, '', true) . ':' . $mall_id;
  80. $region_list = Region::lists(['where'=>[['parent_id'=>1]],'select'=>'id,name','index'=>'id']);
  81. $field_arr = [];
  82. foreach ($region_list as $item){
  83. $field_arr[]='total_pay_money_province_id_'.$item['id'];
  84. }
  85. $result = $redis->hmget($redis_key,...$field_arr);
  86. $list = [];
  87. foreach ($field_arr as $k=>$item){
  88. $arr = explode('_',$item);
  89. $province_id = array_pop($arr);
  90. $list[] = [
  91. 'parent_id'=>1,
  92. 'level'=>'province',
  93. 'name'=>$region_list[$province_id]['name']??'',
  94. 'province_id'=>$province_id,
  95. 'order_money'=>0,
  96. 'order_num'=>0,
  97. 'pay_num'=>0,
  98. 'pay_money'=>!empty($result[$k])?$result[$k]:0,
  99. ];
  100. }
  101. return $list;
  102. }
  103. }