FavoriteForm.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\purchase;
  8. use app\models\PurchaseFavorite;
  9. use yii\base\Model;
  10. use yii\data\Pagination;
  11. use app\modules\admin\models\PlatformForm;
  12. class FavoriteForm extends Model
  13. {
  14. public $store_id;
  15. public $user_id = 0;
  16. public $saas_id = 0;
  17. public $goods_ids;
  18. public $type = 0;
  19. public $page = 1;
  20. public $limit = 999;
  21. public function rules()
  22. {
  23. return [
  24. [['type'], 'default', 'value' => 0],
  25. ['type', 'in', 'range' => [PurchaseFavorite::FAVORITE_GOODS, PurchaseFavorite::FAVORITE_SHOP]],
  26. [['goods_ids', 'page', 'limit', 'saas_id', 'store_id'], 'safe'],
  27. ];
  28. }
  29. public function search()
  30. {
  31. if (!$this->validate()) {
  32. return [
  33. 'code' => 1,
  34. 'msg' => $this->getErrorSummary(false)[0]
  35. ];
  36. }
  37. if ($this->type == PurchaseFavorite::FAVORITE_GOODS) {
  38. $query = PurchaseFavorite::find()->where(['type' => PurchaseFavorite::FAVORITE_GOODS, 'is_delete' => 0, 'saas_id' => $this->saas_id]);
  39. $count = $query->count();
  40. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  41. $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
  42. foreach ($list as $i => $favorite) {
  43. $form = new PlatformForm();
  44. $form->id = $favorite['goods_id'];
  45. $glist = $form->goodsInfo();
  46. $goods = '';
  47. if($glist['code'] == 0 && $glist['data']['count'] == 1){
  48. $goods = $glist['data']['list'][0];
  49. }
  50. if (!$goods) {
  51. continue;
  52. }
  53. if ($goods->is_negotiable) {
  54. $goods->price = Goods::GOODS_NEGOTIABLE;
  55. }
  56. $list[$i]['goods'] = $goods;
  57. }
  58. $newList = [];
  59. foreach ($list as $i => $v){
  60. $supid = $v['goods']['supplier_id'];
  61. $newList[$supid]['supplier'] = $v['goods']['supplier'];
  62. $newList[$supid]['goods_list'][] = $v;
  63. }
  64. return [
  65. 'code' => 0,
  66. 'data' => (object)[
  67. 'row_count' => $count,
  68. 'page_count' => $pagination->pageCount,
  69. 'list' => array_values($newList),
  70. ],
  71. ];
  72. }
  73. }
  74. public function add()
  75. {
  76. if (!$this->validate()) {
  77. return [
  78. 'code' => 1,
  79. 'msg' => $this->getErrorSummary(false)[0]
  80. ];
  81. }
  82. $errors = 0;
  83. foreach ($this->goods_ids as $gid) {
  84. $existFavorite = PurchaseFavorite::findOne([
  85. 'saas_id' => $this->saas_id,
  86. 'goods_id' => $gid,
  87. 'type' => $this->type,
  88. ]);
  89. if ($existFavorite) {
  90. $existFavorite->is_delete = 0;
  91. if (!$existFavorite->save()) {
  92. $errors++;
  93. }
  94. }else{
  95. $favorite = new PurchaseFavorite();
  96. $favorite->store_id = $this->store_id;
  97. $favorite->user_id = $this->user_id;
  98. $favorite->saas_id = $this->saas_id;
  99. $favorite->goods_id = $gid;
  100. $favorite->type = $this->type;
  101. $favorite->created_at = time();
  102. if (!$favorite->save()) {
  103. $errors++;
  104. }
  105. }
  106. }
  107. if($errors == count($this->goods_ids)){
  108. return [
  109. 'code' => 1,
  110. 'msg' => 'fail',
  111. ];
  112. }
  113. if($errors){
  114. return [
  115. 'code' => 0,
  116. 'msg' => '收藏失败数量:' . $errors,
  117. ];
  118. }else{
  119. return [
  120. 'code' => 0,
  121. 'msg' => 'success',
  122. ];
  123. }
  124. }
  125. public function remove()
  126. {
  127. $res = PurchaseFavorite::updateAll(['is_delete' => 1], ['saas_id' => $this->saas_id, 'goods_id' => $this->goods_ids, 'is_delete' => 0]);
  128. if ($res) {
  129. return [
  130. 'code' => 0,
  131. 'msg' => '删除成功',
  132. ];
  133. } else {
  134. return [
  135. 'code' => 1,
  136. 'msg' => 'fail',
  137. 'err' => $res
  138. ];
  139. }
  140. }
  141. }