validate()) { return $this->getErrorSummary(false)[0]; } $where = [ 'id' => $this->id, 'is_delete' => 0, 'status' => 1, 'store_id' => $this->store_id ]; if (\Yii::$app->prod_is_dandianpu()) { unset($where['store_id']); } $goods = Goods::findOne($where); \app\modules\admin\models\erp\InventoryForm::erp2Goods($this->store_id, $goods); \app\modules\admin\models\jushuitan\JuShuiTanForm::syncStoreJstGoodsQty($this->store_id, $goods); 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(); //秒杀活动 $seckill_activity_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $goods->id]) ->leftJoin(['sa' => SeckillActivity::tableName()], 'sag.activity_id = sa.id') ->andWhere(['AND', ['sa.is_delete' => 0], ['>', 'sa.end_time', time()], ['sag.is_delete' => 0]]) ->select('sa.id, sa.self_limit_num, sa.order_limit_num, sa.start_time, sa.end_time, sag.attr, sag.seckill_num, sag.seckill_price, sag.use_attr, sag.sale_num, sag.virtual_sales')->asArray()->all(); $seckill_activity_arr = []; if (!empty($seckill_activity_goods)) { $last_names = array_column($seckill_activity_goods,'start_time'); array_multisort($last_names, SORT_ASC, $seckill_activity_goods); foreach ($seckill_activity_goods as $activity_good) { $activity_good['progress'] = sprintf('%.2f', (($activity_good['sale_num'] + $activity_good['virtual_sales']) / ($activity_good['seckill_num'] + $activity_good['virtual_sales'])) * 100); $activity_good['price'] = $goods->price; $activity_good['seckill_price'] = sprintf('%.2f', $activity_good['seckill_price']); if ($activity_good['start_time'] < time()) { //活动进行中 前端展示距离结束 $activity_good['status'] = 1; $seckill_activity_arr = $activity_good; goto seckill_activity_arr; } if ($activity_good['start_time'] >= time()) {//活动进行中 前端展示距离开始 $activity_good['status'] = 2; $activity_good['progress'] = 0; $seckill_activity_arr = $activity_good; goto seckill_activity_arr; } } seckill_activity_arr: } if (!empty($seckill_activity_arr)) { $seckill_activity_arr['attr'] = json_decode($seckill_activity_arr['attr'], true); if (!empty($seckill_activity_arr['attr'])) { foreach ($seckill_activity_arr['attr'] as &$activity_attr) { $activity_attr['seckill_price'] = sprintf('%.2f', $activity_attr['seckill_price']); } } } //配送时间展示 $delivery_rules = DeliveryRules::find()->where(['id' => $goods->delivery_rules_id, 'is_delete' => 0, 'status' => 1])->select('id, times, type, days, name')->one(); if ($delivery_rules) { $days = "下单" . $delivery_rules['days'] . "天后"; $delivery_rules['times'] = (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']); $delivery_rules['type'] .= ''; } $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, 'confine_count' => $goods->confine_count ?: 0, 'integral' => json_decode($goods->integral, true) ?: ['give' => 0, 'forehead' => 0, 'forehead_integral' => 0], 'integral_price' => $goods->integral_price, 'delivery_rules' => $delivery_rules ?: null, 'seckill_activity_arr' => $seckill_activity_arr ?: null ]; $store_integral = Option::get(OptionSetting::STORE_INTEGRAL, get_store_id(), 'gift', Option::get(OptionSetting::STORE_INTEGRAL, get_store_id(), 'store')['value'])['value']; if ($goods->integral) { $goods_integral = Json::decode($goods->integral); if ($goods_integral['forehead'] > 0) { $data['integral'] = [ 'give' => $goods_integral['give'], 'forehead' => $goods_integral['forehead'], 'forehead_integral' => round($store_integral * $goods_integral['forehead']) ]; } } // 处理app上不显示没加协议的图片 $data['detail'] = str_replace('src="//', 'src="https://', $data['detail']); $now_date = strtotime(date('Y-m-d')); $goods_book = GoodsBook::findOne(['goods_id' => $goods->id]); // 预约参数配置 if ($data['product_type'] == Goods::GOODS_TYPE_DATE) { $data['num'] = Goods::getGoodsNum($goods)['data']; $data['date_book'] = [ 'date_book' => $data['num'] , 'date' => count($data['num']) ]; 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); $date_data = $this->getDateByInterval(7); $count = 0; $new_arr = [ 'data' => [] ]; $order = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id') ->where(['o.trade_status' => [0, 2, 3], 'o.is_delete' => 0, 'o.order_type' => 2, 'goods_id' => $goods['id']]) ->select('od.attr, od.num')->asArray()->all(); foreach ($date_data as $index => $datum) { $new_arr['data'][$index]['date'] = $datum; $new_arr['data'][$index]['time'] = $service_book['data'][0]['time']; } foreach ($new_arr['data'] as &$value) { if (strtotime($value['date']) >= $now_date) { foreach ($value['time'] as &$m) { foreach ($m['times'] as &$n) { $times = explode('-', $n['time']); if (strtotime(date('Y-m-d H:i:s')) <= strtotime($value['date'] . $times[1])) { $count++; } foreach ($order as $item) { if ($item['attr']) { $item['attr'] = json_decode($item['attr'], true); $order_time = explode('-', $item['attr']['time']); if ( strtotime($item['attr']['date'] . ' ' . $times[0]) === strtotime($value['date'] . ' ' . $order_time[0]) && strtotime($item['attr']['date'] . ' ' . $times[1]) === strtotime($value['date'] . ' ' . $order_time[1]) ) { $n['num'] = $n['num'] - $item['num']; } } } } } } } if ($count <= 0) { return [ 'code' => 1, 'msg' => '该商品暂无可预约数据' ]; } $data['service_book'] = $new_arr; } else { return [ 'code' => 1, 'msg' => '商品服务预约配置有误' ]; } } $ProductBatch = ProductBatch::find()->where(['goods_id' => $goods->id, 'is_delete' => 0, 'state' => 1])->one(); $data['have_product_traceability'] = $ProductBatch ? 1 : 0; $data['product_batch_id'] = $ProductBatch ? $ProductBatch->id : 0; $ruleBuyerLoc = \app\models\OrderRulesBuyerLocation::findOne($goods->order_rules_buyer_location_id); $data['order_rules_buyer_location'] = $ruleBuyerLoc; 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(); $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) { $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; } public function getDateByInterval(int $num) :array { //var_dump($st, $et);die; $returnData = []; $i = 0; do { $temp = date('Y-m-d', strtotime('+' . $i . ' day', strtotime(date('Y-m-d')))); $returnData[] = $temp; $i++; } while ($i < $num); return $returnData; } }