validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(FALSE)[0] ]; } $query = MdGoods::find()->alias('mg')->leftJoin(['g' => Goods::tableName()], 'mg.goods_id=g.id')->where([ 'mg.md_id' => $this->md_id, 'g.is_delete' => 0, 'g.store_id' => $this->store_id, 'g.status' => Goods::STATUS_NORMAL ])->select([ 'g.name', 'g.id', 'mg.virtual_sales', 'mg.goods_num', 'md_price' => 'mg.price', 'g.price', 'mg.status', 'g.cover_pic', 'g.use_attr' ]); if (!empty($this->keyword)) { $query->andWhere([ 'like', 'g.name', $this->keyword ]); } switch ($this->status) { case 1: $query->andWhere(['mg.status' => 1]); break; case 0: $query->andWhere(['mg.status' => 0]); break; case 2: $query->andWhere(['mg.goods_num' => 0]); break; default: break; } $result = pagination_make($query, TRUE, 'mg.created_at DESC'); foreach($result['list'] as &$item){ $item['checked'] = 0; } return [ 'code' => 0, 'msg' => 'ok', 'data' => $result ]; } // 批量修改门店商品上下架状态 public function batchUpdateStatus(): array { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(FALSE)[0] ]; } if (empty($this->ids)) { return [ 'code' => 1, 'msg' => '请选择要修改的商品' ]; } if(!strstr($this->ids,',')){ $ids = $this->ids; }else{ $ids = explode(',', $this->ids); } $res = MdGoods::updateAll(['status' => $this->status], ['goods_id' => $ids]); if ($res > 0) { return [ 'code' => 0, 'msg' => '修改成功', ]; } return [ 'code' => 1, 'msg' => '没有找到要修改的数据', ]; } public function getGoodsAttr() { try { $goods = MdGoods::find()->where([ 'md_id' => $this->md_id, 'goods_id' => $this->id, ])->select('id, attr')->one(); $baseGoods = Goods::findOne($goods->goods_id); if (empty($goods->attr) || empty($goods)) { throw new \Exception("获取商品规格库存失败"); } $attr = !empty($goods->attr) ? json_decode($goods->attr, TRUE) : []; $getAttrGroupList = $goods->getAttrGroupList($baseGoods->use_attr); $getAttrGroupList = json_decode(json_encode($getAttrGroupList), TRUE); $title = NULL; if (!empty($getAttrGroupList)) { $title = $getAttrGroupList[0]['attr_list']; } $arr = []; if (!empty($title) && !empty($attr)) { foreach ($title as $index => $item) { foreach ($attr as $at) { if (!empty($at)) { foreach ($at['attr_list'] as $al) { if ($item['attr_id'] === $al['attr_id']) { $arr[$index]['list'][] = $at; } } } } } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'attr' => $attr, 'attr_group' => $arr, 'title' => $title ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setPrice() { try { $goods = MdGoods::find()->where(['md_id' => $this->md_id])->andWhere(['goods_id' => $this->id])->one(); $baseGoods = Goods::findOne($this->id); if (empty($goods) || empty($baseGoods)) { throw new \Exception("获取产品信息失败"); } //判断是否存在以及开启规格 if ($baseGoods->use_attr == 1 && !empty($this->attr)) { // 传过来的规格信息 $attr = $this->attr; $goodsAttr = json_decode($goods['attr'], TRUE); $goodsNum = 0; foreach ($attr as $item) { // 去匹配已经存在的规格信息 $attrIds = array_column($item['attr_list'], 'attr_id'); sort($attrIds); foreach ($goodsAttr as &$goodsAttrItem) { $goodsAttrId = array_column($goodsAttrItem['attr_list'], 'attr_id'); sort($goodsAttrId); if (!array_diff($attrIds, $goodsAttrId)) { $goodsAttrItem['price'] = $item['price']; $goodsAttrItem['num'] = $item['num']; $goodsNum += $goodsAttrItem['num']; } } } $goods->attr = json_encode($goodsAttr); // 同步最新的库存 $goods->goods_num = $goodsNum; // 同步最新的价格 $goods->price = min(array_column($goodsAttr, 'price')); } elseif ($baseGoods->use_attr == 0) { $attr = json_decode($goods->attr, true); $attr[0]['price'] = $this->goods_price; $attr[0]['num'] = $this->goods_num; $goods->attr = json_encode($attr); $goods->price = $this->goods_price; //如果库存不足,则下架处理 if ($this->goods_num == 0) { $goods->status = 0; } } if (!$goods->save()) { throw new \Exception('数据保存错误' . json_encode($goods->errors)); } return [ 'code' => 0, 'msg' => '修改成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }