EliteLevel.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if(!$data){
  110. // 获取信息
  111. $data = self::getAllData();
  112. Yii::$app->cache->set(Cache::ELITE_LEVEL_CONFIG_KEY, $data);
  113. }
  114. // i18n转换
  115. // foreach ($data as &$item) {
  116. // $item['LEVEL_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
  117. // }
  118. return $data;
  119. }
  120. /**
  121. * 更新缓存
  122. * @return array|\yii\db\ActiveRecord[]
  123. */
  124. public static function updateToCache()
  125. {
  126. // 获取配置
  127. $data = self::getAllData();
  128. Yii::$app->cache->set(Cache::ELITE_LEVEL_CONFIG_KEY, $data);
  129. return $data;
  130. }
  131. /**
  132. * 通过排序获取级别
  133. * @param int $sort
  134. * @return mixed
  135. */
  136. public static function getLevelFromSort(int $sort)
  137. {
  138. static $crownLevels;
  139. if(!$crownLevels){
  140. $crownLevels = self::getFromCache();
  141. $crownLevels = array_column($crownLevels, null, 'sort');
  142. }
  143. return $crownLevels[$sort];
  144. }
  145. /**
  146. * 通过排序获取ID
  147. * @param int $sort
  148. * @return mixed
  149. */
  150. public static function getIdFromSort(int $sort)
  151. {
  152. $level = self::getLevelFromSort($sort);
  153. return $level['ID'];
  154. }
  155. /**
  156. * 获取默认级别
  157. * @return mixed
  158. */
  159. public static function getDefaultLevelId()
  160. {
  161. return self::NO_LEVEL_ID;
  162. }
  163. /**
  164. * 获取星级的排序
  165. * @param $id
  166. * @return mixed
  167. */
  168. public static function getSortById($id)
  169. {
  170. $crownLevels = self::getFromCache();
  171. return $crownLevels[$id]['SORT'] ?? 0;
  172. }
  173. /**
  174. * 通过ID获取星级名称
  175. * @param $levelId
  176. * @return mixed'
  177. */
  178. public static function getNameById($levelId)
  179. {
  180. return self::findOneAsArray('ID = :ID', [':ID' => $levelId])['LEVEL_NAME'] ?? '';
  181. }
  182. }