NewsService.php 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  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::whereIn('category_id', $level)
  1380. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1381. ->get(['aLIas_pinyin'])
  1382. ->pluck('aLIas_pinyin')
  1383. ->implode('/');
  1384. } else {
  1385. $pinyin = $category->aLIas_pinyin ?? '';
  1386. }
  1387. if(isset($good->city_id) &&!empty($good->city_id)){
  1388. $city = District::where('id', $good->city_id)->first(['name']);
  1389. $good->city_name = $city->name ?? '';
  1390. }
  1391. // 解析imgurl JSON并取第一条数据
  1392. $imgUrls = json_decode($good->imgurl, true);
  1393. $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
  1394. $good->pinyin = $pinyin;
  1395. return $good;
  1396. });
  1397. }
  1398. /**
  1399. * 获取商品模块
  1400. * @param array $data
  1401. * @return array
  1402. * */
  1403. public function getWebsiteshop(array $data): array
  1404. {
  1405. $input['id'] = $data['id'];
  1406. $input['website_id'] = $data['website_id'];
  1407. $catid = $data['catid'];
  1408. $data = json_decode($input['id'] ?? '', true) ?? [];
  1409. $result['goods'] = array_map(function ($item) use ($input) {
  1410. // 检查parent元素是否存在且不是undefined
  1411. if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
  1412. list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
  1413. $website = $input['website_id'];
  1414. $query = Good::where('good.status', 2)
  1415. ->where('good.website_id', $website);
  1416. switch ($Levelid) {
  1417. case 1:
  1418. case 2:
  1419. case 3:
  1420. $goods = $query->where(function($q) use ($Levelid) {
  1421. $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 0")
  1422. ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 0");
  1423. });
  1424. break;
  1425. case 4:
  1426. $goods = $query;
  1427. break;
  1428. case 5:
  1429. $goods = $query->where('type_id',1);
  1430. break;
  1431. case 6:
  1432. $goods = $query->where('type_id',2);
  1433. break;
  1434. default:
  1435. return [];
  1436. }
  1437. $all_goods = $goods
  1438. ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
  1439. 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
  1440. 'good.website_id')
  1441. ->orderBy('updated_at','desc')
  1442. ->offset($goodStart)
  1443. ->limit($goodNum)
  1444. ->get();
  1445. $all_goods = $this->processGoods($all_goods, $website);
  1446. }
  1447. return $all_goods;
  1448. }, $data);
  1449. $website = $input['website_id'];
  1450. $result['article'] = Article::where(function ($query) use ($website) {
  1451. $query->where(function ($subQuery) use ($website) {
  1452. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website) . "') = 0");
  1453. })->orWhereNull("ignore_ids");
  1454. })
  1455. ->where('catid', $catid)
  1456. ->where('article.status', 1)
  1457. ->leftJoin('article_data', 'article_data.article_id', 'article.catid')
  1458. ->select('article.id', 'article.title', 'article.updated_at', 'introduce', 'islink', 'linkurl','article_data.content')
  1459. ->orderBy('article.updated_at', 'desc')
  1460. ->first();
  1461. return Result::success($result);
  1462. }
  1463. /**
  1464. * 获取商品分类
  1465. * @param array $data
  1466. * @return array
  1467. * */
  1468. public function getWebsiteshopCat(array $data): array
  1469. {
  1470. $website = $data['website_id'];
  1471. $category = WebsiteCategory::where('website_id', $website)
  1472. ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
  1473. ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
  1474. ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
  1475. ->orderBy('sort')
  1476. ->get();
  1477. $cat = $category->map(function ($item) use ($website) {
  1478. $cat_arr_id = json_decode($item->category_arr_id, true) ?? [];
  1479. $pid = $item->pid?? 0;
  1480. if(!empty($cat_arr_id) && is_array($cat_arr_id) && $pid != 0){
  1481. $pinyin = WebsiteCategory::whereIn('category_id', $cat_arr_id)
  1482. ->where('website_id', $website)
  1483. ->orderByRaw('FIELD(category_id, '. implode(',', $cat_arr_id). ')')
  1484. ->get(['aLIas_pinyin'])
  1485. ->pluck('aLIas_pinyin')
  1486. ->implode('/');
  1487. }else{
  1488. $pinyin = $item->aLIas_pinyin ?? '';
  1489. }
  1490. $item->pinyin = $pinyin;
  1491. })
  1492. ;
  1493. if(empty($category)){
  1494. return Result::error("栏目查询失败", 0);
  1495. }
  1496. // $cat_tree = $this->processGoods($cat, $website);
  1497. $goods = Good::where('website_id', $website)
  1498. ->where('status', 2)
  1499. ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
  1500. ->latest('updated_at')
  1501. ->offset(($data['page']-1)*$data['pageSize'])
  1502. ->limit($data['pageSize'])
  1503. ->get();
  1504. if($goods->isEmpty()){
  1505. return Result::error("商品查询失败", 0);
  1506. }
  1507. if(count($goods) > 1){
  1508. $goods = $this->processGoods($goods, $website);
  1509. }else{
  1510. $catid = $goods[0]['catid'] ?? 0;
  1511. $good_category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1512. if (!empty($good_category->pid) && $good_category->pid != 0) {
  1513. $level = json_decode($good_category->category_arr_id);
  1514. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1515. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1516. ->get(['aLIas_pinyin'])
  1517. ->pluck('aLIas_pinyin')
  1518. ->implode('/');
  1519. } else {
  1520. $pinyin = $good_category->aLIas_pinyin ?? '';
  1521. }
  1522. $goods[0]['pinyin'] = $pinyin;
  1523. }
  1524. $cat_tree = Result::buildMenuTree($category);
  1525. $result = [
  1526. 'category' => $cat_tree,
  1527. 'goods' => $goods,
  1528. ];
  1529. // $resul['goods'] = $goods;
  1530. if(empty($result)){
  1531. return Result::error("查询失败", 0);
  1532. }
  1533. return Result::success($result);
  1534. }
  1535. /*
  1536. * 获取商品列表
  1537. * @param array $data
  1538. * @return array
  1539. * */
  1540. public function getWebsiteshopList(array $data): array
  1541. {
  1542. // return Result::success($data);
  1543. $where = [
  1544. 'status' => 2,
  1545. 'website_id' => $data['website_id'],
  1546. ];
  1547. if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
  1548. return Result::error("查询失败", 0);
  1549. }
  1550. if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
  1551. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
  1552. $data['catid'] = $category->category_id ?? 0;
  1553. }
  1554. if(isset($data['keyword']) &&!empty($data['keyword'])){
  1555. array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
  1556. }
  1557. if(isset($data['type_id']) && !empty($data['type_id'])){
  1558. array_push($where, ['type_id', $data['type_id']]);
  1559. }
  1560. $query = Good::where($where)
  1561. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  1562. $query->where(function($q) use ($data) {
  1563. $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
  1564. ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
  1565. });
  1566. })
  1567. ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
  1568. $query->where(function($q) use ($data) {
  1569. $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
  1570. ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
  1571. });
  1572. })
  1573. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
  1574. 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
  1575. ->latest('updated_at');
  1576. // 获取 type_id 为 1 的数据
  1577. $result['type1'] = $this->processGoods(
  1578. $query->clone()
  1579. ->where('type_id', 1)
  1580. ->offset(($data['page'] - 1) * $data['pageSize'])
  1581. ->limit($data['pageSize'])
  1582. ->get(),
  1583. $data
  1584. );
  1585. // 获取 type_id 为 2 的数据
  1586. $result['type2'] = $this->processGoods(
  1587. $query->clone()
  1588. ->where('type_id', 2)
  1589. ->offset(($data['page'] - 1) * $data['pageSize'])
  1590. ->limit($data['pageSize'])
  1591. ->get(),
  1592. $data
  1593. );
  1594. if(empty($result)){
  1595. return Result::error("查询失败", 0);
  1596. }
  1597. return Result::success($result);
  1598. }
  1599. /**
  1600. * 获取商品详情
  1601. * @param array $data
  1602. * @return array
  1603. * */
  1604. public function getWebsiteshopInfo(array $data): array
  1605. {
  1606. $where = [
  1607. 'good.status' => 2,
  1608. 'good.website_id' => $data['website_id'],
  1609. ];
  1610. $goods = Good::where($where)
  1611. ->where('good.id', $data['id'])
  1612. ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
  1613. ->select('good.*','website_category.alias','website_category.category_id')
  1614. ->first();
  1615. if(empty($goods)){
  1616. return Result::error("查询失败", 0);
  1617. }
  1618. $goods->imgurl = json_decode($goods->imgurl, true);
  1619. return Result::success($goods);
  1620. }
  1621. /**
  1622. * 封装处理文章的路由问题
  1623. * */
  1624. function processArticles($articles, $data) {
  1625. if (!is_array($data)) {
  1626. // 可以根据实际情况进行日志记录或抛出异常
  1627. // 这里简单输出错误信息
  1628. echo "Error: \$data is not an array in processArticles. It is of type ". gettype($data). PHP_EOL;
  1629. return $articles;
  1630. }
  1631. return $articles->map(function ($article) use ($data) {
  1632. $catid = $article->cat_arr_id?? '';
  1633. $level = json_decode($catid, true);
  1634. $pinyin = '';
  1635. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1636. if (!empty($category->pid) && $category->pid!= 0) {
  1637. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1638. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1639. ->get(['aLIas_pinyin'])
  1640. ->pluck('aLIas_pinyin')
  1641. ->implode('/');
  1642. } else {
  1643. $pinyin = $category->aLIas_pinyin ?? '';
  1644. }
  1645. $article->pinyin = $pinyin;
  1646. return $article;
  1647. });
  1648. }
  1649. // 封装处理由问题
  1650. function processJob($job, $data) {
  1651. return $job->map(function ($job) use ($data) {
  1652. $category = $job->cat_arr_id?? '';
  1653. $cityid = $job->city_arr_id?? '';
  1654. $city = json_decode($cityid, true);
  1655. $pinyin = '';
  1656. $level = json_decode($category, true);
  1657. // 路由
  1658. if(!empty($level) && is_array($level)){
  1659. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1660. ->where('website_id', $data['website_id']) // 添加网站ID条件
  1661. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1662. ->get(['aLIas_pinyin'])
  1663. ->pluck('aLIas_pinyin')
  1664. ->implode('/');
  1665. if(empty($pinyin)){
  1666. $pinyin = $pinyin->aLIas_pinyin?? '';
  1667. }
  1668. $job->pinyin = $pinyin;
  1669. }
  1670. // 取城市-市??省
  1671. if(!empty($city) && is_array($city)){
  1672. if(isset($city[1] ) && !empty($city[1])){
  1673. $city = District::where('id', $city[1])->first(['name']);
  1674. $job->city_name = $city->name?? '';
  1675. }else if(isset($city[0] ) && !empty($city[0])){
  1676. $city = District::where('id', $city[0])->first(['name']);
  1677. $job->city_name = $city->name?? '';
  1678. }else{
  1679. $job->city_name = '全国';
  1680. }
  1681. }
  1682. // 取公司地址
  1683. if(isset($job->address_arr_id) && !empty($job->address_arr_id)){
  1684. $address_id = json_decode($job->address_arr_id, true) ?? [];
  1685. if(is_array($address_id) && !empty($address_id)){
  1686. $address = District::whereIn('id', $address_id)
  1687. ->orderBy('level', 'asc')
  1688. ->get(['name'])
  1689. ->pluck('name')
  1690. ->implode('');
  1691. // $job->address_name = $address ?? '';
  1692. $job->address_name = ($address ?? '') . ($job->address ?? '');
  1693. }
  1694. }
  1695. // 取行业
  1696. if(!empty($job->hy_id) || !empty($job->industry) || !empty($job->company_hy_id)){
  1697. $hy_name = JobIndustry::when($job, function ($query) use ($job) {
  1698. if(!empty($job->industry)){
  1699. $query->where('hyid', $job->industry);
  1700. }else if(!empty($job->hy_id)){
  1701. $query->where('hyid', $job->hy_id);
  1702. }else{
  1703. $query->where('hyid', $job->company_hy_id);
  1704. }
  1705. })->first(['hyname']);
  1706. $job->hy_name = $hy_name->hyname?? '';
  1707. }
  1708. // 取职位
  1709. if((isset($job->zw_id) && !empty($job->zw_id)) || (isset($job->job) && !empty($job->job))){
  1710. $zwid = $job->job?? $job->zw_id;
  1711. $zw_name = JobPosition::where('zwid', $zwid)->first(['zwname']);
  1712. $job->zw_name = $zw_name->zwname?? '';
  1713. }
  1714. // 取具体职位
  1715. if((isset($job->jtzw_id) &&!empty($job->jtzw_id)) || (isset($job->job_name_get) && !empty($job->job_name_get))){
  1716. $jtzwid = $job->job_name_get ?? $job->jtzw_id;
  1717. $jtzw_name = JobPosition::where('zwid', $jtzwid)->first(['zwname']);
  1718. $job->jtzw_name = $jtzw_name->zwname?? '';
  1719. }
  1720. // 取工作经验
  1721. if(isset($job->experience) &&!empty($job->experience)){
  1722. $experience = JobEnum::where('egroup','years')->where('evalue',$job->experience)->first(['ename']);
  1723. $job->experience_name = $experience->ename?? '';
  1724. }
  1725. // 取学历
  1726. if(isset($job->educational) &&!empty($job->educational)){
  1727. $education = JobEnum::where('egroup','education')->where('evalue',$job->educational)->first(['ename']);
  1728. $job->education_name = $education->ename?? '';
  1729. }
  1730. // 语言
  1731. if(isset($job->language) &&!empty($job->language)){
  1732. $language = JobEnum::where('egroup','language')->where('evalue',$job->language)->first(['ename']);
  1733. $job->language_name = $language->ename?? '';
  1734. }
  1735. // 薪资
  1736. if(isset($job->salary) &&!empty($job->salary)){
  1737. $salary = JobEnum::where('egroup','income')->where('evalue',$job->salary)->first(['ename']);
  1738. $job->salary_name = $salary->ename?? '';
  1739. }
  1740. // 职位性质
  1741. if(isset($job->nature_id) &&!empty($job->nature_id)){
  1742. $job_nature = JobEnum::where('egroup','nature')->where('evalue',$job->nature_id)->first(['ename']);
  1743. $job->job_nature_name = $job_nature->ename?? '';
  1744. }
  1745. // 公司规模
  1746. if(isset($job->company_size) &&!empty($job->company_size)){
  1747. $company_size = JobEnum::where('egroup','cosize')->where('evalue',$job->company_size)->first(['ename']);
  1748. $job->company_size_name = $company_size->ename?? '';
  1749. }
  1750. // 公司性质
  1751. if(isset($job->company_nature) && !empty($job->company_nature)){
  1752. $company_nature = JobNature::where('id',$job->company_nature)->first(['nature_name']);
  1753. $job->company_nature_name = $company_nature->nature_name?? '';
  1754. }
  1755. // $job->pinyin = $pinyin;
  1756. return $job;
  1757. });
  1758. }
  1759. /**
  1760. * 获取书籍模块
  1761. * @param array $data
  1762. * @return array
  1763. * */
  1764. public function getWebsiteBook(array $data): array
  1765. {
  1766. $input['id'] = $data['id'];
  1767. $input['website_id'] = $data['website_id'];
  1768. // 将 JSON 字符串转换为 PHP 数组
  1769. $data = json_decode($input['id'], true);
  1770. // 使用 array_map 处理每个元素
  1771. $result = array_map(function ($item) use ($input) {
  1772. // 检查parent元素是否存在且不是undefined
  1773. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1774. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1775. $website = [
  1776. 'website_id' => $input['website_id'],
  1777. ];
  1778. // 查询栏目名称
  1779. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1780. if(empty($category)){
  1781. $parent_alias = '';
  1782. $parent_pinyin = null;
  1783. $imgBooks = [];
  1784. $textBooks = [];
  1785. }else{
  1786. $parent_alias = $category->alias ?? '';
  1787. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1788. // 查找子分类ID数组
  1789. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1790. array_push($childCategoryIds,$parentCatId);
  1791. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1792. if($parentImgNum!=0){
  1793. // 查询图片新闻
  1794. $imgBooks = Book::where(function($query) use ($parentCatId) {
  1795. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1796. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1797. })
  1798. ->where('book.status', 2)
  1799. ->where('book.img_url', '!=', '')
  1800. ->leftJoin('website_category', function($join) use ($website) {
  1801. $join->on('book.cat_id', '=', 'website_category.category_id')
  1802. ->where('website_category.website_id', '=', $website['website_id']);
  1803. })
  1804. ->select(
  1805. 'book.id',
  1806. 'book.title',
  1807. 'book.img_url',
  1808. 'book.price',
  1809. 'book.market_price',
  1810. 'book.description',
  1811. 'book.cat_id',
  1812. 'book.description',
  1813. 'book.updated_at',
  1814. 'website_category.alias as category_name',
  1815. DB::raw("CASE WHEN book.cat_id = $parentCatId THEN '$parent_pinyin'
  1816. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1817. )
  1818. ->orderBy('updated_at', 'desc')
  1819. ->limit($parentImgNum)
  1820. ->get()->all();
  1821. // 查询文字新闻
  1822. }
  1823. if($parentTextNum!=0){
  1824. $textBooks = [];
  1825. $textBooks = Book::where(function($query) use ($parentCatId) {
  1826. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1827. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1828. })
  1829. ->where('book.status', 2)
  1830. ->leftJoin('website_category', function($join) use ($website) {
  1831. $join->on('book.cat_id', '=', 'website_category.category_id')
  1832. ->where('website_category.website_id', '=', $website['website_id']);
  1833. })
  1834. ->select(
  1835. 'book.id',
  1836. 'book.title',
  1837. 'book.img_url',
  1838. 'book.price',
  1839. 'book.market_price',
  1840. 'book.description',
  1841. 'book.cat_id',
  1842. 'book.description',
  1843. 'book.updated_at',
  1844. 'website_category.alias as category_name',
  1845. DB::raw("CASE WHEN book.cat_id = $parentCatId THEN '$parent_pinyin'
  1846. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1847. )
  1848. ->orderBy('updated_at', 'desc')
  1849. ->limit($parentTextNum)
  1850. ->get()->all();
  1851. }
  1852. }
  1853. }
  1854. $resultItem = [
  1855. 'alias' => $parent_alias ?? '',
  1856. 'category_id' => $parentCatId ?? 0,
  1857. 'pinyin' => $parent_pinyin ?? null,
  1858. 'imgnum' => $imgBooks ?? [],
  1859. 'textnum' => $textBooks ?? [],
  1860. ];
  1861. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1862. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1863. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1864. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1865. ->get()->all();
  1866. if (!empty($childCategory)) {
  1867. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1868. // 查询子栏目名称
  1869. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1870. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1871. ->first();
  1872. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1873. $childImgArticles = [];
  1874. $childTextArticles = [];
  1875. $resultItem['child'] = [];
  1876. }else{
  1877. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1878. // 查询子栏目图片新闻
  1879. $childImgArticles = Book::where('cat_id', $childCatId)
  1880. ->where('status', 2)
  1881. ->where('img_url', '!=', '')
  1882. ->leftJoin('website_category', function($join) use ($website) {
  1883. $join->on('book.cat_id', '=', 'website_category.category_id')
  1884. ->where('website_category.website_id', '=', $website['website_id']);
  1885. })
  1886. ->select(
  1887. 'book.id',
  1888. 'book.title',
  1889. 'book.img_url',
  1890. 'book.price',
  1891. 'book.market_price',
  1892. 'book.description',
  1893. 'book.cat_id',
  1894. 'book.description',
  1895. 'book.updated_at',
  1896. DB::raw("'$child_pinyin' as pinyin"))
  1897. ->orderBy('updated_at', 'desc')
  1898. ->limit($childImgNum)
  1899. ->get()->all();
  1900. // 查询子栏目文字新闻
  1901. $childTextArticles = Book::where('cat_id', $childCatId)
  1902. ->where('status', 2)
  1903. ->leftJoin('website_category', function($join) use ($website) {
  1904. $join->on('book.cat_id', '=', 'website_category.category_id')
  1905. ->where('website_category.website_id', '=', $website['website_id']);
  1906. })
  1907. ->select(
  1908. 'book.id',
  1909. 'book.title',
  1910. 'book.img_url',
  1911. 'book.price',
  1912. 'book.market_price',
  1913. 'book.description',
  1914. 'book.cat_id',
  1915. 'book.description',
  1916. 'book.updated_at',
  1917. DB::raw("'$child_pinyin' as pinyin"))
  1918. ->orderBy('updated_at', 'desc')
  1919. ->limit($childTextNum)
  1920. ->get()->all();
  1921. $resultItem['child'] = [
  1922. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1923. 'category_id' => $childCatId,
  1924. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1925. 'all_childcat' => $childCategory,
  1926. 'imgnum' => $childImgArticles,
  1927. 'textnum' => $childTextArticles,
  1928. ];
  1929. }
  1930. }
  1931. }else{
  1932. $resultItem['child'] = [];
  1933. }
  1934. return $resultItem;
  1935. }, $data);
  1936. return Result::success($result);
  1937. }
  1938. /**
  1939. * 获取书刊列表
  1940. * @param array $data
  1941. * @return array
  1942. * */
  1943. public function getWebsiteBookList(array $data): array
  1944. {
  1945. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  1946. if(empty($web)){
  1947. return Result::error("查询失败", 0);
  1948. }
  1949. $catid = $data['id'];
  1950. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$catid)->orderBy('sort')->first(['pid','category_id','aLias_pinyin']);
  1951. $parent_categpory = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$category['pid'])->orderBy('sort')->first(['aLias_pinyin']);
  1952. $parent_pinyin = $parent_categpory ? $parent_categpory->aLias_pinyin ?? '' : '';
  1953. $category_pinyin = $category ? $category->aLias_pinyin : '';
  1954. $pinyin =$category_pinyin ? $parent_pinyin . '/' . $category_pinyin ?? '' : '' ;
  1955. $categorys = WebsiteCategory::where('website_id', $data['website_id'])
  1956. ->where('pid',$category['pid'])
  1957. ->select(
  1958. 'category_id','alias','aLIas_pinyin','pid','sort','is_url','web_url',
  1959. 'seo_title','seo_keywords','seo_description',
  1960. DB::raw("CONCAT('$parent_pinyin', '/', aLIas_pinyin) as pinyin")
  1961. )
  1962. ->orderBy('sort')
  1963. ->get()->all();
  1964. if(empty($category)){
  1965. return Result::error("查询失败", 0);
  1966. }
  1967. $query = Book::where('book.status', 2)
  1968. ->where('book.website_id', $data['website_id'])
  1969. ->where('book.cat_id', $catid)
  1970. ->select(
  1971. 'book.id',
  1972. 'book.title',
  1973. 'book.img_url',
  1974. 'book.description',
  1975. 'book.updated_at',
  1976. 'book.cat_id',
  1977. DB::raw("'$pinyin' as pinyin")
  1978. )
  1979. ->orderBy('book.updated_at', 'desc');
  1980. $count = $query->count();
  1981. $query = clone $query;
  1982. $Book = $query
  1983. ->limit($data['pageSize'])
  1984. ->offset(($data['page']-1)*$data['pageSize'])
  1985. ->get()->all();
  1986. $result = [
  1987. 'category' => $categorys,
  1988. 'books' => $Book,
  1989. 'count' =>$count,
  1990. ];
  1991. // if(empty($result)){
  1992. // return Result::error("查询失败", 0);
  1993. // }
  1994. return Result::success($result);
  1995. }
  1996. /**
  1997. * 获取书刊详情
  1998. * @param array $data
  1999. * @return array
  2000. * */
  2001. public function getWebsiteBookInfo(array $data): array
  2002. {
  2003. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2004. if(empty($web)){
  2005. return Result::error("查询失败", 0);
  2006. }
  2007. $book = Book::where('status', 2)
  2008. ->where('website_id', $data['website_id'])
  2009. ->where('id', $data['id'])
  2010. ->select(
  2011. 'book.*',
  2012. )
  2013. ->orderBy('updated_at', 'desc')
  2014. ->first();
  2015. if(empty($book)){
  2016. return Result::error("查询失败", 0);
  2017. }
  2018. // 先查询当前分类的 pid
  2019. $carpid = WebsiteCategory::where('website_id', $data['website_id'])
  2020. ->where('category_id', $book['cat_id'])
  2021. ->first(['pid','category_id']);
  2022. $query = WebsiteCategory::where('website_id', $data['website_id']);
  2023. if(!empty($carpid)){
  2024. if($carpid->pid!=0){
  2025. $pid = $carpid['pid'];
  2026. // $parent_pinyin = '';
  2027. }else{
  2028. $pid = $carpid['category_id'];
  2029. // $parent_pinyin = $currentCategory->aLias_pinyin?? '';
  2030. }
  2031. }
  2032. $categorys = $query->where('pid', $pid)
  2033. ->select('category_id','alias','aLIas_pinyin','pid','sort','is_url','web_url','category_arr_id as cat_arr_id',
  2034. )
  2035. ->orderBy('sort')
  2036. ->get();
  2037. $category = $this->processArticle($categorys, $data);
  2038. $result = [
  2039. 'category' => $category,
  2040. 'books' => $book,
  2041. ];
  2042. return Result::success($result);
  2043. }
  2044. /**
  2045. * 尝试
  2046. * @param array $data
  2047. * @return array
  2048. * */
  2049. public function test(array $data): array
  2050. {
  2051. $input['id'] = $data['id'];
  2052. $input['website_id'] = $data['website_id'];
  2053. // 将 JSON 字符串转换为 PHP 数组
  2054. $data = json_decode($input['id'], true);
  2055. $result = [];
  2056. $article = $data;
  2057. $result = array_map(function ($item) use ($input) {
  2058. $website = [
  2059. 'website_id' => $input['website_id'],
  2060. ];
  2061. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  2062. $category = WebsiteCategory::where('pid', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  2063. $parent = [
  2064. 0 => $category['category_id'] ?? [],
  2065. 1 => $parentImgNum?? [],
  2066. 2 => $parentTextNum?? [],
  2067. ];
  2068. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['child']);
  2069. $child = [];
  2070. $resultItem = [
  2071. // 'alias' => $parent_alias ?? '',
  2072. 'category_id' => $parentCatId ?? 0,
  2073. 'pinyin' => $parentImgNum ?? null,
  2074. 'imgnum' => $parentTextNum ?? [],
  2075. // 'textnum' => $textArticles ?? [],
  2076. ]; return $parent;
  2077. }, $data);
  2078. return Result::success($result);
  2079. // });
  2080. }
  2081. /**
  2082. * 封装处理文章的路由问题
  2083. * */
  2084. function processArticle($article, $data) {
  2085. return $article->map(function ($article) use ($data) {
  2086. $catid = $article->cat_arr_id?? '';
  2087. $level = json_decode($catid, true);
  2088. $pinyin = '';
  2089. // $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  2090. if (!empty($level) && is_array($level)) {
  2091. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  2092. ->where('website_id', $data['website_id'])
  2093. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  2094. ->get(['aLIas_pinyin'])
  2095. ->pluck('aLIas_pinyin')
  2096. ->implode('/');
  2097. } else {
  2098. $pinyin = '';
  2099. }
  2100. $article->pinyin = $pinyin;
  2101. return $article;
  2102. });
  2103. }
  2104. /**
  2105. * c端-获取招工招聘
  2106. * @param array $data
  2107. * @return array
  2108. * */
  2109. public function getWebsiteJob(array $data): array
  2110. {
  2111. $web = Website::where('id', $data['website_id'])->first();
  2112. if(empty($web)){
  2113. return Result::error("该网站不存在", 0);
  2114. }
  2115. $job_hunting = JobHunting::where('job_hunting.status', 2)
  2116. ->where('job_hunting.website_id', $data['website_id'])
  2117. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2118. $query->where(function($q) use ($data) {
  2119. $q->WhereRaw("JSON_CONTAINS(job_hunting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2120. });
  2121. })
  2122. ->select('job_hunting.id','job_hunting.cat_arr_id','job_hunting.job','job_hunting.industry',
  2123. 'job_hunting.city_arr_id','job_hunting.experience','job_hunting.updated_at')
  2124. ->orderBy('updated_at', 'desc')
  2125. ->limit($data['job1_num'])
  2126. ->get();
  2127. $web['website_id'] = $data['website_id'];
  2128. if(empty($job_hunting)){
  2129. $job_huntings = "未查询到相关简历信息";
  2130. }else{
  2131. $job_huntings = $this->processJob($job_hunting, $web);
  2132. }
  2133. $job_recruiting = JobRecruiting::where('job_recruiting.status', 1)
  2134. ->where('job_recruiting.website_id', $data['website_id'])
  2135. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2136. $query->where(function($q) use ($data) {
  2137. $q->WhereRaw("JSON_CONTAINS(job_recruiting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2138. });
  2139. })
  2140. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2141. ->select('job_recruiting.id','job_recruiting.cat_arr_id','job_recruiting.title',
  2142. 'job_recruiting.jtzw_id','job_recruiting.hy_id','job_recruiting.city_arr_id',
  2143. 'job_recruiting.due_data','job_recruiting.updated_at','job_recruiting.experience',
  2144. 'job_recruiting.educational','job_company.business_name',)
  2145. ->orderBy('updated_at', 'desc')
  2146. ->limit($data['job2_num'])
  2147. ->get();
  2148. if(empty($job_recruiting->toArray())){
  2149. $job_recruitings = "未查询到相关职位信息";
  2150. }else{
  2151. $job_recruitings = $this->processJob($job_recruiting, $web);
  2152. }
  2153. $hy = JobIndustry::get()->all();
  2154. $zw = JobPosition::where('zwpid',0)->get()->all();
  2155. $jtzw = JobPosition::where('zwpid','!=',0)->get()->all();
  2156. $result = [
  2157. 'job_hunting' => $job_huntings,
  2158. 'job_recuiting' => $job_recruitings,
  2159. 'hy' => $hy,
  2160. 'zw' => $zw,
  2161. 'jtzw' => $jtzw,
  2162. ];
  2163. return Result::success($result);
  2164. }
  2165. /**
  2166. * c端-获取招工招聘列表
  2167. * @param array $data
  2168. * @return array
  2169. * */
  2170. public function getWebsiteJobList(array $data): array
  2171. {
  2172. $where = [];
  2173. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2174. if(empty($web)){
  2175. return Result::error("此网站不存在", 0);
  2176. }
  2177. $website_id['website_id'] = $data['website_id'];
  2178. if(isset($data['hy_id']) && !empty($data['hy_id'])){
  2179. array_push($where, ['hy_id',$data['hy_id']]);
  2180. }
  2181. if(isset($data['zw_id']) && !empty($data['zw_id'])){
  2182. array_push($where, ['zw_id',$data['zw_id']]);
  2183. }
  2184. if(isset($data['jtzw_id']) &&!empty($data['jtzw_id'])){
  2185. array_push($where, ['jtzw_id',$data['jtzw_id']]);
  2186. }
  2187. $query = JobRecruiting::where('job_recruiting.status', 1)
  2188. ->where('job_recruiting.website_id', $data['website_id'])
  2189. ->where($where)
  2190. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2191. $query->where(function($q) use ($data) {
  2192. $q->WhereRaw("JSON_CONTAINS(job_recruiting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2193. });
  2194. })
  2195. ->when(isset($data['catid_id']) &&!empty($data['catid_id']), function ($query) use ($data) {
  2196. $query->where(function($q) use ($data) {
  2197. $q->WhereRaw("JSON_CONTAINS(job_recruiting.cat_arr_id, '". intval($data['catid_id']). "') = 1");
  2198. });
  2199. })
  2200. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2201. ->select('job_recruiting.id','job_recruiting.hy_id','job_recruiting.title','job_recruiting.zw_id',
  2202. 'job_recruiting.jtzw_id','job_recruiting.city_arr_id','job_recruiting.due_data',
  2203. 'job_recruiting.cat_arr_id','job_recruiting.updated_at')
  2204. ->orderBy('updated_at', 'desc');
  2205. $recruit_count = $query->count();
  2206. $query = clone $query;
  2207. $JobRecruiting = $query
  2208. ->offset(($data['page']-1)*$data['pageSize'])
  2209. ->limit($data['pageSize'])
  2210. ->get();
  2211. if(empty($JobRecruiting)){
  2212. $JobRecruiting = "暂无相关职位信息";
  2213. }else{
  2214. $JobRecruiting = $this->processJob($JobRecruiting, $website_id);
  2215. }
  2216. $query = JobHunting::where('status', 2)
  2217. ->where('job_hunting.website_id', $data['website_id'])
  2218. ->where($where)
  2219. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2220. $query->where(function($q) use ($data) {
  2221. $q->WhereRaw("JSON_CONTAINS(job_hunting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2222. });
  2223. })
  2224. ->when(isset($data['catid_id']) &&!empty($data['catid_id']), function ($query) use ($data) {
  2225. $query->where(function($q) use ($data) {
  2226. $q->WhereRaw("JSON_CONTAINS(job_hunting.cat_arr_id, '". intval($data['catid_id']). "') = 1");
  2227. });
  2228. })
  2229. ->select('id','sexy','experience','origin','industry','name','job','job_name_get','city_arr_id','cat_arr_id','created_at','updated_at')
  2230. ->orderBy('updated_at', 'desc');
  2231. $hunt_count = $query->count();
  2232. $query = clone $query;
  2233. $JobHunting = $query
  2234. ->offset(($data['page']-1)*$data['pageSize'])
  2235. ->limit($data['pageSize'])
  2236. ->get();
  2237. if(empty($JobHunting)){
  2238. $JobRecruiting = "暂无相关简历信息";
  2239. }else{
  2240. $JobHunting = $this->processJob($JobHunting, $website_id);
  2241. }
  2242. $result = [
  2243. 'JobRecruiting' => $JobRecruiting,
  2244. 'recruit_count' =>$recruit_count,
  2245. 'JobHunting' => $JobHunting,
  2246. 'hunt_count' =>$hunt_count,
  2247. ];
  2248. return Result::success($result);
  2249. }
  2250. /**
  2251. * c端-获取招工招聘详情
  2252. * @param array $data
  2253. * @return array
  2254. * */
  2255. public function getWebsiteJobInfo(array $data): array
  2256. {
  2257. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2258. if(empty($web)){
  2259. return Result::error("该网站不存在", 0);
  2260. }
  2261. $webid = [
  2262. 'website_id' => $data['website_id'],
  2263. ];
  2264. // 职位相关信息
  2265. if($data['type']==1){
  2266. $query = JobRecruiting::where('status', 1)
  2267. ->where('website_id', $data['website_id'])
  2268. ->select('*');
  2269. $job = $query->where('job_recruiting.id', $data['id'])->get();
  2270. $company = JobCompany::where('job_id', $data['id'])->select('id','business_name','company_hy_id','company_size','company_nature','address_arr_id','address')->get();
  2271. if(!empty($company)){
  2272. $result['company'] = $this->processJob($company, $webid);
  2273. }
  2274. $other_job =JobRecruiting::where('status', 1)
  2275. ->where('website_id', $data['website_id'])
  2276. ->select('title','id','updated_at','cat_arr_id','user_id')
  2277. ->where('id','!=',$data['id'])
  2278. ->where('user_id', '=', $job[0]['user_id'])
  2279. ->limit($data['pageSize'])
  2280. ->get();
  2281. if(!empty($other_job)){
  2282. $result['other_job'] = $this->processJob($other_job, $webid);
  2283. }
  2284. }else{
  2285. $job = JobHunting::where('job_hunting.status', 2)
  2286. ->where('job_hunting.website_id', $data['website_id'])
  2287. ->where('job_hunting.id', $data['id'])
  2288. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  2289. ->select('job_hunting.*','user_name')
  2290. ->get();
  2291. $resume = JobRemuse::where('hunt_id',$data['id'])->get();
  2292. if(!empty($resume->toArray())){
  2293. $result['resume'] = 1;
  2294. } else{
  2295. $result['resume'] = 0;
  2296. }
  2297. }
  2298. if(empty($job->toArray())){
  2299. return Result::error("id参数错误", 0);
  2300. }
  2301. $result['job'] = $this->processJob($job, $webid);
  2302. // 返现对应的栏目
  2303. $catid = json_decode($job[0]['cat_arr_id'],true) ?? '';
  2304. $category = WebsiteCategory::where('website_id', $data['website_id'])
  2305. ->whereIn('category_id', $catid)
  2306. ->select('category_id','alias','aLIas_pinyin','pid')
  2307. ->orderBy('pid')->get()->all();
  2308. if(!empty($category)){
  2309. $result['category'] = $category;
  2310. }
  2311. if(empty($result)){
  2312. return Result::error("参数错误", 0);
  2313. }
  2314. return Result::success($result);
  2315. }
  2316. /**
  2317. * c端-申请职位
  2318. * @param array $data
  2319. * @return array
  2320. * */
  2321. public function getWebsiteJobApply(array $data): array
  2322. {
  2323. // 首先验证网站是否存在
  2324. // return Result::success($data);
  2325. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2326. if(empty($web)){
  2327. return Result::error("该网站不存在", 0);
  2328. }
  2329. // 验证用户是否存在且为个人会员/管理员
  2330. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2331. // 1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2332. if(empty($user) || ($user['type_id'] != 1 && $user['type_id'] != 10000)){
  2333. return Result::error("用户不存在", 0);
  2334. }
  2335. // 去除重复元素和空元素,并保持原始顺序
  2336. $data['recruit_id'] = array_values(array_filter(array_unique($data['recruit_id']), function($value) {
  2337. return !empty($value);
  2338. }));
  2339. // 验证职位是否存在 1:审核通过
  2340. $recruiting = JobRecruiting::where('status', 1)
  2341. ->where('website_id', $data['website_id'])
  2342. ->whereIn('id',$data['recruit_id'])
  2343. ->get(['id as recruit_id','user_id as receiver_id'])->all();
  2344. if(empty($recruiting)){
  2345. return Result::error("该职位不存在", 0);
  2346. }
  2347. // 简历是否存在
  2348. $hunt_id = JobHunting::where('user_id', $data['user_id'])->where('status',2)->first(['id as hunt_id']);
  2349. if(empty($hunt_id)){
  2350. return Result::error("该简历不存在", 0);
  2351. }
  2352. $data['hunt_id'] = $hunt_id['hunt_id'];
  2353. // // 验证是否已投递过该职位
  2354. $apply = JobApply::where('user_id', $data['user_id'])
  2355. ->where('hunt_id', $data['hunt_id'])
  2356. ->where('website_id',$data['website_id'])
  2357. ->whereIn('recruit_id',$data['recruit_id'])
  2358. ->get(['recruit_id']);
  2359. if(!empty($apply->toArray())){
  2360. return Result::error("您已投递过该职位!");
  2361. }
  2362. $insertData = array_map(function($recruiting) use ($data) {
  2363. return [
  2364. 'user_id' => $data['user_id'],
  2365. 'hunt_id' => $data['hunt_id'],
  2366. 'website_id' => $data['website_id'],
  2367. 'recruit_id' => $recruiting['recruit_id'],
  2368. 'receiver_id' => $recruiting['receiver_id'],
  2369. 'status' => 1,
  2370. ];
  2371. }, $recruiting);
  2372. // 批量插入数据
  2373. $result = JobApply::insert($insertData);
  2374. if(empty($result)){
  2375. return Result::error("投递失败", 0);
  2376. }
  2377. return Result::success($result);
  2378. }
  2379. /**
  2380. * 招工招聘-沟通简历
  2381. * @param array $data
  2382. * @return array
  2383. * */
  2384. public function getWebsiteJobResume(array $data): array
  2385. {
  2386. // 首先验证网站是否存在
  2387. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2388. if(empty($web)){
  2389. return Result::error("该网站不存在", 0);
  2390. }
  2391. // return Result::success($data);
  2392. // 验证用户是否存在且为企业会员/管理员
  2393. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2394. // 1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2395. if(empty($user) || ($user['type_id']!= 3 && $user['type_id']!= 10000)){
  2396. return Result::error("用户不存在", 0);
  2397. }
  2398. // 去除重复元素和空元素,并保持原始顺序
  2399. $data['hunt_id'] = array_values(array_filter(array_unique($data['hunt_id']), function($value) {
  2400. return !empty($value);
  2401. }));
  2402. // 验证简历是否存在
  2403. $hunting = JobHunting::where('status', 2)
  2404. ->where('website_id', $data['website_id'])
  2405. ->whereIn('id',$data['hunt_id'])
  2406. ->get(['id as hunt_id','user_id as receiver_id'])->all();
  2407. if(empty($hunting)){
  2408. return Result::error("该简历不存在", 0);
  2409. }
  2410. // 验证是否已沟通过该简历
  2411. $remuse = JobRemuse::where('user_id', $data['user_id'])
  2412. ->where('recruit_id', $data['recruit_id'])
  2413. ->where('website_id',$data['website_id'])
  2414. ->whereIn('hunt_id',$data['hunt_id'])
  2415. ->get(['hunt_id']);
  2416. if(!empty($remuse->toArray())){
  2417. return Result::error("您已沟通过该简历!");
  2418. }
  2419. // 准备要插入的数据数组
  2420. $insertData = array_map(function($hunting) use ($data) {
  2421. return [
  2422. 'user_id' => $data['user_id'],
  2423. 'recruit_id' => $data['recruit_id'],
  2424. 'website_id' => $data['website_id'],
  2425. 'hunt_id' => $hunting['hunt_id'],
  2426. 'receiver_id' => $hunting['receiver_id'],
  2427. 'status' => 1,
  2428. ];
  2429. }, $hunting);
  2430. // 批量插入数据
  2431. $result = JobRemuse::insert($insertData);
  2432. if(empty($result)){
  2433. return Result::error("沟通失败", 0);
  2434. }
  2435. return Result::success($result);
  2436. }
  2437. }