Region.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%REGION}}".
  7. *
  8. * @property string $ID
  9. * @property int $REGION_CODE 编码
  10. * @property int $DEEP 深度
  11. * @property string $REGION_NAME 名称
  12. * @property int $PID 父级id
  13. * @property string $AREA_CODE 区号
  14. * @property int $CREATED_AT 创建时间
  15. * @property int $UPDATED_AT 更新时间
  16. * @property string $UPDATER 更新人
  17. * @property string $ADM_NAME 操作人
  18. */
  19. class Region extends \common\components\ActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%REGION}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['REGION_CODE', 'DEEP', 'PID', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
  35. [['ID'], 'string', 'max' => 32],
  36. [['REGION_NAME'], 'string', 'max' => 128],
  37. [['AREA_CODE'], 'string', 'max' => 6],
  38. [['UPDATER', 'ADM_NAME'], 'string', 'max' => 16],
  39. [['REGION_CODE'], 'unique'],
  40. [['ID'], 'unique'],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'ID' => 'ID',
  50. 'REGION_CODE' => '编码',
  51. 'DEEP' => '深度',
  52. 'REGION_NAME' => '名称',
  53. 'PID' => '父级id',
  54. 'AREA_CODE' => '区号',
  55. 'ZIP_CODE' => '邮编',
  56. 'CREATED_AT' => '创建时间',
  57. 'UPDATED_AT' => '更新时间',
  58. 'UPDATER' => '更新人',
  59. 'ADM_NAME' => '操作人',
  60. ];
  61. }
  62. /**
  63. * 获取中文的地区名称
  64. * @param $regionCode
  65. * @return string
  66. */
  67. public static function getCnName($regionCode){
  68. $allData = self::getFromCache();
  69. return $allData[$regionCode]['REGION_NAME'] ?? '';
  70. }
  71. /**
  72. * 获取邮编
  73. * @param $regionCode
  74. * @return string
  75. */
  76. public static function getZipCode($regionCode) {
  77. $allData = self::getFromCache();
  78. return $allData[$regionCode]['ZIP_CODE'] ?? '';
  79. }
  80. /**
  81. * 获取全部配置,把育成津贴奖金比例解成数组
  82. * @return array|\yii\db\ActiveRecord[]
  83. */
  84. public static function getAllData(){
  85. return static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('REGION_CODE')->asArray()->all();
  86. }
  87. /**
  88. * 从缓存获取信息
  89. * @return array|mixed|\yii\db\ActiveRecord[]
  90. */
  91. public static function getFromCache(){
  92. // Yii::$app->cache->delete(Cache::REGION_CONFIG_KEY);
  93. $data = Yii::$app->cache->get(Cache::REGION_CONFIG_KEY);
  94. if(!$data){
  95. // 获取信息
  96. $data = self::getAllData();
  97. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  98. }
  99. return $data;
  100. }
  101. /**
  102. * 更新缓存
  103. * @return array|\yii\db\ActiveRecord[]
  104. */
  105. public static function updateToCache(){
  106. // 获取配置
  107. $data = self::getAllData();
  108. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  109. return $data;
  110. }
  111. /**
  112. * 根据省编码获取仓库
  113. * @param $regionCode
  114. * @return string
  115. */
  116. public static function getWarehouseByCode($regionCode){
  117. //'540000'(西藏),'710000'(台湾) ,'810000'(香港特别行政区),'820000'(澳门特别行政区)
  118. $ZoningArr = [
  119. '01'=>['110000','120000','130000','370000','140000','210000','310000','320000','330000','340000','410000','150000','220000','230000','500000','510000','610000','620000','640000','630000','650000'],
  120. '02'=>['440000','350000','360000','420000','430000','450000','460000','520000','530000', '540000']
  121. ];
  122. $zoning = '';
  123. foreach ($ZoningArr as $key=>$val){
  124. if(in_array($regionCode,$val)){
  125. $zoning = $key;
  126. break;
  127. }
  128. }
  129. return $zoning;
  130. }
  131. }