validate()) { return $this->getErrorSummary(false)[0]; } $goods = Goods::findOne([ 'id' => $this->id, 'is_delete' => 0, 'status' => 1, 'store_id' => $this->store_id, 'product_type' => Goods::GOODS_TYPE_ADOPT, ]); $verify_card = []; if (!empty($goods->verify_card_id)) { $ids = explode(',', $goods->verify_card_id); $verify_card = Tools::getVerifyList($ids); } else { $verify_card = []; } if (get_md_id()) { $md_goods = MdGoods::findOne(['md_id' => get_md_id(), 'goods_id' => $this->id]); if ($md_goods) { if ($md_goods->status == 0) { return [ 'code' => 1, 'msg' => '商品已下架' ]; } } else { if (!$goods) { return [ 'code' => 1, 'msg' => '商品已下架' ]; } } } else { if (!$goods) { return [ 'code' => 1, 'msg' => '商品已下架' ]; } } $mch = null; if ($goods->mch_id) { $mch = $this->getMch($goods); if (!$mch) { return [ 'code' => 1, 'msg' => '店铺已经打烊了哦~' ]; } } $pic_list = GoodsPic::find()->select('pic_url')->where(['goods_id' => $goods->id, 'is_delete' => 0])->asArray()->all(); $is_favorite = 0; if ($this->user_id) { $exist_favorite = Favorite::find()->where(['user_id' => $this->user_id, 'goods_id' => $goods->id, 'is_delete' => 0])->exists(); if ($exist_favorite) { $is_favorite = 1; } } $service_list = explode(',', $goods->service); // 默认商品服务 if (!$goods->service) { $option = Option::get('good_services', $this->store_id, 'admin', []); foreach ($option as $item) { if (!empty($item['is_default'])) { $service_list = explode(',', $item['service']); break; } } } $new_service_list = []; if (is_array($service_list)) { foreach ($service_list as $item) { $item = trim($item); if ($item) { $new_service_list[] = $item; } } } // 门店逻辑 $is_md = false; if (get_md_id()) { $md_goods = MdGoods::findOne(['goods_id' => $goods->id, 'md_id' => get_md_id()]); if ($md_goods) { $is_md = true; } } $new_price = $is_md ? $md_goods->price : $goods->price; $price_attr = $is_md ? Json::decode($md_goods->attr) : Json::decode($goods->attr); $price = []; if (is_array($price_attr)) { foreach ($price_attr as $v) { if (is_object($v) && $v->price > 0) { $price[] = $v->price; } else { $price[] = floatval($new_price); } } } else { $price = [$goods->price]; } $res_url = GetInfo::getVideoInfo($goods->video_url); $goods->video_url = $res_url['url']; if ($goods->is_negotiable) { $min_price = Goods::GOODS_NEGOTIABLE; } else { $min_price = sprintf('%.2f', min($price)); } $res = CommonGoods::getMMPrice([ 'attr' => $is_md ? $md_goods->attr : $goods->attr, 'attr_setting_type' => $goods->attr_setting_type, 'share_type' => $goods->share_type, 'share_commission_first' => $goods->share_commission_first, 'price' => $new_price, 'individual_share' => $goods->individual_share, 'mch_id' => $goods->mch_id, 'is_level' => $goods->is_level, 'use_attr' => $goods->use_attr, ]); $attr = $price_attr; $goodsPrice = $new_price; $isMemberPrice = false; if ($res['user_is_member'] === true && count($attr) === 1 && $attr[0]['attr_list'][0]['attr_name'] == '默认') { $goodsPrice = $res['min_member_price'] ? $res['min_member_price'] : $new_price; $isMemberPrice = true; } $GoodsFullMinus = GoodsFullMinus::find() ->select('*') ->where(['store_id' => $this->store_id, 'goods_id' => $goods->id]) ->orderBy('full_minus_num ASC')->asArray()->all(); $adopt_goods = AdoptGoods::findOne([ 'goods_id' => $this->id, 'is_delete' => 0, 'store_id' => $this->store_id, ]); $device_id = AdoptGoodsDevice::find()->where(['goods_id'=>$goods['id'],'is_delete'=>0])->select('device_id')->column(); $data = [ 'id' => $goods->id, 'status' => $goods->status, 'pic_list' => $pic_list, 'attr' => $is_md ? $md_goods->attr : $goods->attr, 'cover_pic' => $goods->cover_pic, 'is_negotiable' => $goods->is_negotiable, 'max_price' => sprintf('%.2f', max($price)), 'min_price' => $min_price, 'name' => $goods->name, 'cat_id' => $goods->cat_id, 'price' => sprintf('%.2f', $goodsPrice), 'detail' => $goods->detail, 'sales' => $goods->getSalesVolume() + ($is_md ? $md_goods->virtual_sales : $goods->virtual_sales), 'attr_group_list' => $goods->getAttrGroupList(), 'num' => $goods->getNum(), 'is_favorite' => $is_favorite, 'service_list' => $new_service_list, 'original_price' => sprintf('%.2f', $goods->original_price), 'video_url' => $goods->video_url, 'unit' => $goods->unit, 'use_attr' => intval($goods->use_attr), 'mch' => $mch, 'max_share_price' => sprintf('%.2f', $res['max_share_price']), 'min_member_price' => sprintf('%.2f', $res['min_member_price']), 'is_share' => $res['is_share'], 'is_level' => $res['is_level'], 'is_member_price' => $isMemberPrice, 'full_minus' => $GoodsFullMinus, 'verify_card_id' => $verify_card, 'goods_share_title' => $goods->goods_share_title, 'goods_share_desc' => $goods->goods_share_desc, 'goods_share_logo' => $goods->goods_share_logo, 'product_type' => $goods->product_type, 'growth_cycle' => $adopt_goods->growth_cycle, 'desc' => $adopt_goods->desc, 'yield' => $adopt_goods->yield, 'varieties' => $adopt_goods->varieties, 'adopt_income' => Json::decode($adopt_goods->adopt_income), 'self_pick_date' => Json::decode($adopt_goods->self_pick_date), 'self_pick_times' => Json::decode($adopt_goods->self_pick_times), 'remaining_num' => $goods->getNum() - ($goods->getSalesVolume() + ($is_md ? $md_goods->virtual_sales : $goods->virtual_sales)), 'device_id' => $device_id, 'integral' => json_decode($goods->integral, true), ]; $now_date = strtotime(date('Y-m-d')); $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]); // 预约参数配置 if ($data['product_type'] == Goods::GOODS_TYPE_DATE) { if ($goods_book && !empty($goods_book->date_book)) { $date_book = Json::decode($goods_book->date_book); $count = 0; foreach ($date_book as $value) { if (strtotime($value['date']) >= $now_date) { $count++; } } if ($count <= 0) { return [ 'code' => 1, 'msg' => '商品预约数据不存在' ]; } $data['date_book'] = [ 'date_book' => $date_book, 'date' => count($date_book) ]; } else { return [ 'code' => 1, 'msg' => '该商品暂无可预约数据' ]; } } if ($data['product_type'] == Goods::GOODS_TYPE_TIME) { if ($goods_book && !empty($goods_book->service_book)) { $service_book = Json::decode($goods_book->service_book); $count = 0; foreach ($service_book['data'] as &$value) { if (strtotime($value['date']) >= $now_date) { foreach ($value['time'] as &$m) { foreach ($m['times'] as &$n) { if (strtotime(date('Y-m-d H:i:s')) <= strtotime(date('Y-m-d ' . $n['time']))) { $count++; } } } } } if ($count <= 0) { return [ 'code' => 1, 'msg' => '该商品暂无可预约数据' ]; } $data['service_book'] = $service_book; } else { return [ 'code' => 1, 'msg' => '商品服务预约配置有误' ]; } } return [ 'code' => 0, 'data' => $data ]; } // 快速给购买商品 public function quickGoods($twocatid) { $goods = Goods::find() ->where([ 'store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1, 'quick_purchase' => 1 ]) ->andWhere([ 'in', 'cat_id', $twocatid ])->asArray() ->all(); foreach ($goods as $key => &$value) { $value['attr'] = json_decode($value['attr']); foreach ($value['attr'] as $key2 => $value2) { foreach ($value2->attr_list as $key3 => $value3) { $value['attr_name'] = $value3->attr_name; } $value['num'] = 0; } } return [ 'code' => 0, 'data' => [ 'list' => $goods, ], ]; } /** * 获取优惠详情 */ public function getYouhui() { if (!$this->validate()) { return $this->getErrorSummary(false)[0]; } $data = []; $goods = Goods::findOne($this->id); // 是否赠送积分 $integral = json_decode($goods->integral, true); if ($integral['give'] > 0) { $data[] = [ 'name' => '赠送积分', 'content' => '购买赠送' . $integral['give'] . '积分' ]; } if ($integral['forehead'] > 0) { $data[] = [ 'name' => '积分抵扣', 'content' => '最高可抵扣' . $integral['forehead'] . '元' ]; } $goods_card = Goods::getGoodsCard($goods->id); foreach ($goods_card as $card) { $data[] = [ 'name' => '卡券', 'content' => $card['name'] ]; } // 核销卡列表获取 // 获取商品已添加核销卡 if (empty($goods->verify_card_id)) { $goods_verify_card_list = []; } else { $verify_cards = explode(',', $goods['verify_card_id']); foreach ($verify_cards as $k => $v) { $verifyCard = VerifyCard::find()->select(['id', 'name'])->where(['id' => $v, 'is_delete' => 0])->asArray()->one(); if ($verifyCard) { $goods_verify_card_list[] = $verifyCard; } } } foreach ((array)$goods_verify_card_list as $verify) { $data[] = [ 'name' => '核销卡', 'content' => $verify['name'] ]; } $coupon_list = Coupon::getList($goods->mch_id); $coupon_list_res = []; foreach ($coupon_list as $coupon) { if ($coupon['appoint_type'] !== 3) { if ($coupon['appoint_type'] == 0) { $coupon_list_res[] = $coupon; } else { if ($coupon['appoint_type'] == 2) { $goods_id_arr = array_column($coupon['goods'], 'id'); if (in_array($goods->id, $goods_id_arr) || empty($goods_id_arr)) { $coupon_list_res[] = $coupon; } } else { if ($coupon['appoint_type'] == 1) { if ($goods->mch_id > 0) { $cat_list = MchGoodsCat::find()->where([ 'goods_id' => $goods->id, ])->all(); } else { $cat_list = GoodsCat::find()->where([ 'goods_id' => $goods->id, 'is_delete' => 0, 'store_id' => get_store_id() ])->all(); } $cat_id_arr = array_column($coupon['cat'], 'id'); if (empty($cat_id_arr)) { $coupon_list_res[] = $coupon; } else { foreach ($cat_list as $cat) { if (in_array($cat->cat_id, $cat_id_arr) || empty($cat_id_arr)) { $coupon_list_res[] = $coupon; break; } } } } } } } } $dataRes = [ 'data' => $data, 'coupon' => $coupon_list_res ]; return [ 'code' => 0, 'data' => $dataRes ]; } /** * @description: 获取商品收货地址 * @param {*} * @return {*} */ public function getAddress() { if ($this->address_id) { $query = Address::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'id' => $this->address_id]); if (is_platform()) { $address = $query->andWhere([ 'user_id' => get_saas_user_id() ])->one(); } else { $address = $query->andWhere([ 'user_id' => get_user_id() ])->one(); } } else { $query = Address::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]); if (is_platform()) { $query->andWhere([ 'user_id' => get_saas_user_id() ]); } else { $query->andWhere([ 'user_id' => get_user_id() ]); } $address = $query->orderBy(['is_default' => SORT_DESC])->one(); } $goods = Goods::findOne($this->id); // 计算运费 $express_price = $this->getExpressPrice($address, $goods); // 计算区域限制购买 $is_area = $this->getTerritorialLimitation($address, $goods); $shop_id = null; $shop = null; if ($this->shop_id) { $shop_id = $this->shop_id; } else { $order = Order::find()->where([ 'is_offline' => 1, 'user_id' => get_user_id() ])->orderBy(['id' => SORT_DESC])->one(); if ($order) { $shop_id = $order->shop_id; } } if ($shop_id) { $shop = Shop::findOne($shop_id); } return [ 'code' => 0, 'data' => [ 'address' => $address, 'shop' => $shop, 'express_price' => $express_price, 'is_area' => $is_area ] ]; } /** * 门店推荐 */ public function mchRecommend() { $goods_list = Goods::find()->where([ 'mch_id' => $this->mch_id, 'status' => 1, 'is_delete' => 0 ])->orderBy(['sort' => SORT_ASC]) ->select(['id', 'name', 'cover_pic', 'price'])->limit(6)->all(); return [ 'code' => 0, 'data' => [ 'list' => $goods_list ] ]; } /** * 猜你喜欢 */ public function guessLike() { $cat_list = GoodsCat::find()->where(['goods_id' => $this->id, 'is_delete' => 0])->all(); $cat_id = []; foreach ($cat_list as $val) { $cat_id[] = $val->cat_id; } $goods_list = Goods::find()->alias('g') ->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id=g.id') ->where([ 'g.status' => 1, 'g.is_delete' => 0, 'gc.cat_id' => $cat_id ])->orderBy(['g.sort' => SORT_DESC]) ->select(['g.id', 'g.name', 'g.cover_pic', 'g.price'])->groupBy('gc.goods_id')->limit(6)->all(); return [ 'code' => 0, 'data' => [ 'list' => $goods_list ] ]; } /** * 计算单个商品的运费 */ protected function getExpressPrice($address, $goods) { $expressPrice = 0; if ($address) { //再通过运费规则计算运费 $expressPrice = PostageRules::getExpressPrice($goods->store_id, $address->city_id, $goods, 1, $address->province_id); } return $expressPrice >= 0 ? $expressPrice : 0; } /** * 计算单个商品是否区域限制购买 */ protected function getTerritorialLimitation($address, $goods) { $isArea = 0; if ($address) { $area = TerritorialLimitation::findOne([ 'store_id' => get_store_id(), 'is_delete' => 0, 'is_enable' => 1, ]); if ($area) { $city_id = []; //限制的地区ID $detail = json_decode($area->detail); if (!is_array($detail)) { $detail = []; } foreach ($detail as $key => $value) { foreach ($value->province_list as $key2 => $value2) { $city_id[] = $value2->id; } } $addressArr = [ $address['province_id'], $address['city_id'], $address['district_id'] ]; $addressArray = array_intersect($addressArr, $city_id); if (empty($addressArray)) { $isArea = 1; } } } return $isArea; } /** * User: chiyanying * Date: 2020/9/27 * Time: 17:27 * Notes:获取单个商品信息 */ public static function getGoods($goods_id = 0, $tore_id = 0, $type = 0) { if ($type == 0) { $form = new GoodsForm(); $form->id = $goods_id; $form->store_id = $tore_id; return $form->search()['data']; } } /** * 获取店铺信息 */ public function getMch($goods) { $f = new ShopDataForm(); $f->mch_id = $goods->mch_id; $shop = $f->getShop(); if (isset($shop['code']) && $shop['code'] == 1) { return null; } return $shop; } }