FreeTemplate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%FREE_TEMPLATE}}".
  7. *
  8. * @property int id
  9. * @property string country_id 国家ID
  10. * @property double freight 运费
  11. * @property double free_shipping 免运费阈值
  12. * @property int currency_id 币种ID
  13. * @property int created_at 订单创建时间
  14. * @property int updated_at 订单创建人
  15. */
  16. class FreeTemplate extends \common\components\ActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%FREE_TEMPLATE}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['country_id', 'freight', 'free_shipping', 'currency_id'], 'required'],
  32. [['freight', 'free_shipping', 'currency_id'], 'number'],
  33. [['SN'], 'unique'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'country_id' => '国家ID',
  44. 'freight' => '运费',
  45. 'free_shipping' => '免运费阈值',
  46. 'currency_id' => '币种ID',
  47. 'created_at' => '创建时间',
  48. 'updated_at' => '更新时间',
  49. ];
  50. }
  51. public function country()
  52. {
  53. return $this->hasOne(Countries::class, ['ID' => 'country_id']);
  54. }
  55. public function currency()
  56. {
  57. return $this->hasOne(Currency::class, ['ID' => 'currency_id']);
  58. }
  59. /**
  60. * 从缓存获取信息
  61. * @return array|mixed|\yii\db\ActiveRecord[]
  62. */
  63. public static function getFromCache()
  64. {
  65. $data = Yii::$app->cache->get(Cache::FREE_TEMPLATE_KEY);
  66. if (!$data) {
  67. // 获取信息
  68. $data = static::find()->orderBy('id ASC')->indexBy('id')->asArray()->all();
  69. Yii::$app->cache->set(Cache::FREE_TEMPLATE_KEY, $data);
  70. }
  71. return $data;
  72. }
  73. /**
  74. * @return array|\yii\db\ActiveRecord[]
  75. */
  76. public static function getAllData()
  77. {
  78. return static::find()->indexBy('id')->orderBy('id ASC')->asArray()->all();
  79. }
  80. /**
  81. * 更新缓存
  82. * @return array|\yii\db\ActiveRecord[]
  83. */
  84. public static function updateToCache()
  85. {
  86. // 获取配置
  87. $data = static::find()->orderBy('id ASC')->asArray()->indexBy('id')->all();
  88. Yii::$app->cache->set(Cache::FREE_TEMPLATE_KEY, $data);
  89. return $data;
  90. }
  91. /**
  92. * 通过$countryId获取
  93. * @param string $countryId
  94. * @return array|null
  95. */
  96. public static function getByCountryId(string $countryId): ?array
  97. {
  98. return self::findOneAsArray('country_id=:country_id', [':country_id' => $countryId]);
  99. }
  100. }