| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\integral\models\form;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\plugins\integral\models\SaasIntegralGoods;
- use yii\base\Model;
- use yii\helpers\Json;
- class GoodsForm extends Model
- {
- public $store_id;
- public $attr;
- public $goods_id;
- public $name;
- public $integral;
- public $goods_count;
- public $goods_num;
- public $sales;
- public $sort;
- public $user_num;
- public $cat_id;
- public $detail;
- public function rules() {
- return [
- ['attr', function ($attr, $params) {
- if (!$attr) {
- $this->addError($attr, "{$attr}数据格式错误。");
- }
- }],
- [['goods_id'], 'required'],
- [['cat_id', 'goods_id', 'integral', 'goods_count', 'sales', 'sort', 'user_num', 'goods_num'], 'integer'],
- [['integral', 'goods_count'], 'integer', 'min' => 1],
- [['name'], 'string', 'max' => 250],
- [['name', 'detail'], 'trim'],
- [['detail'], 'string']
- ];
- }
- public function save() {
- if (!$this->validate()) {
- return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
- }
- if (empty($this->detail)) {
- return [
- 'code' => 1,
- 'msg' => '请填写商品详情'
- ];
- }
- if (empty($this->goods_id)) {
- return [
- 'code' => 1,
- 'msg' => '请选择商品'
- ];
- }
- if (empty($this->attr)) {
- return [
- 'code' => 1,
- 'msg' => '商品规格数据为空'
- ];
- }
- if ($this->goods_count > $this->goods_num) {
- return [
- 'code' => 1,
- 'msg' => '积分商品规格库存不能大于本商品规格库存'
- ];
- }
- if (intval($this->integral) < 1) {
- return [
- 'code' => 1,
- 'msg' => '设置积分非法'
- ];
- }
- // 检测是否有重复商品
- $exist_goods = SaasIntegralGoods::find()->where(['store_id' => $this->store_id, 'goods_id' => $this->goods_id, 'is_delete' => 0])->asArray()->all();
- if (!empty($exist_goods) && is_array($exist_goods)) {
- $ids = array_column($this->attr, 'attr_id');
- $exist_goods_attr = [];
- foreach ($exist_goods as $goods) {
- $exist_goods_attr[] = json_decode($goods['attr'], true);
- foreach ($exist_goods_attr as $value) {
- $exist_goods_id = array_column($value, 'attr_id');
- if ($exist_goods_id == $ids) {
- return [
- 'code' => 1,
- 'msg' => '已添加同规格商品'
- ];
- }
- }
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- $integralGoods = new SaasIntegralGoods();
- $integralGoods->store_id = $this->store_id;
- $integralGoods->attr = json_encode($this->attr);
- $integralGoods->goods_id = $this->goods_id;
- $integralGoods->name = empty($this->name) ? Goods::findOne($this->goods_id)->name : $this->name;
- $integralGoods->goods_count = $this->goods_count;
- $integralGoods->integral = $this->integral;
- $integralGoods->user_num = $this->user_num;
- $integralGoods->sales = $this->sales;
- $integralGoods->cat_id = $this->cat_id;
- $integralGoods->sort = $this->sort;
- $integralGoods->detail = $this->detail;
- $integralGoods->is_delete = 0;
- if (!$integralGoods->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $integralGoods->errors[0]
- ];
- }
- $ids = array_column(json_decode($integralGoods->attr, true), 'attr_id');
- // 修改库存
- $goods_attr_res = Goods::findOne($integralGoods->goods_id);
- $goods_attr = json_decode($goods_attr_res->attr, true);
- foreach ($goods_attr as $key => $value) {
- $goods_attr_id = array_column($value['attr_list'], 'attr_id');
- if ($ids == $goods_attr_id) {
- $goods_attr[$key]['num'] -= $integralGoods->goods_count;
- if ($goods_attr[$key]['num'] < 0) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '添加失败'
- ];
- }
- break;
- }
- }
- $goods_attr_res->attr = json_encode($goods_attr);
- if (!$goods_attr_res->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '添加失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '添加成功'
- ];
- }
- public static function getAttrGroupByAttId($att_id)
- {
- $cache_key = 'get_attr_group_by_attr_id_' . $att_id;
- $attr_group = \Yii::$app->cache->get($cache_key);
- if ($attr_group) {
- return $attr_group;
- }
- $attr_group = AttrGroup::find()->alias('ag')
- ->where(['ag.id' => Attr::find()->select('attr_group_id')->distinct()->where(['id' => $att_id])])
- ->limit(1)->one();
- if (!$attr_group) {
- return $attr_group;
- }
- \Yii::$app->cache->set($cache_key, $attr_group, 10);
- return $attr_group;
- }
- }
|