FoodAddCartForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  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 $saas_id;
  18. public $store_id;
  19. public $goods_id;
  20. public $attr;
  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. ];
  31. }
  32. public function save()
  33. {
  34. try {
  35. if (!$this->validate()) {
  36. return [
  37. 'code' => 1,
  38. 'msg' => $this->getErrorSummary(false)[0]
  39. ];
  40. }
  41. $goods = Goods::findOne([
  42. 'id' => $this->goods_id,
  43. 'store_id' => $this->store_id,
  44. 'is_delete' => 0,
  45. 'status' => 1,
  46. ]);
  47. if (!$goods) {
  48. return [
  49. 'code' => 1,
  50. 'msg' => '商品不存在或已下架',
  51. ];
  52. }
  53. $this->attr = json_decode($this->attr, true);
  54. $attr = [];
  55. foreach ($this->attr as $item) {
  56. if (!empty($item['attr_id'])) {
  57. $attr[] = intval($item['attr_id']);
  58. }
  59. }
  60. sort($attr);
  61. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  62. $food_flag = FoodFlag::findOne($this->flag_id);
  63. $cart = FoodCart::findOne([
  64. 'goods_id' => $this->goods_id,
  65. 'user_id' => $this->user_id,
  66. 'saas_id' => $this->saas_id,
  67. 'is_delete' => 0,
  68. 'attr' => $attr,
  69. 'flag_id' => $this->flag_id
  70. ]);
  71. if (!$cart) {
  72. $cart = new FoodCart();
  73. $cart->goods_id = $this->goods_id;
  74. $cart->user_id = $this->user_id;
  75. $cart->saas_id = $this->saas_id;
  76. $cart->num = 0;
  77. $cart->flag_id = $this->flag_id;
  78. $cart->created_at = time();
  79. $cart->is_delete = 0;
  80. $cart->attr = $attr;
  81. }
  82. $cart->status = 1;
  83. if ($food_flag->user_id == $this->user_id || $food_flag->type == 1) {
  84. $cart->status = 1;
  85. }
  86. $cart->num += $this->num;
  87. $cart_num = FoodCart::find()->where(['flag_id' => $this->flag_id, 'goods_id' => $this->goods_id, 'is_delete' => 0])->sum('num');
  88. if (($cart_num + $cart->num) > $goods->goods_num) {
  89. return [
  90. 'code' => 1,
  91. 'msg' => '库存不足',
  92. ];
  93. }
  94. if ($cart->save()) {
  95. return [
  96. 'code' => 0,
  97. 'msg' => '添加购物车成功',
  98. 'data' => [
  99. 'flag_id' => $this->flag_id
  100. ],
  101. ];
  102. } else {
  103. return ['code' => 1, 'msg' => $cart->errors];
  104. }
  105. } catch (\Exception $e) {
  106. return [
  107. 'code' => 1,
  108. 'msg' => $e->getMessage() . 'line=' . $e->getLine()
  109. ];
  110. }
  111. }
  112. }