Region.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 COUNTRY_ID 国家
  14. * @property string $AREA_CODE 区号
  15. * @property int $CREATED_AT 创建时间
  16. * @property int $UPDATED_AT 更新时间
  17. * @property string $UPDATER 更新人
  18. * @property string $ADM_NAME 操作人
  19. */
  20. class Region extends \common\components\ActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%REGION}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['REGION_CODE', 'DEEP', 'PID', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
  36. [['ID'], 'string', 'max' => 32],
  37. [['REGION_NAME'], 'string', 'max' => 128],
  38. [['AREA_CODE'], 'string', 'max' => 6],
  39. [['UPDATER', 'ADM_NAME'], 'string', 'max' => 16],
  40. [['REGION_CODE'], 'unique'],
  41. [['ID'], 'unique'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'ID' => 'ID',
  51. 'REGION_CODE' => '编码',
  52. 'DEEP' => '深度',
  53. 'REGION_NAME' => '名称',
  54. 'PID' => '父级id',
  55. 'COUNTRY_ID' => '国家',
  56. 'AREA_CODE' => '区号',
  57. 'CREATED_AT' => '创建时间',
  58. 'UPDATED_AT' => '更新时间',
  59. 'UPDATER' => '更新人',
  60. 'ADM_NAME' => '操作人',
  61. ];
  62. }
  63. /**
  64. * 获取中文的地区名称
  65. * @param $regionCode
  66. * @return string
  67. */
  68. public static function getCnName($regionCode){
  69. // self::updateToCache();
  70. if (!$regionCode) return '';
  71. $allData = self::getFromCache();
  72. return $allData[$regionCode]['REGION_NAME'] ?? '';
  73. }
  74. /**
  75. * 获取全部配置,把育成津贴奖金比例解成数组
  76. * @return array|\yii\db\ActiveRecord[]
  77. */
  78. public static function getAllData(){
  79. return static::find()->where('STATUS=1')->orderBy('REGION_NAME ASC')->indexBy('REGION_CODE')->asArray()->all();
  80. }
  81. /**
  82. * 从缓存获取信息
  83. * @return array|mixed|\yii\db\ActiveRecord[]
  84. */
  85. public static function getFromCache(){
  86. $data = Yii::$app->cache->get(Cache::REGION_CONFIG_KEY);
  87. $data = null;
  88. if(!$data){
  89. // 获取信息
  90. $data = self::getAllData();
  91. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  92. }
  93. return $data;
  94. }
  95. public static function getByCountryId($countyId)
  96. {
  97. $result = [];
  98. $regions = self::getFromCache();
  99. foreach ($regions as $region) {
  100. if ($region['COUNTRY_ID'] == $countyId) {
  101. $result[] = ['value' => $region['REGION_CODE'], 'label' => $region['REGION_NAME'],];
  102. }
  103. }
  104. return $result;
  105. }
  106. /**
  107. * 更新缓存
  108. * @return array|\yii\db\ActiveRecord[]
  109. */
  110. public static function updateToCache(){
  111. // 获取配置
  112. $data = self::getAllData();
  113. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  114. return $data;
  115. }
  116. /**
  117. * 根据省编码获取仓库
  118. * @param $regionCode
  119. * @return string
  120. */
  121. public static function getWarehouseByCode($regionCode){
  122. //'540000','710000','810000','820000'
  123. $ZoningArr = [
  124. '01' => ['10100','10200','10300','10400','10500','10600','10700','10800','10900',
  125. '11000','11100','11200','11300','11400','11500','11600','11700','11800',
  126. '11900','12000','12100','12200','12300','12400','12500','12600','12700',
  127. '12800','12900','13000','13100','13200','13300','13400','13500','13600','19900',
  128. '10000', '10001', '10002', '10003', '10004', '10005', '10006',
  129. ]
  130. ];
  131. $zoning = '';
  132. foreach ($ZoningArr as $key=>$val){
  133. if(in_array($regionCode,$val)){
  134. $zoning = $key;
  135. break;
  136. }
  137. }
  138. return $zoning;
  139. }
  140. }