EliteLevel.php 5.3 KB

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