RebateSetting.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @copyright: Copyright (c) 2021 广东七件事集团
  5. * @Author: lun
  6. * @Date: 2021-12-31 23:43:05
  7. */
  8. namespace app\plugins\rebate\models;
  9. use app\models\BaseActiveRecord;
  10. use Yii;
  11. /**
  12. * This is the model class for table "qimall_addons_rebate_setting".
  13. *
  14. * @property int $id
  15. * @property int $mall_id
  16. * @property string $key
  17. * @property string $value
  18. * @property int $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int $created_at 创建时间
  20. * @property int $updated_at 修改时间
  21. */
  22. class RebateSetting extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return 'qimall_addons_rebate_setting';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'key', 'value'], 'required'],
  38. [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['value'], 'string'],
  40. [['key'], 'string', 'max' => 255],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => 'Mall ID',
  51. 'key' => 'Key',
  52. 'value' => 'Value',
  53. 'status' => 'Status',
  54. 'created_at' => 'Created At',
  55. 'updated_at' => 'Updated At',
  56. ];
  57. }
  58. /**
  59. * 字符串转数字
  60. *
  61. * @param [type] $key
  62. * @param [type] $str
  63. * @return void
  64. */
  65. public static function strToNumber($key, $str)
  66. {
  67. $default = ['title', 'settlement_method', 'explain'];
  68. if (in_array($key, $default)) {
  69. return round($str, 2);
  70. }
  71. return $str;
  72. }
  73. /**
  74. * 保存数据
  75. *
  76. * @return void
  77. */
  78. public static function setData($mall_id, $params)
  79. {
  80. try {
  81. $setting_list = [];
  82. foreach ($params as $index => $item) {
  83. $setting_list[] = [
  84. 'key' => $index,
  85. 'value' => $item
  86. ];
  87. }
  88. foreach ($setting_list as $item) {
  89. $setting = self::findOne(['mall_id' => $mall_id, 'key' => $item['key']]);
  90. if (!$setting) {
  91. $setting = new self();
  92. }
  93. $setting->key = $item['key'];
  94. $setting->value = $item['value'];
  95. $setting->mall_id = $mall_id;
  96. $setting->status = 1;
  97. $setting->save();
  98. }
  99. return true;
  100. } catch (\Exception $e) {
  101. self::$error = $e->getMessage();
  102. return false;
  103. }
  104. }
  105. /**
  106. * @param null $key
  107. * @return bool|string|void
  108. */
  109. public static function getValueByKey($key = null, $mall_id = null)
  110. {
  111. if (!$key) {
  112. return false;
  113. }
  114. if (!$mall_id) {
  115. $mall_id = Yii::$app->mall->id;
  116. }
  117. if (!$mall_id) {
  118. return false;
  119. }
  120. $model = self::findOne(['mall_id' => $mall_id, 'status' => 1, 'key' => $key]);
  121. if ($model) {
  122. return $model->value;
  123. }
  124. return false;
  125. }
  126. }