// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\service; use app\api\model\Category as CategoryModel; use app\api\model\Goods as GoodsModel; use app\api\model\GoodsSku as GoodsSkuModel; use app\api\model\Setting as SettingModel; use app\api\model\UploadFile; use app\api\service\User as UserService; use app\common\enum\Setting as SettingEnum; use app\common\library\helper; use app\common\model\goods\GoodsHotWord; use app\common\model\store\Shop as ShopModel; use app\common\model\store\shop\Comment; use app\common\service\Goods as GoodsService; /** * 商品服务类 * Class Goods * @package app\api\service */ class Goods extends GoodsService { /** * 获取商品的指定的某个SKU信息 * @param int $goodsId * @param string $goodsSkuId * @return GoodsSkuModel|array|null */ public static function getSkuInfo(int $goodsId, string $goodsSkuId) { $detail = GoodsSkuModel::detail($goodsId, $goodsSkuId); if (!empty($detail['image'])) { $detail['goods_image'] = $detail['image']['preview_url']; } return $detail; } /** * @notes:获取店铺相关图片信息 * @param $storeInfo * @param $type * @return array * @author: wanghousheng */ public function getCommonConfig($storeInfo, $type): array { //1新品首发背景图 2限时秒杀背景图 3排行榜背景图 4大牌正品是轮播 5预售规则 6 新人首单 $data['imgurl'] = []; switch ($type) { case 1: if (!empty($storeInfo['new_product_img_id'])) { $img_ids = explode(",", $storeInfo['new_product_img_id']); $img_list = UploadFile::getFileList($img_ids); if (!empty($img_list)) { $data['imgurl'] = array_column($img_list->toArray(), 'preview_url'); } } break; case 2: if (!empty($storeInfo['flash_sale_img_id'])) { $img_ids = explode(",", $storeInfo['flash_sale_img_id']); $img_list = UploadFile::getFileList($img_ids); if (!empty($img_list)) { $data['imgurl'] = array_column($img_list->toArray(), 'preview_url'); } } break; case 3: if (!empty($storeInfo['ranking_img_id'])) { $img_ids = explode(",", $storeInfo['ranking_img_id']); $img_list = UploadFile::getFileList($img_ids); if (!empty($img_list)) { $data['imgurl'] = array_column($img_list->toArray(), 'preview_url'); } } break; case 4: if (!empty($storeInfo['big_brand_img_id'])) { $img_ids = explode(",", $storeInfo['big_brand_img_id']); $img_list = UploadFile::getFileList($img_ids); if (!empty($img_list)) { $data['imgurl'] = array_column($img_list->toArray(), 'preview_url'); } } break; case 5: if (!empty($storeInfo['presale_img_id'])) { $img_ids = explode(",", $storeInfo['presale_img_id']); $img_list = UploadFile::getFileList($img_ids); if (!empty($img_list)) { $data['imgurl'] = array_column($img_list->toArray(), 'preview_url'); } } break; case 6: if (!empty($storeInfo['new_people_order_img_id'])) { $img_ids = explode(",", $storeInfo['new_people_order_img_id']); $img_list = UploadFile::getFileList($img_ids); if (!empty($img_list)) { $data['imgurl'] = array_column($img_list->toArray(), 'preview_url'); } } break; } return $data; } /** * 获取(新品首发,大牌正品,新人首单)商铺的url */ public function getStoreImgUrl($img_id) { $data = []; if (!empty($img_id)) { $img_ids = explode(",", $img_id); $infos = \app\common\model\UploadFile::where('file_id', 'in', $img_ids)->field('file_path,domain')->select(); if (!empty($infos)) { foreach ($infos as $info) { $data[] = getUrl($info['file_path'], $info['domain']); } } } return $data; } /** * 推荐的商品列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function recommended(): array { // 获取商品推荐数据 $setting = $this->getRecommendedSetting(); // 获取商品数据 $model = new GoodsModel; if ($setting['params']['source'] === 'choice') { // 数据来源:手动 if (empty($setting['params']['goodsIds'])) return []; $goodsList = $model->getListByIdsFromApi($setting['params']['goodsIds']); } else { // 数据来源:自动 $goodsList = $model->getList([ 'status' => 10, 'categoryId' => is_array($setting['params']['auto']['category']) ? implode(',', $setting['params']['auto']['category']) : $setting['params']['auto']['category'], 'sortType' => $setting['params']['auto']['goodsSort'], ], 50); // ], $setting['params']['auto']['showNum']); $goodsList = $goodsList ? $goodsList->toArray()['data'] : []; } //if (empty($goodsList) && $goodsList->isEmpty()) { if (empty($goodsList)) { return []; } return $this->formatGoodsList($goodsList); } /** * 推荐的商品列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function recommendedNew(array $param = []): array { // 获取商品推荐数据 $setting = $this->getRecommendedSetting(); // 获取商品数据 $model = new GoodsModel; if ($setting['params']['source'] === 'choice') { // 数据来源:手动 // if (empty($setting['params']['goodsIds'])) return []; // $goodsList = $model->getListByIdsFromApi($setting['params']['goodsIds']); // 数据来源:自动 $goodsList = $model->getList([ 'status' => 10, 'goodsIds' => implode(",",$setting['params']['goodsIds']), 'sortType' => "all", 'page' => $param['page'] ?? 1, 'storeInfo' => $param['storeInfo'] ?? [], ], 10); } else { // 数据来源:自动 $goodsList = $model->getList([ 'status' => 10, 'categoryId' => is_array($setting['params']['auto']['category']) ? implode(',', $setting['params']['auto']['category']) : $setting['params']['auto']['category'], 'sortType' => $setting['params']['auto']['goodsSort'], 'page' => $param['page'] ?? 1, 'storeInfo' => $param['storeInfo'] ?? [], ], 10); } return $this->formatGoodsList($goodsList); } //大牌正品列表 public function brandList(): array { $model = new GoodsModel; $parmas = $this->request->get(); $goodsList = $model->getList($parmas); if (empty($goodsList) && $goodsList->isEmpty()) { return []; } $list = $goodsList->toArray(); //$list['data'] = $this->newFormatGoodsList($list['data']); return $list; } //排行榜分类列表 一级 public function charts($storeid): array { $model = new CategoryModel; $list = $model->where([ 'parent_id' => 0, 'status' => 1, 'is_paihang' => 1, 'store_id' => $storeid ])->order('sort asc, category_id desc')->select()->toArray(); return $list; } public function chartsGoodsList($storeid) { $model = new GoodsModel; $goodsList = $model->getList([ 'status' => 10, 'is_delete' => 0, 'is_paihang' => 1, 'store_id' => $storeid, 'categoryId' => (int)$_GET['categoryId'], ], 15); if (empty($goodsList) && $goodsList->isEmpty()) { return []; } $list = $goodsList->toArray(); //$list['data'] = $this->newFormatGoodsList($list['data']); return $list; } public function chartsGoodsJing(): array { $model = new CategoryModel; $list = $model->where([ 'parent_id' => 0, 'status' => 1, 'is_paihang' => 1, ])->order('category_id desc')->select()->toArray(); $model = new GoodsModel; foreach ($list as &$v) { $goodsList = $model->getList([ 'status' => 10, 'is_delete' => 0, 'is_paihang' => 1, 'categoryId' => $v['category_id'], ], 3); $v['goods_list'] = $goodsList->toArray()['data']; // if (!$goodsList->isEmpty()) { // $list2 = $goodsList->toArray()['data']; // $v['goods_list'] = $this->newFormatGoodsList($list2); // } } return $list; } //同城信息 public function cityInfo($storeid): array { $model = new ShopModel; $info = $model->with(['wechatImage'])->where([ 'is_delete' => 0, 'status' => 1, 'store_id' => $storeid, 'is_main' => 1, ])->field('shop_name,star,shop_hours,summary,phone, province_id,city_id,region_id,shop_id,address,latitude,longitude,wechat_img_id,history1,history2,history3,remark,history,parking_name,parking_desc,parking_latitude,parking_longitude,shop_image_id,fuwu_hours,fuwu_num')->find(); if (!$info) { return []; } $info = $info->toArray(); $info['remark'] = json_decode($info['remark'], true); $info['is_open'] = 0; $info['history'] = json_decode($info['history'], true); $info['evaluate'] = Comment::with('file') ->field(['nickname,content,image_id']) ->hidden(['file']) ->where('is_delete', '=', 0) ->where('shop_id', '=', $info['shop_id']) ->select(); $file = UploadFile::whereIn('file_id', explode(',', $info['shop_image_id']))->select(); $info['imageList'] = helper::getArrayColumn($file, 'preview_url'); $info['service_num_for_me'] = 0; $info['summary'] = mb_substr($info['summary'],0,32); //当前用户门店自提订单数 $userId = UserService::getCurrentLoginUserId(false); if ($userId) { $info['service_num_for_me'] = (new \app\api\model\Order())->where(['user_id' => $userId, 'extract_shop_id' => $info['shop_id']])->count(); } return $info; } public function hotWord($page): array { if (empty($page) || $page == 1) { $offset = 0; } else { $offset = $page * 5; } return GoodsHotWord::order('sort desc')->limit($offset, 5)->select()->toArray(); } // //抢购会场列表 // public function panicList() // { // //只拉第一个活动 // $model = new SharpActiveModel; // // $info = $model->alias('a') // ->where([ // 'a.is_delete' => 0, // 'a.status' => 1, // 'a.store_id' => $_GET['store_id'], //// 'sharpActiveTime.status' => 1, // ])->order(['a.active_id' => 'desc'])->find()->toArray(); // if (empty($info)) { // return []; // } // // $info['create_date'] = date('Y-m-d H:i:s', $info['active_date']); // $aid = $info['active_id']; // $time = ActiveTimeModel::where([ // 'status' => 1, // 'active_id' => $aid, // ])->order('active_time asc')->select()->toArray(); // if (empty($time)) { // return []; // } // // // 获取秒杀活动会场首页数据 // $service = new ActiveService; // //// $miaosha = $service->getGoodsListByActiveTimeId((int)$time[0]['active_time_id'])->toArray()['data'] ?? []; // // $info['create_time'] = date('Y-m-d', $info['active_date']) . ' ' . $time[0]['active_time'] . ':00'; // $info['lats_time'] = strtotime($info['create_time']) - time(); // $list['info'] = $info; // $list['detail'] = $time; // // return $list; // } public static function getGoodsPrice($goods_id, $price, $cost_price) { $catService = new \app\store\model\GoodsCategoryRel(); $catIds = $catService->where(['goods_id' => $goods_id])->column('category_id'); if (UserService::isstore()) { $price = $cost_price; } elseif (UserService::isPlusMember()) { $price = \app\common\model\PriceSet::membershipPrice($price, $cost_price, $catIds); } elseif (UserService::isDealerMember()) { $priceArr = \app\common\model\PriceSet::distributionPrice($price, $cost_price, $catIds); $price = $priceArr['distributionPrice']; } return $price; } /** * 格式化商品列表 * @param $goodsList * @return array */ private function newFormatGoodsList($goodsList): array { $data = []; foreach ($goodsList as $goods) { $temp = [ 'goods_id' => $goods['goods_id'], 'goods_name' => $goods['goods_name'], 'selling_point' => $goods['selling_point'], 'goods_image' => $goods['goods_image'], 'goods_price_min' => $goods['goods_price_min'],//商品价格 'goods_price_max' => $goods['goods_price_max'], 'line_price_min' => $goods['goods_price_min'],//划线价格等于市场价 'line_price_max' => $goods['line_price_max'], 'goods_sales' => $goods['goods_sales'], 'remaizhishu' => $goods['remaizhishu'], ]; $currtime = time(); if (UserService::isLogin()) { $catService = new \app\store\model\GoodsCategoryRel(); $catIds = $catService->where(['goods_id' => $goods['goods_id']])->column('category_id'); //价格判断 if (UserService::isstore()) { $priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds); $temp['goods_price_min_plus'] = $priceArr['membershipPrice']; $temp['goods_price_min_dealer'] = $priceArr['distributionPrice']; $temp['goods_price_min'] = $goods['goods_price_min_plus'];//$goods['cost_price_min'];//店长售价,展示为成本价 } elseif (UserService::isPlusMember()) { $temp['goods_price_min'] = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds); } elseif (UserService::isDealerMember()) { $priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds); $temp['goods_price_min'] = $priceArr['distributionPrice']; } if ($goods['sec_start_time'] && strtotime($goods['sec_start_time']) <= $currtime && (strtotime($goods['sec_start_time']) + 24 * 3600) >= $currtime) { $temp['goods_price_min'] = $goods['seckill_price']; } } $data[] = $temp; } return $data; } /** * 格式化商品列表 * @param $goodsList * @return array */ private function formatGoodsList($goodsList): array { // echo "
";
        // print_r($goodsList->toArray());
        // exit();
        // $data = [];
        // foreach ($goodsList as $goods) {
        //     $temp = [
        //         'goods_id' => $goods['goods_id'],
        //         'goods_name' => $goods['goods_name'],
        //         'selling_point' => $goods['selling_point'],
        //         'goods_image' => $goods['goods_image'],
        //         'goods_price_min' => $goods['goods_price_min'],//商品价格
        //         'goods_price_max' => $goods['goods_price_max'],
        //         'line_price_min' => $goods['goods_price_min'],//划线价格等于市场价
        //         'line_price_max' => $goods['line_price_max'],
        //         'goods_sales' => $goods['goods_sales'],
        //         'remaizhishu' => $goods['remaizhishu'],
        //     ];
        //     var_dump($goods->toArray());
        //     var_dump($temp);
        //     exit();
        //     if (UserService::isLogin()) {
        //         $catService = new \app\store\model\GoodsCategoryRel();
        //         $catIds = $catService->where(['goods_id' => $goods['goods_id']])->column('category_id');
        //         // $price_list_plus = $price_list_dealer = [];
        //         // foreach ($catIds as $k => $v) {
        //         //     $price_list_plus[] = GoodsPriceModel::getDiscountPrice($v, 1, $goods['goods_price_min']);
        //         //     $price_list_dealer[] = GoodsPriceModel::getDiscountPrice($v, 2, $goods['goods_price_min']);
        //         // }

        //         //价格判断
        //         if (UserService::isstore()) {

        //             //$priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);

        //             // $temp['goods_price_min_plus'] = $priceArr['membershipPrice'];
        //             // $temp['goods_price_min_dealer'] = $priceArr['distributionPrice'];
        //             $temp['goods_price_min'] = $goods['cost_price_min'];
        //             // $temp['goods_price_min_plus'] = min($price_list_plus);
        //             // $temp['goods_price_min_dealer'] = min($price_list_dealer);
        //         } elseif (UserService::isPlusMember()) {
        //             //$temp['goods_price_min_plus'] = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
        //             $temp['goods_price_min'] = \app\common\model\PriceSet::membershipPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
        //             // $temp['goods_price_min'] = $temp['goods_price_min_plus'];
        //             // $temp['goods_price_max'] = $temp['goods_price_min_plus'];
        //             //$temp['goods_price_min_plus'] = min($price_list_plus);
        //         } elseif (UserService::isDealerMember()) {
        //             $priceArr = \app\common\model\PriceSet::distributionPrice($goods['goods_price_min'], $goods['cost_price_min'], $catIds);
        //             //$temp['goods_price_min_dealer'] = $priceArr['distributionPrice'];
        //             $temp['goods_price_min'] = $priceArr['goods_price_min_dealer'];
        //             //$temp['goods_price_min_dealer'] = min($price_list_dealer);
        //         }
        //     }
        //     $data[] = $temp;
        // }
        return is_array($goodsList) ? $goodsList : $goodsList->toArray();
    }


    /**
     * 获取商品推荐设置
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getRecommendedSetting(): array
    {
        return SettingModel::getItem(SettingEnum::RECOMMENDED);
    }
}