Region.php 3.6 KB

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