NewsService.php 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760
  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. * 优化后的获取网站所有文章接口
  942. *
  943. * @param array $data 请求数据
  944. * @return array 处理结果
  945. */
  946. public function getWebsiteAllArticle(array $data): array
  947. {
  948. $startTime = microtime(true); // 记录开始时间
  949. try {
  950. // 解析输入数据
  951. $input = [
  952. 'id' => $data['id'] ?? '',
  953. 'website_id' => $data['website_id'] ?? 0
  954. ];
  955. // 验证必要参数
  956. if (empty($input['website_id'])) {
  957. return Result::error("缺少网站ID参数", 0);
  958. }
  959. // 将 JSON 字符串转换为 PHP 数组
  960. $items = json_decode($input['id'], true);
  961. if (json_last_error() !== JSON_ERROR_NONE || !is_array($items)) {
  962. return Result::error("无效的JSON格式", 0);
  963. }
  964. // 提取所有需要查询的分类ID
  965. $parentCatIds = [];
  966. $childCatIds = [];
  967. foreach ($items as $item) {
  968. if (isset($item['parent']) && $item['parent'] && $item['parent'] !== 'undefined') {
  969. $parts = explode(',', $item['parent']);
  970. if (count($parts) === 3) {
  971. $parentCatIds[] = $parts[0];
  972. }
  973. }
  974. if (isset($item['child']) && $item['child'] && $item['child'] !== 'undefined') {
  975. $parts = explode(',', $item['child']);
  976. if (count($parts) === 3) {
  977. $childCatIds[] = $parts[0];
  978. }
  979. }
  980. }
  981. // 去重分类ID
  982. $parentCatIds = array_unique($parentCatIds);
  983. $childCatIds = array_unique($childCatIds);
  984. // 批量查询分类信息
  985. $websiteCondition = ['website_id' => $input['website_id']];
  986. $categories = $this->getCategoriesByIds(array_merge($parentCatIds, $childCatIds), $websiteCondition);
  987. // 处理每个项目
  988. $result = [];
  989. foreach ($items as $item) {
  990. $resultItem = [
  991. 'alias' => null,
  992. 'category_id' => null,
  993. 'pinyin' => null,
  994. 'imgnum' => [],
  995. 'textnum' => [],
  996. 'child' => []
  997. ];
  998. // 处理父级栏目
  999. if (isset($item['parent']) && $item['parent'] && $item['parent'] !== 'undefined') {
  1000. $parts = explode(',', $item['parent']);
  1001. if (count($parts) !== 3) {
  1002. return Result::error("父级栏目:缺少必需参数错误", 0);
  1003. }
  1004. list($parentCatId, $parentImgNum, $parentTextNum) = $parts;
  1005. // 从已查询的分类中获取信息
  1006. $category = $categories[$parentCatId] ?? null;
  1007. if ($category) {
  1008. $resultItem['alias'] = $category->alias;
  1009. $resultItem['category_id'] = $category->category_id;
  1010. $resultItem['pinyin'] = $category->alias_pinyin;
  1011. // 查询图片新闻
  1012. if ($parentImgNum > 0) {
  1013. $resultItem['imgnum'] = $this->getArticles(
  1014. $parentCatId,
  1015. $input['website_id'],
  1016. (int) $parentImgNum,
  1017. true
  1018. );
  1019. }
  1020. // 查询文字新闻
  1021. if ($parentTextNum > 0) {
  1022. $resultItem['textnum'] = $this->getArticles(
  1023. $parentCatId,
  1024. $input['website_id'],
  1025. (int) $parentTextNum,
  1026. false
  1027. );
  1028. }
  1029. }
  1030. }
  1031. // 处理子级栏目
  1032. if (isset($item['child']) && $item['child'] && $item['child'] !== 'undefined') {
  1033. $parts = explode(',', $item['child']);
  1034. if (count($parts) !== 3) {
  1035. return Result::error("子级栏目:缺少必需参数错误", 0);
  1036. }
  1037. list($childCatId, $childImgNum, $childTextNum) = $parts;
  1038. // 从已查询的分类中获取信息
  1039. $childCategory = $categories[$childCatId] ?? null;
  1040. if ($childCategory) {
  1041. $childData = [
  1042. 'alias' => $childCategory->alias,
  1043. 'category_id' => $childCategory->category_id,
  1044. 'pinyin' => $childCategory->alias_pinyin,
  1045. 'all_childcat' => [],
  1046. 'imgnum' => [],
  1047. 'textnum' => []
  1048. ];
  1049. if(isset($childCategory->pid) && $childCategory->pid != 0){
  1050. // 查询此层级的所有子栏目
  1051. $childData['all_childcat'] = $this->getChildCategories(
  1052. $childCategory->pid,
  1053. $input['website_id']
  1054. );
  1055. }
  1056. // 查询子栏目图片新闻
  1057. if ($childImgNum > 0) {
  1058. $childData['imgnum'] = $this->getArticles(
  1059. $childCatId,
  1060. $input['website_id'],
  1061. (int) $childImgNum,
  1062. true
  1063. );
  1064. }
  1065. // 查询子栏目文字新闻
  1066. if ($childTextNum > 0) {
  1067. $childData['textnum'] = $this->getArticles(
  1068. $childCatId,
  1069. $input['website_id'],
  1070. (int) $childTextNum,
  1071. false
  1072. );
  1073. }
  1074. $resultItem['child'] = $childData;
  1075. }
  1076. }
  1077. $result[] = $resultItem;
  1078. // 检查是否超时
  1079. $elapsedTime = microtime(true) - $startTime;
  1080. if ($elapsedTime > 5) {
  1081. return Result::error("请求处理时间已超过5秒,已经达到".$elapsedTime, 0);
  1082. }
  1083. }
  1084. return Result::success($result);
  1085. } catch (\Exception $e) {
  1086. return Result::error("处理请求时出错: " . $e->getMessage(), 0);
  1087. }
  1088. }
  1089. /**
  1090. * 批量获取分类信息
  1091. *
  1092. * @Cacheable(prefix="categories", value="#{ids}_{website['website_id']}", ttl=3600)
  1093. */
  1094. protected function getCategoriesByIds(array $ids, array $website): array
  1095. {
  1096. if (empty($ids)) {
  1097. return [];
  1098. }
  1099. $categories = WebsiteCategory::whereIn('category_id', $ids)
  1100. ->where($website)
  1101. ->get(['alias', 'category_id', 'alias_pinyin', 'type', 'pid', 'category_arr_id']);
  1102. $result = [];
  1103. foreach ($categories as $category) {
  1104. $result[$category->category_id] = $category;
  1105. }
  1106. return $result;
  1107. }
  1108. /**
  1109. * 获取文章列表
  1110. *
  1111. * @Cacheable(prefix="articles", value="#{catId}_{websiteId}_{limit}_{isImage}", ttl=300)
  1112. */
  1113. protected function getArticles(int $catId, int $websiteId, int $limit, bool $isImage = false): array
  1114. {
  1115. $query = Article::where('article.status', 1)
  1116. ->where(function($query) use ($catId) {
  1117. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$catId\"')")
  1118. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$catId')");
  1119. })
  1120. ->where(function ($query) use ($websiteId) {
  1121. $query->where(function ($subQuery) use ($websiteId) {
  1122. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '$websiteId') = 0")
  1123. ->orWhereNull("ignore_ids");
  1124. });
  1125. })
  1126. ->leftJoin('website_category', function($join) use ($websiteId) {
  1127. $join->on('article.catid', '=', 'website_category.category_id')
  1128. ->where('website_category.website_id', '=', $websiteId);
  1129. })
  1130. ->select(
  1131. 'article.id', 'article.title', 'article.imgurl', 'article.author', 'article.updated_at',
  1132. 'article.introduce', 'article.islink', 'article.linkurl', 'article.copyfrom', 'article.cat_arr_id',
  1133. 'article.catid', 'website_category.alias as category_name',
  1134. )
  1135. ->orderBy('updated_at', 'desc')
  1136. ->limit($limit);
  1137. if ($isImage) {
  1138. $query->where('imgurl', '!=', '');
  1139. }
  1140. $articles = $query->get();
  1141. // 处理文章路由
  1142. if (!empty($articles)) {
  1143. $articles = $this->processArticles($articles, ['website_id' => $websiteId]);
  1144. }
  1145. return $articles->toArray();
  1146. }
  1147. /**
  1148. * 获取子分类
  1149. *
  1150. * @Cacheable(prefix="child_categories", value="#{pid}_{websiteId}", ttl=3600)
  1151. */
  1152. protected function getChildCategories(int $pid, int $websiteId): array
  1153. {
  1154. $categories = WebsiteCategory::where('website_id', $websiteId)
  1155. ->where("pid", $pid)
  1156. ->get(['alias', 'category_id', 'alias_pinyin', 'pid', 'category_arr_id']);
  1157. // 处理分类路由
  1158. if (!empty($categories)) {
  1159. $categories = $this->processArticles($categories, ['website_id' => $websiteId]);
  1160. }
  1161. return $categories->toArray();
  1162. }
  1163. /**
  1164. * 乡村网-获取特殊新闻模块
  1165. * @param array $data
  1166. * @return array
  1167. */
  1168. public function getWebsiteArticles(array $data): array
  1169. {
  1170. $input['id'] = $data['id'];
  1171. $input['website_id'] = $data['website_id'];
  1172. $data = json_decode($input['id'], true);
  1173. // 使用 array_map 处理每个元素
  1174. $result = array_map(function ($item) use ($input) {
  1175. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1176. $website = [
  1177. 'website_id' => $input['website_id']
  1178. ];
  1179. // 查询栏目名称
  1180. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1181. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1182. if(empty($category)){
  1183. $imgArticles = [];
  1184. $textArticles = [];
  1185. // return Result::error("暂无此栏目",0);
  1186. }else{
  1187. $child_category = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1188. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1189. // return Result::success($website);
  1190. // 查询图片新闻
  1191. // 合并查询条件
  1192. $baseQuery = Article::where(function ($query) use ($website) {
  1193. $query->where(function ($subQuery) use ($website) {
  1194. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1195. })->orWhereNull("ignore_ids");
  1196. })
  1197. ->where('website_category.website_id', $website['website_id'])
  1198. ->where('article.status', 1)
  1199. ->leftJoin('website_category', 'website_category.category_id', 'article.catid');
  1200. // 查询文字新闻
  1201. $textArticles = clone $baseQuery;
  1202. $textArticles = $textArticles->whereIn('catid',$child_category)
  1203. ->select(
  1204. 'article.id',
  1205. 'article.title',
  1206. // 'article.imgurl',
  1207. 'article.author',
  1208. 'article.updated_at',
  1209. 'article.introduce',
  1210. 'article.islink',
  1211. 'article.linkurl',
  1212. 'article.copyfrom',
  1213. 'website_category.category_id',
  1214. 'website_category.alias',
  1215. 'website_category.aLIas_pinyin'
  1216. )
  1217. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1218. ->orderBy('article.updated_at', 'desc')
  1219. ->limit($parentTextNum)
  1220. ->get()
  1221. ->all();
  1222. if(empty($textArticles)){
  1223. $textArticles = clone $baseQuery;
  1224. $textArticles = $textArticles->where('catid',$parentCatId)
  1225. ->select(
  1226. 'article.id',
  1227. 'article.title',
  1228. // 'article.imgurl',
  1229. 'article.author',
  1230. 'article.updated_at',
  1231. 'article.introduce',
  1232. 'article.islink',
  1233. 'article.linkurl',
  1234. 'article.copyfrom',
  1235. 'website_category.category_id',
  1236. 'website_category.alias',
  1237. 'website_category.aLIas_pinyin'
  1238. )
  1239. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1240. ->orderBy('article.updated_at', 'desc')
  1241. ->limit($parentTextNum)
  1242. ->get()
  1243. ->all();
  1244. }
  1245. // 查询图片新闻
  1246. $imgArticles = clone $baseQuery;
  1247. $imgArticles = $imgArticles->where('imgurl', '!=', '')->whereIn('catid',$child_category)
  1248. ->select(
  1249. 'article.id',
  1250. 'article.title',
  1251. 'article.imgurl',
  1252. 'article.author',
  1253. 'article.updated_at',
  1254. 'article.introduce',
  1255. 'article.islink',
  1256. 'article.linkurl',
  1257. 'article.copyfrom',
  1258. 'website_category.category_id',
  1259. 'website_category.alias',
  1260. 'website_category.aLIas_pinyin'
  1261. )
  1262. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1263. ->orderBy('article.updated_at', 'desc')
  1264. ->limit($parentImgNum)
  1265. ->get()
  1266. ->all();
  1267. if(empty($imgArticles)){
  1268. $imgArticles = clone $baseQuery;
  1269. $imgArticles = $imgArticles->where('imgurl', '!=', '')->where('catid',$parentCatId)
  1270. ->select(
  1271. 'article.id',
  1272. 'article.title',
  1273. 'article.imgurl',
  1274. 'article.author',
  1275. 'article.updated_at',
  1276. 'article.introduce',
  1277. 'article.islink',
  1278. 'article.linkurl',
  1279. 'article.copyfrom',
  1280. 'website_category.category_id',
  1281. 'website_category.alias',
  1282. 'website_category.aLIas_pinyin'
  1283. )
  1284. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1285. ->orderBy('article.updated_at', 'desc')
  1286. ->limit($parentImgNum)
  1287. ->get()
  1288. ->all();
  1289. }
  1290. }
  1291. $resultItem = [
  1292. 'alias' => $category ? $category->alias : null,
  1293. 'category_id' => $parentCatId,
  1294. 'pinyin' => $pinyin ? $pinyin : null,
  1295. 'imgnum' => $imgArticles ?? [],
  1296. 'textnum' => $textArticles ?? [],
  1297. ];
  1298. return $resultItem;
  1299. }, $data);
  1300. return Result::success($result);
  1301. }
  1302. // 封装处理商品的路由问题
  1303. function processGoods($goods, $data) {
  1304. return $goods->map(function ($good) use ($data) {
  1305. $catid = $good->catid ?? 0;
  1306. // $pinyin = '';
  1307. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1308. if (!empty($category->pid) && $category->pid != 0) {
  1309. $level = json_decode($category->category_arr_id);
  1310. $pinyin = WebsiteCategory::where('website_id', $data['website_id'])
  1311. ->whereIn('category_id', $level)
  1312. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1313. ->get(['aLIas_pinyin'])
  1314. ->pluck('aLIas_pinyin')
  1315. ->implode('/');
  1316. } else {
  1317. $pinyin = $category->aLIas_pinyin ?? '';
  1318. }
  1319. if(isset($good->city_id) &&!empty($good->city_id)){
  1320. $city = District::where('id', $good->city_id)->first(['name']);
  1321. $good->city_name = $city->name ?? '';
  1322. }
  1323. // 解析imgurl JSON并取第一条数据
  1324. $imgUrls = json_decode($good->imgurl, true);
  1325. $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
  1326. $good->pinyin = $pinyin;
  1327. return $good;
  1328. });
  1329. }
  1330. /**
  1331. * 获取商城首页-根据栏目id
  1332. * @param array $data
  1333. * @return array
  1334. * */
  1335. function getWebsiteCatidshop(array $data): array
  1336. {
  1337. $input['catid'] = $data['catid'];
  1338. $input['website_id'] = $data['website_id'];
  1339. $data = json_decode($input['catid'] ?? '', true) ?? [];
  1340. $result = array_map(function ($item) use ($input) {
  1341. if (isset($item['catid']) && $item['catid'] != 'undefined' && $item['catid']!= "") {
  1342. list($catid, $goodStart,$goodNum) = explode(',', $item['catid']);
  1343. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $input['website_id'])->first(['type']);
  1344. // 类型:1资讯(默认)2商品3书刊音像4招聘5求职类型:1资讯(默认)2商品3书刊音像4招聘5求职6招工招聘
  1345. if(empty($category) || $category->type != 2){
  1346. return Result::error("暂无此栏目",0);
  1347. }else{
  1348. $website['website_id'] = $input['website_id'];
  1349. $goods = Good::where('good.status', 2)
  1350. ->where('good.website_id', $website['website_id'])
  1351. ->whereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($catid). "') = 1")
  1352. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
  1353. 'good.catid','good.type_id','good.price','good.com','good.level','good.unit')
  1354. ->orderBy('updated_at','desc')
  1355. ->offset($goodStart)
  1356. ->limit($goodNum);
  1357. $all_goods = $goods->get();
  1358. $all_goods = $this->processGoods($all_goods, $website);
  1359. }
  1360. return $all_goods ?? [];
  1361. }
  1362. }, $data);
  1363. return Result::success($result);
  1364. }
  1365. /**
  1366. * 获取商品模块
  1367. * @param array $data
  1368. * @return array
  1369. * */
  1370. public function getWebsiteshop(array $data): array
  1371. {
  1372. $input['id'] = $data['id'];
  1373. $input['website_id'] = $data['website_id'];
  1374. $catid = $data['catid'];
  1375. $data = json_decode($input['id'] ?? '', true) ?? [];
  1376. $result['goods'] = array_map(function ($item) use ($input) {
  1377. // 检查parent元素是否存在且不是undefined
  1378. if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
  1379. list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
  1380. $website = $input['website_id'];
  1381. $query = Good::where('good.status', 2)
  1382. ->where('good.website_id', $website);
  1383. switch ($Levelid) {
  1384. case 1:
  1385. case 2:
  1386. case 3:
  1387. $goods = $query->where(function($q) use ($Levelid) {
  1388. $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 1")
  1389. ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 1");
  1390. });
  1391. break;
  1392. case 4:
  1393. $goods = $query;
  1394. break;
  1395. case 5:
  1396. $goods = $query->where('type_id',1);
  1397. break;
  1398. case 6:
  1399. $goods = $query->where('type_id',2);
  1400. break;
  1401. default:
  1402. return [];
  1403. }
  1404. $all_goods = $goods
  1405. ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
  1406. 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
  1407. 'good.website_id')
  1408. ->orderBy('updated_at','desc')
  1409. ->offset($goodStart)
  1410. ->limit($goodNum)
  1411. ->get();
  1412. $web['website_id'] = $website;
  1413. $all_goods = $this->processGoods($all_goods, $web);
  1414. }
  1415. return $all_goods;
  1416. }, $data);
  1417. $website = $input['website_id'];
  1418. $result['article'] = Article::where(function ($query) use ($website) {
  1419. $query->where(function ($subQuery) use ($website) {
  1420. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website) . "') = 0");
  1421. })->orWhereNull("ignore_ids");
  1422. })
  1423. ->where('catid', $catid)
  1424. ->where('article.status', 1)
  1425. ->leftJoin('article_data', 'article_data.article_id', 'article.id')
  1426. ->select('article.id', 'article.title', 'article.updated_at', 'introduce', 'islink', 'linkurl','article_data.content')
  1427. ->orderBy('article.updated_at', 'desc')
  1428. ->first();
  1429. return Result::success($result);
  1430. }
  1431. /**
  1432. * 获取商品分类
  1433. * @param array $data
  1434. * @return array
  1435. * */
  1436. public function getWebsiteshopCat(array $data): array
  1437. {
  1438. $website = $data['website_id'];
  1439. $category = WebsiteCategory::where('website_id', $website)
  1440. ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
  1441. ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
  1442. ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id' ,'sort')
  1443. ->orderBy('sort')
  1444. ->get();
  1445. $cat = $category->map(function ($item) use ($website) {
  1446. $cat_arr_id = json_decode($item->category_arr_id, true) ?? [];
  1447. $pid = $item->pid?? 0;
  1448. if(!empty($cat_arr_id) && is_array($cat_arr_id) && $pid != 0){
  1449. $pinyin = WebsiteCategory::whereIn('category_id', $cat_arr_id)
  1450. ->where('website_id', $website)
  1451. ->orderByRaw('FIELD(category_id, '. implode(',', $cat_arr_id). ')')
  1452. ->get(['aLIas_pinyin'])
  1453. ->pluck('aLIas_pinyin')
  1454. ->implode('/');
  1455. }else{
  1456. $pinyin = $item->aLIas_pinyin ?? '';
  1457. }
  1458. $item->pinyin = $pinyin;
  1459. });
  1460. if(empty($category)){
  1461. return Result::error("栏目查询失败", 0);
  1462. }
  1463. $cat_tree = Result::buildMenuTree($category);
  1464. $web['website_id'] = $website;
  1465. $goods = Good::where('website_id', $website)
  1466. ->where('status', 2)
  1467. ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
  1468. ->latest('updated_at')
  1469. ->offset(($data['page']-1)*$data['pageSize'])
  1470. ->limit($data['pageSize'])
  1471. ->get();
  1472. if(!empty($goods)){
  1473. if($goods->count() > 1 && !empty($goods)){
  1474. $goods = $this->processGoods($goods, $web);
  1475. }
  1476. }else{
  1477. $goods = [];
  1478. }
  1479. $result = [
  1480. 'category' => $cat_tree,
  1481. 'goods' => $goods,
  1482. ];
  1483. // $resul['goods'] = $goods;
  1484. if(empty($result)){
  1485. return Result::error("查询失败", 0);
  1486. }
  1487. return Result::success($result);
  1488. }
  1489. /*
  1490. * 获取商品列表
  1491. * @param array $data
  1492. * @return array
  1493. * */
  1494. public function getWebsiteshopList(array $data): array
  1495. {
  1496. // return Result::success($data);
  1497. $where = [
  1498. 'status' => 2,
  1499. 'website_id' => $data['website_id'],
  1500. ];
  1501. if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
  1502. return Result::error("查询失败", 0);
  1503. }
  1504. if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
  1505. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
  1506. $data['catid'] = $category->category_id ?? 0;
  1507. }
  1508. if(isset($data['keyword']) &&!empty($data['keyword'])){
  1509. array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
  1510. }
  1511. if(isset($data['type_id']) && !empty($data['type_id'])){
  1512. array_push($where, ['type_id', $data['type_id']]);
  1513. }
  1514. $query = Good::where($where)
  1515. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  1516. $query->where(function($q) use ($data) {
  1517. $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
  1518. ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
  1519. });
  1520. })
  1521. ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
  1522. $query->where(function($q) use ($data) {
  1523. $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
  1524. ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
  1525. });
  1526. })
  1527. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at', 'good.com',
  1528. 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
  1529. ->latest('updated_at');
  1530. $result['type1_count'] = $query->clone()->where('type_id', 1)->count();
  1531. // 获取 type_id 为 1 的数据
  1532. $result['type1'] = $this->processGoods(
  1533. $query->clone()
  1534. ->where('type_id', 1)
  1535. ->offset(($data['page'] - 1) * $data['pageSize'])
  1536. ->limit($data['pageSize'])
  1537. ->get(),
  1538. $data
  1539. );
  1540. $result['type2_count'] = $query->clone()->where('type_id', 2)->count();
  1541. // 获取 type_id 为 2 的数据
  1542. $result['type2'] = $this->processGoods(
  1543. $query->clone()
  1544. ->where('type_id', 2)
  1545. ->offset(($data['page'] - 1) * $data['pageSize'])
  1546. ->limit($data['pageSize'])
  1547. ->get(),
  1548. $data
  1549. );
  1550. if(empty($result)){
  1551. return Result::error("查询失败", 0);
  1552. }
  1553. return Result::success($result);
  1554. }
  1555. /**
  1556. * 获取商品详情
  1557. * @param array $data
  1558. * @return array
  1559. * */
  1560. public function getWebsiteshopInfo(array $data): array
  1561. {
  1562. $where = [
  1563. 'good.status' => 2,
  1564. 'good.website_id' => $data['website_id'],
  1565. 'good.id' => $data['id'],
  1566. ];
  1567. $goods = Good::where($where)
  1568. ->where('good.id', $data['id'])
  1569. ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
  1570. ->select('good.*','website_category.alias','website_category.category_id')
  1571. ->first();
  1572. if(empty($goods)){
  1573. return Result::error("查询失败", 0);
  1574. }
  1575. $goods->imgurl = json_decode($goods->imgurl, true);
  1576. return Result::success($goods);
  1577. }
  1578. /**
  1579. * 封装处理文章的路由问题
  1580. * */
  1581. function processArticles($articles, $data) {
  1582. // 检查 $articles 是否为数组或可迭代对象
  1583. if (is_array($articles) || is_iterable($articles) &&!empty($articles)) {
  1584. return $articles->map(function ($article) use ($data) {
  1585. if(isset($article->cat_arr_id) &&!empty($article->cat_arr_id)){
  1586. $catid = $article->cat_arr_id?? '';
  1587. }
  1588. if(isset($article->category_arr_id) &&!empty($article->category_arr_id)){
  1589. $catid = $article->category_arr_id?? '';
  1590. }
  1591. // $catid = $article->cat_arr_id || $article->category_arr_id ?? '';
  1592. $level = json_decode($catid, true);
  1593. if (!is_array($level)) {
  1594. return $article;
  1595. } else {
  1596. $level = array_map('intval', $level);
  1597. }
  1598. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1599. ->where($data)
  1600. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1601. ->pluck('aLIas_pinyin')
  1602. ->implode('/');
  1603. $article->pinyin = $pinyin ?? '';
  1604. return $article;
  1605. });
  1606. } else {
  1607. // 若 $articles 不是数组或可迭代对象,直接处理单条数据
  1608. $catid = $articles->cat_arr_id?? '';
  1609. $level = json_decode($catid, true);
  1610. if (!is_array($level)) {
  1611. return $articles;
  1612. } else {
  1613. $level = array_map('intval', $level);
  1614. }
  1615. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1616. ->where($data)
  1617. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1618. ->pluck('aLIas_pinyin')
  1619. ->implode('/');
  1620. $articles->pinyin = $pinyin ?? '';
  1621. return $articles;
  1622. }
  1623. }
  1624. // 封装处理由问题
  1625. function processJob($job, $data) {
  1626. return $job->map(function ($job) use ($data) {
  1627. $category = $job->cat_arr_id?? '';
  1628. $cityid = $job->city_arr_id?? '';
  1629. $city = json_decode($cityid, true);
  1630. $pinyin = '';
  1631. $level = json_decode($category, true);
  1632. // 路由
  1633. if(!empty($level) && is_array($level)){
  1634. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1635. ->where('website_id', $data['website_id']) // 添加网站ID条件
  1636. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1637. ->get(['aLIas_pinyin'])
  1638. ->pluck('aLIas_pinyin')
  1639. ->implode('/');
  1640. if(empty($pinyin)){
  1641. $pinyin = $pinyin->aLIas_pinyin?? '';
  1642. }
  1643. $job->pinyin = $pinyin;
  1644. }
  1645. // 取职位-城市 市??省
  1646. if(!empty($city) && is_array($city)){
  1647. if(isset($city[1] ) && !empty($city[1])){
  1648. $city = District::where('id', $city[1])->first(['name']);
  1649. $job->city_name = $city->name?? '';
  1650. }else if(isset($city[0] ) && !empty($city[0])){
  1651. $city = District::where('id', $city[0])->first(['name']);
  1652. $job->city_name = $city->name?? '';
  1653. }else{
  1654. $job->city_name = '全国';
  1655. }
  1656. }
  1657. // 获取简历最后一级地区
  1658. if(isset($job->city_id) &&!empty($job->city_id)){
  1659. $city = District::where('id', $job->city_id)->first(['name']);
  1660. $job->hunt_cityname = $city->name?? '';
  1661. }
  1662. // 组合详细地址
  1663. if(isset($job->address_arr_id) && !empty($job->address_arr_id)){
  1664. $address_id = json_decode($job->address_arr_id, true) ?? [];
  1665. if(is_array($address_id) && !empty($address_id)){
  1666. $address = District::whereIn('id', $address_id)
  1667. ->orderBy('level', 'asc')
  1668. ->get(['name'])
  1669. ->pluck('name')
  1670. ->implode('');
  1671. // $job->address_name = $address ?? '';
  1672. $job->address_name = ($address ?? '') . ($job->address ?? '');
  1673. }
  1674. }
  1675. // 取行业
  1676. if(!empty($job->hy_id) || !empty($job->industry) || !empty($job->company_hy_id || !empty($job->job_industry))){
  1677. $hy_name = JobIndustry::when($job, function ($query) use ($job) {
  1678. if(!empty($job->industry)){
  1679. $query->where('hyid', $job->industry);
  1680. }else if(!empty($job->hy_id)){
  1681. $query->where('hyid', $job->hy_id);
  1682. }else if(!empty($job->company_hy_id)){
  1683. $query->where('hyid', $job->company_hy_id);
  1684. } else{
  1685. $query->where('hyid', $job->job_industry);
  1686. }
  1687. })->first(['hyname']);
  1688. $job->hy_name = $hy_name->hyname?? '';
  1689. }
  1690. // 取职位类别
  1691. if((isset($job->zw_id) && !empty($job->zw_id)) || (isset($job->job) && !empty($job->job)) || (isset($job->job_typename) && !empty($job->job_typename))){
  1692. $zwid = $job->job ?? $job->zw_id ?? $job->job_typename;
  1693. $zw_name = JobPosition::where('zwid', $zwid)->first(['zwname']);
  1694. $job->zw_name = $zw_name->zwname?? '';
  1695. }
  1696. // 取具体职位
  1697. 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))){
  1698. $jtzwid = $job->job_name_get ?? $job->jtzw_id ?? $job->job_name;
  1699. $jtzw_name = JobPosition::where('zwid', $jtzwid)->first(['zwname']);
  1700. $job->jtzw_name = $jtzw_name->zwname?? '';
  1701. }
  1702. // 取工作经验
  1703. if(isset($job->experience) &&!empty($job->experience)){
  1704. $experience = JobEnum::where('egroup','years')->where('evalue',$job->experience)->first(['ename']);
  1705. $job->experience_name = $experience->ename?? '';
  1706. }
  1707. // 取学历
  1708. if(isset($job->educational) &&!empty($job->educational) || isset($job->school_education) && !empty($job->school_education)) {
  1709. $education = $job->educational ?? $job->school_education;
  1710. $education = JobEnum::where('egroup','education')->where('evalue',$education)->first(['ename']);
  1711. $job->education_name = $education->ename?? '';
  1712. }
  1713. // 语言
  1714. if(isset($job->language) &&!empty($job->language)){
  1715. $language = JobEnum::where('egroup','language')->where('evalue',$job->language)->first(['ename']);
  1716. $job->language_name = $language->ename?? '';
  1717. }
  1718. // 薪资
  1719. if(isset($job->salary) &&!empty($job->salary)){
  1720. $salary = JobEnum::where('egroup','income')->where('evalue',$job->salary)->first(['ename']);
  1721. $job->salary_name = $salary->ename?? '';
  1722. }
  1723. // 职位性质
  1724. if(isset($job->nature_id) &&!empty($job->nature_id)){
  1725. $job_nature = JobEnum::where('egroup','nature')->where('evalue',$job->nature_id)->first(['ename']);
  1726. $job->job_nature_name = $job_nature->ename?? '';
  1727. }
  1728. // 公司规模
  1729. if(isset($job->company_size) &&!empty($job->company_size)){
  1730. $company_size = JobEnum::where('egroup','cosize')->where('evalue',$job->company_size)->first(['ename']);
  1731. $job->company_size_name = $company_size->ename?? '';
  1732. }
  1733. // 公司性质
  1734. if(isset($job->company_nature) && !empty($job->company_nature)){
  1735. $company_nature = JobNature::where('id',$job->company_nature)->first(['nature_name']);
  1736. $job->company_nature_name = $company_nature->nature_name?? '';
  1737. }
  1738. // $job->pinyin = $pinyin;
  1739. return $job;
  1740. });
  1741. }
  1742. /**
  1743. * 获取书籍模块
  1744. * @param array $data
  1745. * @return array
  1746. * */
  1747. public function getWebsiteBook(array $data): array
  1748. {
  1749. $input['id'] = $data['id'];
  1750. $input['website_id'] = $data['website_id'];
  1751. // 将 JSON 字符串转换为 PHP 数组
  1752. $data = json_decode($input['id'], true);
  1753. // 使用 array_map 处理每个元素
  1754. $result = array_map(function ($item) use ($input) {
  1755. // 检查parent元素是否存在且不是undefined
  1756. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1757. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1758. $website = [
  1759. 'website_id' => $input['website_id'],
  1760. ];
  1761. // 查询栏目名称
  1762. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1763. if(empty($category)){
  1764. $parent_alias = '';
  1765. $parent_pinyin = null;
  1766. $imgBooks = [];
  1767. $textBooks = [];
  1768. }else{
  1769. $parent_alias = $category->alias ?? '';
  1770. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1771. // 查找子分类ID数组
  1772. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1773. array_push($childCategoryIds,$parentCatId);
  1774. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1775. if($parentImgNum!=0){
  1776. // 查询图片新闻
  1777. $imgBooks = Book::where(function($query) use ($parentCatId) {
  1778. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1779. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1780. })
  1781. ->where('book.status', 2)
  1782. ->where('book.img_url', '!=', '')
  1783. ->leftJoin('website_category', function($join) use ($website) {
  1784. $join->on('book.cat_id', '=', 'website_category.category_id')
  1785. ->where('website_category.website_id', '=', $website['website_id']);
  1786. })
  1787. ->select(
  1788. 'book.id',
  1789. 'book.title',
  1790. 'book.img_url',
  1791. 'book.price',
  1792. 'book.market_price',
  1793. 'book.description',
  1794. 'book.cat_id',
  1795. 'book.description',
  1796. 'book.updated_at',
  1797. 'website_category.alias as category_name',
  1798. DB::raw("CASE WHEN book.cat_id = $parentCatId THEN '$parent_pinyin'
  1799. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1800. )
  1801. ->orderBy('updated_at', 'desc')
  1802. ->limit($parentImgNum)
  1803. ->get()->all();
  1804. // 查询文字新闻
  1805. }
  1806. if($parentTextNum!=0){
  1807. $textBooks = [];
  1808. $textBooks = Book::where(function($query) use ($parentCatId) {
  1809. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1810. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1811. })
  1812. ->where('book.status', 2)
  1813. ->leftJoin('website_category', function($join) use ($website) {
  1814. $join->on('book.cat_id', '=', 'website_category.category_id')
  1815. ->where('website_category.website_id', '=', $website['website_id']);
  1816. })
  1817. ->select(
  1818. 'book.id',
  1819. 'book.title',
  1820. 'book.img_url',
  1821. 'book.price',
  1822. 'book.market_price',
  1823. 'book.description',
  1824. 'book.cat_id',
  1825. 'book.description',
  1826. 'book.updated_at',
  1827. 'website_category.alias as category_name',
  1828. DB::raw("CASE WHEN book.cat_id = $parentCatId THEN '$parent_pinyin'
  1829. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1830. )
  1831. ->orderBy('updated_at', 'desc')
  1832. ->limit($parentTextNum)
  1833. ->get()->all();
  1834. }
  1835. }
  1836. }
  1837. $resultItem = [
  1838. 'alias' => $parent_alias ?? '',
  1839. 'category_id' => $parentCatId ?? 0,
  1840. 'pinyin' => $parent_pinyin ?? null,
  1841. 'imgnum' => $imgBooks ?? [],
  1842. 'textnum' => $textBooks ?? [],
  1843. ];
  1844. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1845. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1846. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1847. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1848. ->get()->all();
  1849. if (!empty($childCategory)) {
  1850. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1851. // 查询子栏目名称
  1852. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1853. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1854. ->first();
  1855. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1856. $childImgArticles = [];
  1857. $childTextArticles = [];
  1858. $resultItem['child'] = [];
  1859. }else{
  1860. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1861. // 查询子栏目图片新闻
  1862. $childImgArticles = Book::where('cat_id', $childCatId)
  1863. ->where('status', 2)
  1864. ->where('img_url', '!=', '')
  1865. ->leftJoin('website_category', function($join) use ($website) {
  1866. $join->on('book.cat_id', '=', 'website_category.category_id')
  1867. ->where('website_category.website_id', '=', $website['website_id']);
  1868. })
  1869. ->select(
  1870. 'book.id',
  1871. 'book.title',
  1872. 'book.img_url',
  1873. 'book.price',
  1874. 'book.market_price',
  1875. 'book.description',
  1876. 'book.cat_id',
  1877. 'book.description',
  1878. 'book.updated_at',
  1879. DB::raw("'$child_pinyin' as pinyin"))
  1880. ->orderBy('updated_at', 'desc')
  1881. ->limit($childImgNum)
  1882. ->get()->all();
  1883. // 查询子栏目文字新闻
  1884. $childTextArticles = Book::where('cat_id', $childCatId)
  1885. ->where('status', 2)
  1886. ->leftJoin('website_category', function($join) use ($website) {
  1887. $join->on('book.cat_id', '=', 'website_category.category_id')
  1888. ->where('website_category.website_id', '=', $website['website_id']);
  1889. })
  1890. ->select(
  1891. 'book.id',
  1892. 'book.title',
  1893. 'book.img_url',
  1894. 'book.price',
  1895. 'book.market_price',
  1896. 'book.description',
  1897. 'book.cat_id',
  1898. 'book.description',
  1899. 'book.updated_at',
  1900. DB::raw("'$child_pinyin' as pinyin"))
  1901. ->orderBy('updated_at', 'desc')
  1902. ->limit($childTextNum)
  1903. ->get()->all();
  1904. $resultItem['child'] = [
  1905. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1906. 'category_id' => $childCatId,
  1907. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1908. 'all_childcat' => $childCategory,
  1909. 'imgnum' => $childImgArticles,
  1910. 'textnum' => $childTextArticles,
  1911. ];
  1912. }
  1913. }
  1914. }else{
  1915. $resultItem['child'] = [];
  1916. }
  1917. return $resultItem;
  1918. }, $data);
  1919. return Result::success($result);
  1920. }
  1921. /**
  1922. * 获取书刊列表
  1923. * @param array $data
  1924. * @return array
  1925. * */
  1926. public function getWebsiteBookList(array $data): array
  1927. {
  1928. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  1929. if(empty($web)){
  1930. return Result::error("查询失败", 0);
  1931. }
  1932. $catid = $data['id'];
  1933. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$catid)->orderBy('sort')->first(['pid','category_id','aLias_pinyin']);
  1934. $parent_categpory = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$category['pid'])->orderBy('sort')->first(['aLias_pinyin']);
  1935. $parent_pinyin = $parent_categpory ? $parent_categpory->aLias_pinyin ?? '' : '';
  1936. $category_pinyin = $category ? $category->aLias_pinyin : '';
  1937. $pinyin =$category_pinyin ? $parent_pinyin . '/' . $category_pinyin ?? '' : '' ;
  1938. $categorys = WebsiteCategory::where('website_id', $data['website_id'])
  1939. ->where('pid',$category['pid'])
  1940. ->select(
  1941. 'category_id','alias','aLIas_pinyin','pid','sort','is_url','web_url',
  1942. 'seo_title','seo_keywords','seo_description',
  1943. DB::raw("CONCAT('$parent_pinyin', '/', aLIas_pinyin) as pinyin")
  1944. )
  1945. ->orderBy('sort')
  1946. ->get()->all();
  1947. if(empty($category)){
  1948. return Result::error("查询失败", 0);
  1949. }
  1950. $query = Book::where('book.status', 2)
  1951. ->where('book.website_id', $data['website_id'])
  1952. ->where('book.cat_id', $catid)
  1953. ->select(
  1954. 'book.id',
  1955. 'book.title',
  1956. 'book.img_url',
  1957. 'book.description',
  1958. 'book.updated_at',
  1959. 'book.cat_id',
  1960. DB::raw("'$pinyin' as pinyin")
  1961. )
  1962. ->orderBy('book.updated_at', 'desc');
  1963. $count = $query->count();
  1964. $query = clone $query;
  1965. $Book = $query
  1966. ->limit($data['pageSize'])
  1967. ->offset(($data['page']-1)*$data['pageSize'])
  1968. ->get()->all();
  1969. $result = [
  1970. 'category' => $categorys,
  1971. 'books' => $Book,
  1972. 'count' =>$count,
  1973. ];
  1974. // if(empty($result)){
  1975. // return Result::error("查询失败", 0);
  1976. // }
  1977. return Result::success($result);
  1978. }
  1979. /**
  1980. * 获取书刊详情
  1981. * @param array $data
  1982. * @return array
  1983. * */
  1984. public function getWebsiteBookInfo(array $data): array
  1985. {
  1986. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  1987. if(empty($web)){
  1988. return Result::error("查询失败", 0);
  1989. }
  1990. $book = Book::where('status', 2)
  1991. ->where('website_id', $data['website_id'])
  1992. ->where('id', $data['id'])
  1993. ->select(
  1994. 'book.*',
  1995. )
  1996. ->orderBy('updated_at', 'desc')
  1997. ->first();
  1998. if(empty($book)){
  1999. return Result::error("查询失败", 0);
  2000. }
  2001. // 先查询当前分类的 pid
  2002. $carpid = WebsiteCategory::where('website_id', $data['website_id'])
  2003. ->where('category_id', $book['cat_id'])
  2004. ->first(['pid','category_id']);
  2005. $query = WebsiteCategory::where('website_id', $data['website_id']);
  2006. if(!empty($carpid)){
  2007. if($carpid->pid!=0){
  2008. $pid = $carpid['pid'];
  2009. // $parent_pinyin = '';
  2010. }else{
  2011. $pid = $carpid['category_id'];
  2012. // $parent_pinyin = $currentCategory->aLias_pinyin?? '';
  2013. }
  2014. }
  2015. $categorys = $query->where('pid', $pid)
  2016. ->select('category_id','alias','aLIas_pinyin','pid','sort','is_url','web_url','category_arr_id as cat_arr_id',
  2017. )
  2018. ->orderBy('sort')
  2019. ->get();
  2020. $category = $this->processArticle($categorys, $data);
  2021. $result = [
  2022. 'category' => $category,
  2023. 'books' => $book,
  2024. ];
  2025. return Result::success($result);
  2026. }
  2027. /**
  2028. * 尝试
  2029. * @param array $data
  2030. * @return array
  2031. * */
  2032. public function test(array $data): array
  2033. {
  2034. $input['id'] = $data['id'];
  2035. $input['website_id'] = $data['website_id'];
  2036. // 将 JSON 字符串转换为 PHP 数组
  2037. $data = json_decode($input['id'], true);
  2038. $result = [];
  2039. $article = $data;
  2040. $result = array_map(function ($item) use ($input) {
  2041. $website = [
  2042. 'website_id' => $input['website_id'],
  2043. ];
  2044. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  2045. $category = WebsiteCategory::where('pid', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  2046. $parent = [
  2047. 0 => $category['category_id'] ?? [],
  2048. 1 => $parentImgNum?? [],
  2049. 2 => $parentTextNum?? [],
  2050. ];
  2051. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['child']);
  2052. $child = [];
  2053. $resultItem = [
  2054. // 'alias' => $parent_alias ?? '',
  2055. 'category_id' => $parentCatId ?? 0,
  2056. 'pinyin' => $parentImgNum ?? null,
  2057. 'imgnum' => $parentTextNum ?? [],
  2058. // 'textnum' => $textArticles ?? [],
  2059. ]; return $parent;
  2060. }, $data);
  2061. return Result::success($result);
  2062. // });
  2063. }
  2064. /**
  2065. * 封装处理文章的路由问题
  2066. * */
  2067. function processArticle($article, $data) {
  2068. return $article->map(function ($article) use ($data) {
  2069. $catid = $article->cat_arr_id?? '';
  2070. $level = json_decode($catid, true);
  2071. $pinyin = '';
  2072. // $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  2073. if (!empty($level) && is_array($level)) {
  2074. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  2075. ->where('website_id', $data['website_id'])
  2076. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  2077. ->get(['aLIas_pinyin'])
  2078. ->pluck('aLIas_pinyin')
  2079. ->implode('/');
  2080. } else {
  2081. $pinyin = '';
  2082. }
  2083. $article->pinyin = $pinyin;
  2084. return $article;
  2085. });
  2086. }
  2087. /**
  2088. * c端-获取招工招聘下拉选框
  2089. * @param array $data
  2090. * @return array
  2091. * */
  2092. public function getWebsiteJobSelect(array $data): array
  2093. {
  2094. $web = Website::where('id', $data['website_id'])->first();
  2095. if(empty($web)){
  2096. return Result::error("该网站不存在", 0);
  2097. }
  2098. $hy = JobIndustry::get()->all();
  2099. $zw = JobPosition::where('zwpid',0)->get()->all();
  2100. $jtzw = JobPosition::where('zwpid','!=',0)->get()->all();
  2101. $result = [
  2102. 'hy' => $hy,
  2103. 'zw' => $zw,
  2104. 'jtzw' => $jtzw,
  2105. ];
  2106. if(empty($result)){
  2107. return Result::error("查询失败", 0);
  2108. }
  2109. return Result::success($result);
  2110. }
  2111. /**
  2112. * c端-获取招工招聘
  2113. * @param array $data
  2114. * @return array
  2115. * */
  2116. public function getWebsiteJob(array $data): array
  2117. {
  2118. $web = Website::where('id', $data['website_id'])->first();
  2119. if(empty($web)){
  2120. return Result::error("该网站不存在", 0);
  2121. }
  2122. $job_hunting = JobHunting::where('job_hunting.status', 2)
  2123. ->where('job_hunting.website_id', $data['website_id'])
  2124. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2125. $query->where(function($q) use ($data) {
  2126. $q->WhereRaw("JSON_CONTAINS(job_hunting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2127. });
  2128. })
  2129. ->select('job_hunting.id','job_hunting.cat_arr_id','job_hunting.job_name_get','job_hunting.industry','job_hunting.name','job_hunting.sexy'
  2130. ,'job_hunting.origin','job_hunting.city_arr_id','job_hunting.experience','job_hunting.updated_at','job_hunting.salary')
  2131. ->orderBy('updated_at', 'desc')
  2132. ->limit($data['job1_num'])
  2133. ->get();
  2134. $web['website_id'] = $data['website_id'];
  2135. if(empty($job_hunting)){
  2136. $job_huntings = "未查询到相关简历信息";
  2137. }else{
  2138. $job_huntings = $this->processJob($job_hunting, $web);
  2139. }
  2140. // 0:待审核 1:已通过 2:已拒绝
  2141. $job_recruiting = JobRecruiting::where('job_recruiting.status', 1)
  2142. ->where('job_recruiting.website_id', $data['website_id'])
  2143. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2144. $query->where(function($q) use ($data) {
  2145. $q->WhereRaw("JSON_CONTAINS(job_recruiting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2146. });
  2147. })
  2148. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2149. ->select('job_recruiting.id','job_recruiting.cat_arr_id','job_recruiting.title',
  2150. 'job_recruiting.jtzw_id','job_recruiting.hy_id','job_recruiting.city_arr_id',
  2151. 'job_recruiting.due_data','job_recruiting.updated_at','job_recruiting.experience',
  2152. 'job_recruiting.educational','job_recruiting.salary','job_recruiting.zw_id','job_company.business_name')
  2153. ->orderBy('updated_at', 'desc')
  2154. ->limit($data['job2_num'])
  2155. ->get();
  2156. if(empty($job_recruiting->toArray())){
  2157. $job_recruitings = "未查询到相关职位信息";
  2158. }else{
  2159. $job_recruitings = $this->processJob($job_recruiting, $web);
  2160. }
  2161. $result = [
  2162. 'job_hunting' => $job_huntings,
  2163. 'job_recuiting' => $job_recruitings,
  2164. ];
  2165. return Result::success($result);
  2166. }
  2167. /**
  2168. * c端-获取招工招聘列表
  2169. * @param array $data
  2170. * @return array
  2171. * */
  2172. public function getWebsiteJobList(array $data): array
  2173. {
  2174. $recruit_where = [];
  2175. $hunt_where = [];
  2176. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2177. if(empty($web)){
  2178. return Result::error("此网站不存在", 0);
  2179. }
  2180. $website_id['website_id'] = $data['website_id'];
  2181. if((isset($data['type']) && $data['type'] == 1) || !isset($data['type'])){
  2182. if(isset($data['zw_id']) && !empty($data['zw_id'])){
  2183. array_push($recruit_where, ['zw_id',$data['zw_id']]);
  2184. }
  2185. if(isset($data['jtzw_id']) &&!empty($data['jtzw_id'])){
  2186. array_push($recruit_where, ['jtzw_id',$data['jtzw_id']]);
  2187. }
  2188. if(isset($data['hy_id']) && !empty($data['hy_id'])){
  2189. array_push($recruit_where, ['hy_id',$data['hy_id']]);
  2190. }
  2191. $query = JobRecruiting::where('job_recruiting.status', 1)
  2192. ->where('job_recruiting.website_id', $data['website_id'])
  2193. ->where($recruit_where)
  2194. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2195. $query->where(function($q) use ($data) {
  2196. $q->WhereRaw("JSON_CONTAINS(job_recruiting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2197. });
  2198. })
  2199. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  2200. $query->where(function($q) use ($data) {
  2201. $q->WhereRaw("JSON_CONTAINS(job_recruiting.cat_arr_id, '". intval($data['catid']). "') = 1");
  2202. });
  2203. })
  2204. ->leftJoin('job_company', 'job_recruiting.id', '=', 'job_company.job_id')
  2205. ->select('job_recruiting.id','job_recruiting.hy_id','job_recruiting.title','job_recruiting.zw_id','job_recruiting.educational',
  2206. 'job_recruiting.jtzw_id','job_recruiting.city_arr_id','job_recruiting.due_data','job_recruiting.experience',
  2207. 'job_recruiting.cat_arr_id','job_recruiting.updated_at','job_company.business_name')
  2208. ->orderBy('updated_at', 'desc');
  2209. $recruit_count = $query->count();
  2210. $query = clone $query;
  2211. $JobRecruiting = $query
  2212. ->offset(($data['page']-1)*$data['pageSize'])
  2213. ->limit($data['pageSize'])
  2214. ->get();
  2215. if(empty($JobRecruiting)){
  2216. $JobRecruiting = "暂无相关职位信息";
  2217. }else{
  2218. $JobRecruiting = $this->processJob($JobRecruiting, $website_id);
  2219. }
  2220. }
  2221. if((isset($data['type']) && $data['type'] == 2) || !isset($data['type'])){
  2222. if(isset($data['zw_id']) && !empty($data['zw_id'])){
  2223. array_push($hunt_where, ['job',$data['zw_id']]);
  2224. }
  2225. if(isset($data['jtzw_id']) &&!empty($data['jtzw_id'])){
  2226. array_push($hunt_where, ['job_name_get',$data['jtzw_id']]);
  2227. }
  2228. if(isset($data['hy_id']) && !empty($data['hy_id'])){
  2229. array_push($hunt_where, ['industry',$data['hy_id']]);
  2230. }
  2231. $query = JobHunting::where('status', 2)
  2232. ->where('job_hunting.website_id', $data['website_id'])
  2233. ->where($hunt_where)
  2234. ->when(isset($data['city_id']) &&!empty($data['city_id']), function ($query) use ($data) {
  2235. $query->where(function($q) use ($data) {
  2236. $q->WhereRaw("JSON_CONTAINS(job_hunting.city_arr_id, '". intval($data['city_id']). "') = 1");
  2237. });
  2238. })
  2239. ->when(isset($data['catid_id']) &&!empty($data['catid_id']), function ($query) use ($data) {
  2240. $query->where(function($q) use ($data) {
  2241. $q->WhereRaw("JSON_CONTAINS(job_hunting.cat_arr_id, '". intval($data['catid_id']). "') = 1");
  2242. });
  2243. })
  2244. ->select('id','sexy','experience','origin','industry','name','job','job_name_get','city_arr_id','cat_arr_id','created_at','updated_at')
  2245. ->orderBy('updated_at', 'desc');
  2246. $hunt_count = $query->count();
  2247. $query = clone $query;
  2248. $JobHunting = $query
  2249. ->offset(($data['page']-1)*$data['pageSize'])
  2250. ->limit($data['pageSize'])
  2251. ->get();
  2252. if(empty($JobHunting)){
  2253. $JobRecruiting = "暂无相关简历信息";
  2254. }else{
  2255. $JobHunting = $this->processJob($JobHunting, $website_id);
  2256. }
  2257. }
  2258. $result = [
  2259. 'JobRecruiting' => $JobRecruiting ?? [],
  2260. 'recruit_count' =>$recruit_count ?? 0,
  2261. 'JobHunting' => $JobHunting ?? [],
  2262. 'hunt_count' =>$hunt_count ?? 0,
  2263. ];
  2264. return Result::success($result);
  2265. }
  2266. /**
  2267. * c端-获取招工招聘详情
  2268. * @param array $data
  2269. * @return array
  2270. * */
  2271. public function getWebsiteJobInfo(array $data): array
  2272. {
  2273. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2274. if(empty($web)){
  2275. return Result::error("该网站不存在", 0);
  2276. }
  2277. $webid = [
  2278. 'website_id' => $data['website_id'],
  2279. ];
  2280. // 职位相关信息
  2281. if($data['type']==1){
  2282. $query = JobRecruiting::where('status', 1)
  2283. ->where('website_id', $data['website_id'])
  2284. ->select('*');
  2285. $job = $query->where('job_recruiting.id', $data['id'])->get();
  2286. $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();
  2287. if(!empty($company)){
  2288. $result['company'] = $this->processJob($company, $webid);
  2289. }
  2290. $other_job =JobRecruiting::where('status', 1)
  2291. ->where('website_id', $data['website_id'])
  2292. ->select('title','id','updated_at','cat_arr_id','user_id')
  2293. ->where('id','!=',$data['id'])
  2294. ->where('user_id', '=', $job[0]['user_id'])
  2295. ->limit($data['pageSize'])
  2296. ->get();
  2297. if(!empty($other_job)){
  2298. $result['other_job'] = $this->processJob($other_job, $webid);
  2299. }
  2300. }else{
  2301. // 简历相关信息
  2302. $job = JobHunting::where('job_hunting.status', 2)
  2303. ->where('job_hunting.website_id', $data['website_id'])
  2304. ->where('job_hunting.id', $data['id'])
  2305. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  2306. ->select('job_hunting.*','user_name')
  2307. ->get();
  2308. $resume = JobRemuse::where('hunt_id',$data['id'])->get();
  2309. if(!empty($resume->toArray())){
  2310. $result['resume'] = 1;
  2311. } else{
  2312. $result['resume'] = 0;
  2313. }
  2314. }
  2315. if(empty($job->toArray())){
  2316. return Result::error("id参数错误", 0);
  2317. }
  2318. if(!empty($job[0]['job_experience'])){
  2319. $job_experience = json_decode($job[0]['job_experience'], true) ?? [];
  2320. if(!empty($job_experience)){
  2321. // $hy = [];
  2322. foreach($job_experience as $key => $value){
  2323. // $id = $value['id'];
  2324. $hy[$key] = $value['job_industry'];
  2325. $zw_id[$key] = $value['job_typename'];
  2326. $jtzw_id[$key] = $value['job_name'];
  2327. }
  2328. $hy_table = JobIndustry::whereIn('hyid', $hy)->select('hyid','hyname')->get();
  2329. $zw_table = JobPosition::select('zwid','zwname')->get();
  2330. // 先将关联表转换为索引数组,提高查找效率
  2331. $hyLookup = [];
  2332. foreach ($hy_table as $item) {
  2333. $hyLookup[$item['hyid']] = $item['hyname'];
  2334. }
  2335. $zwLookup = [];
  2336. foreach ($zw_table as $item) {
  2337. $zwLookup[$item['zwid']] = $item['zwname'];
  2338. }
  2339. foreach ($job_experience as $key => &$value) {
  2340. // 处理行业名称
  2341. if (isset($hy[$key]) && isset($hyLookup[$hy[$key]])) {
  2342. $value['hy_name'] = $hyLookup[$hy[$key]];
  2343. }
  2344. // 处理职位名称
  2345. if (isset($zw_id[$key]) && isset($zwLookup[$zw_id[$key]])) {
  2346. $value['zw_name'] = $zwLookup[$zw_id[$key]];
  2347. }
  2348. // 处理具体职位名称
  2349. if (isset($jtzw_id[$key]) && isset($zwLookup[$jtzw_id[$key]])) {
  2350. $value['jtzw_name'] = $zwLookup[$jtzw_id[$key]];
  2351. }
  2352. }
  2353. // 释放引用,防止意外修改后续代码
  2354. unset($value);
  2355. }
  2356. $result['job_experience'] = $job_experience?? [];
  2357. }
  2358. if(!empty($job[0]['education_experience'])){
  2359. $education_experience = json_decode($job[0]['education_experience'],true)?? '';
  2360. if(!empty($education_experience)){
  2361. foreach($education_experience as $key => $value){
  2362. // $id = $value['id'];
  2363. $education[$key] = $value['school_education'];
  2364. }
  2365. $education_table = JobEnum::where('egroup', 'education')->whereIn('evalue',$education)->select('evalue','ename')->get();
  2366. // // 先将关联表转换为索引数组,提高查找效率
  2367. $educationLookup = [];
  2368. foreach ($education_table as $item) {
  2369. $educationLookup[$item['evalue']] = $item['ename'];
  2370. }
  2371. foreach ($education_experience as $key => &$value) {
  2372. // 处理学历名称
  2373. if (isset($education[$key]) && isset($educationLookup[$education[$key]])) {
  2374. $value['education_name'] = $educationLookup[$education[$key]];
  2375. }
  2376. }
  2377. }
  2378. $result['education_experience'] = $job_experience?? [];
  2379. }
  2380. $result['job'] = $this->processJob($job, $webid);
  2381. // // 返现对应的栏目
  2382. $catid = json_decode($job[0]['cat_arr_id'],true) ?? '';
  2383. $category = WebsiteCategory::where('website_id', $data['website_id'])
  2384. ->whereIn('category_id', $catid)
  2385. ->select('category_id','alias','aLIas_pinyin','pid')
  2386. ->orderBy('pid')->get()->all();
  2387. if(!empty($category)){
  2388. $result['category'] = $category;
  2389. }
  2390. $result['job'] = $job;
  2391. $result['job_experience'] = $job_experience?? [];
  2392. $result['education_experience'] = $education_experience?? [];
  2393. if(empty($result)){
  2394. return Result::error("参数错误", 0);
  2395. }
  2396. return Result::success($result);
  2397. }
  2398. /**
  2399. * c端-申请职位
  2400. * @param array $data
  2401. * @return array
  2402. * */
  2403. public function getWebsiteJobApply(array $data): array
  2404. {
  2405. // 首先验证网站是否存在
  2406. // return Result::success($data);
  2407. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2408. if(empty($web)){
  2409. return Result::error("该网站不存在", 0);
  2410. }
  2411. // 验证用户是否存在且为个人会员/管理员
  2412. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2413. // 1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2414. if(empty($user) || ($user['type_id'] != 1 && $user['type_id'] != 10000)){
  2415. return Result::error("用户不存在", 0);
  2416. }
  2417. // 去除重复元素和空元素,并保持原始顺序
  2418. $data['recruit_id'] = array_values(array_filter(array_unique($data['recruit_id']), function($value) {
  2419. return !empty($value);
  2420. }));
  2421. // 验证职位是否存在 1:审核通过
  2422. $recruiting = JobRecruiting::where('status', 1)
  2423. ->where('website_id', $data['website_id'])
  2424. ->whereIn('id',$data['recruit_id'])
  2425. ->get(['id as recruit_id','user_id as receiver_id'])->all();
  2426. if(empty($recruiting)){
  2427. return Result::error("该职位不存在", 0);
  2428. }
  2429. // 简历是否存在
  2430. $hunt_id = JobHunting::where('user_id', $data['user_id'])->where('status',2)->first(['id as hunt_id']);
  2431. if(empty($hunt_id)){
  2432. return Result::error("该简历不存在", 0);
  2433. }
  2434. $data['hunt_id'] = $hunt_id['hunt_id'];
  2435. // // 验证是否已投递过该职位
  2436. $apply = JobApply::where('user_id', $data['user_id'])
  2437. ->where('hunt_id', $data['hunt_id'])
  2438. ->where('website_id',$data['website_id'])
  2439. ->whereIn('recruit_id',$data['recruit_id'])
  2440. ->get(['recruit_id']);
  2441. if(!empty($apply->toArray())){
  2442. return Result::error("您已投递过该职位!");
  2443. }
  2444. $insertData = array_map(function($recruiting) use ($data) {
  2445. return [
  2446. 'user_id' => $data['user_id'],
  2447. 'hunt_id' => $data['hunt_id'],
  2448. 'website_id' => $data['website_id'],
  2449. 'recruit_id' => $recruiting['recruit_id'],
  2450. 'receiver_id' => $recruiting['receiver_id'],
  2451. 'status' => 1,
  2452. ];
  2453. }, $recruiting);
  2454. // 批量插入数据
  2455. $result = JobApply::insert($insertData);
  2456. if(empty($result)){
  2457. return Result::error("投递失败", 0);
  2458. }
  2459. return Result::success($result);
  2460. }
  2461. /**
  2462. * 招工招聘-沟通简历
  2463. * @param array $data
  2464. * @return array
  2465. * */
  2466. public function getWebsiteJobResume(array $data): array
  2467. {
  2468. // 首先验证网站是否存在
  2469. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2470. if(empty($web)){
  2471. return Result::error("该网站不存在", 0);
  2472. }
  2473. // return Result::success($data);
  2474. // 验证用户是否存在且为企业会员/管理员
  2475. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2476. // 1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)
  2477. if(empty($user) || ($user['type_id']!= 3 && $user['type_id']!= 10000)){
  2478. return Result::error("用户不存在", 0);
  2479. }
  2480. // 去除重复元素和空元素,并保持原始顺序
  2481. $data['hunt_id'] = array_values(array_filter(array_unique($data['hunt_id']), function($value) {
  2482. return !empty($value);
  2483. }));
  2484. // 验证简历是否存在
  2485. $hunting = JobHunting::where('status', 2)
  2486. ->where('website_id', $data['website_id'])
  2487. ->whereIn('id',$data['hunt_id'])
  2488. ->get(['id as hunt_id','user_id as receiver_id'])->all();
  2489. if(empty($hunting)){
  2490. return Result::error("该简历不存在", 0);
  2491. }
  2492. // 验证是否已沟通过该简历
  2493. $remuse = JobRemuse::where('user_id', $data['user_id'])
  2494. ->where('recruit_id', $data['recruit_id'])
  2495. ->where('website_id',$data['website_id'])
  2496. ->whereIn('hunt_id',$data['hunt_id'])
  2497. ->get(['hunt_id']);
  2498. if(!empty($remuse->toArray())){
  2499. return Result::error("您已沟通过该简历!");
  2500. }
  2501. // 准备要插入的数据数组
  2502. $insertData = array_map(function($hunting) use ($data) {
  2503. return [
  2504. 'user_id' => $data['user_id'],
  2505. 'recruit_id' => $data['recruit_id'],
  2506. 'website_id' => $data['website_id'],
  2507. 'hunt_id' => $hunting['hunt_id'],
  2508. 'receiver_id' => $hunting['receiver_id'],
  2509. 'status' => 1,
  2510. ];
  2511. }, $hunting);
  2512. // 批量插入数据
  2513. $result = JobRemuse::insert($insertData);
  2514. if(empty($result)){
  2515. return Result::error("沟通失败", 0);
  2516. }
  2517. return Result::success($result);
  2518. }
  2519. /**
  2520. * 招工招聘 -我的职位(企业会员)
  2521. * @param array $data
  2522. * @return array
  2523. */
  2524. public function getWebsiteJobRecruiting(array $data): array
  2525. {
  2526. $web = Website::where('id', $data['website_id'])->first('id');
  2527. if(empty($web)){
  2528. return Result::error("该网站不存在", 0);
  2529. }
  2530. $user = User::where('id', $data['user_id'])->first(['id','type_id']);
  2531. if(empty($user) || ($user['type_id']!= 3 && $user['type_id']!= 10000)){
  2532. return Result::error("用户暂无权限!", 0);
  2533. }
  2534. // '状态 0:待审核;1:已审核通过;(只有企业会员需要审核);2:已拒绝;
  2535. $result = JobRecruiting::where('website_id', $data['website_id'])
  2536. ->when(isset($user['type_id']) && $user['type_id']== 3, function ($query) use ($user) {
  2537. $query->where('user_id', $user['id']);
  2538. })
  2539. ->where('status', 1)
  2540. ->select('id','title','website_id','user_id','updated_at')
  2541. ->orderBy('updated_at', 'desc')
  2542. ->limit($data['pageSize'])
  2543. ->get();
  2544. if(empty($result)){
  2545. return Result::error("暂无相关职位信息", 0);
  2546. }
  2547. return Result::success($result);
  2548. }
  2549. /**
  2550. * c端 - 验证路由
  2551. * @param array $data
  2552. * @return array
  2553. */
  2554. public function checkWebsiteRoute(array $data): array
  2555. {
  2556. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  2557. if(empty($web)){
  2558. return Result::error("该网站不存在", 0);
  2559. }
  2560. // 验证栏目路由
  2561. $last_category = WebsiteCategory::where('website_id', $data['website_id'])
  2562. ->where('aLIas_pinyin', $data['last_route'])
  2563. ->first();
  2564. if(empty($last_category)){
  2565. return Result::error("该栏目不存在", 0);
  2566. }
  2567. if(isset($data['id']) && !empty($data['id'])){
  2568. // `type` int unsigned DEFAULT '1' COMMENT '类型:1资讯(默认)2商品3书刊音像4招聘5求职类型:1资讯(默认)2商品3书刊音像4招聘5求职6招工招聘'
  2569. switch ($last_category['type']) {
  2570. case 1:
  2571. // 文章
  2572. // `status` int DEFAULT '1' COMMENT '状态: 2:已拒绝 ;1:已发布,0待发布 草稿箱 404删除(移除)
  2573. $article = Article::where('status', 1)
  2574. ->where('id', $data['id'])
  2575. ->where(function ($query) use ($data) {
  2576. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  2577. ->orWhereNull("ignore_ids");
  2578. })
  2579. ->first(['cat_arr_id']);
  2580. if(empty($article)){
  2581. return Result::error("该文章不存在", 0);
  2582. }
  2583. // return Result::success($article);
  2584. break;
  2585. case 2:
  2586. // 商品
  2587. // `status` int DEFAULT '1' COMMENT '审核状态,1待审核2已审核3已拒绝',
  2588. $article = Good::where('website_id', $data['website_id'])
  2589. ->where('status', 2)
  2590. ->where('id', $data['id'])
  2591. ->first(['cat_arr_id']);
  2592. if(empty($article)){
  2593. return Result::error("该商品不存在", 0);
  2594. }
  2595. break;
  2596. case 3:
  2597. // 书刊信息
  2598. // `status` int DEFAULT '1' COMMENT '审核状态,1待审核2已审核3已拒绝',
  2599. $article = Book::where('website_id', $data['website_id'])
  2600. ->where('status', 2)
  2601. ->where('id', $data['id'])
  2602. ->first(['id','cat_arr_id']);
  2603. if(empty($article)){
  2604. return Result::error("该书刊不存在", 0);
  2605. }
  2606. break;
  2607. case 4:
  2608. // 招聘
  2609. // `status` int DEFAULT '0' COMMENT '状态 0:待审核;1:已审核;(只有企业会员需要审核)',
  2610. $article = JobRecruiting::where('website_id', $data['website_id'])
  2611. ->where('status', 1)
  2612. ->where('id', $data['id'])
  2613. ->first(['cat_arr_id']);
  2614. if(empty($article)){
  2615. return Result::error("该招聘不存在", 0);
  2616. }
  2617. break;
  2618. case 5:
  2619. // 求职
  2620. // `status` int DEFAULT '1' COMMENT '审核状态,1待审核2已审核3已拒绝',
  2621. $article = JobHunting::where('website_id', $data['website_id'])
  2622. ->where('status', 2)
  2623. ->where('id', $data['id'])
  2624. ->first(['cat_arr_id']);
  2625. if(empty($article)){
  2626. return Result::error("该求职不存在", 0);
  2627. }
  2628. break;
  2629. case 6:
  2630. // 招工招聘
  2631. // `status` int DEFAULT '1' COMMENT '审核状态,1待审核2已审核3已拒绝',
  2632. $article = JobRecruiting::where('website_id', $data['website_id'])
  2633. ->where('status', 1)
  2634. ->where('id', $data['id'])
  2635. ->first(['cat_arr_id']);
  2636. if(empty($article)){
  2637. $article = JobHunting::where('website_id', $data['website_id'])
  2638. ->where('status', 2)
  2639. ->where('id', $data['id'])
  2640. ->first(['cat_arr_id']);
  2641. }
  2642. if(empty($article)){
  2643. return Result::error("该招工招聘不存在", 0);
  2644. }
  2645. break;
  2646. default:
  2647. return Result::error("该数据不存在", 0);
  2648. break;
  2649. }
  2650. $catid = json_decode($article['cat_arr_id'], true);
  2651. if(empty($catid)){
  2652. return Result::error("该数据不存在", 0);
  2653. }
  2654. $pinyin = WebsiteCategory::where('website_id', $data['website_id'])
  2655. ->orderByRaw('FIELD(category_id, '. implode(',', $catid). ')')
  2656. ->whereIn('category_id', $catid)
  2657. ->pluck('aLIas_pinyin')
  2658. ->implode('/');
  2659. }
  2660. if($last_category['pid']!=0){
  2661. $cat_arr = json_decode($last_category['category_arr_id'], true);
  2662. $pinyin = WebsiteCategory::where('website_id', $data['website_id'])
  2663. ->orderByRaw('FIELD(category_id, '. implode(',', $cat_arr). ')')
  2664. ->whereIn('category_id', $cat_arr)
  2665. ->pluck('aLIas_pinyin')
  2666. ->implode('/');
  2667. }else{
  2668. $pinyin = $last_category['aLIas_pinyin'];
  2669. }
  2670. if(empty($pinyin) || $pinyin!=$data['all_route']){
  2671. return Result::error('非法路径!');
  2672. }else{
  2673. return Result::success($pinyin);
  2674. }
  2675. }
  2676. }