NewsService.php 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\ArticleData;
  5. use App\Model\Category;
  6. use App\Model\Website;
  7. use App\Model\WebsiteCategory;
  8. use App\Model\Good;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\RpcServer\Annotation\RpcService;
  11. use App\Tools\Result;
  12. use App\Model\ArticleSurvey;
  13. use App\Model\District;
  14. use App\Tools\buildMenuTree;
  15. use App\Model\Book;
  16. use App\Model\JobHunting;
  17. use App\Model\JobRecruiting;
  18. use App\Model\JobPosition;
  19. use App\Model\JobIndustry;
  20. use App\Model\JobEnum;
  21. use App\Model\JobCompany;
  22. use App\Model\JobNature;
  23. use App\Model\User;
  24. use App\Model\JobApply;
  25. use App\Model\JobRemuse;
  26. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  27. class NewsService implements NewsServiceInterface
  28. {
  29. /**
  30. * 获取导航池列表
  31. * @param array $data
  32. * @return array
  33. */
  34. public function getCategoryList(array $data): array
  35. {
  36. $where = [];
  37. if(isset($data['name']) && $data['name']){
  38. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  39. }
  40. if(isset($data['department_id']) && $data['department_id']){
  41. array_push($where, ['category.department_id','=',$data['department_id']]);
  42. }
  43. if(isset($data['city_id']) && $data['city_id']){
  44. array_push($where, ['category.city_id','=',$data['city_id']]);
  45. }
  46. $rep = Category::where($where)
  47. ->leftJoin('district','category.city_id','district.id')
  48. ->leftJoin('department','category.department_id','department.id')
  49. ->select("category.*","district.name as city_name","department.name as department_name")
  50. ->limit($data['pageSize'])->orderBy("category.sort","desc")->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  51. $count = Category::where($where)->count();
  52. $data = [
  53. 'rows'=>$rep->toArray(),
  54. 'count'=>$count
  55. ];
  56. if(empty($rep->toArray())){
  57. return Result::error("没有导航池数据");
  58. }
  59. return Result::success($data);
  60. }
  61. /**
  62. * @param array $data
  63. * @return array
  64. */
  65. public function categoryList(array $data): array
  66. {
  67. $where[] = [
  68. 'pid','=',$data['pid']
  69. ];
  70. if(isset($data['name'])){
  71. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  72. }
  73. var_dump($where);
  74. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  75. if(empty($result)){
  76. return Result::error("没有栏目数据");
  77. }
  78. return Result::success($result);
  79. }
  80. /**
  81. * @param array $data
  82. * @return array
  83. */
  84. public function addCategory(array $data): array
  85. {
  86. $id = Category::insertGetId($data);
  87. if(empty($id)){
  88. return Result::error("添加失败");
  89. }
  90. return Result::success(['id'=>$id]);
  91. }
  92. /**
  93. * @param array $data
  94. * @return array
  95. */
  96. public function delCategory(array $data): array
  97. {
  98. $categoryList = Category::where(['pid'=>$data['id']])->get();
  99. var_dump("分类列表:",$data,$categoryList);
  100. if($categoryList->toArray()){
  101. return Result::error("分类下面有子分类不能删除");
  102. }
  103. $articleList = Article::where(['catid'=>$data['id']])->get();
  104. var_dump("文章列表:",$articleList);
  105. if($articleList->toArray()){
  106. return Result::error("分类下面有资讯不能删除");
  107. }
  108. $result = Category::where($data)->delete();
  109. if(!$result){
  110. return Result::error("删除失败");
  111. }
  112. return Result::success($result);
  113. }
  114. /**
  115. * @param array $data
  116. * @return array
  117. */
  118. public function updateCategory(array $data): array
  119. {
  120. $where = [
  121. 'id'=>$data['id']
  122. ];
  123. $result = Category::where($where)->update($data);
  124. if($result){
  125. return Result::success($result);
  126. }else{
  127. return Result::error("更新失败");
  128. }
  129. }
  130. /**
  131. * 获取导航池信息
  132. * @param array $data
  133. * @return array
  134. */
  135. public function getCategoryInfo(array $data): array
  136. {
  137. $where = [
  138. 'id'=>$data['id']
  139. ];
  140. $result = Category::where($where)->first();
  141. if($result){
  142. return Result::success($result);
  143. }else{
  144. return Result::error("更新失败");
  145. }
  146. }
  147. /**
  148. * @param array $data
  149. * @return array
  150. */
  151. public function getArticleList(array $data): array
  152. {
  153. $where= [];
  154. if(isset($data['title']) && $data['title']){
  155. array_push($where,['article.title','like','%'.$data['title'].'%']);
  156. }
  157. if(isset($data['category_name']) && $data['category_name']){
  158. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  159. }
  160. if(isset($data['author']) && $data['author']){
  161. array_push($where,['article.author','=',$data['author']]);
  162. }
  163. if(isset($data['islink']) && $data['islink']!==""){
  164. array_push($where,['article.islink','=',$data['islink']]);
  165. }
  166. if(isset($data['status']) && $data['status']!==""){
  167. array_push($where,['article.status','=',$data['status']]);
  168. }
  169. $rep = Article::where($where)
  170. ->whereNotIn('article.status',[404])
  171. ->leftJoin('category','article.catid','category.id')
  172. ->select("article.*","category.name as category_name")
  173. ->orderBy("article.id","desc")
  174. ->limit($data['pageSize'])
  175. ->offset(($data['page']-1)*$data['pageSize'])->get();
  176. $count = Article::where($where)->whereNotIn('article.status',[404])
  177. ->leftJoin('category','article.catid','category.id')->count();
  178. $data = [
  179. 'rows'=>$rep->toArray(),
  180. 'count'=>$count
  181. ];
  182. if(empty($rep)){
  183. return Result::error("没有信息数据");
  184. }
  185. return Result::success($data);
  186. }
  187. /**
  188. * @param array $data
  189. * @return array
  190. */
  191. public function addArticle(array $data): array
  192. {
  193. Db::beginTransaction();
  194. try{
  195. $articleData = $data;
  196. unset($articleData['content']);
  197. $id = Article::insertGetId($articleData);
  198. $articleDataContent = [
  199. 'article_id'=>$id,
  200. 'content'=>$data['content']
  201. ];
  202. ArticleData::insertGetId($articleDataContent);
  203. Db::commit();
  204. } catch(\Throwable $ex){
  205. Db::rollBack();
  206. var_dump($ex->getMessage());
  207. return Result::error("创建失败",0);
  208. }
  209. return Result::success(['id'=>$id]);
  210. }
  211. /**
  212. * @param array $data
  213. * @return array
  214. */
  215. public function delArticle(array $data): array
  216. {
  217. $result = Article::where($data)->update(['status'=>404]);
  218. if(!$result){
  219. return Result::error("删除失败");
  220. }
  221. return Result::success($result);
  222. }
  223. /**
  224. * @param array $data
  225. * @return array
  226. */
  227. public function updateArticle(array $data): array
  228. {
  229. Db::beginTransaction();
  230. try{
  231. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  232. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  233. $articleData = $data;
  234. unset($articleData['content']);
  235. unset($articleData['status_name']);
  236. unset($articleData['name']);
  237. unset($articleData['content']);
  238. unset($articleData['pid_arr']);
  239. unset($articleData['pid']);
  240. $id = Article::where(['id'=>$data['id']])->update($articleData);
  241. $articleDataContent = [
  242. 'content'=>$data['content']
  243. ];
  244. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  245. } catch(\Throwable $ex){
  246. Db::rollBack();
  247. var_dump($ex->getMessage());
  248. return Result::error("更新失败",0);
  249. }
  250. return Result::success([]);
  251. }
  252. /**
  253. * 更新资讯状态
  254. * @param array $data
  255. * @return array
  256. */
  257. public function upArticleStatus(array $data):array
  258. {
  259. $result = Article::where(['id'=>$data['id']])->update($data);
  260. if($result){
  261. return Result::success();
  262. }else{
  263. return Result::error("更新状态失败",0);
  264. }
  265. }
  266. /**
  267. * 获取新闻详情
  268. * @param array $data
  269. * @return array
  270. */
  271. public function getArticleInfo(array $data): array
  272. {
  273. $where = [
  274. 'article.id'=>$data['id'],
  275. // 'article.status'=>1
  276. ];
  277. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  278. if(empty($result)){
  279. return Result::error("查询失败",0);
  280. }
  281. return Result::success($result);
  282. }
  283. /**
  284. * 获取头条新闻
  285. * @param array $data
  286. * @return array
  287. */
  288. public function getWebsiteArticlett(array $data): array
  289. {
  290. // return Result::success($data['website_id']);
  291. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  292. $category = array_values(array_unique($category->toArray()));
  293. if ($category) {
  294. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  295. $where = [
  296. 'status' => 1,
  297. ];
  298. var_dump($data, 'data-----------------');
  299. // level=7 根据文章key来匹配文章
  300. if (isset($data['level']) && $data['level'] == 7) {
  301. // 根据文章id获取key
  302. // $data['id'] = 50142;
  303. if (isset($data['id']) && !empty($data['id'])) {
  304. $keyword = Article::where('id', $data['id'])->value('keyword');
  305. $keywordArray = explode(',', $keyword);
  306. $whereL7 = [];
  307. foreach ($keywordArray as $k => $v) {
  308. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  309. }
  310. // 原始查询
  311. $result['level7'] = Article::where($whereL7)
  312. ->when(!empty($data['imgnum']), function ($query) use ($data) {
  313. $query->where('article.imgurl', '!=', '')
  314. ->select('article.*')
  315. ->offset($data['placeid'])
  316. ->limit($data['imgnum']);
  317. })
  318. ->when(!empty($data['textnum']), function ($query) use ($data) {
  319. $query->select('article.*')
  320. ->offset($data['placeid'])
  321. ->limit($data['textnum']);
  322. })
  323. ->orderBy('updated_at', 'desc')
  324. ->get()
  325. ->map(function ($article ) use ($data) {
  326. $catid = $article->catid;
  327. $pinyin = '';
  328. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  329. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  330. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  331. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  332. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  333. }
  334. } else {
  335. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  336. }
  337. $article->pinyin = $pinyin;
  338. return $article;
  339. });
  340. if (empty($result)) {
  341. return Result::success();
  342. }
  343. return Result::success($result);
  344. } else {
  345. return Result::error("参数错误level=7,id不能为空", 0);
  346. }
  347. }
  348. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  349. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  350. var_dump($where, 'where-----------------');
  351. // 初始化基础查询
  352. $query = Article::where($where)
  353. ->whereIn("catid", $category)
  354. ->where(function ($query) use ($data) {
  355. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  356. ->orWhereNull("ignore_ids");
  357. });
  358. // 处理 level 相关查询条件
  359. $query->when($data['level'] == 5, function ($query) {
  360. $query->inRandomOrder()
  361. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  362. })->when($data['level'] == 4, function ($query) {
  363. $query->orderBy("article.updated_at", "desc");
  364. })->when(!empty($data['level']), function ($query) use ($data) {
  365. if ($data['level'] != 4 && $data['level'] != 5) {
  366. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  367. ->orderBy("article.updated_at", "desc");
  368. }
  369. });
  370. // 初始化结果数组
  371. $result = [];
  372. // $input = $data['website_id'];
  373. // 处理图片新闻查询
  374. if (!empty($data['imgnum'])) {
  375. // 原错误提示表明 cleanBindings 方法需要传入数组类型的参数,而实际传入了 null。
  376. // 这里克隆查询构建器,确保绑定参数为数组,避免该错误。
  377. $imgQuery = clone $query;
  378. if ($imgQuery->getBindings() === null) {
  379. $imgQuery->setBindings([]);
  380. }
  381. $result['img'] = $imgQuery->where('article.imgurl', '!=', '')
  382. ->select('article.id',
  383. 'article.title',
  384. 'article.imgurl',
  385. 'article.author',
  386. 'article.updated_at',
  387. 'article.introduce',
  388. 'article.islink',
  389. 'article.linkurl',
  390. 'article.copyfrom',
  391. 'article.catid')
  392. ->offset($data['placeid'])
  393. ->limit($data['imgnum'])
  394. ->get()
  395. ->map(function ($article ) use ($data) {
  396. $catid = $article->catid;
  397. $pinyin = '';
  398. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  399. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  400. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  401. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  402. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  403. }
  404. } else {
  405. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  406. }
  407. $article->pinyin = $pinyin;
  408. return $article;
  409. });
  410. // $result['img'] = $this->processArticles($img_article, $input);
  411. }
  412. // 处理文字新闻查询
  413. if (!empty($data['textnum'])) {
  414. $textQuery = clone $query;
  415. $result['text'] = $textQuery
  416. ->select('article.id',
  417. 'article.title',
  418. 'article.imgurl',
  419. 'article.author',
  420. 'article.updated_at',
  421. 'article.introduce',
  422. 'article.islink',
  423. 'article.linkurl',
  424. 'article.copyfrom',
  425. 'article.catid')
  426. ->offset($data['placeid'])
  427. ->limit($data['textnum'])
  428. ->get()
  429. ->map(function ($article ) use ($data) {
  430. $catid = $article->catid;
  431. $pinyin = '';
  432. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  433. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  434. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  435. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  436. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  437. }
  438. } else {
  439. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  440. }
  441. $article->pinyin = $pinyin;
  442. return $article;
  443. });
  444. // $result['text'] = $this->processArticles($text_article, $input);
  445. }
  446. if (empty($result) || count($result) == 0) {
  447. return Result::error("暂无头条新闻");
  448. }
  449. return Result::success($result);
  450. } else {
  451. return Result::error("本网站下暂无相关栏目", 0);
  452. }
  453. }
  454. /**
  455. * 获取模块新闻
  456. * @param array $data
  457. * @return array
  458. */
  459. public function getWebsiteModelArticles(array $data): array
  460. {
  461. $catid = $data['catid'];
  462. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  463. $category = $category->toArray();
  464. if (!empty($category)) {
  465. $where = [
  466. 'status' => 1,
  467. 'catid' => $catid,
  468. ];
  469. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  470. // 1:文字新闻;2:轮播图;3:图文;
  471. // 级别:0:未分类
  472. // 3:推荐图片
  473. if ($data['level'] == 1) {
  474. $data['level'] = 0;
  475. }
  476. $result = Article::where($where)
  477. ->where(function ($query) use ($data) {
  478. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  479. ->orWhereNull("level")
  480. ->orWhereRaw("level = '[]'");
  481. })
  482. ->where(function ($query) use ($data) {
  483. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  484. ->orWhereNull("ignore_ids");
  485. })
  486. ->orderBy("updated_at", "desc")
  487. ->offset($placeid)
  488. ->limit($data['pagesize'])
  489. ->get();
  490. if (empty($result)) {
  491. return Result::error("此栏目暂无相关新闻", 0);
  492. }
  493. } else {
  494. return Result::error("此网站暂无此栏目", 0);
  495. }
  496. return Result::success($result);
  497. }
  498. /**
  499. *获取新闻列表
  500. * @param array $data
  501. * @return array
  502. */
  503. public function getWebsiteArticleList(array $data): array
  504. {
  505. // return Result::success($data);
  506. $where[] = ['status', '=', 1];
  507. if (isset($data['catid']) && !empty($data['catid'])) {
  508. if (is_array($data['catid'])) {
  509. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  510. array_push($where, ['catid', 'in', $data['catid']]);
  511. } else {
  512. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  513. array_push($where, ['catid', '=', $data['catid']]);
  514. }
  515. if (empty($category)) {
  516. return Result::error("此网站暂无此栏目", 0);
  517. }
  518. }
  519. // return Result::success($where);
  520. $rep = Article::where(function ($query) use ($where) {
  521. foreach ($where as $condition) {
  522. if ($condition[1] === 'in') {
  523. $query->whereIn($condition[0], $condition[2]);
  524. } else {
  525. $query->where($condition[0], $condition[1], $condition[2]);
  526. }
  527. }
  528. })
  529. ->where(function ($query) use ($data) {
  530. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  531. ->orWhereNull("ignore_ids");
  532. })
  533. ->select(
  534. 'article.id',
  535. 'article.title',
  536. 'article.imgurl',
  537. 'article.author',
  538. 'article.updated_at',
  539. 'article.introduce',
  540. 'article.islink',
  541. 'article.linkurl',
  542. 'article.copyfrom',
  543. 'article.catid')
  544. ->orderBy("updated_at", "desc")
  545. ->offset(($data['page'] - 1) * $data['pageSize'])
  546. ->limit($data['pageSize'])
  547. ->get()
  548. ->map(function ($article ) use ($data) {
  549. $catid = $article->catid ?? 0;
  550. $pinyin = '';
  551. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  552. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  553. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  554. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  555. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  556. }
  557. $article->pinyin = $pinyin;
  558. return $article;
  559. });
  560. $count = Article::where(function ($query) use ($where) {
  561. foreach ($where as $condition) {
  562. if ($condition[1] === 'in') {
  563. $query->whereIn($condition[0], $condition[2]);
  564. } else {
  565. $query->where($condition[0], $condition[1], $condition[2]);
  566. }
  567. }
  568. })->count();
  569. $data = [
  570. 'rows' => $rep->toArray(),
  571. 'count' => $count,
  572. ];
  573. if (empty($rep)) {
  574. return Result::error("没有信息数据");
  575. }
  576. return Result::success($data);
  577. }
  578. /**
  579. * 前端-获取新闻详情
  580. * @param array $data
  581. * @return array
  582. */
  583. public function selectWebsiteArticleInfo(array $data): array
  584. {
  585. $where = [
  586. 'article.id'=>$data['id'],
  587. 'article.status'=>1
  588. ];
  589. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  590. ->where(function ($query) use ($data) {
  591. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  592. ->orWhereNull("ignore_ids");
  593. })
  594. ->first();
  595. if(empty($result)){
  596. return Result::error("暂无此新闻!",0);
  597. }
  598. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  599. if(empty($category)){
  600. return Result::error("查询失败",0);
  601. }
  602. $result['category_id'] = $category['category_id'];
  603. $result['cat_name'] = $category['alias'];
  604. return Result::success($result);
  605. }
  606. /**
  607. * 前端-获取网站调查问卷
  608. * @param array $data
  609. * @return array
  610. */
  611. public function getWebsiteSurvey(array $data): array
  612. {
  613. if(isset($data['website_id']) && !empty($data['website_id'])){
  614. $website = Website::where('id',$data['website_id'])->first();
  615. if(empty($website)){
  616. return Result::error("暂无此网站",0);
  617. }
  618. }
  619. if(isset($data['art_id']) && !empty($data['art_id'])){
  620. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  621. if(empty($article)){
  622. return Result::error("暂无此文章",0);
  623. }
  624. // return Result::error($data,0);
  625. $where['art_id'] = $data['art_id'];
  626. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  627. }else{
  628. $survey = Article::where(function ($query) {
  629. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  630. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  631. })
  632. ->where('status',1)
  633. ->where('is_survey',1)
  634. ->select('survey_id')
  635. ->orderBy('updated_at','desc')
  636. ->first();
  637. if(empty($survey)){
  638. return Result::error("暂无调查问卷",0);
  639. }
  640. $where['sur_id'] = $survey['survey_id'];
  641. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  642. }
  643. // return Result::success($where);
  644. $result = ArticleSurvey::where($where)
  645. ->where(function ($query) {
  646. $query->where('is_other', 0)
  647. ->orWhere(function ($subQuery) {
  648. $subQuery->where('is_other', 1)
  649. ->where('other_id', 0);
  650. });
  651. })
  652. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  653. ->select('article_survey.*', 'article.survey_type')
  654. ->get()->all();
  655. if(empty($result)){
  656. return Result::error("此文章暂无调查问卷",0);
  657. }
  658. return Result::success($result);
  659. }
  660. /**
  661. * 前端-添加网站调查问卷选项
  662. * @param array $data
  663. * @return array
  664. */
  665. public function addWebsiteSurveyOption(array $data): array
  666. {
  667. if(isset($data['website_id']) && !empty($data['website_id'])){
  668. $website = Website::where('id',$data['website_id'])->first();
  669. if(empty($website)){
  670. return Result::error("暂无此网站",0);
  671. }
  672. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  673. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('is_other',1)->where('other_id',0)->first();
  674. if(empty($survey)){
  675. return Result::error("此调查问卷不可添加选项",0);
  676. }
  677. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  678. $choice = [
  679. 'art_id'=>$survey['art_id'],
  680. 'website_id'=>$data['website_id'],
  681. 'survey_name'=>$survey['survey_name'],
  682. 'choice_name'=>$data['choice_name'],
  683. 'sur_id'=>$survey['sur_id'],
  684. 'is_other'=>1,
  685. 'other_id'=>$survey['id'],
  686. ];
  687. $result = ArticleSurvey::insertGetId($choice);
  688. if(empty($result)){
  689. return Result::error("添加失败",0);
  690. }
  691. return Result::success($result);
  692. }
  693. }
  694. return Result::error("添加失败",0);
  695. }
  696. return Result::error("添加失败",0);
  697. }
  698. /**
  699. * 前端-调查问卷投票
  700. * @param array $data
  701. * @return array
  702. */
  703. public function addWebsiteSurveyVote(array $data): array
  704. {
  705. // return Result::success($data);
  706. if(isset($data['website_id']) && !empty($data['website_id'])){
  707. $website = Website::where('id',$data['website_id'])->first();
  708. if(empty($website)){
  709. return Result::error("暂无此网站",0);
  710. }
  711. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  712. $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
  713. // return Result::success($survey);
  714. if(empty($is_survey)){
  715. return Result::error("此调查问卷不存在",0);
  716. }
  717. // return Result::success($survey);
  718. // 调查问卷类型
  719. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  720. //多选 若是json型则转化成数组类型
  721. if (strpos($data['choice_id'], '[') === 0) {
  722. $data['choice_id'] = json_decode($data['choice_id'], true);
  723. } else {
  724. // 单选 也转换成数组
  725. $data['choice_id'] = [$data['choice_id']];
  726. }
  727. $data['choice_id'] = array_map('intval', $data['choice_id']);
  728. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  729. // ->where('website_id',$data['website_id'])
  730. ->where('is_other',1)
  731. ->where('other_id',0)
  732. ->first();
  733. if(!empty($other)){
  734. return Result::error("请选择已有的选项!",0);
  735. }
  736. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  737. // ->where('website_id',$data['website_id'])
  738. ->where('is_other',1)
  739. ->where('other_id','!=',0)
  740. ->first();
  741. // return Result::success($choice['other']);
  742. $choice_id = $data['choice_id'];
  743. if(!empty($choice['other'])){
  744. // array_push($data['choice_id'],$choice['other']['other_id']);
  745. if(!empty($choice_id)){
  746. $key = array_search($choice['other']['id'], $choice_id);
  747. if ($key!== false) {
  748. unset($choice_id[$key]);
  749. $choice_id = array_values($choice_id);
  750. }
  751. array_push($choice_id,$choice['other']['other_id']);
  752. }else{
  753. $choice_id[0] = $choice['other']['other_id'];
  754. }
  755. array_push($data['choice_id'],$choice['other']['other_id']);
  756. }
  757. // return Result::success($data);
  758. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  759. // ->where('website_id',$data['website_id'])
  760. ->increment('results', 1);
  761. if(empty($choice)){
  762. return Result::error("请选择已有的选项!",0);
  763. }
  764. $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
  765. // ->where('website_id',$data['website_id'])
  766. ->where('other_id', 0)
  767. ->get();
  768. $survey['choice'] = $choice_id;
  769. return Result::success($survey);
  770. }
  771. return Result::error("参数必填!");
  772. }
  773. return Result::error("此调查问卷不存在",0);
  774. }
  775. return Result::error("参数必填!");
  776. }
  777. /**
  778. * 后端-获取网站调查问卷列表
  779. * @param array $data
  780. * @return array
  781. */
  782. public function getSurveyList(array $data): array
  783. {
  784. $where = [];
  785. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  786. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  787. }
  788. if(isset($data['survey_type']) && $data['survey_type'] != null){
  789. array_push($where,['survey_type','=',$data['survey_type']]);
  790. }
  791. if(isset($data['is_survey']) && $data['is_survey'] != null){
  792. array_push($where,['is_survey','=',$data['is_survey']]);
  793. }
  794. // return Result::success($where);
  795. if (!empty($where)) {
  796. $query = Article::where($where)->where(function ($q) {
  797. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  798. });
  799. } else {
  800. $query = Article::where(function ($q) {
  801. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  802. });
  803. }
  804. $count = $query->count();
  805. $survey = $query->orderByDesc('id')
  806. ->limit($data['pageSize'])
  807. ->offset(($data['page']-1)*$data['pageSize'])
  808. ->get();
  809. if(empty($survey->toArray())){
  810. return Result::error("暂无调查问卷!",0);
  811. }
  812. $result = [
  813. 'rows'=>$survey,
  814. 'count'=>$count
  815. ];
  816. return Result::success($result);
  817. }
  818. /**
  819. * 后端-获取网站调查问卷详情
  820. * @param array $data
  821. * @return array
  822. */
  823. public function getSurveyInfo(array $data): array
  824. {
  825. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  826. $where = [ 'sur_id'=>$data['sur_id']];
  827. $choose = ArticleSurvey::where($where)->where('is_other',0)
  828. ->leftJoin('article','article_survey.art_id','article.id')
  829. ->select('article_survey.*','article.survey_type')
  830. ->get()->all();
  831. if(empty($choose)){
  832. return Result::error("此调查问卷不存在",0);
  833. }
  834. $resultsArray = array_column($choose, 'results');
  835. $total = array_sum($resultsArray);
  836. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  837. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  838. // $total = 0;
  839. if(!empty($other)){
  840. $total = $total + $other['results'];
  841. $other['choice_name'] ='(其他)';
  842. if(!empty($others)){
  843. $other['hasChildren'] = true;
  844. // array_push($other,['hasChildren','=',true]);
  845. $other['children'] = $others;
  846. $other_choices = [$other->toArray()];
  847. $mer_choice = array_merge($choose,$other_choices);
  848. $value_choice = array_values($mer_choice);
  849. }
  850. else{
  851. // return Result::error('1111');
  852. $other_choices = [$other->toArray()];
  853. $other_choices = array_merge($choose,$other_choices);
  854. $value_choice = array_values($other_choices);
  855. // return Result::success($result);
  856. }
  857. }else{
  858. $value_choice = $choose;
  859. }
  860. $result = [
  861. 'choose'=>$value_choice,
  862. 'total'=>$total
  863. ];
  864. }
  865. return Result::success($result);
  866. }
  867. /**
  868. * 前端-搜索新闻列表
  869. * @param array $data
  870. * @return array
  871. */
  872. public function selectWebsiteArticle(array $data): array
  873. {
  874. $where = [];
  875. // 初始化查询构造器
  876. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  877. $query = Article::where('status', 1)
  878. ->whereIn('catid', $category)
  879. ->where(function ($query) use ($data) {
  880. $query->where(function ($subQuery) use ($data) {
  881. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  882. })->orWhereNull("ignore_ids");
  883. });
  884. // return Result::success($all_articles);
  885. // 检查是否存在 cityid 参数
  886. if (isset($data['cityid']) && !empty($data['cityid'])) {
  887. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  888. }
  889. // 检查是否存在 department_id 参数
  890. if (isset($data['department_id']) && !empty($data['department_id'])) {
  891. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  892. }
  893. // 检查是否存在 keyword 参数
  894. if (isset($data['keyword']) && !empty($data['keyword'])) {
  895. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  896. }
  897. // 计算总数
  898. $count = $query->count();
  899. // 分页查询
  900. $articles = $query
  901. ->select(
  902. 'article.id',
  903. 'article.title',
  904. 'article.imgurl',
  905. 'article.author',
  906. 'article.updated_at',
  907. 'article.introduce',
  908. 'article.islink',
  909. 'article.linkurl',
  910. 'article.copyfrom',
  911. 'article.catid',
  912. 'article.department_arr_id',
  913. 'article.city_arr_id',)
  914. ->orderBy("updated_at", "desc")
  915. ->offset(($data['page'] - 1) * $data['pageSize'])
  916. ->limit($data['pageSize'])
  917. ->get()
  918. ->map(function ($article ) use ($data) {
  919. $catid = $article->catid ?? 0;
  920. $pinyin = '';
  921. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  922. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  923. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  924. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  925. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  926. }
  927. $article->pinyin = $pinyin;
  928. return $article;
  929. });
  930. if (empty($articles)) {
  931. return Result::error("没有符合条件的资讯数据");
  932. }
  933. $data = [
  934. 'rows' => $articles,
  935. 'count' => $count,
  936. ];
  937. return Result::success($data);
  938. }
  939. /**
  940. * 模块新闻加强版
  941. * @param array $data
  942. * @return array
  943. */
  944. public function getWebsiteCatidArticle(array $data): array
  945. {
  946. // return Result::success($data);
  947. $where = [
  948. // 'category.status' => 1,
  949. 'website_category.category_id' => $data['catid'],
  950. 'website_category.website_id' => $data['website_id'],
  951. // 'article.status' => 1,
  952. ];
  953. // $category = WebsiteCategory::where($where);
  954. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  955. $child_catnum = $data['child_catnum']?? 1;
  956. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  957. $childCategoryIds = array_column($category['child'], 'category_id');
  958. if(empty($childCategoryIds)){
  959. return Result::error("暂无子栏目",0);
  960. }
  961. $imgArticles = [];
  962. $textArticles = [];
  963. // return Result::success($childCategoryIds);
  964. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  965. // 初始化子分类图片新闻和文字新闻数组
  966. // 查询所有子级栏目的图文新闻
  967. $imgArticles = Article::where('catid', $childCategoryIds[0])
  968. ->where(function ($query) use ($data) {
  969. $query->where(function ($subQuery) use ($data) {
  970. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  971. })->orWhereNull("ignore_ids");
  972. })
  973. ->where('status', 1)
  974. ->where('imgurl', '!=', '')
  975. ->select('*')
  976. ->orderBy('updated_at', 'desc')
  977. ->limit($data['child_imgnum'])
  978. ->get();
  979. }
  980. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  981. // 查询所有子级栏目的文字新闻
  982. $textArticles = Article::where('catid', $childCategoryIds[0])
  983. ->where('status', 1)
  984. ->where(function ($query) use ($data) {
  985. $query->where(function ($subQuery) use ($data) {
  986. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  987. })->orWhereNull("ignore_ids");
  988. })
  989. // ->where(function ($query) {
  990. // $query->whereNull('imgurl')
  991. // ->orWhere('imgurl', '');
  992. // })
  993. ->select('*')
  994. ->orderBy('updated_at', 'desc')
  995. ->limit($data['child_textnum'])
  996. ->get();
  997. }
  998. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  999. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  1000. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  1001. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  1002. return $child;
  1003. }, $category['child']);
  1004. }
  1005. // }
  1006. if (isset($data['img_num']) && !empty($data['img_num'])) {
  1007. $category['img'] = WebsiteCategory::where($where)
  1008. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1009. ->where('article.status', 1)
  1010. ->where('article.imgurl', '!=', '')
  1011. ->select('article.*','website_category.category_id','website_category.alias')
  1012. ->orderBy('article.updated_at', 'desc')
  1013. ->limit($data['img_num'])
  1014. ->get();
  1015. }
  1016. if (isset($data['text_num']) && !empty($data['text_num'])) {
  1017. $category['text'] = WebsiteCategory::where($where)
  1018. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1019. ->where('article.status', 1)
  1020. // ->where(function ($query) {
  1021. // $query->whereNull('article.imgurl')
  1022. // ->orWhere('article.imgurl', '');
  1023. // })
  1024. ->select('article.*','website_category.category_id','website_category.alias')
  1025. ->orderBy('article.updated_at', 'desc')
  1026. ->limit($data['text_num'])
  1027. ->get();
  1028. }
  1029. // $category = $category->get();
  1030. if(empty($category)){
  1031. return Result::error("查询失败", 0);
  1032. }
  1033. return Result::success($category);
  1034. }
  1035. /**
  1036. * 模块新闻加强plus版
  1037. * @param array $data
  1038. * @return array
  1039. */
  1040. public function getWebsiteAllArticle(array $data): array
  1041. {
  1042. // 修正传入的字符串,将单引号替换为双引号
  1043. $input['id'] = $data['id'];
  1044. $input['website_id'] = $data['website_id'];
  1045. // 将 JSON 字符串转换为 PHP 数组
  1046. $data = json_decode($input['id'], true);
  1047. // 使用 array_map 处理每个元素
  1048. $result = array_map(function ($item) use ($input) {
  1049. // 检查parent元素是否存在且不是undefined
  1050. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1051. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1052. $website = [
  1053. 'website_id' => $input['website_id'],
  1054. ];
  1055. // 查询栏目名称
  1056. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1057. if(empty($category)){
  1058. $parent_alias = '';
  1059. $parent_pinyin = null;
  1060. $imgArticles = [];
  1061. $textArticles = [];
  1062. }else{
  1063. $parent_alias = $category->alias ?? '';
  1064. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1065. // 查找子分类ID数组
  1066. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1067. array_push($childCategoryIds,$parentCatId);
  1068. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1069. if($parentImgNum!=0){
  1070. // 查询图片新闻
  1071. $imgArticles = Article::where('article.status', 1)
  1072. ->where(function($query) use ($parentCatId) {
  1073. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1074. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1075. })
  1076. ->where(function ($query) use ($website) {
  1077. $query->where(function ($subQuery) use ($website) {
  1078. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1079. ->orWhereNull("ignore_ids");
  1080. });
  1081. })
  1082. ->where('imgurl', '!=', '')
  1083. ->leftJoin('website_category', function($join) use ($website) {
  1084. $join->on('article.catid', '=', 'website_category.category_id')
  1085. ->where('website_category.website_id', '=', $website['website_id']);
  1086. })
  1087. ->select(
  1088. 'article.id',
  1089. 'article.title',
  1090. 'article.imgurl',
  1091. 'article.author',
  1092. 'article.updated_at',
  1093. 'article.introduce',
  1094. 'article.islink',
  1095. 'article.linkurl',
  1096. 'article.copyfrom',
  1097. 'article.catid',
  1098. 'website_category.alias as category_name',
  1099. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1100. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1101. )
  1102. ->orderBy('updated_at', 'desc')
  1103. ->limit($parentImgNum)
  1104. ->get()->all();
  1105. // 查询文字新闻
  1106. }
  1107. if($parentTextNum!=0){
  1108. $textArticles = [];
  1109. $textArticles = Article::where('article.status', 1)
  1110. ->where(function($query) use ($parentCatId) {
  1111. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1112. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1113. })
  1114. ->where(function ($query) use ($website) {
  1115. $query->where(function ($subQuery) use ($website) {
  1116. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1117. ->orWhereNull("ignore_ids");
  1118. });
  1119. })
  1120. ->leftJoin('website_category', function($join) use ($website) {
  1121. $join->on('article.catid', '=', 'website_category.category_id')
  1122. ->where('website_category.website_id', '=', $website['website_id']);
  1123. })
  1124. ->select(
  1125. 'article.id',
  1126. 'article.title',
  1127. 'article.imgurl',
  1128. 'article.author',
  1129. 'article.updated_at',
  1130. 'article.introduce',
  1131. 'article.islink',
  1132. 'article.linkurl',
  1133. 'article.copyfrom',
  1134. 'article.catid',
  1135. 'website_category.alias as category_name',
  1136. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1137. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1138. )
  1139. ->orderBy('updated_at', 'desc')
  1140. ->limit($parentTextNum)
  1141. ->get()->all();
  1142. }
  1143. }
  1144. }
  1145. $resultItem = [
  1146. 'alias' => $parent_alias ?? '',
  1147. 'category_id' => $parentCatId ?? 0,
  1148. 'pinyin' => $parent_pinyin ?? null,
  1149. 'imgnum' => $imgArticles ?? [],
  1150. 'textnum' => $textArticles ?? [],
  1151. ];
  1152. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1153. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1154. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1155. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1156. ->get()->all();
  1157. if (!empty($childCategory)) {
  1158. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1159. // 查询子栏目名称
  1160. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1161. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1162. ->first();
  1163. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1164. $childImgArticles = [];
  1165. $childTextArticles = [];
  1166. $resultItem['child'] = [];
  1167. }else{
  1168. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1169. // 查询子栏目图片新闻
  1170. $childImgArticles = Article::where('catid', $childCatId)
  1171. ->where('status', 1)
  1172. ->where(function ($query) use ($website) {
  1173. $query->where(function ($subQuery) use ($website) {
  1174. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1175. })->orWhereNull("ignore_ids");
  1176. })
  1177. ->select(
  1178. 'article.id',
  1179. 'article.title',
  1180. 'article.imgurl',
  1181. 'article.author',
  1182. 'article.updated_at',
  1183. 'article.introduce',
  1184. 'article.islink',
  1185. 'article.linkurl',
  1186. 'article.copyfrom',
  1187. DB::raw("'$child_pinyin' as pinyin"))
  1188. ->where('imgurl', '!=', '')
  1189. ->orderBy('updated_at', 'desc')
  1190. ->limit($childImgNum)
  1191. ->get()->all();
  1192. // 查询子栏目文字新闻
  1193. $childTextArticles = Article::where('catid', $childCatId)
  1194. ->where('status', 1)
  1195. ->where(function ($query) use ($website) {
  1196. $query->where(function ($subQuery) use ($website) {
  1197. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1198. })->orWhereNull("ignore_ids");
  1199. })
  1200. ->select(
  1201. 'article.id',
  1202. 'article.title',
  1203. 'article.author',
  1204. 'article.updated_at',
  1205. 'article.introduce',
  1206. 'article.islink',
  1207. 'article.linkurl',
  1208. 'article.copyfrom',
  1209. DB::raw("'$child_pinyin' as pinyin"))
  1210. ->orderBy('updated_at', 'desc')
  1211. ->limit($childTextNum)
  1212. ->get()->all();
  1213. $resultItem['child'] = [
  1214. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1215. 'category_id' => $childCatId,
  1216. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1217. 'all_childcat' => $childCategory,
  1218. 'imgnum' => $childImgArticles,
  1219. 'textnum' => $childTextArticles,
  1220. ];
  1221. }
  1222. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1223. }
  1224. }else{
  1225. $resultItem['child'] = [];
  1226. }
  1227. return $resultItem;
  1228. }, $data);
  1229. return Result::success($result);
  1230. // return Result::success($data);
  1231. }
  1232. /**
  1233. * 乡村网-获取特殊新闻模块
  1234. * @param array $data
  1235. * @return array
  1236. */
  1237. public function getWebsiteArticles(array $data): array
  1238. {
  1239. $input['id'] = $data['id'];
  1240. $input['website_id'] = $data['website_id'];
  1241. $data = json_decode($input['id'], true);
  1242. // 使用 array_map 处理每个元素
  1243. $result = array_map(function ($item) use ($input) {
  1244. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1245. $website = [
  1246. 'website_id' => $input['website_id']
  1247. ];
  1248. // 查询栏目名称
  1249. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1250. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1251. if(empty($category)){
  1252. $imgArticles = [];
  1253. $textArticles = [];
  1254. // return Result::error("暂无此栏目",0);
  1255. }else{
  1256. $child_category = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1257. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1258. // return Result::success($website);
  1259. // 查询图片新闻
  1260. // 合并查询条件
  1261. $baseQuery = Article::where(function ($query) use ($website) {
  1262. $query->where(function ($subQuery) use ($website) {
  1263. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1264. })->orWhereNull("ignore_ids");
  1265. })
  1266. ->where('website_category.website_id', $website['website_id'])
  1267. ->where('article.status', 1)
  1268. ->leftJoin('website_category', 'website_category.category_id', 'article.catid');
  1269. // 查询文字新闻
  1270. $textArticles = clone $baseQuery;
  1271. $textArticles = $textArticles->whereIn('catid',$child_category)
  1272. ->select(
  1273. 'article.id',
  1274. 'article.title',
  1275. // 'article.imgurl',
  1276. 'article.author',
  1277. 'article.updated_at',
  1278. 'article.introduce',
  1279. 'article.islink',
  1280. 'article.linkurl',
  1281. 'article.copyfrom',
  1282. 'website_category.category_id',
  1283. 'website_category.alias',
  1284. 'website_category.aLIas_pinyin'
  1285. )
  1286. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1287. ->orderBy('article.updated_at', 'desc')
  1288. ->limit($parentTextNum)
  1289. ->get()
  1290. ->all();
  1291. if(empty($textArticles)){
  1292. $textArticles = clone $baseQuery;
  1293. $textArticles = $textArticles->where('catid',$parentCatId)
  1294. ->select(
  1295. 'article.id',
  1296. 'article.title',
  1297. // 'article.imgurl',
  1298. 'article.author',
  1299. 'article.updated_at',
  1300. 'article.introduce',
  1301. 'article.islink',
  1302. 'article.linkurl',
  1303. 'article.copyfrom',
  1304. 'website_category.category_id',
  1305. 'website_category.alias',
  1306. 'website_category.aLIas_pinyin'
  1307. )
  1308. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1309. ->orderBy('article.updated_at', 'desc')
  1310. ->limit($parentTextNum)
  1311. ->get()
  1312. ->all();
  1313. }
  1314. // 查询图片新闻
  1315. $imgArticles = clone $baseQuery;
  1316. $imgArticles = $imgArticles->where('imgurl', '!=', '')->whereIn('catid',$child_category)
  1317. ->select(
  1318. 'article.id',
  1319. 'article.title',
  1320. 'article.imgurl',
  1321. 'article.author',
  1322. 'article.updated_at',
  1323. 'article.introduce',
  1324. 'article.islink',
  1325. 'article.linkurl',
  1326. 'article.copyfrom',
  1327. 'website_category.category_id',
  1328. 'website_category.alias',
  1329. 'website_category.aLIas_pinyin'
  1330. )
  1331. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1332. ->orderBy('article.updated_at', 'desc')
  1333. ->limit($parentImgNum)
  1334. ->get()
  1335. ->all();
  1336. if(empty($imgArticles)){
  1337. $imgArticles = clone $baseQuery;
  1338. $imgArticles = $imgArticles->where('imgurl', '!=', '')->where('catid',$parentCatId)
  1339. ->select(
  1340. 'article.id',
  1341. 'article.title',
  1342. 'article.imgurl',
  1343. 'article.author',
  1344. 'article.updated_at',
  1345. 'article.introduce',
  1346. 'article.islink',
  1347. 'article.linkurl',
  1348. 'article.copyfrom',
  1349. 'website_category.category_id',
  1350. 'website_category.alias',
  1351. 'website_category.aLIas_pinyin'
  1352. )
  1353. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1354. ->orderBy('article.updated_at', 'desc')
  1355. ->limit($parentImgNum)
  1356. ->get()
  1357. ->all();
  1358. }
  1359. }
  1360. $resultItem = [
  1361. 'alias' => $category ? $category->alias : null,
  1362. 'category_id' => $parentCatId,
  1363. 'pinyin' => $pinyin ? $pinyin : null,
  1364. 'imgnum' => $imgArticles ?? [],
  1365. 'textnum' => $textArticles ?? [],
  1366. ];
  1367. return $resultItem;
  1368. }, $data);
  1369. return Result::success($result);
  1370. }
  1371. // 封装处理商品的路由问题
  1372. function processGoods($goods, $data) {
  1373. return $goods->map(function ($good) use ($data) {
  1374. $catid = $good->catid ?? 0;
  1375. // $pinyin = '';
  1376. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1377. if (!empty($category->pid) && $category->pid != 0) {
  1378. $level = json_decode($category->category_arr_id);
  1379. $pinyin = WebsiteCategory::where('website_id', $data['website_id'])
  1380. ->whereIn('category_id', $level)
  1381. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1382. ->get(['aLIas_pinyin'])
  1383. ->pluck('aLIas_pinyin')
  1384. ->implode('/');
  1385. } else {
  1386. $pinyin = $category->aLIas_pinyin ?? '';
  1387. }
  1388. if(isset($good->city_id) &&!empty($good->city_id)){
  1389. $city = District::where('id', $good->city_id)->first(['name']);
  1390. $good->city_name = $city->name ?? '';
  1391. }
  1392. // 解析imgurl JSON并取第一条数据
  1393. $imgUrls = json_decode($good->imgurl, true);
  1394. $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
  1395. $good->pinyin = $pinyin;
  1396. return $good;
  1397. });
  1398. }
  1399. /**
  1400. * 获取商品模块
  1401. * @param array $data
  1402. * @return array
  1403. * */
  1404. public function getWebsiteshop(array $data): array
  1405. {
  1406. $input['id'] = $data['id'];
  1407. $input['website_id'] = $data['website_id'];
  1408. $catid = $data['catid'];
  1409. $data = json_decode($input['id'] ?? '', true) ?? [];
  1410. $result['goods'] = array_map(function ($item) use ($input) {
  1411. // 检查parent元素是否存在且不是undefined
  1412. if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
  1413. list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
  1414. $website = $input['website_id'];
  1415. $query = Good::where('good.status', 2)
  1416. ->where('good.website_id', $website);
  1417. switch ($Levelid) {
  1418. case 1:
  1419. case 2:
  1420. case 3:
  1421. $goods = $query->where(function($q) use ($Levelid) {
  1422. $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 1")
  1423. ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 1");
  1424. });
  1425. break;
  1426. case 4:
  1427. $goods = $query;
  1428. break;
  1429. case 5:
  1430. $goods = $query->where('type_id',1);
  1431. break;
  1432. case 6:
  1433. $goods = $query->where('type_id',2);
  1434. break;
  1435. default:
  1436. return [];
  1437. }
  1438. $all_goods = $goods
  1439. ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
  1440. 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
  1441. 'good.website_id')
  1442. ->orderBy('updated_at','desc')
  1443. ->offset($goodStart)
  1444. ->limit($goodNum)
  1445. ->get();
  1446. $web['website_id'] = $website;
  1447. $all_goods = $this->processGoods($all_goods, $web);
  1448. }
  1449. return $all_goods;
  1450. }, $data);
  1451. $website = $input['website_id'];
  1452. $result['article'] = Article::where(function ($query) use ($website) {
  1453. $query->where(function ($subQuery) use ($website) {
  1454. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website) . "') = 0");
  1455. })->orWhereNull("ignore_ids");
  1456. })
  1457. ->where('catid', $catid)
  1458. ->where('article.status', 1)
  1459. ->leftJoin('article_data', 'article_data.article_id', 'article.id')
  1460. ->select('article.id', 'article.title', 'article.updated_at', 'introduce', 'islink', 'linkurl','article_data.content')
  1461. ->orderBy('article.updated_at', 'desc')
  1462. ->first();
  1463. return Result::success($result);
  1464. }
  1465. /**
  1466. * 获取商品分类
  1467. * @param array $data
  1468. * @return array
  1469. * */
  1470. public function getWebsiteshopCat(array $data): array
  1471. {
  1472. $website = $data['website_id'];
  1473. $category = WebsiteCategory::where('website_id', $website)
  1474. ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
  1475. ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
  1476. ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
  1477. ->orderBy('sort')
  1478. ->get();
  1479. $cat = $category->map(function ($item) use ($website) {
  1480. $cat_arr_id = json_decode($item->category_arr_id, true) ?? [];
  1481. $pid = $item->pid?? 0;
  1482. if(!empty($cat_arr_id) && is_array($cat_arr_id) && $pid != 0){
  1483. $pinyin = WebsiteCategory::whereIn('category_id', $cat_arr_id)
  1484. ->where('website_id', $website)
  1485. ->orderByRaw('FIELD(category_id, '. implode(',', $cat_arr_id). ')')
  1486. ->get(['aLIas_pinyin'])
  1487. ->pluck('aLIas_pinyin')
  1488. ->implode('/');
  1489. }else{
  1490. $pinyin = $item->aLIas_pinyin ?? '';
  1491. }
  1492. $item->pinyin = $pinyin;
  1493. });
  1494. if(empty($category)){
  1495. return Result::error("栏目查询失败", 0);
  1496. }
  1497. $cat_tree = Result::buildMenuTree($category);
  1498. $web['website_id'] = $website;
  1499. $goods = Good::where('website_id', $website)
  1500. ->where('status', 2)
  1501. ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
  1502. ->latest('updated_at')
  1503. ->offset(($data['page']-1)*$data['pageSize'])
  1504. ->limit($data['pageSize'])
  1505. ->get();
  1506. if($goods->isEmpty()){
  1507. return Result::error("商品查询失败", 0);
  1508. }
  1509. if($goods->count() > 1){
  1510. $goods = $this->processGoods($goods, $web);
  1511. }else{
  1512. $catid = $goods[0]['catid'] ?? 0;
  1513. $good_category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1514. if (!empty($good_category->pid) && $good_category->pid != 0) {
  1515. $level = json_decode($good_category->category_arr_id);
  1516. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1517. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1518. ->get(['aLIas_pinyin'])
  1519. ->pluck('aLIas_pinyin')
  1520. ->implode('/');
  1521. } else {
  1522. $pinyin = $good_category->aLIas_pinyin ?? '';
  1523. }
  1524. $goods[0]['pinyin'] = $pinyin;
  1525. }
  1526. $result = [
  1527. 'category' => $cat_tree,
  1528. 'goods' => $goods,
  1529. ];
  1530. // $resul['goods'] = $goods;
  1531. if(empty($result)){
  1532. return Result::error("查询失败", 0);
  1533. }
  1534. return Result::success($result);
  1535. }
  1536. /*
  1537. * 获取商品列表
  1538. * @param array $data
  1539. * @return array
  1540. * */
  1541. public function getWebsiteshopList(array $data): array
  1542. {
  1543. // return Result::success($data);
  1544. $where = [
  1545. 'status' => 2,
  1546. 'website_id' => $data['website_id'],
  1547. ];
  1548. if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
  1549. return Result::error("查询失败", 0);
  1550. }
  1551. if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
  1552. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
  1553. $data['catid'] = $category->category_id ?? 0;
  1554. }
  1555. if(isset($data['keyword']) &&!empty($data['keyword'])){
  1556. array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
  1557. }
  1558. if(isset($data['type_id']) && !empty($data['type_id'])){
  1559. array_push($where, ['type_id', $data['type_id']]);
  1560. }
  1561. $query = Good::where($where)
  1562. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  1563. $query->where(function($q) use ($data) {
  1564. $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
  1565. ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
  1566. });
  1567. })
  1568. ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
  1569. $query->where(function($q) use ($data) {
  1570. $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
  1571. ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
  1572. });
  1573. })
  1574. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
  1575. 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
  1576. ->latest('updated_at');
  1577. // 获取 type_id 为 1 的数据
  1578. $result['type1'] = $this->processGoods(
  1579. $query->clone()
  1580. ->where('type_id', 1)
  1581. ->offset(($data['page'] - 1) * $data['pageSize'])
  1582. ->limit($data['pageSize'])
  1583. ->get(),
  1584. $data
  1585. );
  1586. // 获取 type_id 为 2 的数据
  1587. $result['type2'] = $this->processGoods(
  1588. $query->clone()
  1589. ->where('type_id', 2)
  1590. ->offset(($data['page'] - 1) * $data['pageSize'])
  1591. ->limit($data['pageSize'])
  1592. ->get(),
  1593. $data
  1594. );
  1595. if(empty($result)){
  1596. return Result::error("查询失败", 0);
  1597. }
  1598. return Result::success($result);
  1599. }
  1600. /**
  1601. * 获取商品详情
  1602. * @param array $data
  1603. * @return array
  1604. * */
  1605. public function getWebsiteshopInfo(array $data): array
  1606. {
  1607. $where = [
  1608. 'good.status' => 2,
  1609. 'good.website_id' => $data['website_id'],
  1610. 'good.id' => $data['id'],
  1611. ];
  1612. $goods = Good::where($where)
  1613. ->where('good.id', $data['id'])
  1614. ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
  1615. ->select('good.*','website_category.alias','website_category.category_id')
  1616. ->first();
  1617. if(empty($goods)){
  1618. return Result::error("查询失败", 0);
  1619. }
  1620. $goods->imgurl = json_decode($goods->imgurl, true);
  1621. return Result::success($goods);
  1622. }
  1623. /**
  1624. * 封装处理文章的路由问题
  1625. * */
  1626. function processArticles($articles, $data) {
  1627. if (!is_array($data)) {
  1628. // 可以根据实际情况进行日志记录或抛出异常
  1629. // 这里简单输出错误信息
  1630. echo "Error: \$data is not an array in processArticles. It is of type ". gettype($data). PHP_EOL;
  1631. return $articles;
  1632. }
  1633. return $articles->map(function ($article) use ($data) {
  1634. $catid = $article->cat_arr_id?? '';
  1635. $level = json_decode($catid, true);
  1636. $pinyin = '';
  1637. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1638. if (!empty($category->pid) && $category->pid!= 0) {
  1639. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1640. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1641. ->get(['aLIas_pinyin'])
  1642. ->pluck('aLIas_pinyin')
  1643. ->implode('/');
  1644. } else {
  1645. $pinyin = $category->aLIas_pinyin ?? '';
  1646. }
  1647. $article->pinyin = $pinyin;
  1648. return $article;
  1649. });
  1650. }
  1651. // 封装处理由问题
  1652. function processJob($job, $data) {
  1653. return $job->map(function ($job) use ($data) {
  1654. $category = $job->cat_arr_id?? '';
  1655. $cityid = $job->city_arr_id?? '';
  1656. $city = json_decode($cityid, true);
  1657. $pinyin = '';
  1658. $level = json_decode($category, true);
  1659. // 路由
  1660. if(!empty($level) && is_array($level)){
  1661. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1662. ->where('website_id', $data['website_id']) // 添加网站ID条件
  1663. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1664. ->get(['aLIas_pinyin'])
  1665. ->pluck('aLIas_pinyin')
  1666. ->implode('/');
  1667. if(empty($pinyin)){
  1668. $pinyin = $pinyin->aLIas_pinyin?? '';
  1669. }
  1670. $job->pinyin = $pinyin;
  1671. }
  1672. // 取职位-城市 市??省
  1673. if(!empty($city) && is_array($city)){
  1674. if(isset($city[1] ) && !empty($city[1])){
  1675. $city = District::where('id', $city[1])->first(['name']);
  1676. $job->city_name = $city->name?? '';
  1677. }else if(isset($city[0] ) && !empty($city[0])){
  1678. $city = District::where('id', $city[0])->first(['name']);
  1679. $job->city_name = $city->name?? '';
  1680. }else{
  1681. $job->city_name = '全国';
  1682. }
  1683. }
  1684. // 获取简历最后一级地区
  1685. if(isset($job->city_id) &&!empty($job->city_id)){
  1686. $city = District::where('id', $job->city_id)->first(['name']);
  1687. $job->hunt_cityname = $city->name?? '';
  1688. }
  1689. // 组合详细地址
  1690. if(isset($job->address_arr_id) && !empty($job->address_arr_id)){
  1691. $address_id = json_decode($job->address_arr_id, true) ?? [];
  1692. if(is_array($address_id) && !empty($address_id)){
  1693. $address = District::whereIn('id', $address_id)
  1694. ->orderBy('level', 'asc')
  1695. ->get(['name'])
  1696. ->pluck('name')
  1697. ->implode('');
  1698. // $job->address_name = $address ?? '';
  1699. $job->address_name = ($address ?? '') . ($job->address ?? '');
  1700. }
  1701. }
  1702. // 取行业
  1703. if(!empty($job->hy_id) || !empty($job->industry) || !empty($job->company_hy_id)){
  1704. $hy_name = JobIndustry::when($job, function ($query) use ($job) {
  1705. if(!empty($job->industry)){
  1706. $query->where('hyid', $job->industry);
  1707. }else if(!empty($job->hy_id)){
  1708. $query->where('hyid', $job->hy_id);
  1709. }else{
  1710. $query->where('hyid', $job->company_hy_id);
  1711. }
  1712. })->first(['hyname']);
  1713. $job->hy_name = $hy_name->hyname?? '';
  1714. }
  1715. // 取职位
  1716. if((isset($job->zw_id) && !empty($job->zw_id)) || (isset($job->job) && !empty($job->job))){
  1717. $zwid = $job->job?? $job->zw_id;
  1718. $zw_name = JobPosition::where('zwid', $zwid)->first(['zwname']);
  1719. $job->zw_name = $zw_name->zwname?? '';
  1720. }
  1721. // 取具体职位
  1722. if((isset($job->jtzw_id) &&!empty($job->jtzw_id)) || (isset($job->job_name_get) && !empty($job->job_name_get))){
  1723. $jtzwid = $job->job_name_get ?? $job->jtzw_id;
  1724. $jtzw_name = JobPosition::where('zwid', $jtzwid)->first(['zwname']);
  1725. $job->jtzw_name = $jtzw_name->zwname?? '';
  1726. }
  1727. // 取工作经验
  1728. if(isset($job->experience) &&!empty($job->experience)){
  1729. $experience = JobEnum::where('egroup','years')->where('evalue',$job->experience)->first(['ename']);
  1730. $job->experience_name = $experience->ename?? '';
  1731. }
  1732. // 取学历
  1733. if(isset($job->educational) &&!empty($job->educational)){
  1734. $education = JobEnum::where('egroup','education')->where('evalue',$job->educational)->first(['ename']);
  1735. $job->education_name = $education->ename?? '';
  1736. }
  1737. // 语言
  1738. if(isset($job->language) &&!empty($job->language)){
  1739. $language = JobEnum::where('egroup','language')->where('evalue',$job->language)->first(['ename']);
  1740. $job->language_name = $language->ename?? '';
  1741. }
  1742. // 薪资
  1743. if(isset($job->salary) &&!empty($job->salary)){
  1744. $salary = JobEnum::where('egroup','income')->where('evalue',$job->salary)->first(['ename']);
  1745. $job->salary_name = $salary->ename?? '';
  1746. }
  1747. // 职位性质
  1748. if(isset($job->nature_id) &&!empty($job->nature_id)){
  1749. $job_nature = JobEnum::where('egroup','nature')->where('evalue',$job->nature_id)->first(['ename']);
  1750. $job->job_nature_name = $job_nature->ename?? '';
  1751. }
  1752. // 公司规模
  1753. if(isset($job->company_size) &&!empty($job->company_size)){
  1754. $company_size = JobEnum::where('egroup','cosize')->where('evalue',$job->company_size)->first(['ename']);
  1755. $job->company_size_name = $company_size->ename?? '';
  1756. }
  1757. // 公司性质
  1758. if(isset($job->company_nature) && !empty($job->company_nature)){
  1759. $company_nature = JobNature::where('id',$job->company_nature)->first(['nature_name']);
  1760. $job->company_nature_name = $company_nature->nature_name?? '';
  1761. }
  1762. // $job->pinyin = $pinyin;
  1763. return $job;
  1764. });
  1765. }
  1766. /**
  1767. * 获取书籍模块
  1768. * @param array $data
  1769. * @return array
  1770. * */
  1771. public function getWebsiteBook(array $data): array
  1772. {
  1773. $input['id'] = $data['id'];
  1774. $input['website_id'] = $data['website_id'];
  1775. // 将 JSON 字符串转换为 PHP 数组
  1776. $data = json_decode($input['id'], true);
  1777. // 使用 array_map 处理每个元素
  1778. $result = array_map(function ($item) use ($input) {
  1779. // 检查parent元素是否存在且不是undefined
  1780. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1781. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1782. $website = [
  1783. 'website_id' => $input['website_id'],
  1784. ];
  1785. // 查询栏目名称
  1786. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1787. if(empty($category)){
  1788. $parent_alias = '';
  1789. $parent_pinyin = null;
  1790. $imgBooks = [];
  1791. $textBooks = [];
  1792. }else{
  1793. $parent_alias = $category->alias ?? '';
  1794. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1795. // 查找子分类ID数组
  1796. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1797. array_push($childCategoryIds,$parentCatId);
  1798. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1799. if($parentImgNum!=0){
  1800. // 查询图片新闻
  1801. $imgBooks = Book::where(function($query) use ($parentCatId) {
  1802. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1803. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1804. })
  1805. ->where('book.status', 2)
  1806. ->where('book.img_url', '!=', '')
  1807. ->leftJoin('website_category', function($join) use ($website) {
  1808. $join->on('book.cat_id', '=', 'website_category.category_id')
  1809. ->where('website_category.website_id', '=', $website['website_id']);
  1810. })
  1811. ->select(
  1812. 'book.id',
  1813. 'book.title',
  1814. 'book.img_url',
  1815. 'book.price',
  1816. 'book.market_price',
  1817. 'book.description',
  1818. 'book.cat_id',
  1819. 'book.description',
  1820. 'book.updated_at',
  1821. 'website_category.alias as category_name',
  1822. DB::raw("CASE WHEN book.cat_id = $parentCatId THEN '$parent_pinyin'
  1823. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1824. )
  1825. ->orderBy('updated_at', 'desc')
  1826. ->limit($parentImgNum)
  1827. ->get()->all();
  1828. // 查询文字新闻
  1829. }
  1830. if($parentTextNum!=0){
  1831. $textBooks = [];
  1832. $textBooks = Book::where(function($query) use ($parentCatId) {
  1833. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1834. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1835. })
  1836. ->where('book.status', 2)
  1837. ->leftJoin('website_category', function($join) use ($website) {
  1838. $join->on('book.cat_id', '=', 'website_category.category_id')
  1839. ->where('website_category.website_id', '=', $website['website_id']);
  1840. })
  1841. ->select(
  1842. 'book.id',
  1843. 'book.title',
  1844. 'book.img_url',
  1845. 'book.price',
  1846. 'book.market_price',
  1847. 'book.description',
  1848. 'book.cat_id',
  1849. 'book.description',
  1850. 'book.updated_at',
  1851. 'website_category.alias as category_name',
  1852. DB::raw("CASE WHEN book.cat_id = $parentCatId THEN '$parent_pinyin'
  1853. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1854. )
  1855. ->orderBy('updated_at', 'desc')
  1856. ->limit($parentTextNum)
  1857. ->get()->all();
  1858. }
  1859. }
  1860. }
  1861. $resultItem = [
  1862. 'alias' => $parent_alias ?? '',
  1863. 'category_id' => $parentCatId ?? 0,
  1864. 'pinyin' => $parent_pinyin ?? null,
  1865. 'imgnum' => $imgBooks ?? [],
  1866. 'textnum' => $textBooks ?? [],
  1867. ];
  1868. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1869. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1870. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1871. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1872. ->get()->all();
  1873. if (!empty($childCategory)) {
  1874. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1875. // 查询子栏目名称
  1876. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1877. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1878. ->first();
  1879. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1880. $childImgArticles = [];
  1881. $childTextArticles = [];
  1882. $resultItem['child'] = [];
  1883. }else{
  1884. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1885. // 查询子栏目图片新闻
  1886. $childImgArticles = Book::where('cat_id', $childCatId)
  1887. ->where('status', 2)
  1888. ->where('img_url', '!=', '')
  1889. ->leftJoin('website_category', function($join) use ($website) {
  1890. $join->on('book.cat_id', '=', 'website_category.category_id')
  1891. ->where('website_category.website_id', '=', $website['website_id']);
  1892. })
  1893. ->select(
  1894. 'book.id',
  1895. 'book.title',
  1896. 'book.img_url',
  1897. 'book.price',
  1898. 'book.market_price',
  1899. 'book.description',
  1900. 'book.cat_id',
  1901. 'book.description',
  1902. 'book.updated_at',
  1903. DB::raw("'$child_pinyin' as pinyin"))
  1904. ->orderBy('updated_at', 'desc')
  1905. ->limit($childImgNum)
  1906. ->get()->all();
  1907. // 查询子栏目文字新闻
  1908. $childTextArticles = Book::where('cat_id', $childCatId)
  1909. ->where('status', 2)
  1910. ->leftJoin('website_category', function($join) use ($website) {
  1911. $join->on('book.cat_id', '=', 'website_category.category_id')
  1912. ->where('website_category.website_id', '=', $website['website_id']);
  1913. })
  1914. ->select(
  1915. 'book.id',
  1916. 'book.title',
  1917. 'book.img_url',
  1918. 'book.price',
  1919. 'book.market_price',
  1920. 'book.description',
  1921. 'book.cat_id',
  1922. 'book.description',
  1923. 'book.updated_at',
  1924. DB::raw("'$child_pinyin' as pinyin"))
  1925. ->orderBy('updated_at', 'desc')
  1926. ->limit($childTextNum)
  1927. ->get()->all();
  1928. $resultItem['child'] = [
  1929. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1930. 'category_id' => $childCatId,
  1931. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1932. 'all_childcat' => $childCategory,
  1933. 'imgnum' => $childImgArticles,
  1934. 'textnum' => $childTextArticles,
  1935. ];
  1936. }
  1937. }
  1938. }else{
  1939. $resultItem['child'] = [];
  1940. }
  1941. return $resultItem;
  1942. }, $data);
  1943. return Result::success($result);
  1944. }
  1945. /**
  1946. * 获取书刊列表
  1947. * @param array $data
  1948. * @return array
  1949. * */
  1950. public function getWebsiteBookList(array $data): array
  1951. {
  1952. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  1953. if(empty($web)){
  1954. return Result::error("查询失败", 0);
  1955. }
  1956. $catid = $data['id'];
  1957. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$catid)->orderBy('sort')->first(['pid','category_id','aLias_pinyin']);
  1958. $parent_categpory = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$category['pid'])->orderBy('sort')->first(['aLias_pinyin']);
  1959. $parent_pinyin = $parent_categpory ? $parent_categpory->aLias_pinyin ?? '' : '';
  1960. $category_pinyin = $category ? $category->aLias_pinyin : '';
  1961. $pinyin =$category_pinyin ? $parent_pinyin . '/' . $category_pinyin ?? '' : '' ;
  1962. $categorys = WebsiteCategory::where('website_id', $data['website_id'])
  1963. ->where('pid',$category['pid'])
  1964. ->select(
  1965. 'category_id','alias','aLIas_pinyin','pid','sort','is_url','web_url',
  1966. 'seo_title','seo_keywords','seo_description',
  1967. DB::raw("CONCAT('$parent_pinyin', '/', aLIas_pinyin) as pinyin")
  1968. )
  1969. ->orderBy('sort')
  1970. ->get()->all();
  1971. if(empty($category)){
  1972. return Result::error("查询失败", 0);
  1973. }
  1974. $query = Book::where('book.status', 2)
  1975. ->where('book.website_id', $data['website_id'])
  1976. ->where('book.cat_id', $catid)
  1977. ->select(
  1978. 'book.id',
  1979. 'book.title',
  1980. 'book.img_url',
  1981. 'book.description',
  1982. 'book.updated_at',
  1983. 'book.cat_id',
  1984. DB::raw("'$pinyin' as pinyin")
  1985. )
  1986. ->orderBy('book.updated_at', 'desc');
  1987. $count = $query->count();
  1988. $query = clone $query;
  1989. $Book = $query
  1990. ->limit($data['pageSize'])
  1991. ->offset(($data['page']-1)*$data['pageSize'])
  1992. ->get()->all();
  1993. $result = [
  1994. 'category' => $categorys,
  1995. 'books' => $Book,
  1996. 'count' =>$count,
  1997. ];
  1998. // if(empty($result)){
  1999. // return Result::error("查询失败", 0);
  2000. // }
  2001. return Result::success($result);
  2002. }
  2003. /**
  2004. * 获取书刊详情
  2005. * @param array $data
  2006. * @return array
  2007. * */
  2008. public function getWebsiteBookInfo(array $data): array
  2009. {
  2010. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2011. if(empty($web)){
  2012. return Result::error("查询失败", 0);
  2013. }
  2014. $book = Book::where('status', 2)
  2015. ->where('website_id', $data['website_id'])
  2016. ->where('id', $data['id'])
  2017. ->select(
  2018. 'book.*',
  2019. )
  2020. ->orderBy('updated_at', 'desc')
  2021. ->first();
  2022. if(empty($book)){
  2023. return Result::error("查询失败", 0);
  2024. }
  2025. // 先查询当前分类的 pid
  2026. $carpid = WebsiteCategory::where('website_id', $data['website_id'])
  2027. ->where('category_id', $book['cat_id'])
  2028. ->first(['pid','category_id']);
  2029. $query = WebsiteCategory::where('website_id', $data['website_id']);
  2030. if(!empty($carpid)){
  2031. if($carpid->pid!=0){
  2032. $pid = $carpid['pid'];
  2033. // $parent_pinyin = '';
  2034. }else{
  2035. $pid = $carpid['category_id'];
  2036. // $parent_pinyin = $currentCategory->aLias_pinyin?? '';
  2037. }
  2038. }
  2039. $categorys = $query->where('pid', $pid)
  2040. ->select('category_id','alias','aLIas_pinyin','pid','sort','is_url','web_url','category_arr_id as cat_arr_id',
  2041. )
  2042. ->orderBy('sort')
  2043. ->get();
  2044. $category = $this->processArticle($categorys, $data);
  2045. $result = [
  2046. 'category' => $category,
  2047. 'books' => $book,
  2048. ];
  2049. return Result::success($result);
  2050. }
  2051. /**
  2052. * 尝试
  2053. * @param array $data
  2054. * @return array
  2055. * */
  2056. public function test(array $data): array
  2057. {
  2058. $input['id'] = $data['id'];
  2059. $input['website_id'] = $data['website_id'];
  2060. // 将 JSON 字符串转换为 PHP 数组
  2061. $data = json_decode($input['id'], true);
  2062. $result = [];
  2063. $article = $data;
  2064. $result = array_map(function ($item) use ($input) {
  2065. $website = [
  2066. 'website_id' => $input['website_id'],
  2067. ];
  2068. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  2069. $category = WebsiteCategory::where('pid', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  2070. $parent = [
  2071. 0 => $category['category_id'] ?? [],
  2072. 1 => $parentImgNum?? [],
  2073. 2 => $parentTextNum?? [],
  2074. ];
  2075. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['child']);
  2076. $child = [];
  2077. $resultItem = [
  2078. // 'alias' => $parent_alias ?? '',
  2079. 'category_id' => $parentCatId ?? 0,
  2080. 'pinyin' => $parentImgNum ?? null,
  2081. 'imgnum' => $parentTextNum ?? [],
  2082. // 'textnum' => $textArticles ?? [],
  2083. ]; return $parent;
  2084. }, $data);
  2085. return Result::success($result);
  2086. // });
  2087. }
  2088. /**
  2089. * 封装处理文章的路由问题
  2090. * */
  2091. function processArticle($article, $data) {
  2092. return $article->map(function ($article) use ($data) {
  2093. $catid = $article->cat_arr_id?? '';
  2094. $level = json_decode($catid, true);
  2095. $pinyin = '';
  2096. // $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  2097. if (!empty($level) && is_array($level)) {
  2098. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  2099. ->where('website_id', $data['website_id'])
  2100. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  2101. ->get(['aLIas_pinyin'])
  2102. ->pluck('aLIas_pinyin')
  2103. ->implode('/');
  2104. } else {
  2105. $pinyin = '';
  2106. }
  2107. $article->pinyin = $pinyin;
  2108. return $article;
  2109. });
  2110. }
  2111. /**
  2112. * c端-获取招工招聘
  2113. * @param array $data
  2114. * @return array
  2115. * */
  2116. public function getWebsiteJob(array $data): array
  2117. {
  2118. $web = Website::where('id', $data['website_id'])->first();
  2119. if(empty($web)){
  2120. return Result::error("该网站不存在", 0);
  2121. }
  2122. $job_hunting = JobHunting::where('job_hunting.status', 2)
  2123. ->where('job_hunting.website_id', $data['website_id'])
  2124. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2125. $query->where(function($q) use ($data) {
  2126. $q->WhereRaw("JSON_CONTAINS(job_hunting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2127. });
  2128. })
  2129. ->select('job_hunting.id','job_hunting.cat_arr_id','job_hunting.job','job_hunting.industry',
  2130. 'job_hunting.city_arr_id','job_hunting.experience','job_hunting.updated_at')
  2131. ->orderBy('updated_at', 'desc')
  2132. ->limit($data['job1_num'])
  2133. ->get();
  2134. $web['website_id'] = $data['website_id'];
  2135. if(empty($job_hunting)){
  2136. $job_huntings = "未查询到相关简历信息";
  2137. }else{
  2138. $job_huntings = $this->processJob($job_hunting, $web);
  2139. }
  2140. $job_recruiting = JobRecruiting::where('job_recruiting.status', 1)
  2141. ->where('job_recruiting.website_id', $data['website_id'])
  2142. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2143. $query->where(function($q) use ($data) {
  2144. $q->WhereRaw("JSON_CONTAINS(job_recruiting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2145. });
  2146. })
  2147. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2148. ->select('job_recruiting.id','job_recruiting.cat_arr_id','job_recruiting.title',
  2149. 'job_recruiting.jtzw_id','job_recruiting.hy_id','job_recruiting.city_arr_id',
  2150. 'job_recruiting.due_data','job_recruiting.updated_at','job_recruiting.experience',
  2151. 'job_recruiting.educational','job_company.business_name',)
  2152. ->orderBy('updated_at', 'desc')
  2153. ->limit($data['job2_num'])
  2154. ->get();
  2155. if(empty($job_recruiting->toArray())){
  2156. $job_recruitings = "未查询到相关职位信息";
  2157. }else{
  2158. $job_recruitings = $this->processJob($job_recruiting, $web);
  2159. }
  2160. $hy = JobIndustry::get()->all();
  2161. $zw = JobPosition::where('zwpid',0)->get()->all();
  2162. $jtzw = JobPosition::where('zwpid','!=',0)->get()->all();
  2163. $result = [
  2164. 'job_hunting' => $job_huntings,
  2165. 'job_recuiting' => $job_recruitings,
  2166. 'hy' => $hy,
  2167. 'zw' => $zw,
  2168. 'jtzw' => $jtzw,
  2169. ];
  2170. return Result::success($result);
  2171. }
  2172. /**
  2173. * c端-获取招工招聘列表
  2174. * @param array $data
  2175. * @return array
  2176. * */
  2177. public function getWebsiteJobList(array $data): array
  2178. {
  2179. $where = [];
  2180. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2181. if(empty($web)){
  2182. return Result::error("此网站不存在", 0);
  2183. }
  2184. $website_id['website_id'] = $data['website_id'];
  2185. if(isset($data['hy_id']) && !empty($data['hy_id'])){
  2186. array_push($where, ['hy_id',$data['hy_id']]);
  2187. }
  2188. if(isset($data['zw_id']) && !empty($data['zw_id'])){
  2189. array_push($where, ['zw_id',$data['zw_id']]);
  2190. }
  2191. if(isset($data['jtzw_id']) &&!empty($data['jtzw_id'])){
  2192. array_push($where, ['jtzw_id',$data['jtzw_id']]);
  2193. }
  2194. $query = JobRecruiting::where('job_recruiting.status', 1)
  2195. ->where('job_recruiting.website_id', $data['website_id'])
  2196. ->where($where)
  2197. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2198. $query->where(function($q) use ($data) {
  2199. $q->WhereRaw("JSON_CONTAINS(job_recruiting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2200. });
  2201. })
  2202. ->when(isset($data['catid_id']) &&!empty($data['catid_id']), function ($query) use ($data) {
  2203. $query->where(function($q) use ($data) {
  2204. $q->WhereRaw("JSON_CONTAINS(job_recruiting.cat_arr_id, '". intval($data['catid_id']). "') = 1");
  2205. });
  2206. })
  2207. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2208. ->select('job_recruiting.id','job_recruiting.hy_id','job_recruiting.title','job_recruiting.zw_id',
  2209. 'job_recruiting.jtzw_id','job_recruiting.city_arr_id','job_recruiting.due_data',
  2210. 'job_recruiting.cat_arr_id','job_recruiting.updated_at')
  2211. ->orderBy('updated_at', 'desc');
  2212. $recruit_count = $query->count();
  2213. $query = clone $query;
  2214. $JobRecruiting = $query
  2215. ->offset(($data['page']-1)*$data['pageSize'])
  2216. ->limit($data['pageSize'])
  2217. ->get();
  2218. if(empty($JobRecruiting)){
  2219. $JobRecruiting = "暂无相关职位信息";
  2220. }else{
  2221. $JobRecruiting = $this->processJob($JobRecruiting, $website_id);
  2222. }
  2223. $query = JobHunting::where('status', 2)
  2224. ->where('job_hunting.website_id', $data['website_id'])
  2225. ->where($where)
  2226. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2227. $query->where(function($q) use ($data) {
  2228. $q->WhereRaw("JSON_CONTAINS(job_hunting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2229. });
  2230. })
  2231. ->when(isset($data['catid_id']) &&!empty($data['catid_id']), function ($query) use ($data) {
  2232. $query->where(function($q) use ($data) {
  2233. $q->WhereRaw("JSON_CONTAINS(job_hunting.cat_arr_id, '". intval($data['catid_id']). "') = 1");
  2234. });
  2235. })
  2236. ->select('id','sexy','experience','origin','industry','name','job','job_name_get','city_arr_id','cat_arr_id','created_at','updated_at')
  2237. ->orderBy('updated_at', 'desc');
  2238. $hunt_count = $query->count();
  2239. $query = clone $query;
  2240. $JobHunting = $query
  2241. ->offset(($data['page']-1)*$data['pageSize'])
  2242. ->limit($data['pageSize'])
  2243. ->get();
  2244. if(empty($JobHunting)){
  2245. $JobRecruiting = "暂无相关简历信息";
  2246. }else{
  2247. $JobHunting = $this->processJob($JobHunting, $website_id);
  2248. }
  2249. $result = [
  2250. 'JobRecruiting' => $JobRecruiting,
  2251. 'recruit_count' =>$recruit_count,
  2252. 'JobHunting' => $JobHunting,
  2253. 'hunt_count' =>$hunt_count,
  2254. ];
  2255. return Result::success($result);
  2256. }
  2257. /**
  2258. * c端-获取招工招聘详情
  2259. * @param array $data
  2260. * @return array
  2261. * */
  2262. public function getWebsiteJobInfo(array $data): array
  2263. {
  2264. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2265. if(empty($web)){
  2266. return Result::error("该网站不存在", 0);
  2267. }
  2268. $webid = [
  2269. 'website_id' => $data['website_id'],
  2270. ];
  2271. // 职位相关信息
  2272. if($data['type']==1){
  2273. $query = JobRecruiting::where('status', 1)
  2274. ->where('website_id', $data['website_id'])
  2275. ->select('*');
  2276. $job = $query->where('job_recruiting.id', $data['id'])->get();
  2277. $company = JobCompany::where('job_id', $data['id'])->select('id','business_name','company_hy_id','company_size','company_nature','address_arr_id','address')->get();
  2278. if(!empty($company)){
  2279. $result['company'] = $this->processJob($company, $webid);
  2280. }
  2281. $other_job =JobRecruiting::where('status', 1)
  2282. ->where('website_id', $data['website_id'])
  2283. ->select('title','id','updated_at','cat_arr_id','user_id')
  2284. ->where('id','!=',$data['id'])
  2285. ->where('user_id', '=', $job[0]['user_id'])
  2286. ->limit($data['pageSize'])
  2287. ->get();
  2288. if(!empty($other_job)){
  2289. $result['other_job'] = $this->processJob($other_job, $webid);
  2290. }
  2291. }else{
  2292. $job = JobHunting::where('job_hunting.status', 2)
  2293. ->where('job_hunting.website_id', $data['website_id'])
  2294. ->where('job_hunting.id', $data['id'])
  2295. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  2296. ->select('job_hunting.*','user_name')
  2297. ->get();
  2298. $resume = JobRemuse::where('hunt_id',$data['id'])->get();
  2299. if(!empty($resume->toArray())){
  2300. $result['resume'] = 1;
  2301. } else{
  2302. $result['resume'] = 0;
  2303. }
  2304. }
  2305. if(empty($job->toArray())){
  2306. return Result::error("id参数错误", 0);
  2307. }
  2308. $result['job'] = $this->processJob($job, $webid);
  2309. // 返现对应的栏目
  2310. $catid = json_decode($job[0]['cat_arr_id'],true) ?? '';
  2311. $category = WebsiteCategory::where('website_id', $data['website_id'])
  2312. ->whereIn('category_id', $catid)
  2313. ->select('category_id','alias','aLIas_pinyin','pid')
  2314. ->orderBy('pid')->get()->all();
  2315. if(!empty($category)){
  2316. $result['category'] = $category;
  2317. }
  2318. if(empty($result)){
  2319. return Result::error("参数错误", 0);
  2320. }
  2321. return Result::success($result);
  2322. }
  2323. /**
  2324. * c端-申请职位
  2325. * @param array $data
  2326. * @return array
  2327. * */
  2328. public function getWebsiteJobApply(array $data): array
  2329. {
  2330. // 首先验证网站是否存在
  2331. // return Result::success($data);
  2332. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2333. if(empty($web)){
  2334. return Result::error("该网站不存在", 0);
  2335. }
  2336. // 验证用户是否存在且为个人会员/管理员
  2337. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2338. // 1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2339. if(empty($user) || ($user['type_id'] != 1 && $user['type_id'] != 10000)){
  2340. return Result::error("用户不存在", 0);
  2341. }
  2342. // 去除重复元素和空元素,并保持原始顺序
  2343. $data['recruit_id'] = array_values(array_filter(array_unique($data['recruit_id']), function($value) {
  2344. return !empty($value);
  2345. }));
  2346. // 验证职位是否存在 1:审核通过
  2347. $recruiting = JobRecruiting::where('status', 1)
  2348. ->where('website_id', $data['website_id'])
  2349. ->whereIn('id',$data['recruit_id'])
  2350. ->get(['id as recruit_id','user_id as receiver_id'])->all();
  2351. if(empty($recruiting)){
  2352. return Result::error("该职位不存在", 0);
  2353. }
  2354. // 简历是否存在
  2355. $hunt_id = JobHunting::where('user_id', $data['user_id'])->where('status',2)->first(['id as hunt_id']);
  2356. if(empty($hunt_id)){
  2357. return Result::error("该简历不存在", 0);
  2358. }
  2359. $data['hunt_id'] = $hunt_id['hunt_id'];
  2360. // // 验证是否已投递过该职位
  2361. $apply = JobApply::where('user_id', $data['user_id'])
  2362. ->where('hunt_id', $data['hunt_id'])
  2363. ->where('website_id',$data['website_id'])
  2364. ->whereIn('recruit_id',$data['recruit_id'])
  2365. ->get(['recruit_id']);
  2366. if(!empty($apply->toArray())){
  2367. return Result::error("您已投递过该职位!");
  2368. }
  2369. $insertData = array_map(function($recruiting) use ($data) {
  2370. return [
  2371. 'user_id' => $data['user_id'],
  2372. 'hunt_id' => $data['hunt_id'],
  2373. 'website_id' => $data['website_id'],
  2374. 'recruit_id' => $recruiting['recruit_id'],
  2375. 'receiver_id' => $recruiting['receiver_id'],
  2376. 'status' => 1,
  2377. ];
  2378. }, $recruiting);
  2379. // 批量插入数据
  2380. $result = JobApply::insert($insertData);
  2381. if(empty($result)){
  2382. return Result::error("投递失败", 0);
  2383. }
  2384. return Result::success($result);
  2385. }
  2386. /**
  2387. * 招工招聘-沟通简历
  2388. * @param array $data
  2389. * @return array
  2390. * */
  2391. public function getWebsiteJobResume(array $data): array
  2392. {
  2393. // 首先验证网站是否存在
  2394. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2395. if(empty($web)){
  2396. return Result::error("该网站不存在", 0);
  2397. }
  2398. // return Result::success($data);
  2399. // 验证用户是否存在且为企业会员/管理员
  2400. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2401. // 1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2402. if(empty($user) || ($user['type_id']!= 3 && $user['type_id']!= 10000)){
  2403. return Result::error("用户不存在", 0);
  2404. }
  2405. // 去除重复元素和空元素,并保持原始顺序
  2406. $data['hunt_id'] = array_values(array_filter(array_unique($data['hunt_id']), function($value) {
  2407. return !empty($value);
  2408. }));
  2409. // 验证简历是否存在
  2410. $hunting = JobHunting::where('status', 2)
  2411. ->where('website_id', $data['website_id'])
  2412. ->whereIn('id',$data['hunt_id'])
  2413. ->get(['id as hunt_id','user_id as receiver_id'])->all();
  2414. if(empty($hunting)){
  2415. return Result::error("该简历不存在", 0);
  2416. }
  2417. // 验证是否已沟通过该简历
  2418. $remuse = JobRemuse::where('user_id', $data['user_id'])
  2419. ->where('recruit_id', $data['recruit_id'])
  2420. ->where('website_id',$data['website_id'])
  2421. ->whereIn('hunt_id',$data['hunt_id'])
  2422. ->get(['hunt_id']);
  2423. if(!empty($remuse->toArray())){
  2424. return Result::error("您已沟通过该简历!");
  2425. }
  2426. // 准备要插入的数据数组
  2427. $insertData = array_map(function($hunting) use ($data) {
  2428. return [
  2429. 'user_id' => $data['user_id'],
  2430. 'recruit_id' => $data['recruit_id'],
  2431. 'website_id' => $data['website_id'],
  2432. 'hunt_id' => $hunting['hunt_id'],
  2433. 'receiver_id' => $hunting['receiver_id'],
  2434. 'status' => 1,
  2435. ];
  2436. }, $hunting);
  2437. // 批量插入数据
  2438. $result = JobRemuse::insert($insertData);
  2439. if(empty($result)){
  2440. return Result::error("沟通失败", 0);
  2441. }
  2442. return Result::success($result);
  2443. }
  2444. }