Region.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 'CREATED_AT' => '创建时间',
  56. 'UPDATED_AT' => '更新时间',
  57. 'UPDATER' => '更新人',
  58. 'ADM_NAME' => '操作人',
  59. ];
  60. }
  61. /**
  62. * 获取中文的地区名称
  63. * @param $regionCode
  64. * @return string
  65. */
  66. public static function getCnName($regionCode){
  67. // self::updateToCache();
  68. if (!$regionCode) return '';
  69. $allData = self::getFromCache();
  70. return $allData[$regionCode]['REGION_NAME'] ?? '';
  71. }
  72. /**
  73. * 获取全部配置,把育成津贴奖金比例解成数组
  74. * @return array|\yii\db\ActiveRecord[]
  75. */
  76. public static function getAllData(){
  77. return static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('REGION_CODE')->asArray()->all();
  78. }
  79. /**
  80. * 从缓存获取信息
  81. * @return array|mixed|\yii\db\ActiveRecord[]
  82. */
  83. public static function getFromCache(){
  84. $data = Yii::$app->cache->get(Cache::REGION_CONFIG_KEY);
  85. if(!$data){
  86. // 获取信息
  87. $data = self::getAllData();
  88. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  89. }
  90. return $data;
  91. }
  92. /**
  93. * 更新缓存
  94. * @return array|\yii\db\ActiveRecord[]
  95. */
  96. public static function updateToCache(){
  97. // 获取配置
  98. $data = self::getAllData();
  99. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  100. return $data;
  101. }
  102. /**
  103. * 根据省编码获取仓库
  104. * @param $regionCode
  105. * @return string
  106. */
  107. public static function getWarehouseByCode($regionCode){
  108. //'540000','710000','810000','820000'
  109. $ZoningArr = [
  110. '01'=>['10100','10200','10300','10400','10500','10600','10700','10800','10900',
  111. '11000','11100','11200','11300','11400','11500','11600','11700','11800',
  112. '11900','12000','12100','12200','12300','12400','12500','12600','12700',
  113. '12800','12900','13000','13100','13200','13300','13400','13500','13600','19900']
  114. ];
  115. $zoning = '';
  116. foreach ($ZoningArr as $key=>$val){
  117. if(in_array($regionCode,$val)){
  118. $zoning = $key;
  119. break;
  120. }
  121. }
  122. return $zoning;
  123. }
  124. }