ScoreExpansionRecommendRewardGoods.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace addons\ScoreExpansion\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\goods\Goods;
  5. use Exception;
  6. use RuntimeException;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_score_expansion_recommend_reward_goods".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int $goods_id 商品ID
  14. * @property int $is_alone 是否独立设置
  15. * @property string|null $setting 独立设置
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class ScoreExpansionRecommendRewardGoods extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return 'qimall_addons_score_expansion_recommend_reward_goods';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'goods_id', 'is_alone', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['setting'], 'safe'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'goods_id' => 'Goods ID',
  48. 'is_alone' => 'Is Alone',
  49. 'setting' => 'Setting',
  50. 'status' => 'Status',
  51. 'created_at' => 'Created At',
  52. 'updated_at' => 'Updated At',
  53. ];
  54. }
  55. public function getGoods()
  56. {
  57. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  58. }
  59. /**
  60. * @param array $params
  61. *
  62. * @return bool|self
  63. */
  64. public static function setData(array $params)
  65. {
  66. try {
  67. if (!empty($params['id'])) {
  68. $model = static::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  69. if (!$model) {
  70. throw new RuntimeException('数据不存在或已删除');
  71. }
  72. if ($model->mall_id != $params['mall_id']) {
  73. throw new RuntimeException('不允许操作此数据');
  74. }
  75. } else {
  76. $model = new static();
  77. $model->loadDefaultValues();
  78. }
  79. if (!$model->load($params, '')) {
  80. throw new RuntimeException($model->getErrorMessage());
  81. }
  82. if (!$model->save()) {
  83. throw new RuntimeException($model->getErrorMessage());
  84. }
  85. return $model;
  86. } catch (Exception $e) {
  87. static::$static_error = $e->getMessage();
  88. return false;
  89. }
  90. }
  91. }