BargainCate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace addons\Bargain\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_bargain_cate".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property string $name 名称
  12. * @property int $status 状态[-1:删除;0:禁用;1启用]
  13. * @property int $created_at
  14. * @property int $updated_at
  15. */
  16. class BargainCate extends BaseActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%addons_bargain_cate}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['mall_id', 'name'], 'required'],
  32. [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
  33. [['name'], 'string', 'max' => 20],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => '商城id',
  44. 'name' => '名称',
  45. 'status' => '状态[-1:删除;0:禁用;1启用]',
  46. 'created_at' => 'Created At',
  47. 'updated_at' => 'Updated At',
  48. ];
  49. }
  50. public static function setData(array $params)
  51. {
  52. try {
  53. if (!empty($params['id'])) {
  54. $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  55. if (empty($model)) throw new \Exception('服务不存在或者已删除');
  56. } else {
  57. $model = new self();
  58. }
  59. $model->loadDefaultValues();
  60. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  61. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  62. return $model;
  63. } catch (\Exception $e) {
  64. self::$static_error = $e->getMessage();
  65. return false;
  66. }
  67. }
  68. public static function getCateListByDiy($mall_id, $post)
  69. {
  70. $where[] = ['mall_id' => $mall_id];
  71. $where[] = ['status' => StatusEnum::ENABLED];
  72. if (!empty($post['keyword'])) $where[] = ['like', 'name', trim($post['keyword']) . '%', false];
  73. $params['limit'] = 10;
  74. $params['where'] = $where;
  75. $params['select'] = 'id,name,created_at';
  76. $list = self::listPage($params);
  77. $first = [
  78. 'created_at' => 0,
  79. 'id' => 0,
  80. 'is_list' => 0,
  81. 'label' => "全部",
  82. 'name' => "全部",
  83. 'pid' => 0,
  84. 'title' => "全部",
  85. 'value' => 1,
  86. ];
  87. if (!empty($list['list'])) {
  88. foreach ($list['list'] as &$item) {
  89. $item['is_list'] = 0;
  90. $item['label'] = $item['name'];
  91. $item['title'] = $item['name'];
  92. $item['value'] = $item['id'];
  93. $item['pid'] = 0;
  94. }
  95. }
  96. array_unshift($list['list'],$first);
  97. return $list;
  98. }
  99. }