request->isAjax) { if (\Yii::$app->request->isGet) { //商品列表筛选 $get = Yii::$app->request->get(); $rules = [ [['goods_id', 'goods_type'], 'integer'], [['goods_name', 'sort_field'], 'string'], [['type'], 'in', 'range' => ['all', 'checking', 'onsale', 'offline', 'sell-out', 'stock-lack']], [['sort_type'], 'in', 'range' => ['asc', 'desc']], [['cats'], 'each', 'rule' => ['integer']], [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'] ]; $res = Yii::$app->services->validate->validate($get, $rules); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // $where = [['g.mall_id' => $this->mall_id, 'g.store_id' => $this->store_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]; $where[] = ['g.mall_id' => $this->mall_id, 'yg.status' => StatusEnum::ENABLED, 'g.goods_type' => 1, 'yg.store_id' => $this->store_id]; // 筛选 isset($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])]; isset($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])]; isset($get['goods_name']) && $where[] = ['like', 'g.goods_name', trim($get['goods_name']) . '%', false]; if(!empty($get['cats'])){ $goods_cate_relate_list = StoreGoodsCateRelate::lists(['where'=>[['cate_id'=>$get['cats']]]]); $goods_ids = array_column($goods_cate_relate_list,'goods_id'); $where[] = ['in', 'g.id', $goods_ids]; } isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']]; !empty($get['goods_type']) && $where[] = ['=', 'g.goods_type', 1]; if (isset($get['type'])) { switch ($get['type']) { case 'onsale': $where[] = ['=', 'yg.is_on_sale', WhetherEnum::ENABLED]; $where[] = ['>', 'yg.stock', 0]; break; case 'offline': $where[] = ['=', 'yg.is_on_sale', WhetherEnum::DISABLED]; break; case 'sell-out': $where[] = ['=', 'yg.stock', 0]; break; } } // with $with = ['cats', 'yunGoods']; // 排序 $sort = ''; if (isset($get['sort_field']) && isset($get['sort_type'])) $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type']; $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC'; $params = [ 'alias' => 'yg', 'where' => $where, 'with' => $with, 'limit' => 20, 'order' => $sort, 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = yg.goods_id"]], 'select' => 'yg.id,yg.sales_num,g.id as goods_id, g.goods_name, g.is_on_sale as goods_sale, g.price, g.cost_price, g.unit, g.cover_pic, yg.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, g.status, yg.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_remark, g.sort, g.created_at, g.stock_warning, yg.freight_type', ]; //获取标签 $goods_label= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.goods_label'); $goods_label = empty($goods_label) ? [] : json_decode($goods_label,true); $page_data['goods_label']=$goods_label; $page_data = HappinessStoreGoods::listPage($params, false, true); $goods_label = array_column($goods_label,null,'level'); if ($page_data === false) return $this->error(HappinessStoreGoods::getStaticError()); if (!empty($page_data['list'])) { foreach ($page_data['list'] as &$item) { $item['freight_type'] = json_decode($item['freight_type'],true); $item['label_name']=$goods_label[$item['yunGoods']['label_id']]?$goods_label[$item['yunGoods']['label_id']]['name']:''; //最高盈利 $item['max_income'] = bcmul($item['price'],bcdiv($item['yunGoods']['store_scale'], 100,4),2); } } return $this->success('操作成功', $page_data); } else { // 商品操作 $form = Yii::$app->request->post(); $rules = [[['id'], 'required'], [['id'], 'integer'],[['type'], 'string']]; switch ($form['type']) { case 'destroy': //删除 $form['status'] = -1; $rules[] = [['status'], 'required']; break; case 'is_on_sale': //上下架 $rules[] = [['is_on_sale'], 'required']; $rules[] = [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]]; break; } $res = Yii::$app->services->validate->validate($form, $rules); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $form['mall_id'] = $this->mall_id; $form['store_id'] = $this->store_id; $res = HappinessStoreGoods::setData($form); if ($res === false) return $this->error(HappinessStoreGoods::getStaticError()); //删除商品规格 if ($form['status']) { HappinessStoreGoodsAttr::updateAll(['status' => $form['status']], ['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'yun_goods_id' => $form['id']]); } if ($form['type'] == "is_on_sale") { HappinessStoreGoodsAttr::updateAll(['is_on_sale' => $form['is_on_sale']], ['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'yun_goods_id' => $form['id']]); } return $this->success(); } } return $this->render($this->action->id); } //获取幸福小店商品数据 public function actionSearchGoodsData() { $request = Yii::$app->request; try { if ($request->isAjax && $request->isGet) { $get = $request->get(); $res = Yii::$app->services->validate->validate($get, [ [['keyword','label_id'], 'string'], ]); if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage()); $info = (new GoodsService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->searchGoodsData($get); //获取标签 $goods_label= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.goods_label'); $goods_label = empty($goods_label) ? [] : json_decode($goods_label,true); $goods_label_name=array_column($goods_label,null,'level'); if(!empty($info['list'])) { $newList=[]; foreach ($info['list'] as $item) { $oData=$item; unset($oData['attr']); foreach ($item['attr'] as $attr) { $newItem=$oData; $newItem['id']=$attr['attr_id']; $newItem['goods_name']=$oData['goods_name'].'-'.$attr['name']; $newItem['price']=$attr['price']; $newItem['stock']=$attr['stock']; $newItem['cover_pic']=isset($attr['pic_url'])?$attr['pic_url']:$item['cover_pic']; $newItem['max_income']=bcmul($attr['price'],bcdiv($item['store_scale'],100,4),2); $newItem['label_name']=isset($goods_label_name[$item['label_id']])?$goods_label_name[$item['label_id']]['name']:''; $newList[]=$newItem; } } $info['list']=$newList; } if(!empty($goods_label)) { foreach ($goods_label as &$item) { $item['name']=$item['name'].'('.$item['income_start'].'%-'.$item['income_end'].'%)'; } } $info['goods_label']=$goods_label; return $this->success('获取成功', $info); } } catch (\Exception $e) { return $this->error($e->getMessage()); } } /** * 商品编辑 * @Author bing * @DateTime 2021-08-20 14:49:55 * @return mixed * @copyright: Copyright (c) 2020 广东七件事集团 */ public function actionEdit() { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isGet) { // 反显商品信息 $get = \Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer']]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $info = (new GoodsService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->getGoodsDetail($get); //提货券余额 $goods_label= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.goods_label'); $goods_label = empty($goods_label) ? [] : json_decode($goods_label,true); $goods_label=array_column($goods_label,null,'level'); $voucher=HappinessVoucherWallet::find()->where(['mall_id'=>$this->mall_id,'store_id'=>$this->store_id, 'status'=>StatusEnum::ENABLED])->asArray()->all(); $tip="您当前没有提货券"; if(!empty($voucher)) { $tip=""; foreach ($voucher as &$item) { $item['label_name']=isset($goods_label[$item['level']]) ?$goods_label[$item['level']]['name']:''; $tip.=!empty($tip)?',':''; $tip.=$item['label_name'].'的提货券余额为'.$item['balance']; } } $info['voucher']=$voucher; $info['tip']=$tip; return $this->success('操作成功', $info); } else { // 编辑商品 try { $form = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($form, [ [['goods_list'], 'safe'] ]); if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage()); $res = (new GoodsService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->addGoods ($form['goods_list']); return $this->success('操作成功'); } catch (\Exception $e) { return $this->error($e->getMessage()); } } } return $this->render($this->action->id); } public function actionMutipleOff() { if (!\Yii::$app->request->isPost) { return $this->error('请求方式错误'); } $good_ids = \Yii::$app->request->post('good_ids'); if (empty($good_ids) || !is_array($good_ids)) { return $this->error('参数错误'); } if (empty($good_ids)) { return $this->success('操作成功'); } $res = HappinessStoreGoods::updateAll( [ 'is_on_sale' => 0, 'updated_at' => time(), ], [ 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'id' => $good_ids ] ); if (!$res) { return $this->error('操作失败:' . HappinessStoreGoods::getStaticError()); } HappinessStoreGoodsAttr::updateAll(['is_on_sale' => 0], ['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'yun_goods_id' => $good_ids]); return $this->success('操作成功'); } public function actionMutipleOn() { if (!\Yii::$app->request->isPost) { return $this->error('请求方式错误'); } $good_ids = \Yii::$app->request->post('good_ids'); if (empty($good_ids) || !is_array($good_ids)) { return $this->error('参数错误'); } if (empty($good_ids)) { return $this->success('操作成功'); } $res = HappinessStoreGoods::updateAll( [ 'is_on_sale' => 1, 'updated_at' => time(), ], [ 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'id' => $good_ids ] ); if (!$res) { return $this->error('操作失败:' . HappinessStoreGoods::getStaticError()); } HappinessStoreGoodsAttr::updateAll(['is_on_sale' => 1], ['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'yun_goods_id' => $good_ids]); return $this->success('操作成功'); } public function actionMoveStock() { if (Yii::$app->request->isPost) { $form = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($form, [ [['goods_id'], 'required'], [['attr'], 'safe'], ]); if (!$res) { return $this->error(Yii::$app->services->validate->getErrorMessage()); } $service=new MoveService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]); $res = $service->addMove($form); if (!$res) { return $this->error('操作失败:' . $service->getError()); } return $this->success('已添加调货申请'); } if(Yii::$app->request->isGet) { $good_id = \Yii::$app->request->get('goods_id'); $goods = HappinessStoreGoods::findOne(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'goods_id' => $good_id]); if (!$goods) { return $this->error('商品不存在'); } $happiness_goods = HappinessGoods::findOne(['mall_id' => $this->mall_id, 'goods_id' => $goods['goods_id'], 'status' => StatusEnum::ENABLED,'is_on_sale'=>StatusEnum::ENABLED]); if (!$happiness_goods) { return $this->error('选品库商品不存在或者已下架'); } $mall_goods=Goods::findOne(['mall_id' => $this->mall_id, 'id' => $happiness_goods['goods_id'], 'status' => StatusEnum::ENABLED,'is_on_sale'=>StatusEnum::ENABLED]); if (!$mall_goods) { return $this->error('平台商品不存在或者已下架'); } //获取商品属性 $goods_attr = HappinessStoreGoodsAttr::find()->with(['attr.goods'])->where(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'goods_id' => $good_id, 'status' => StatusEnum::ENABLED])->asArray()->all(); //查询调货中的数量 $moveList=HappinessMoveItem::find()->with(['goods'])->where(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'goods_id' => $good_id,'status'=>StatusEnum::ENABLED,'order_status'=>HappinessEnum::MOVE_NO_AUDIT])->asArray()->all(); if(!empty($goods_attr)) { foreach ($goods_attr as &$item) { $item['move_num']=0; $apply_num=0; if(!empty($moveList)) { foreach ($moveList as $move) { foreach ($move['goods'] as $move_goods) { if($move_goods['attr_id']==$item['attr_id']) { $apply_num+=$move_goods['num']; } } } } $item['apply_num']=$apply_num; } } return $this->success('操作成功', $goods_attr); } } public function actionShip() { if (Yii::$app->request->isPost) { // 编辑商品 $form = Yii::$app->request->post(); $form['mall_id'] = $this->mall_id; $form['store_id'] = $this->store_id; if(isset($form['freight_type'])) { $form['freight_type'] = json_encode($form['freight_type']); } $res = HappinessStoreGoods::setData($form); if ($res === false) return $this->error(HappinessStoreGoods::getStaticError()); return $this->success('操作成功'); } if (Yii::$app->request->isGet) { $id=\Yii::$app->request->get('id'); $storeGoods=HappinessStoreGoods::find()->where(['id'=>$id,'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->one(); if(empty($storeGoods)) { return $this->error('商品不存在'); } $happinessGoods=HappinessGoods::find()->where(['goods_id'=>$storeGoods['goods_id'],'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->one(); if(!$happinessGoods) { return $this->error('平台商品库商品不存在'); } $happinessGoods['freight_type']=json_decode($happinessGoods['freight_type'],true); return $this->success('操作成功',$happinessGoods['freight_type']); } } }