EliteLevel.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use common\helpers\LoggerTool;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%CROWN_LEVEL}}".
  8. *
  9. * @property string $ID
  10. * @property string $LEVEL_NAME 级别名称
  11. * @property int $ICON_TYPE 图标类型
  12. * @property int $ICON_NUM 图标个数
  13. * @property int $RX_PERCENT 图标个数
  14. * @property string $MIN_LEVEL_ID 上级ID
  15. * @property string $LEVEL_SCORE 级别分数
  16. * @property string $UPGRADE_SCORE 升级分数
  17. * @property string $TOURISM_PERCENT 旅游奖比例
  18. * @property string $GARAGE_PERCENT 车房奖比例
  19. * @property int $SORT 排序
  20. * @property int $CREATED_AT 创建时间
  21. * @property int $UPDATED_AT 更新时间
  22. * @property string $CREATE_ADMIN 创建人
  23. * @property string $UPDATE_ADMIN 更新人
  24. */
  25. class EliteLevel extends \common\components\ActiveRecord
  26. {
  27. const NO_LEVEL_ID = 'N0RI23JKCP4OBLZ6GAXYWH9D1QS8NA7V';
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%CROWN_LEVEL}}';
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['LEVEL_NAME', 'CREATED_AT', 'CREATE_ADMIN'], 'required'],
  42. [['SORT', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
  43. [['ID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
  44. [['LEVEL_NAME'], 'string', 'max' => 20],
  45. [['LEVEL_NAME'], 'unique'],
  46. [['ID'], 'unique'],
  47. ];
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'ID' => 'ID',
  56. 'LEVEL_NAME' => '级别名称',
  57. 'SORT' => '排序',
  58. 'CREATED_AT' => '创建时间',
  59. 'UPDATED_AT' => '更新时间',
  60. 'CREATE_ADMIN' => '创建人',
  61. 'UPDATE_ADMIN' => '更新人',
  62. ];
  63. }
  64. /**
  65. * 获取全部级别以数字索引的方式展现
  66. * @return array|\yii\db\ActiveRecord[]
  67. */
  68. public static function getAllDataWithNumIndex()
  69. {
  70. return static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('SORT')->asArray()->all();
  71. }
  72. /**
  73. * 获取全部配置
  74. * @return array|\yii\db\ActiveRecord[]
  75. */
  76. public static function getAllData()
  77. {
  78. return static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('ID')->asArray()->all();
  79. }
  80. // 获取级别配置一维度数组,id为键,级别等级为值
  81. public static function getIdConvertLevelSort()
  82. {
  83. $ret = [];
  84. $allData = static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('ID')->asArray()->all();
  85. foreach($allData as $data) {
  86. $ret[$data['ID']] = $data['SORT'];
  87. }
  88. return $ret;
  89. }
  90. /**
  91. * 从缓存获取信息
  92. * @return array|mixed|\yii\db\ActiveRecord[]
  93. */
  94. public static function getIdConvertLevelSortCache()
  95. {
  96. $key = Cache::ELITE_LEVEL_CONFIG_KEY . ':idsort';
  97. $data = Yii::$app->cache->get($key);
  98. if(!$data){
  99. // 获取信息
  100. $data = self::getIdConvertLevelSort();
  101. Yii::$app->cache->set($key, $data);
  102. }
  103. return $data;
  104. }
  105. /**
  106. * 从缓存获取信息
  107. * @return array|mixed|\yii\db\ActiveRecord[]
  108. */
  109. public static function getFromCache()
  110. {
  111. $data = Yii::$app->cache->get(Cache::ELITE_LEVEL_CONFIG_KEY);
  112. if(!$data){
  113. // 获取信息
  114. $data = self::getAllData();
  115. Yii::$app->cache->set(Cache::ELITE_LEVEL_CONFIG_KEY, $data);
  116. }
  117. // i18n转换
  118. foreach ($data as &$item) {
  119. $item['LEVEL_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
  120. }
  121. return $data;
  122. }
  123. /**
  124. * 更新缓存
  125. * @return array|\yii\db\ActiveRecord[]
  126. */
  127. public static function updateToCache()
  128. {
  129. // 获取配置
  130. $data = self::getAllData();
  131. Yii::$app->cache->set(Cache::ELITE_LEVEL_CONFIG_KEY, $data);
  132. return $data;
  133. }
  134. /**
  135. * 通过排序获取级别
  136. * @param int $sort
  137. * @return mixed
  138. */
  139. public static function getLevelFromSort(int $sort)
  140. {
  141. static $crownLevels;
  142. if(!$crownLevels){
  143. $crownLevels = self::getFromCache();
  144. $crownLevels = array_column($crownLevels, null, 'sort');
  145. }
  146. return $crownLevels[$sort];
  147. }
  148. /**
  149. * 通过排序获取ID
  150. * @param int $sort
  151. * @return mixed
  152. */
  153. public static function getIdFromSort(int $sort)
  154. {
  155. $level = self::getLevelFromSort($sort);
  156. return $level['ID'];
  157. }
  158. /**
  159. * 获取默认级别
  160. * @return mixed
  161. */
  162. public static function getDefaultLevelId()
  163. {
  164. return self::NO_LEVEL_ID;
  165. }
  166. /**
  167. * 获取星级的排序
  168. * @param $id
  169. * @return mixed
  170. */
  171. public static function getSortById($id)
  172. {
  173. $crownLevels = self::getFromCache();
  174. return $crownLevels[$id]['SORT'] ?? 0;
  175. }
  176. /**
  177. * 通过ID获取星级名称
  178. * @param $levelId
  179. * @return mixed'
  180. */
  181. public static function getNameById($levelId)
  182. {
  183. return self::findOneAsArray('ID = :ID', [':ID' => $levelId])['LEVEL_NAME'] ?? '';
  184. }
  185. }