LuckyGroupCategory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace addons\LuckyGroup\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_lucky_group_category".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $sort 序号
  12. * @property string $title 图片标题
  13. * @property string $pic_path 图片保存路径
  14. * @property string $bgcolor 背景色
  15. * @property string $pic_adv 详情广告图
  16. * @property string $adv_link 广告图片跳转链接
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class LuckyGroupCategory extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_lucky_group_category}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id'], 'required'],
  37. [['mall_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['title', 'pic_path', 'pic_adv'], 'string', 'max' => 255],
  39. [['bgcolor'], 'string', 'max' => 100],
  40. ['adv_link', 'string', 'max' => 1000]
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => 'Mall ID',
  51. 'sort' => '序号',
  52. 'title' => '图片标题',
  53. 'pic_path' => '图片保存路径',
  54. 'bgcolor' => '背景色',
  55. 'pic_adv' => '详情广告图',
  56. 'adv_link' => '广告图片跳转链接',
  57. 'status' => '状态[-1:删除;0:禁用;1启用]',
  58. 'created_at' => 'Created At',
  59. 'updated_at' => 'Updated At',
  60. ];
  61. }
  62. public static function setData(int $mall_id, array $data)
  63. {
  64. if (!empty($data['id'])) {
  65. $model = self::findOne(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $data['id']]);
  66. if (empty($model)) {
  67. self::$static_error = '分类数据异常';
  68. return false;
  69. }
  70. } else {
  71. $model = new self();
  72. $model->loadDefaultValues();
  73. $model->status = StatusEnum::ENABLED;
  74. $model->mall_id = $mall_id;
  75. unset($data['id']);
  76. }
  77. foreach ($data as $key => $val) {
  78. if ('adv_link' === $key && !empty($val) && is_array($val)) {
  79. $val = json_encode($val);
  80. }
  81. $model->$key = $val;
  82. }
  83. if (!$model->save()) {
  84. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  85. return false;
  86. }
  87. return $model;
  88. }
  89. public static function getCateListByDiy($mall_id, $post)
  90. {
  91. $where[] = ['mall_id' => $mall_id];
  92. $where[] = ['status' => StatusEnum::ENABLED];
  93. if (!empty($post['keyword'])) $where[] = ['like', 'title', trim($post['keyword']) . '%', false];
  94. $params['limit'] = 10;
  95. $params['where'] = $where;
  96. $params['select'] = 'id,title,created_at';
  97. $list = self::listPage($params);
  98. $first = [
  99. 'created_at' => 0,
  100. 'id' => 0,
  101. 'is_list' => 0,
  102. 'label' => "全部",
  103. 'name' => "全部",
  104. 'pid' => 0,
  105. 'title' => "全部",
  106. 'value' => 1,
  107. ];
  108. if (!empty($list['list'])) {
  109. foreach ($list['list'] as &$item) {
  110. $item['is_list'] = 0;
  111. $item['label'] = $item['title'];
  112. $item['title'] = $item['title'];
  113. $item['value'] = $item['id'];
  114. $item['pid'] = 0;
  115. }
  116. }
  117. array_unshift($list['list'],$first);
  118. return $list;
  119. }
  120. }