FoodAddCartForm.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Cart;
  9. use app\models\FoodCart;
  10. use app\models\FoodFlag;
  11. use app\models\Goods;
  12. use yii\base\BaseObject;
  13. use yii\base\Model;
  14. class FoodAddCartForm extends Model
  15. {
  16. public $user_id;
  17. public $store_id;
  18. public $goods_id;
  19. public $attr;
  20. public $food_ext_goods;
  21. public $num;
  22. public $md_id;
  23. public $flag_id;
  24. public function rules()
  25. {
  26. return [
  27. [['goods_id', 'attr', 'num', 'md_id'], 'required'],
  28. [['goods_id', 'num', 'flag_id'], 'integer'],
  29. [['num'], 'integer', 'min' => 1],
  30. [['food_ext_goods'], 'safe'],
  31. ];
  32. }
  33. public function save()
  34. {
  35. try {
  36. if (!$this->validate()) {
  37. return [
  38. 'code' => 1,
  39. 'msg' => $this->getErrorSummary(false)[0]
  40. ];
  41. }
  42. $goods = Goods::findOne([
  43. 'id' => $this->goods_id,
  44. 'store_id' => $this->store_id,
  45. 'is_delete' => 0,
  46. 'status' => 1,
  47. ]);
  48. if (!$goods) {
  49. return [
  50. 'code' => 1,
  51. 'msg' => '商品不存在或已下架',
  52. ];
  53. }
  54. $this->attr = json_decode($this->attr, true);
  55. $attr = [];
  56. foreach ($this->attr as $item) {
  57. if (!empty($item['attr_id'])) {
  58. $attr[] = intval($item['attr_id']);
  59. }
  60. }
  61. sort($attr);
  62. if ($goods->use_attr) {
  63. $attrs = $goods->attr;
  64. if (!empty($attrs)) {
  65. $attrs = json_decode($attrs, true);
  66. foreach ($attrs as $item) {
  67. $attr_list = array_column($item['attr_list'], 'attr_id');
  68. sort($attr_list);
  69. if (!array_diff($attr_list, $attr)) {
  70. if ($item['num'] <= 0) {
  71. return [
  72. 'code' => 1,
  73. 'msg' => '商品规格数量不足',
  74. ];
  75. }
  76. }
  77. }
  78. }
  79. }
  80. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  81. $this->food_ext_goods = (array)json_decode($this->food_ext_goods, true);
  82. $food_ext_goods = json_encode($this->food_ext_goods, JSON_UNESCAPED_UNICODE);
  83. $food_flag = FoodFlag::findOne($this->flag_id);
  84. $cart = FoodCart::findOne([
  85. 'goods_id' => $this->goods_id,
  86. 'user_id' => $this->user_id,
  87. 'is_delete' => 0,
  88. 'attr' => $attr,
  89. 'food_ext_goods' => $food_ext_goods,
  90. 'flag_id' => $this->flag_id
  91. ]);
  92. if (!$cart) {
  93. $cart = new FoodCart();
  94. $cart->goods_id = $this->goods_id;
  95. $cart->user_id = $this->user_id;
  96. $cart->num = 0;
  97. $cart->flag_id = $this->flag_id;
  98. $cart->created_at = time();
  99. $cart->is_delete = 0;
  100. $cart->attr = $attr;
  101. $cart->food_ext_goods = $food_ext_goods;
  102. }
  103. $cart->status = 1;
  104. if ($food_flag->user_id == $this->user_id || $food_flag->type == 1) {
  105. $cart->status = 1;
  106. }
  107. $cart->num += $this->num;
  108. $cart_num = FoodCart::find()->where(['flag_id' => $this->flag_id, 'goods_id' => $this->goods_id, 'is_delete' => 0])->sum('num');
  109. if (($cart_num + $cart->num) > $goods->goods_num) {
  110. return [
  111. 'code' => 1,
  112. 'msg' => '库存不足',
  113. ];
  114. }
  115. if ($cart->save()) {
  116. return [
  117. 'code' => 0,
  118. 'msg' => '添加购物车成功',
  119. 'data' => [
  120. 'flag_id' => $this->flag_id
  121. ],
  122. ];
  123. } else {
  124. return ['code' => 1, 'msg' => $cart->errors];
  125. }
  126. } catch (\Exception $e) {
  127. return [
  128. 'code' => 1,
  129. 'msg' => $e->getMessage() . 'line=' . $e->getLine()
  130. ];
  131. }
  132. }
  133. }