|
@@ -6,10 +6,13 @@ use App\Model\ArticleData;
|
|
|
use App\Model\Category;
|
|
|
use App\Model\Website;
|
|
|
use App\Model\WebsiteCategory;
|
|
|
+use App\Model\Good;
|
|
|
use Hyperf\DbConnection\Db;
|
|
|
use Hyperf\RpcServer\Annotation\RpcService;
|
|
|
use App\Tools\Result;
|
|
|
use App\Model\ArticleSurvey;
|
|
|
+use App\Model\District;
|
|
|
+use App\Tools\buildMenuTree;
|
|
|
#[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
|
class NewsService implements NewsServiceInterface
|
|
|
{
|
|
@@ -486,8 +489,8 @@ class NewsService implements NewsServiceInterface
|
|
|
'article.copyfrom',
|
|
|
'article.catid')
|
|
|
->orderBy("updated_at", "desc")
|
|
|
- ->limit($data['pageSize'])
|
|
|
->offset(($data['page'] - 1) * $data['pageSize'])
|
|
|
+ ->limit($data['pageSize'])
|
|
|
->get()
|
|
|
->map(function ($article ) use ($data) {
|
|
|
$catid = $article->catid ?? 0;
|
|
@@ -1370,6 +1373,227 @@ class NewsService implements NewsServiceInterface
|
|
|
|
|
|
return Result::success($result);
|
|
|
}
|
|
|
+ // 封装处理数据的函数
|
|
|
+ function processGoods($goods, $data) {
|
|
|
+ return $goods->map(function ($good) use ($data) {
|
|
|
+ $catid = $good->catid ?? 0;
|
|
|
+ $pinyin = '';
|
|
|
+ $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
|
|
|
+ if (!empty($category->pid) && $category->pid != 0) {
|
|
|
+ $level = json_decode($category->category_arr_id);
|
|
|
+ $pinyin = WebsiteCategory::whereIn('category_id', $level)
|
|
|
+ ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
|
|
|
+ ->get(['aLIas_pinyin'])
|
|
|
+ ->pluck('aLIas_pinyin')
|
|
|
+ ->implode('/');
|
|
|
+ } else {
|
|
|
+ $pinyin = $category->aLIas_pinyin ?? '';
|
|
|
+ }
|
|
|
+ if(isset($good->city_id) &&!empty($good->city_id)){
|
|
|
+ $city = District::where('id', $good->city_id)->first(['name']);
|
|
|
+ $good->city_name = $city->name ?? '';
|
|
|
+ }
|
|
|
+ // 解析imgurl JSON并取第一条数据
|
|
|
+ $imgUrls = json_decode($good->imgurl, true);
|
|
|
+ $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
|
|
|
+ $good->pinyin = $pinyin;
|
|
|
+ return $good;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取商品模块
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ * */
|
|
|
+ public function getWebsiteshop(array $data): array
|
|
|
+ {
|
|
|
+ $input['id'] = $data['id'];
|
|
|
+ $input['website_id'] = $data['website_id'];
|
|
|
+ // return Result::success($input);
|
|
|
+ $data = json_decode($input['id'] ?? '', true) ?? [];
|
|
|
+ $result = array_map(function ($item) use ($input) {
|
|
|
+ // 检查parent元素是否存在且不是undefined
|
|
|
+ if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
|
|
|
+ list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
|
|
|
+ $website = $input['website_id'];
|
|
|
+ $query = Good::where('good.status', 2)
|
|
|
+ ->where('good.website_id', $website);
|
|
|
+ switch ($Levelid) {
|
|
|
+ case 1:
|
|
|
+ case 2:
|
|
|
+ case 3:
|
|
|
+ $goods = $query->where(function($q) use ($Levelid) {
|
|
|
+ $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 0")
|
|
|
+ ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 0");
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ $goods = $query;
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ $goods = $query->where('type_id',1);
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ $goods = $query->where('type_id',2);
|
|
|
+
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $all_goods = $goods
|
|
|
+ ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
|
|
|
+ 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
|
|
|
+ 'good.website_id')
|
|
|
+ ->orderBy('updated_at','desc')
|
|
|
+ ->offset($goodStart)
|
|
|
+ ->limit($goodNum)
|
|
|
+ ->get();
|
|
|
+ $all_goods = $this->processGoods($all_goods, $input);
|
|
|
+ }
|
|
|
+ return $all_goods;
|
|
|
+ }, $data);
|
|
|
+ return Result::success($result);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取商品分类
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ * */
|
|
|
+ public function getWebsiteshopCat(array $data): array
|
|
|
+ {
|
|
|
+ $category = WebsiteCategory::where('website_id', $data['website_id'])
|
|
|
+ ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
|
|
|
+ ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
|
|
|
+ ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
|
|
|
+ ->orderBy('sort')
|
|
|
+ ->get()
|
|
|
+ ->map(function ($item) use ($data) {
|
|
|
+ $pinyin = [];
|
|
|
+ $level = json_decode($item->category_arr_id);
|
|
|
+ // 递归获取所有父级栏目的拼音
|
|
|
+ if($item->pid!= 0){
|
|
|
+ $pinyin = WebsiteCategory::whereIn('category_id', $level)
|
|
|
+ ->orderByRaw('FIELD(category_id, '.implode(',', $level).')')
|
|
|
+ ->get(['aLIas_pinyin'])
|
|
|
+ ->pluck('aLIas_pinyin')
|
|
|
+ ->implode('/');
|
|
|
+ }else{
|
|
|
+ $pinyin = $item->aLIas_pinyin;
|
|
|
+ }
|
|
|
+ $item->aLIas_pinyin = $pinyin;
|
|
|
+ return $item;
|
|
|
+ });
|
|
|
+ $goods = Good::where('website_id', $data['website_id'])
|
|
|
+ ->where('status', 2)
|
|
|
+ ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
|
|
|
+ ->latest('updated_at')
|
|
|
+ ->offset(($data['page']-1)*$data['pageSize'])
|
|
|
+ ->limit($data['pageSize'])
|
|
|
+ ->get();
|
|
|
+ $goods = $this->processGoods($goods, $data);
|
|
|
+ if(empty($category)){
|
|
|
+ return Result::error("栏目查询失败", 0);
|
|
|
+ }
|
|
|
+ if(empty($goods)){
|
|
|
+ return Result::error("商品查询失败", 0);
|
|
|
+ }
|
|
|
+ // 调用Tool中的buildMenuTree方法构建菜单树
|
|
|
+ $cat_tree = Result::buildMenuTree($category);
|
|
|
+ $result = [
|
|
|
+ 'category' => $cat_tree,
|
|
|
+ 'goods' => $goods,
|
|
|
+ ];
|
|
|
+ // $resul['goods'] = $goods;
|
|
|
+ if(empty($result)){
|
|
|
+ return Result::error("查询失败", 0);
|
|
|
+ }
|
|
|
+ return Result::success($result);
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * 获取商品列表
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ * */
|
|
|
+ public function getWebsiteshopList(array $data): array
|
|
|
+ {
|
|
|
+ // return Result::success($data);
|
|
|
+ $where = [
|
|
|
+ 'status' => 2,
|
|
|
+ 'website_id' => $data['website_id'],
|
|
|
+ ];
|
|
|
+ if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
|
|
|
+ return Result::error("查询失败", 0);
|
|
|
+ }
|
|
|
+ if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
|
|
|
+ $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
|
|
|
+ $data['catid'] = $category->category_id ?? 0;
|
|
|
+ }
|
|
|
+ if(isset($data['keyword']) &&!empty($data['keyword'])){
|
|
|
+ array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
|
|
|
+ }
|
|
|
+ if(isset($data['type_id']) && !empty($data['type_id'])){
|
|
|
+ array_push($where, ['type_id', $data['type_id']]);
|
|
|
+ }
|
|
|
+ $query = Good::where($where)
|
|
|
+ ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
|
|
|
+ $query->where(function($q) use ($data) {
|
|
|
+ $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
|
|
|
+ ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
|
|
|
+ });
|
|
|
+ })
|
|
|
+ ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
|
|
|
+ $query->where(function($q) use ($data) {
|
|
|
+ $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
|
|
|
+ ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
|
|
|
+ });
|
|
|
+ })
|
|
|
+ ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
|
|
|
+ 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
|
|
|
+ ->latest('updated_at');
|
|
|
+ // 获取 type_id 为 1 的数据
|
|
|
+ $result['type1'] = $this->processGoods(
|
|
|
+ $query->clone()
|
|
|
+ ->where('type_id', 1)
|
|
|
+ ->offset(($data['page'] - 1) * $data['pageSize'])
|
|
|
+ ->limit($data['pageSize'])
|
|
|
+ ->get(),
|
|
|
+ $data
|
|
|
+ );
|
|
|
+ // 获取 type_id 为 2 的数据
|
|
|
+ $result['type2'] = $this->processGoods(
|
|
|
+ $query->clone()
|
|
|
+ ->where('type_id', 2)
|
|
|
+ ->offset(($data['page'] - 1) * $data['pageSize'])
|
|
|
+ ->limit($data['pageSize'])
|
|
|
+ ->get(),
|
|
|
+ $data
|
|
|
+ );
|
|
|
+ if(empty($result)){
|
|
|
+ return Result::error("查询失败", 0);
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+ return Result::success($result);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取商品详情
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ * */
|
|
|
+ public function getWebsiteshopInfo(array $data): array
|
|
|
+ {
|
|
|
+ $where = [
|
|
|
+ 'good.status' => 2,
|
|
|
+ 'good.website_id' => $data['website_id'],
|
|
|
+ ];
|
|
|
+ $goods = Good::where($where)
|
|
|
+ ->where('good.id', $data['id'])
|
|
|
+ ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
|
|
|
+ ->select('good.*','website_category.alias','website_category.category_id')
|
|
|
+ ->first();
|
|
|
+ if(empty($goods)){
|
|
|
+ return Result::error("查询失败", 0);
|
|
|
+ }
|
|
|
+ $goods->imgurl = json_decode($goods->imgurl, true);
|
|
|
+ return Result::success($goods);
|
|
|
+ }
|
|
|
}
|