FreeTemplate.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. [['country_id'], 'unique'],
  34. ];
  35. }
  36. public function behaviors()
  37. {
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'id',
  46. 'country_id' => '国家ID',
  47. 'freight' => '运费',
  48. 'free_shipping' => '免运费阈值',
  49. 'currency_id' => '币种ID',
  50. 'created_at' => '创建时间',
  51. 'updated_at' => '更新时间',
  52. ];
  53. }
  54. public function country()
  55. {
  56. return $this->hasOne(Countries::class, ['id' => 'country_id']);
  57. }
  58. public function currency()
  59. {
  60. return $this->hasOne(Currency::class, ['id' => 'currency_id']);
  61. }
  62. /**
  63. * 从缓存获取信息
  64. * @return array|mixed|\yii\db\ActiveRecord[]
  65. */
  66. public static function getFromCache()
  67. {
  68. $data = Yii::$app->cache->get(Cache::FREE_TEMPLATE_KEY);
  69. if (!$data) {
  70. // 获取信息
  71. $data = self::getAllData();
  72. Yii::$app->cache->set(Cache::FREE_TEMPLATE_KEY, $data);
  73. }
  74. return $data;
  75. }
  76. /**
  77. * @return array|\yii\db\ActiveRecord[]
  78. */
  79. public static function getAllData()
  80. {
  81. return static::find()->indexBy('id')->orderBy('id ASC')->asArray()->all();
  82. }
  83. /**
  84. * 更新缓存
  85. * @return array|\yii\db\ActiveRecord[]
  86. */
  87. public static function updateToCache()
  88. {
  89. // 获取配置
  90. $data = self::getAllData();
  91. Yii::$app->cache->set(Cache::FREE_TEMPLATE_KEY, $data);
  92. return $data;
  93. }
  94. /**
  95. * 通过$countryId获取
  96. * @param string $countryId
  97. * @return array|null
  98. */
  99. public static function getByCountryId(string $countryId): ?array
  100. {
  101. return self::findOneAsArray('country_id=:country_id', [':country_id' => $countryId]);
  102. }
  103. public static function deleteOne(string $countryId): bool
  104. {
  105. return self::deleteAll('country_id=:country_id', [':country_id' => $countryId]);
  106. }
  107. }