EliteLevel.php 5.1 KB

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