GoodsFullMinusForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Attr;
  9. use app\models\AttrGroup;
  10. use app\models\Goods;
  11. use app\models\Level;
  12. use app\modules\mch\models\LevelListForm;
  13. use yii\base\Model;
  14. use yii\helpers\Json;
  15. class GoodsFullMinusForm extends Model
  16. {
  17. public $store_id;
  18. public $full_minus_num;
  19. public $attr;
  20. public $model;
  21. public $goods_id;
  22. public $goods;
  23. /**
  24. * @inheritdoc
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%goods_full_minus}}';
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['store_id', 'full_minus_num', ], 'required'],
  37. [['store_id', 'full_minus_num', 'goods_id',], 'integer'],
  38. [['full_minus_num',], 'integer', 'min' => 2, 'max' => 10000],
  39. [['attr'], 'safe']
  40. ];
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'store_id' => 'Store ID',
  50. 'goods_id' => 'Goods ID',
  51. 'full_minus_num' => '满减价格数量',
  52. 'attr' => '规格的库存及价格',
  53. ];
  54. }
  55. public function save()
  56. {
  57. if (!$this->validate()) {
  58. return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
  59. }
  60. $this->model->store_id = $this->store_id;
  61. $this->model->full_minus_num = $this->full_minus_num;
  62. if ($this->goods_id) {
  63. $this->model->goods_id = $this->goods_id;
  64. $id = $this->goods_id;
  65. } else {
  66. $id = $this->model->goods_id;
  67. }
  68. $goods = Goods::find()->select('id,attr,use_attr')->where(['is_delete' => 0, 'id' => $id, 'store_id' => $this->store_id])->one();
  69. $old = json_decode($goods->attr, true);
  70. $new = $this->attr;
  71. foreach ($new as $k1 => $v1) {
  72. foreach ($old as $k => $v) {
  73. if ($new['attr_list'] == $old['attr_list']) {
  74. if ($new[$k]['price'] > 99999999.99 || $new[$k]['price'] < 0) {
  75. return [
  76. 'code' => 1,
  77. 'msg' => '满减价格价超过限制',
  78. ];
  79. }
  80. $old[$k]['price'] = round($new[$k]['price'], 2);
  81. }
  82. }
  83. }
  84. $goodsFullMinus = $this->model;
  85. $this->setAttr($goods, $goodsFullMinus);
  86. if ($this->model->save()) {
  87. return [
  88. 'code' => 0,
  89. 'msg' => '保存成功',
  90. ];
  91. } else {
  92. return ['code'=> 1, 'msg' => $this->model->errors[0]];
  93. }
  94. }
  95. /**
  96. * @param Goods $goods
  97. */
  98. private function setAttr($goods, $goodsFullMinus)
  99. {
  100. $levelList = Level::find()->where([
  101. 'store_id' => get_store_id(),
  102. 'is_delete' => 0,
  103. 'status' => 1
  104. ])->select(['id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
  105. if (empty($this->attr) || !is_array($this->attr)) {
  106. return;
  107. }
  108. $new_attr = [];
  109. foreach ($this->attr as $i => $item) {
  110. $new_attr_item = [
  111. 'attr_list' => [],
  112. 'num' => intval($item['num']),
  113. 'price' => doubleval($item['price']),
  114. 'no' => $item['no'] ? $item['no'] : '',
  115. 'pic' => $item['pic'] ? $item['pic'] : '',
  116. 'share_commission_first' => $item['share_commission_first'] ? $item['share_commission_first'] : '',
  117. 'share_commission_second' => $item['share_commission_second'] ? $item['share_commission_second'] : '',
  118. 'share_commission_third' => $item['share_commission_third'] ? $item['share_commission_third'] : '',
  119. ];
  120. foreach ($levelList as $level) {
  121. $keyName = "member" . $level['level'];
  122. $valueName = $item[$keyName] ? $item[$keyName]: '';
  123. $new_attr_item[$keyName] = $valueName;
  124. }
  125. foreach ($item['attr_list'] as $a) {
  126. $attr_model = Attr::findOne(['id' => $a['attr_id'], 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
  127. $new_attr_item['attr_list'][] = [
  128. 'attr_id' => $attr_model->id,
  129. 'attr_name' => $attr_model->attr_name,
  130. ];
  131. }
  132. $new_attr[] = $new_attr_item;
  133. }
  134. $goodsFullMinus->attr = Json::encode($new_attr);
  135. $goodsFullMinus->save();
  136. }
  137. /**
  138. * @return array
  139. */
  140. private function getDefaultAttr()
  141. {
  142. $default_attr_name = '默认';
  143. $default_attr_group_name = '规格';
  144. $attr = Attr::findOne([
  145. 'attr_name' => $default_attr_name,
  146. 'is_delete' => 0,
  147. 'is_default' => 1,
  148. ]);
  149. $attr_group = null;
  150. if (!$attr) {
  151. $attr_group = AttrGroup::findOne([
  152. 'attr_group_name' => $default_attr_group_name,
  153. 'is_delete' => 0,
  154. ]);
  155. if (!$attr_group) {
  156. $attr_group = new AttrGroup();
  157. $attr_group->store_id = $this->store_id;
  158. $attr_group->attr_group_name = $default_attr_group_name;
  159. $attr_group->is_delete = 0;
  160. $attr_group->save(false);
  161. }
  162. $attr = new Attr();
  163. $attr->attr_group_id = $attr_group->id;
  164. $attr->attr_name = $default_attr_name;
  165. $attr->is_delete = 0;
  166. $attr->is_default = 1;
  167. $attr->save(false);
  168. } else {
  169. $attr_group = AttrGroup::findOne($attr->attr_group_id);
  170. }
  171. return [$attr, $attr_group];
  172. }
  173. }