LuckyGroupGoods.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\LuckyGroup\common\models;
  3. use addons\LuckyGroup\common\enums\LuckyGroupEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\goods\Goods;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_lucky_group_goods".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int $goods_id 商品id
  14. * @property int $activity_id 活动ID
  15. * @property int $join_num 参团人数
  16. * @property int $open_times 成团期数,每开一次算一期
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class LuckyGroupGoods extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_lucky_group_goods}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'goods_id', 'activity_id', 'join_num', 'open_times', 'status', 'created_at', 'updated_at'], 'integer'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => '商城ID',
  47. 'goods_id' => '商品id',
  48. 'activity_id' => '活动ID',
  49. 'join_num' => '参团人数',
  50. 'open_times' => '成团期数,每开一次算一期',
  51. 'status' => '状态[-1:删除;0:禁用;1启用]',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public function getActivity()
  57. {
  58. return $this->hasOne(LuckyGroupActivity::class, ['id' => 'activity_id'])->select('id,attend_user_type,title,virtual_num,attend_award_setting,group_num,random_win_num');
  59. }
  60. public function getGoods()
  61. {
  62. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,original_price,cover_pic');
  63. }
  64. public static function setData(int $mall_id, array $data)
  65. {
  66. if (empty($data['id']) && (empty($data['goods_id']) && empty($data['activity_id']))) {
  67. self::$static_error = '商品 id 和活动 id 不能为空';
  68. return false;
  69. }
  70. if (!empty($data['id'])) {
  71. $model = self::find()
  72. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']])
  73. ->one();
  74. if (empty($model)) {
  75. self::$static_error = '拼团活动商品数据异常';
  76. return false;
  77. }
  78. } elseif (!empty($data['goods_id']) && !empty($data['activity_id'])) {
  79. $model = self::find()
  80. ->where(['mall_id' => $mall_id, 'goods_id' => $data['goods_id'], 'activity_id' => $data['activity_id']])
  81. ->one();
  82. if (empty($model)) {
  83. $model = new self();
  84. $model->loadDefaultValues();
  85. $model->status = StatusEnum::ENABLED;
  86. $model->mall_id = $mall_id;
  87. $model->join_num = 0;
  88. $model->open_times = 0;
  89. unset($data['id']);
  90. }
  91. } else {
  92. $model = new self();
  93. $model->loadDefaultValues();
  94. $model->status = StatusEnum::ENABLED;
  95. $model->mall_id = $mall_id;
  96. $model->join_num = 0;
  97. $model->open_times = 0;
  98. unset($data['id']);
  99. }
  100. foreach ($data as $key => $val) {
  101. $model->$key = $val;
  102. }
  103. if (!$model->save()) {
  104. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  105. return false;
  106. }
  107. return $model;
  108. }
  109. public static function getGoodsList($mall_id,$goods_ids)
  110. {
  111. $where[] = ['mall_id' => $mall_id];
  112. if(!empty($goods_ids)) $where[] = ['goods_id' => $goods_ids];
  113. $where[] = ['status' => StatusEnum::ENABLED];
  114. $params['where'] = $where;
  115. $params['order'] = 'id DESC';
  116. $params['with'] = ['goods' => function ($query) {
  117. $query->where(['status' => StatusEnum::ENABLED]);
  118. }];
  119. $params['limit'] = 10;
  120. $params['select'] = 'id,activity_id,goods_id,created_at';
  121. $list = LuckyGroupGoods::lists($params);
  122. if (!empty($list)) {
  123. $activity_id_arr = array_column($list, 'activity_id');
  124. $activity_arr = LuckyGroupActivity::lists(['where' => [['id' => $activity_id_arr]], 'index' => 'id']);
  125. foreach ($list as &$item) {
  126. $item['img'] = $item['goods']['cover_pic'];
  127. $item['title'] = $item['goods']['goods_name'];
  128. $item['sales_num'] = $item['goods']['sales_num'] ?? 0;
  129. $item['price'] = $item['goods']['price'];
  130. $item['original_price'] = $item['goods']['original_price'];
  131. $item['tips1'] = '';
  132. $item['tips2'] = '';
  133. if (isset($activity_arr[$item['activity_id']])) {
  134. $item['tips1'] = $activity_arr[$item['activity_id']]['group_num'] . '人拼团,' . $activity_arr[$item['activity_id']]['random_win_num'] . '人拼中';
  135. $activity_arr[$item['activity_id']]['attend_user_type'];
  136. $attend_award_setting = json_decode($activity_arr[$item['activity_id']]['attend_award_setting'], true);
  137. $string = '';
  138. $balance_name = \Yii::$app->services->setting->mall->get($mall_id, 'balance.balance_name') ?? '余额';
  139. $score_name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name') ?? '积分';
  140. if (!empty($attend_award_setting['score'])) {
  141. $string .= $attend_award_setting['score'] . $score_name.',';
  142. }
  143. if (!empty($attend_award_setting['balance'])) {
  144. $string .= $attend_award_setting['balance'] . $balance_name;
  145. }
  146. if ($string && !empty($activity_arr[$item['activity_id']]['attend_user_type'])) {
  147. $string = trim($string, ',');
  148. $item['tips2'] = LuckyGroupEnum::getAttendMap($activity_arr[$item['activity_id']]['attend_user_type']) . '立得' . $string;
  149. }
  150. }
  151. $item['original_price'] = $item['goods']['original_price'];
  152. $item['subtitle'] = '';
  153. unset($item['goods']);
  154. }
  155. return $list;
  156. }
  157. }
  158. }