NewsService.php 120 KB

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