| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\models\Level;
- use app\modules\mch\models\LevelListForm;
- use yii\base\Model;
- use yii\helpers\Json;
- class GoodsFullMinusForm extends Model
- {
- public $store_id;
- public $full_minus_num;
- public $attr;
- public $model;
- public $goods_id;
- public $goods;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%goods_full_minus}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['store_id', 'full_minus_num', ], 'required'],
- [['store_id', 'full_minus_num', 'goods_id',], 'integer'],
- [['full_minus_num',], 'integer', 'min' => 2, 'max' => 10000],
- [['attr'], 'safe']
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'goods_id' => 'Goods ID',
- 'full_minus_num' => '满减价格数量',
- 'attr' => '规格的库存及价格',
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
- }
- $this->model->store_id = $this->store_id;
- $this->model->full_minus_num = $this->full_minus_num;
- if ($this->goods_id) {
- $this->model->goods_id = $this->goods_id;
- $id = $this->goods_id;
- } else {
- $id = $this->model->goods_id;
- }
- $goods = Goods::find()->select('id,attr,use_attr')->where(['is_delete' => 0, 'id' => $id, 'store_id' => $this->store_id])->one();
- $old = json_decode($goods->attr, true);
- $new = $this->attr;
- foreach ($new as $k1 => $v1) {
- foreach ($old as $k => $v) {
- if ($new['attr_list'] == $old['attr_list']) {
- if ($new[$k]['price'] > 99999999.99 || $new[$k]['price'] < 0) {
- return [
- 'code' => 1,
- 'msg' => '满减价格价超过限制',
- ];
- }
- $old[$k]['price'] = round($new[$k]['price'], 2);
- }
- }
- }
- $goodsFullMinus = $this->model;
- $this->setAttr($goods, $goodsFullMinus);
- if ($this->model->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- } else {
- return ['code'=> 1, 'msg' => $this->model->errors[0]];
- }
- }
- /**
- * @param Goods $goods
- */
- private function setAttr($goods, $goodsFullMinus)
- {
- $levelList = Level::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'status' => 1
- ])->select(['id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
- if (empty($this->attr) || !is_array($this->attr)) {
- return;
- }
- $new_attr = [];
- foreach ($this->attr as $i => $item) {
- $new_attr_item = [
- 'attr_list' => [],
- 'num' => intval($item['num']),
- 'price' => doubleval($item['price']),
- 'no' => $item['no'] ? $item['no'] : '',
- 'pic' => $item['pic'] ? $item['pic'] : '',
- 'share_commission_first' => $item['share_commission_first'] ? $item['share_commission_first'] : '',
- 'share_commission_second' => $item['share_commission_second'] ? $item['share_commission_second'] : '',
- 'share_commission_third' => $item['share_commission_third'] ? $item['share_commission_third'] : '',
- ];
- foreach ($levelList as $level) {
- $keyName = "member" . $level['level'];
- $valueName = $item[$keyName] ? $item[$keyName]: '';
- $new_attr_item[$keyName] = $valueName;
- }
- foreach ($item['attr_list'] as $a) {
- $attr_model = Attr::findOne(['id' => $a['attr_id'], 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
- $new_attr_item['attr_list'][] = [
- 'attr_id' => $attr_model->id,
- 'attr_name' => $attr_model->attr_name,
- ];
- }
- $new_attr[] = $new_attr_item;
- }
- $goodsFullMinus->attr = Json::encode($new_attr);
- $goodsFullMinus->save();
- }
- /**
- * @return array
- */
- private function getDefaultAttr()
- {
- $default_attr_name = '默认';
- $default_attr_group_name = '规格';
- $attr = Attr::findOne([
- 'attr_name' => $default_attr_name,
- 'is_delete' => 0,
- 'is_default' => 1,
- ]);
- $attr_group = null;
- if (!$attr) {
- $attr_group = AttrGroup::findOne([
- 'attr_group_name' => $default_attr_group_name,
- 'is_delete' => 0,
- ]);
- if (!$attr_group) {
- $attr_group = new AttrGroup();
- $attr_group->store_id = $this->store_id;
- $attr_group->attr_group_name = $default_attr_group_name;
- $attr_group->is_delete = 0;
- $attr_group->save(false);
- }
- $attr = new Attr();
- $attr->attr_group_id = $attr_group->id;
- $attr->attr_name = $default_attr_name;
- $attr->is_delete = 0;
- $attr->is_default = 1;
- $attr->save(false);
- } else {
- $attr_group = AttrGroup::findOne($attr->attr_group_id);
- }
- return [$attr, $attr_group];
- }
- }
|