GoodsForm.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\integral\models\form;
  8. use app\models\Attr;
  9. use app\models\AttrGroup;
  10. use app\models\Goods;
  11. use app\plugins\integral\models\SaasIntegralGoods;
  12. use yii\base\Model;
  13. use yii\helpers\Json;
  14. class GoodsForm extends Model
  15. {
  16. public $store_id;
  17. public $attr;
  18. public $goods_id;
  19. public $name;
  20. public $integral;
  21. public $goods_count;
  22. public $goods_num;
  23. public $sales;
  24. public $sort;
  25. public $user_num;
  26. public $cat_id;
  27. public $detail;
  28. public function rules() {
  29. return [
  30. ['attr', function ($attr, $params) {
  31. if (!$attr) {
  32. $this->addError($attr, "{$attr}数据格式错误。");
  33. }
  34. }],
  35. [['goods_id'], 'required'],
  36. [['cat_id', 'goods_id', 'integral', 'goods_count', 'sales', 'sort', 'user_num', 'goods_num'], 'integer'],
  37. [['integral', 'goods_count'], 'integer', 'min' => 1],
  38. [['name'], 'string', 'max' => 250],
  39. [['name', 'detail'], 'trim'],
  40. [['detail'], 'string']
  41. ];
  42. }
  43. public function save() {
  44. if (!$this->validate()) {
  45. return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
  46. }
  47. if (empty($this->detail)) {
  48. return [
  49. 'code' => 1,
  50. 'msg' => '请填写商品详情'
  51. ];
  52. }
  53. if (empty($this->goods_id)) {
  54. return [
  55. 'code' => 1,
  56. 'msg' => '请选择商品'
  57. ];
  58. }
  59. if (empty($this->attr)) {
  60. return [
  61. 'code' => 1,
  62. 'msg' => '商品规格数据为空'
  63. ];
  64. }
  65. if ($this->goods_count > $this->goods_num) {
  66. return [
  67. 'code' => 1,
  68. 'msg' => '积分商品规格库存不能大于本商品规格库存'
  69. ];
  70. }
  71. if (intval($this->integral) < 1) {
  72. return [
  73. 'code' => 1,
  74. 'msg' => '设置积分非法'
  75. ];
  76. }
  77. // 检测是否有重复商品
  78. $exist_goods = SaasIntegralGoods::find()->where(['store_id' => $this->store_id, 'goods_id' => $this->goods_id, 'is_delete' => 0])->asArray()->all();
  79. if (!empty($exist_goods) && is_array($exist_goods)) {
  80. $ids = array_column($this->attr, 'attr_id');
  81. $exist_goods_attr = [];
  82. foreach ($exist_goods as $goods) {
  83. $exist_goods_attr[] = json_decode($goods['attr'], true);
  84. foreach ($exist_goods_attr as $value) {
  85. $exist_goods_id = array_column($value, 'attr_id');
  86. if ($exist_goods_id == $ids) {
  87. return [
  88. 'code' => 1,
  89. 'msg' => '已添加同规格商品'
  90. ];
  91. }
  92. }
  93. }
  94. }
  95. $t = \Yii::$app->db->beginTransaction();
  96. $integralGoods = new SaasIntegralGoods();
  97. $integralGoods->store_id = $this->store_id;
  98. $integralGoods->attr = json_encode($this->attr);
  99. $integralGoods->goods_id = $this->goods_id;
  100. $integralGoods->name = empty($this->name) ? Goods::findOne($this->goods_id)->name : $this->name;
  101. $integralGoods->goods_count = $this->goods_count;
  102. $integralGoods->integral = $this->integral;
  103. $integralGoods->user_num = $this->user_num;
  104. $integralGoods->sales = $this->sales;
  105. $integralGoods->cat_id = $this->cat_id;
  106. $integralGoods->sort = $this->sort;
  107. $integralGoods->detail = $this->detail;
  108. $integralGoods->is_delete = 0;
  109. if (!$integralGoods->save()) {
  110. $t->rollBack();
  111. return [
  112. 'code' => 1,
  113. 'msg' => $integralGoods->errors[0]
  114. ];
  115. }
  116. $ids = array_column(json_decode($integralGoods->attr, true), 'attr_id');
  117. // 修改库存
  118. $goods_attr_res = Goods::findOne($integralGoods->goods_id);
  119. $goods_attr = json_decode($goods_attr_res->attr, true);
  120. foreach ($goods_attr as $key => $value) {
  121. $goods_attr_id = array_column($value['attr_list'], 'attr_id');
  122. if ($ids == $goods_attr_id) {
  123. $goods_attr[$key]['num'] -= $integralGoods->goods_count;
  124. if ($goods_attr[$key]['num'] < 0) {
  125. $t->rollBack();
  126. return [
  127. 'code' => 1,
  128. 'msg' => '添加失败'
  129. ];
  130. }
  131. break;
  132. }
  133. }
  134. $goods_attr_res->attr = json_encode($goods_attr);
  135. if (!$goods_attr_res->save()) {
  136. $t->rollBack();
  137. return [
  138. 'code' => 1,
  139. 'msg' => '添加失败'
  140. ];
  141. }
  142. $t->commit();
  143. return [
  144. 'code' => 0,
  145. 'msg' => '添加成功'
  146. ];
  147. }
  148. public static function getAttrGroupByAttId($att_id)
  149. {
  150. $cache_key = 'get_attr_group_by_attr_id_' . $att_id;
  151. $attr_group = \Yii::$app->cache->get($cache_key);
  152. if ($attr_group) {
  153. return $attr_group;
  154. }
  155. $attr_group = AttrGroup::find()->alias('ag')
  156. ->where(['ag.id' => Attr::find()->select('attr_group_id')->distinct()->where(['id' => $att_id])])
  157. ->limit(1)->one();
  158. if (!$attr_group) {
  159. return $attr_group;
  160. }
  161. \Yii::$app->cache->set($cache_key, $attr_group, 10);
  162. return $attr_group;
  163. }
  164. }