NewsService.php 111 KB

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