services->setting->addons->get($params['mall_id'], HappinessEnum::ADDONS_NAME, 'basic.goods_label'); if (!empty($label_list)) { $label_list = Json::decode($label_list, true); if (!empty($label_list)) { foreach ($label_list as $key => $value) { if ($value['level'] == $params['label_id']) { return $value['name']; } } } } return ''; } //获取商品列表 public function getGoodsList($params) { try { $where = [ ['bg.mall_id' => $this->mall_id], ['bg.status' => StatusEnum::ENABLED], ]; if (!empty($params['store_id'])) $where[] = ['bg.store_id' => $params['store_id']]; if (!empty($params['goods_name'])) { $where[] = ['like' ,'g.goods_name', $params['goods_name']]; } isset($params['goods_id']) && $where[] = ['=', 'bg.goods_id', $params['goods_id']]; if (!empty($params['goods_type'])) $where[] = ['g.goods_type' => $params['goods_type']]; isset($params['start']) && $where[] = ['>=', 'g.created_at', strtotime($params['start'])]; isset($params['end']) && $where[] = ['<=', 'g.created_at', strtotime($params['end'])]; isset($params['goods_name']) && $where[] = ['like', 'g.goods_name', trim($params['goods_name']) . '%', false]; if (isset($params['type'])) { switch ($params['type']) { case 'onsale': $where[] = ['=', 'bg.is_on_sale', WhetherEnum::ENABLED]; $where[] = ['>', 'bg.stock', 0]; break; case 'offline': $where[] = ['=', 'bg.is_on_sale', WhetherEnum::DISABLED]; break; case 'sell-out': $where[] = ['=', 'bg.stock', 0]; break; } } $goods_params = [ 'alias' => 'bg', 'where' => $where, 'order' => 'id desc', 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = bg.goods_id"]], 'select' => 'bg.id,bg.goods_id, bg.sales_num, g.goods_name, g.freight_type, g.price, g.is_on_sale as goods_sale, g.cost_price, g.unit, g.cover_pic, bg.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, bg.status, bg.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_status, g.check_remark, g.sort, g.created_at, g.stock_warning', 'with' => ['cats', 'goods', 'attr','yunAttr', 'yunGoods'], ]; $list = HappinessStoreGoods::listPage($goods_params); if (!empty($list['list'])) { foreach ($list['list'] as &$item) { $item['freight_type'] = explode(',', $item['freight_type']); $item['can_change_status'] = false; if (in_array(2, $item['freight_type']) || in_array(3, $item['freight_type'])) { $item['can_change_status'] = true; } if ($item['goods_sale'] == 0) $item['can_change_status'] = false; if ($item['yunGoods']['is_on_sale'] == 0) $item['can_change_status'] = false; // foreach ($item['yunAttr'] as $key => $value) { // $item['yunAttr'][$key]['name'] = $item['attr'][$value['attr_id']]['name']; // $item['yunAttr'][$key]['price'] = $item['attr'][$value['attr_id']]['price']; // } foreach ($item['attr'] as $key => $value) { if (empty($item['yunAttr'][$value['id']])) { $item['yunAttr'][$value['id']] = [ 'attr_id' => $value['id'], 'goods_id' => $value['goods_id'], 'name' => $value['name'], 'price' => $value['price'], ]; } else { $item['yunAttr'][$value['id']]['name'] = $value['name']; $item['yunAttr'][$value['id']]['price'] = $value['price']; } } $item['yunAttr'] = array_values($item['yunAttr']); } } return $list; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //门店商品上下架 public function storeGoodsMutiple($params) { try { if(empty($params['good_ids'])) throw new Exception('错误参数'); $res = HappinessStoreGoods::updateAll( [ 'is_on_sale' => $params['type'], 'updated_at' => time(), ], [ 'mall_id' => $this->mall_id, 'store_id' => $params['store_id'], 'id' => $params['good_ids'] ] ); if (!$res) throw new Exception('操作失败'); HappinessStoreGoodsAttr::updateAll(['is_on_sale' => $params['type']], ['mall_id' => $this->mall_id, 'store_id' => $params['store_id'], 'yun_goods_id' => $params['good_ids']]); return true; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //获取商品详情 public function getGoodsDetail($params) { try { $info = []; //存在id,获取详情数据 if (!empty($params['id'])) { $detail = HappinessStoreGoods::getOne([ ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['id' => $params['id']], ], true, ['goods', 'attr', 'yunAttr']); foreach ($detail['yunAttr'] as &$item) { $item['name'] = $detail['attr'][$item['attr_id']]['name']; $item['price'] = $detail['attr'][$item['attr_id']]['price']; } foreach ($detail['attr'] as $key => $value) { if (empty($detail['yunAttr'][$value['id']])) { $detail['yunAttr'][$value['id']] = [ 'attr_id' => $value['id'], 'goods_id' => $value['goods_id'], 'name' => $value['name'], 'price' => $value['price'], ]; } else { $detail['yunAttr'][$value['id']]['name'] = $value['name']; $detail['yunAttr'][$value['id']]['price'] = $value['price']; } } $detail['yunAttr'] = array_values($detail['yunAttr']); $info['detail'] = $detail; } return $info; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //添加、编辑商品 public function addGoods($params) { $attr_params=[]; $label=[]; //获取商品价格 $attr_ids=array_column($params, 'attr_id'); $attrs=GoodsAttr::find()->where(['id' => $attr_ids,'status' => StatusEnum::ENABLED])->select('id,stock,price,goods_id') ->asArray()->indexBy('id')->all(); $happinessGoodsList=HappinessGoods::find()->where(['goods_id' => array_column($params, 'goods_id'),'status' => StatusEnum::ENABLED,'mall_id' => $this->mall_id])->asArray()->indexBy('goods_id')->all(); if(empty($happinessGoodsList)) { throw new Exception('未检测到任何商品'); } foreach ($params as &$item) { if(!isset($happinessGoodsList[$item['goods_id']])) { throw new Exception('选品库中不存在商品(ID:'.$item['goods_id'].')'); } $label_id=$happinessGoodsList[$item['goods_id']]['label_id']; $attr_params[]=[ 'mall_id' => $this->mall_id, 'goods_id' => $item['goods_id'], 'status' => StatusEnum::ENABLED, 'label_id'=>$label_id, 'store_id' => $this->store_id, 'buy_num' => $item['buy_num'], 'attr_id' => $item['attr_id'], 'voucher'=>bcmul($item['buy_num'], $attrs[$item['attr_id']]['price'], 2) ]; if(!isset($label[$label_id])) { $label[$label_id]=bcmul($item['buy_num'], $attrs[$item['attr_id']]['price'], 2); } else { $label[$label_id] += bcmul($item['buy_num'], $attrs[$item['attr_id']]['price'], 2); } } //检测提货券数量 $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'); $label_ids=array_column($params, 'label_id'); $store_labels=HappinessVoucherWallet::find()->where( ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id'=>$this->store_id, 'level' => $label_ids ])->asArray()->indexBy('level')->all(); $wallet_log=[]; foreach ($label as $key => $item) { $label_name=isset($goods_label[$key])?$goods_label[$key]['name']:'未知'; if(empty($store_labels[$key])) { throw new Exception($label_name.'提货券当前为0'); } else { if($item > $store_labels[$key]['balance']) { throw new Exception($label_name.'提货券数量不足,当前只有'.$store_labels[$key]['balance'].',共需'.$item); } } $wallet_log[] = [ 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'level' => $key, 'change_type' => CapitalLog::CHANGE_TYPE_SUB, 'change_num' => -$item, 'desc' => '小店进货扣除', 'source_table' => HappinessVoucherWallet::tableName(), 'source_table_id' => $this->store_id, 'from_type' => HappinessVoucherEnum::FROM_STORE_BUY, 'wallet_type' => HappinessVoucherEnum::WALLET_VALUE, 'status' => 1, 'order_no' => '' ]; } $tran=Yii::$app->db->beginTransaction(); try { //添加提货单 $goods_ids=[]; // foreach ($goods_params as $key => $item) // { // $goods = HappinessStoreGoods::setData($item); // if (!$goods) throw new Exception(HappinessStoreGoods::$static_error); // $goods_ids[$item['goods_id']]=$goods->id; // } // foreach ($attr_params as $key => &$item) // { // $item['yun_goods_id']=$goods_ids[$item['goods_id']]; // $attr = HappinessStoreGoodsAttr::setData($item); // if (!$attr) throw new Exception(HappinessStoreGoodsAttr::$static_error); // } $voucherService=new VoucherService; $res=$voucherService->addVoucher($attr_params,$this->mall_id, $this->store_id); if (!$res) throw new Exception($voucherService->getError()); //扣除提货券 if(!empty($wallet_log)) { foreach ($wallet_log as $item) { if(abs($item['change_num'])>0) { $res=WalletLogic::handle($item); if (!$res) throw new Exception(WalletLogic::getStaticError()); } } } $tran->commit(); return true; } catch (\Exception $e) { $tran->rollBack(); throw new Exception($e->getMessage().$e->getFile()); } } //删除商品 public function deleteGoods($params) { try { $check = HappinessStoreGoods::getOne([['mall_id' => $this->mall_id], ['id' => $params['id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]); if (empty($check)) throw new Exception('查询不到该商品数据'); $check->status = $params['status']; $res = $check->save(); if (!$res) throw new Exception('操作失败'); return true; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } //搜索商品信息 public function searchGoodsData($params) { try { $where = [ ['yg.mall_id' => $this->mall_id], ['yg.status' => StatusEnum::ENABLED], // ['>','g.stock',0], ['yg.is_on_sale'=>1], ]; if (!empty($params['keyword'])) $where[] = ['or', ['like', 'g.id', $params['keyword']], ['like', 'g.goods_name', $params['keyword']]]; if ($params['label_id']>0) $where[] = ['yg.label_id' => $params['label_id']]; $with = ['cats', 'attr']; $params = [ 'alias' => 'yg', 'where' => $where, 'with' => $with, 'limit' => 20, 'order' => 'id desc', 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = yg.goods_id"]], 'select' => 'yg.min_num,yg.store_scale,yg.label_id,yg.id,yg.mall_id,yg.sales_num,g.id as goods_id, g.goods_name, 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, g.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_remark, g.sort, g.created_at, g.stock_warning, g.freight_type', ]; $list = HappinessGoods::listPage($params, false, true); return $list; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public function searchStoreGoodsData($params) { try { $where = [ ['yg.mall_id' => $this->mall_id], ['yg.status' => StatusEnum::ENABLED], // ['>','g.stock',0], ['sg.store_id' => $this->store_id], ['yg.is_on_sale'=>1], ]; if (!empty($params['keyword'])) $where[] = ['or', ['like', 'g.id', $params['keyword']], ['like', 'g.goods_name', $params['keyword']]]; if ($params['label_id']>0) $where[] = ['yg.label_id' => $params['label_id']]; $with = ['yunGoods.cats', 'yunAttr.attr']; $params = [ 'alias' => 'sg', 'where' => $where, 'with' => $with, 'limit' => 20, 'order' => 'id desc', 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = sg.goods_id and g.status=1"], ['LEFT JOIN', HappinessGoods::tableName(). ' as yg', "yg.goods_id = sg.goods_id and sg.status=1"]], 'select' => 'yg.min_num,yg.store_scale,yg.label_id,sg.id,yg.mall_id,yg.sales_num,g.id as goods_id, g.goods_name, 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, g.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_remark, g.sort, g.created_at, g.stock_warning, g.freight_type', ]; $list = HappinessStoreGoods::listPage($params, false, true); if($list==false) { $error=HappinessStoreGoods::getStaticError(); } return $list; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public function cateGoodsList($params) { try { if (!empty($params['type']) && $params['type'] == 'all') { //查询三级的所有 $goods_cate_list = GoodsCate::findAll(['parent_id' => $params['cate_id'], 'mall_id' => $this->mall_id, 'is_show' => 1]); $cate_id_arr = array_column($goods_cate_list, 'id'); $cate_id_arr[] = $params['cate_id']; } else { if (empty($params['cate_id'])) { //顶级 //$goods_cate_list = GoodsCate::findAll(['parent_id' => 0, 'mall_id' => $this->mall_id, 'is_show' => 1]); //查询所有分类的商品 $goods_cate_list = GoodsCate::findAll(['mall_id' => $this->mall_id, 'is_show' => 1]); $cate_id_arr = array_column($goods_cate_list, 'id'); } else { $goods_cate = GoodsCate::findOne(['id' => $params['cate_id'], 'mall_id' => $this->mall_id, 'is_show' => 1]); $c = [$params['cate_id']]; $cate_id_arr = []; $cate_id_arr1 = []; GoodsCate::getAllCate($this->mall_id, $c, $cate_id_arr1); if (!empty($cate_id_arr1)) { foreach ($cate_id_arr1 as $item) { if (!empty($item)) { $cats = explode(",", $item); array_push($cate_id_arr, ...$cats); } } } if (!empty($goods_cate)) { if ($goods_cate['parent_id'] == 0) { $goods_cate_list = GoodsCate::findAll(['parent_id' => $params['cate_id'], 'mall_id' => $this->mall_id, 'is_show' => 1]); $cid_arr2 = array_column($goods_cate_list, 'id'); $goods_cate_list = GoodsCate::findAll(['parent_id' => $cid_arr2, 'mall_id' => $this->mall_id, 'is_show' => 1]); $cid_arr3 = array_column($goods_cate_list, 'id'); $cate_id_arr = array_merge($cate_id_arr, $cid_arr2, $cid_arr3); } } } } $goods_id_arr = GoodsCateRelate::find()->where(['cate_id' => $cate_id_arr])->groupBy('goods_id')->select('goods_id')->column(); if (!empty($params['keyword'])) { $goods_info = Goods::lists([ 'where' => [ ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['is_on_sale' => 1], ['id' => $goods_id_arr], ['like', 'goods_name', $params['keyword']], ] ]); if (!empty($goods_info)) { $goods_id_arr = array_column($goods_info, 'id'); } else { $goods_id_arr = []; } } $where = [ ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['is_on_sale' => 1], ['store_id' => $params['store_id']], ['goods_id' => $goods_id_arr], ]; $goods_params = [ 'where' => $where, 'with' => ['good', 'attrs'], 'page' => $params['page'], 'limit' => $params['limit'], 'is_on_sale' => 1, 'order' => 'id desc', ]; $list = HappinessStoreGoods::listPage($goods_params); if($list===false) { throw new Exception(HappinessStoreGoods::getStaticError()); } $cart_list = Cart::getCartList($this->user_id); $goods_num_arr = []; foreach ($cart_list as $v) { $row = json_decode($v, true); if (isset($goods_num_arr[$row['goods_id']])) { $goods_num_arr[$row['goods_id']] += $row['num']; } else { $goods_num_arr[$row['goods_id']] = $row['num']; } } $pension_scale = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.pension_scale')??0; if (!empty($list['list'])) { foreach ($list['list'] as &$item) { $item['good']['num'] = !empty($goods_num_arr[$item['goods_id']]) ? $goods_num_arr[$item['goods_id']] : 0; $item['pension_price']= bcmul($item['good']['price'],bcdiv($pension_scale,100,4),2); if (!empty($item['attrs'])) { $item['good']['attr'] = $item['attrs']; $item['good']['attr_id'] = 0; foreach($item['attrs'] as $val){ if($val['is_show'] == StatusEnum::ENABLED){ $item['good']['attr_id'] = $val['id']; break; } } unset($item['attrs']); } } } return $list; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } public function cartListAppendFields($params) { if (empty($params)) return []; $data = $params['data']; foreach ($data as $k => $v) { $list = $v['list']; // 商城订单 if ($k == 0) { //组合当前商品id $goods_id = array_column($list, 'goods_id'); $attr_id = array_column($list, 'attr_id'); //获取商品规格库存量 $goods_attr_data = GoodsAttr::lists([ 'where' => [ ['status' => StatusEnum::ENABLED], ['id' => $attr_id], ], 'index' => 'id', ]); //获取商品是否幸福小店门店商品 $happiness_goods = HappinessStoreGoods::lists([ 'where' => [ ['mall_id' => $params['mall_id']], ['status' => StatusEnum::ENABLED], ['goods_id' => $goods_id], ], 'index' => 'goods_id', ]); //获取幸福小店门店商品规格库存量 $happiness_goods_attr = HappinessStoreGoodsAttr::lists([ 'where' => [ ['mall_id' => $params['mall_id']], ['status' => StatusEnum::ENABLED], ['attr_id' => $attr_id], ['goods_id' => $goods_id], ['is_on_sale' => 1], ['>', 'stock', 0], ], 'index' => 'attr_id', ]); foreach ($list as &$item) { if ($item['is_on_sale'] == 1) { $item['is_happiness_goods'] = 0; if (!empty($happiness_goods[$item['goods_id']])) { $item['is_happiness_goods'] = 1; //判断主商城该商品是否库存为0 if (!empty($goods_attr_data[$item['attr_id']]) && $goods_attr_data[$item['attr_id']]['stock'] <= 0) { //库存为0则为失效商品 $item['is_on_sale'] = 0; //如果已被设为幸福小店门店商品,且存在库存,则仍为可购买商品 if (!empty($happiness_goods_attr[$item['attr_id']])) { $item['is_on_sale'] = 1; } } } } } $data[$k]['list'] = $list; } } return $data; } }