SettingLog.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use Yii;
  4. use yii\helpers\Json;
  5. /**
  6. * This is the model class for table "{{%addons_star_chain_setting_log}}".
  7. *
  8. * @property int $id
  9. * @property int|null $mall_id
  10. * @property string|null $before_setting 更新前配置
  11. * @property int|null $created_at 创建时间
  12. */
  13. class SettingLog extends BaseModel
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static function tableName()
  19. {
  20. return '{{%addons_star_chain_setting_log}}';
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [['mall_id', 'created_at'], 'integer'],
  29. [['before_setting'], 'string'],
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function attributeLabels()
  36. {
  37. return [
  38. 'id' => 'ID',
  39. 'mall_id' => 'Mall ID',
  40. 'before_setting' => 'Before Setting',
  41. 'created_at' => 'Created At',
  42. ];
  43. }
  44. /**
  45. * 添加setting修改日志
  46. *
  47. * @param Setting $setting
  48. * @return boolean
  49. */
  50. public static function addSettingLog(Setting $setting)
  51. {
  52. // 修改前设置
  53. $old_setting = $setting->oldAttributes;
  54. // 新增为空时不操作
  55. if (empty($old_setting)) return true;
  56. // before_setting
  57. $before_setting = [
  58. 'app_key' => $old_setting['app_key'],
  59. 'app_secret' => $old_setting['app_secret'],
  60. 'auto_update' => Json::decode($old_setting['auto_update']),
  61. 'sell' => Json::decode($old_setting['sell']),
  62. 'cost' => Json::decode($old_setting['cost']),
  63. 'original' => Json::decode($old_setting['original']),
  64. ];
  65. $data = [
  66. 'mall_id' => $old_setting['mall_id'],
  67. 'before_setting' => Json::encode($before_setting),
  68. ];
  69. $model = new self();
  70. return ($model->load($data, '') && $model->save());
  71. }
  72. }