RebateAd.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @copyright: Copyright (c) 2021 广东七件事集团
  5. * @Author: lun
  6. * @Date: 2021-12-31 20:52:51
  7. */
  8. namespace app\plugins\rebate\models;
  9. use app\models\BaseActiveRecord;
  10. use Yii;
  11. use yii\helpers\Json;
  12. /**
  13. * This is the model class for table "qimall_addons_rebate_ad".
  14. *
  15. * @property int $id
  16. * @property int $mall_id 商城id
  17. * @property string $title 标题
  18. * @property string $cover 广告图片
  19. * @property string|null $link 跳转链接
  20. * @property int|null $sort 排序,越大排越前
  21. * @property int $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int $created_at 创建时间
  23. * @property int $updated_at 更新时间
  24. */
  25. class RebateAd extends BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return 'qimall_addons_rebate_ad';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id'], 'required'],
  41. [['mall_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['title'], 'string', 'max' => 20],
  43. [['cover', 'link'], 'string', 'max' => 255],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => 'Mall ID',
  54. 'title' => 'Title',
  55. 'cover' => 'Cover',
  56. 'link' => 'Link',
  57. 'sort' => 'Sort',
  58. 'status' => 'Status',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. ];
  62. }
  63. /**
  64. * 保存数据
  65. *
  66. * @param [type] $mall_id
  67. * @param array $params
  68. * @return void
  69. */
  70. public static function setData($mall_id, array $params){
  71. try{
  72. if(!empty($params['id'])){
  73. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['=','status', 1]]);
  74. if(empty($model)) throw new \Exception('活动不存在或已删除');
  75. }else{
  76. $model = new self();
  77. $model->loadDefaultValues();
  78. $model->mall_id = $mall_id;
  79. }
  80. // 将内容转为json字符串
  81. !empty($params['link']) && $params['link'] = Json::encode($params['link']);
  82. if(!$model->load($params,'') || !$model->save()) throw new \yii\db\Exception($model->getErrorMessage());
  83. return $model;
  84. }catch(\Exception $e){
  85. self::$error = $e->getMessage();
  86. return false;
  87. }
  88. }
  89. }