BlindBoxCate.php 3.5 KB

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