Region.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $allData = self::getFromCache();
  68. return $allData[$regionCode]['REGION_NAME'] ?? '';
  69. }
  70. /**
  71. * 获取全部配置,把育成津贴奖金比例解成数组
  72. * @return array|\yii\db\ActiveRecord[]
  73. */
  74. public static function getAllData(){
  75. return static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('REGION_CODE')->asArray()->all();
  76. }
  77. /**
  78. * 从缓存获取信息
  79. * @return array|mixed|\yii\db\ActiveRecord[]
  80. */
  81. public static function getFromCache(){
  82. $data = Yii::$app->cache->get(Cache::REGION_CONFIG_KEY);
  83. if(!$data){
  84. // 获取信息
  85. $data = self::getAllData();
  86. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  87. }
  88. return $data;
  89. }
  90. /**
  91. * 更新缓存
  92. * @return array|\yii\db\ActiveRecord[]
  93. */
  94. public static function updateToCache(){
  95. // 获取配置
  96. $data = self::getAllData();
  97. Yii::$app->cache->set(Cache::REGION_CONFIG_KEY, $data);
  98. return $data;
  99. }
  100. /**
  101. * 根据省编码获取仓库
  102. * @param $regionCode
  103. * @return string
  104. */
  105. public static function getWarehouseByCode($regionCode){
  106. //'540000','710000','810000','820000'
  107. $ZoningArr = [
  108. '01'=>['110000','120000','130000','370000','140000','210000','310000','320000','330000','340000','410000','150000','220000','230000','500000','510000','610000','620000','640000','630000','650000'],
  109. '02'=>['440000','350000','360000','420000','430000','450000','460000','520000','530000']
  110. ];
  111. $zoning = '';
  112. foreach ($ZoningArr as $key=>$val){
  113. if(in_array($regionCode,$val)){
  114. $zoning = $key;
  115. break;
  116. }
  117. }
  118. return $zoning;
  119. }
  120. }