BargainBanners.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace addons\Bargain\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_course_banners}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $sort 序号
  11. * @property string $pic_link 图片跳转链接
  12. * @property string $pic_path 图片保存路径
  13. * @property int $is_show 是否展示,1:是,0:不是
  14. * @property int $status 活动状态,1:启用,0:禁用,-1删除
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class BargainBanners extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_bargain_banners}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id'], 'required'],
  34. [['mall_id', 'sort', 'is_show', 'status', 'created_at', 'updated_at'], 'integer'],
  35. [['pic_path'], 'string', 'max' => 255],
  36. ['pic_link', 'string', 'max' => 1000],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'sort' => '序号',
  48. 'pic_link' => '图片跳转链接',
  49. 'pic_path' => '图片保存路径',
  50. 'is_show' => '是否展示,1:是,0:不是',
  51. 'status' => '活动状态,1:启用,0:禁用,-1删除',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public static function setData(int $mall_id, array $data)
  57. {
  58. if (!empty($data['pic_link']) && is_array($data['pic_link'])) {
  59. $data['pic_link'] = json_encode($data['pic_link']);
  60. }
  61. if (!empty($data['id'])) {
  62. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  63. if (empty($model)) {
  64. self::$static_error = '数据异常';
  65. return false;
  66. }
  67. } else {
  68. $model = new self();
  69. $model->loadDefaultValues();
  70. $model->mall_id = $mall_id;
  71. $model->status = StatusEnum::ENABLED;
  72. }
  73. foreach ($data as $key => $val) {
  74. $model->$key = $val;
  75. }
  76. if (!$model->save()) {
  77. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  78. return false;
  79. }
  80. return $model;
  81. }
  82. }