BlindBoxGoods.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace addons\BlindBox\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\goods\Goods;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_blind_box_goods".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $goods_id 商品id
  13. * @property int $cate_id 分类id
  14. * @property float $contribution 贡献值
  15. * @property int $contribution_percent 贡献值类型:1:百分比;0:固定值
  16. * @property float $profit 利润值
  17. * @property int $profit_percent 利润值类型:1:百分比;0:固定值
  18. * @property int $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int $created_at
  20. * @property int $updated_at
  21. */
  22. class BlindBoxGoods extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_blind_box_goods}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'goods_id', 'cate_id', 'contribution_percent', 'profit_percent', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['contribution', 'profit'], 'number'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => '商城id',
  49. 'goods_id' => '商品id',
  50. 'cate_id' => '分类id',
  51. 'contribution' => '贡献值',
  52. 'contribution_percent' => '贡献值类型:1:百分比;0:固定值',
  53. 'profit' => '利润值',
  54. 'profit_percent' => '利润值类型:1:百分比;0:固定值',
  55. 'status' => '状态[-1:删除;0:禁用;1启用]',
  56. 'created_at' => 'Created At',
  57. 'updated_at' => 'Updated At',
  58. ];
  59. }
  60. // 关联模型
  61. public function getGoods()
  62. {
  63. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  64. }
  65. public static function setData(array $params)
  66. {
  67. $t = Yii::$app->db->beginTransaction();
  68. try {
  69. if (!empty($params['id'])) {
  70. $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  71. if (empty($model)) throw new \Exception('服务不存在或已删除');
  72. } else {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. }
  76. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  77. $res = $model->save();
  78. if ($res == false) throw new \Exception($model->getErrorMessage());
  79. BlindBoxCate::updateGoodsNum($model['cate_id']);
  80. $t->commit();
  81. return $model;
  82. } catch (\Exception $e) {
  83. $t->rollBack();
  84. self::$static_error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. public static function addData(array $params)
  89. {
  90. $t = Yii::$app->db->beginTransaction();
  91. try {
  92. foreach ($params['goods_id'] as $goods_id) {
  93. $where = ['goods_id' => $goods_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  94. if (!empty($params['mall_id'])) $where['mall_id'] = $params['mall_id'];
  95. $model = self::findOne($where);
  96. if (empty($model)) $model = new self();
  97. $data['goods_id'] = (int)$goods_id;
  98. $data['mall_id'] = (int)$params['mall_id'];
  99. $data['contribution_percent'] = $params['contribution_percent'];
  100. $data['contribution'] = $params['contribution'];
  101. $data['profit_percent'] = $params['profit_percent'];
  102. $data['profit'] = $params['profit'];
  103. $data['cate_id'] = $params['cate_id'];
  104. $model->loadDefaultValues();
  105. if (!$model->load($data, '')) throw new \Exception($model->getErrorMessage());
  106. $res = $model->save();
  107. if ($res == false) throw new \Exception($model->getErrorMessage());
  108. }
  109. BlindBoxCate::updateGoodsNum($params['cate_id']);
  110. $t->commit();
  111. return true;
  112. } catch (\Exception $e) {
  113. $t->rollBack();
  114. self::$static_error = $e->getMessage();
  115. return false;
  116. }
  117. }
  118. public static function getGoodsList($mall_id, $goods_ids)
  119. {
  120. $where[] = ['mall_id' => $mall_id];
  121. if (!empty($goods_ids)) $where[] = ['goods_id' => $goods_ids];
  122. $where[] = ['status' => StatusEnum::ENABLED];
  123. $params['where'] = $where;
  124. $params['order'] = 'id DESC';
  125. $params['with'] = ['goods' => function ($query) {
  126. $query->where(['status' => StatusEnum::ENABLED]);
  127. }];
  128. $params['limit'] = 10;
  129. $params['select'] = 'id,goods_id,created_at';
  130. $list = BlindBoxGoods::lists($params);
  131. if (!empty($list)) {
  132. foreach ($list as &$item) {
  133. $item['goods_name'] = $item['cover_pic'] = '';
  134. $item['price']= 0 ;
  135. if(isset($item['goods'])){
  136. $item['goods_name'] = $item['goods']['goods_name'];
  137. $item['cover_pic'] = $item['goods']['cover_pic'];
  138. $item['price'] = $item['goods']['price'];
  139. unset($item['goods']);
  140. }
  141. }
  142. }
  143. return $list;
  144. }
  145. public static function del($mall_id, $ids)
  146. {
  147. $t = Yii::$app->db->beginTransaction();
  148. try {
  149. $res = BlindBoxGoods::updateAll(['status' => StatusEnum::DELETE], ['id' => $ids, 'mall_id' => $mall_id]);
  150. if ($res === false) throw new \Exception(BlindBoxGoods::getStaticError());
  151. $list = BlindBoxGoods::find()->where(['id' => $ids, 'mall_id' => $mall_id])->select('id,cate_id')->all();
  152. foreach ($list as $item) {
  153. BlindBoxCate::updateGoodsNum($item['cate_id']);
  154. }
  155. $t->commit();
  156. return true;
  157. } catch (\Exception $e) {
  158. $t->rollBack();
  159. self::$static_error = $e->getMessage();
  160. return false;
  161. }
  162. }
  163. public static function getGoodsListByDiy($mall_id,$post){
  164. $where[] = ['mall_id' => $mall_id];
  165. $where[] = ['status' => StatusEnum::ENABLED];
  166. if (!empty($post['keyword'])) {
  167. $goods_list = Goods::lists(['where' => [['mall_id' => $mall_id], ['like', 'goods_name', trim($post['keyword']) . '%', false], ['status' => StatusEnum::ENABLED]], 'select' => 'id']);
  168. $goods_id_arr = array_column($goods_list, 'id');
  169. $where[] = ['goods_id' => $goods_id_arr];
  170. }
  171. $params['where'] = $where;
  172. $params['order'] = 'id DESC';
  173. $params['with'] = ['goods' => function ($query) use($mall_id){
  174. $query->where(['mall_id'=>$mall_id,'status' => StatusEnum::ENABLED]);
  175. }];
  176. $params['limit'] = 10;
  177. $params['select']='id,goods_id,created_at';
  178. $list = BlindBoxGoods::listPage($params,false,true);
  179. if(!empty($list['list'])){
  180. foreach ($list['list'] as &$item){
  181. $item['img'] = $item['goods']['cover_pic'];
  182. $item['title'] = $item['goods']['goods_name'];
  183. $item['sales_num'] = $item['goods']['sales_num'];
  184. $item['price'] = '';
  185. $item['original_price'] = '';
  186. $item['subtitle'] = '';
  187. unset($item['goods']);
  188. }
  189. }
  190. return $list;
  191. }
  192. }