NewsService.php 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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\WebsiteCategory;
  7. use App\Model\ArticleSurvey;
  8. use App\Model\jobHunting;
  9. use App\Model\JobEnum;
  10. use App\Model\JobIndustry;
  11. use App\Model\JobPosition;
  12. use App\Model\JobRecruiting;
  13. use App\Model\Good;
  14. use App\Model\JobNature;
  15. use App\Model\Website;
  16. use Hyperf\DbConnection\Db;
  17. use Hyperf\RpcServer\Annotation\RpcService;
  18. use App\Tools\Result;
  19. use Ramsey\Uuid\Uuid;
  20. use Hyperf\Utils\Random;
  21. use Fukuball\Jieba\Jieba;
  22. use Fukuball\Jieba\Finalseg;
  23. use Hyperf\Utils\Collection;
  24. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  25. class NewsService implements NewsServiceInterface
  26. {
  27. /**
  28. * 获取导航池列表
  29. * @param array $data
  30. * @return array
  31. */
  32. public function getCategoryList(array $data): array
  33. {
  34. $page = (int)$data['page']??1;
  35. $perPage = (int)$data['pageSize']??10;
  36. $search =$data['name']??'';
  37. // 查找所有匹配的分类
  38. $matchedCategories = $this->findMatchedCategories($search);
  39. // 查找所有匹配分类的父级分类
  40. $allRequiredCategories = $this->findAllParents($matchedCategories);
  41. // 提取一级分类
  42. $topLevelCategories = [];
  43. foreach ($allRequiredCategories as $category) {
  44. if ($category->pid === 0) {
  45. $topLevelCategories[] = $category;
  46. }
  47. }
  48. // 分页处理
  49. $offset = ($page - 1) * $perPage;
  50. $paginatedTopLevelCategories = array_slice($topLevelCategories, $offset, $perPage);
  51. $categoryTree = [];
  52. foreach ($paginatedTopLevelCategories as $category) {
  53. $categoryTree[] = $this->buildCategoryTree($category, $allRequiredCategories);
  54. }
  55. $return = [
  56. 'rows' => $categoryTree,
  57. 'total' => count($topLevelCategories),
  58. ];
  59. return Result::success($return);
  60. }
  61. private function findMatchedCategories($search)
  62. {
  63. if ($search) {
  64. return Category::where('name', 'like', "%{$search}%")->get();
  65. }
  66. return Category::all();
  67. }
  68. private function findAllParents($categories)
  69. {
  70. $allCategories = [];
  71. foreach ($categories as $category) {
  72. $parentId = $category->pid;
  73. while ($parentId > 0) {
  74. $parent = Category::find($parentId);
  75. if ($parent) {
  76. $allCategories[$parent->id] = $parent;
  77. $parentId = $parent->pid;
  78. } else {
  79. break;
  80. }
  81. }
  82. $allCategories[$category->id] = $category;
  83. }
  84. return array_values($allCategories);
  85. }
  86. private function buildCategoryTree($category, $allRequiredCategories)
  87. {
  88. $categoryData = $category->toArray();
  89. $children = [];
  90. foreach ($allRequiredCategories as $c) {
  91. if ($c->pid === $category->id) {
  92. $children[] = $this->buildCategoryTree($c, $allRequiredCategories);
  93. }
  94. }
  95. if (!empty($children)) {
  96. $categoryData['children'] = $children;
  97. }
  98. return $categoryData;
  99. }
  100. public function myCategoryList(array $data): array
  101. {
  102. $sszq = $data['sszq'] ?? '';
  103. unset($data['sszq']);
  104. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  105. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  106. $where[] = [
  107. 'pid', '=', $data['pid'],
  108. ];
  109. if (isset($data['name'])) {
  110. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  111. }
  112. var_dump($where);
  113. //根据分类id,从category表中取出分类信息
  114. $result = Category::where($where)
  115. ->whereIn('category.id', $catorytids)
  116. ->select('category.*', 'category.id as category_id')->get();
  117. if (empty($result)) {
  118. return Result::error("没有栏目数据");
  119. }
  120. return Result::success($result);
  121. }
  122. /**
  123. * @param array $data
  124. * @return array
  125. */
  126. public function categoryList(array $data): array
  127. {
  128. $where[] = [
  129. 'pid', '=', $data['pid'],
  130. ];
  131. if (isset($data['name'])) {
  132. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  133. }
  134. var_dump($where);
  135. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  136. if (empty($result)) {
  137. return Result::error("没有栏目数据");
  138. }
  139. return Result::success($result);
  140. }
  141. /**
  142. * @param array $data
  143. * @return array
  144. */
  145. public function addCategory(array $data): array
  146. {
  147. if (isset($data['id'])) {
  148. unset($data['id']);
  149. }
  150. $id = Category::insertGetId($data);
  151. if (empty($id)) {
  152. return Result::error("添加失败");
  153. }
  154. return Result::success(['id' => $id]);
  155. }
  156. /**
  157. * @param array $data
  158. * @return array
  159. */
  160. public function delCategory(array $data): array
  161. {
  162. $categoryList = Category::where(['pid' => $data['id']])->get();
  163. var_dump("分类列表:", $data, $categoryList);
  164. if ($categoryList->toArray()) {
  165. return Result::error("分类下面有子分类不能删除");
  166. }
  167. $articleList = Article::where(['catid' => $data['id']])->get();
  168. var_dump("文章列表:", $articleList);
  169. if ($articleList->toArray()) {
  170. return Result::error("分类下面有资讯不能删除");
  171. }
  172. $result = Category::where($data)->delete();
  173. if (!$result) {
  174. return Result::error("删除失败");
  175. }
  176. return Result::success($result);
  177. }
  178. /**
  179. * @param array $data
  180. * @return array
  181. */
  182. public function updateCategory(array $data): array
  183. {
  184. $where = [
  185. 'id' => $data['id'],
  186. ];
  187. $result = Category::where($where)->update($data);
  188. if ($result) {
  189. return Result::success($result);
  190. } else {
  191. return Result::error("更新失败");
  192. }
  193. }
  194. /**
  195. * 获取导航池信息
  196. * @param array $data
  197. * @return array
  198. */
  199. public function getCategoryInfo(array $data): array
  200. {
  201. $where = [
  202. 'id' => $data['id'],
  203. ];
  204. $result = Category::where($where)->first();
  205. if ($result) {
  206. return Result::success($result);
  207. } else {
  208. return Result::error("更新失败");
  209. }
  210. }
  211. /**
  212. * @param array $data
  213. * @return array
  214. */
  215. public function getArticleList(array $data): array
  216. {
  217. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  218. $type_id = $data['type_id'];
  219. unset($data['type_id']);
  220. $user_id = $data['user_id'];
  221. unset($data['user_id']);
  222. $where = [];
  223. $status1 = [];
  224. if (isset($data['id']) && $data['id']) {
  225. array_push($where, ['article.id', '=', $data['id']]);
  226. }
  227. if (isset($data['title']) && $data['title']) {
  228. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  229. }
  230. if (isset($data['category_name']) && $data['category_name']) {
  231. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  232. }
  233. if (isset($data['author']) && $data['author']) {
  234. array_push($where, ['article.author', '=', $data['author']]);
  235. }
  236. if (isset($data['islink']) && $data['islink'] !== "") {
  237. array_push($where, ['article.islink', '=', $data['islink']]);
  238. }
  239. if (isset($data['status']) && $data['status'] !== "") {
  240. array_push($where, ['article.status', '=', $data['status']]);
  241. }
  242. if (isset($data['status1'])) {
  243. $status1 = json_decode(($data['status1']));
  244. }
  245. //不是管理员展示个人数据;
  246. if ($type_id != 10000) {
  247. $where[] = ['article.admin_user_id', '=', $user_id];
  248. }
  249. $rep = Article::where($where)
  250. ->whereNotIn('article.status', [404])
  251. ->when($status1, function ($query) use ($status1) {
  252. if (isset($status1) && $status1) {
  253. $query->whereIn('article.status', $status1);
  254. }
  255. })
  256. ->leftJoin('category', 'article.catid', 'category.id')
  257. ->select("article.*", "category.name as category_name")
  258. ->orderBy("article.updated_at", "desc")
  259. ->limit($data['pageSize'])
  260. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  261. $count = Article::where($where)->whereNotIn('article.status', [404])
  262. ->when($status1, function ($query) use ($status1) {
  263. if (isset($status1) && $status1) {
  264. $query->whereIn('article.status', $status1);
  265. }
  266. })
  267. ->leftJoin('category', 'article.catid', 'category.id')->count();
  268. $data = [
  269. 'rows' => $rep->toArray(),
  270. 'count' => $count,
  271. ];
  272. if (empty($rep)) {
  273. return Result::error("没有信息数据");
  274. }
  275. return Result::success($data);
  276. }
  277. /**
  278. * @param array $data
  279. * @return array
  280. */
  281. public function addArticle(array $data): array
  282. {
  283. var_dump($data, '----------12-----------1');
  284. unset($data['user_type']);
  285. unset($data['nav_add_pool_id']);
  286. // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  287. Db::beginTransaction();
  288. try {
  289. //处理投票
  290. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  291. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  292. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  293. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  294. unset($data['is_survey']);
  295. unset($data['survey_name']);
  296. unset($data['suvey_array']);
  297. // unset($data['website_id']);
  298. // unset($data['web_site_id']);
  299. // $data['web_site_id'] = is_array($data['web_site_id']) ? json_encode($data['web_site_id']) : ($data['web_site_id']);
  300. if ($data['hits'] == '') {
  301. $data['hits'] = 0;
  302. }
  303. if ($data['is_original'] == '') {
  304. $data['is_original'] = 0;
  305. }
  306. if ($data['status'] == '') {
  307. $data['status'] = 0;
  308. }
  309. $articleData = $data;
  310. unset($articleData['content']);
  311. //自动处理缩略图、关键字、描述
  312. if ($articleData['imgurl'] == '') {
  313. //如果没有图,设置level=0
  314. $levelArr = json_decode($articleData['level'], true);
  315. var_dump($levelArr, '----------levelArr-----------1');
  316. //content中提取图片第一个图,正则提取
  317. $reg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  318. preg_match_all($reg, $data['content'], $matches);
  319. if (isset($matches[1][0])) {
  320. $articleData['imgurl'] = $matches[1][0];
  321. //如果有图,设置level=3
  322. if (!in_array(3, $levelArr)) {$levelArr[] = 3;}
  323. $articleData['level'] = json_encode($levelArr);
  324. } else {
  325. if (!in_array(0, $levelArr)) {
  326. $levelArr[] = 0;
  327. }
  328. $articleData['level'] = json_encode($levelArr);
  329. }
  330. }
  331. var_dump($articleData['level'], '----------$articleData[level]----------1');
  332. if ($articleData['keyword'] == '') {
  333. //提取标题+内容中的关键词
  334. $articleData['keyword'] = $data['title'];
  335. // . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
  336. Jieba::init(); // 初始化 jieba-php
  337. Finalseg::init();
  338. $segList = Jieba::cut($articleData['keyword']);
  339. $segList1 = array_slice($segList, 0, 8);
  340. $articleData['keyword'] = implode(',', $segList1);
  341. }
  342. if ($articleData['introduce'] == '') {
  343. //提取内容中的描述
  344. $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
  345. }
  346. $id = Article::insertGetId($articleData);
  347. $articleDataContent = [
  348. 'article_id' => $id,
  349. 'content' => $data['content'],
  350. ];
  351. ArticleData::insertGetId($articleDataContent);
  352. //处理投票
  353. if ($is_survey == 1) {
  354. //生成年月日时分秒+8位随机数
  355. $uuid = date('YmdHis') . rand(10000000, 99999999);
  356. var_dump($suvey_array, 'suvey_array________');
  357. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  358. var_dump($suveys_array, '---------------------1');
  359. var_dump($suvey_array, '---------------------2');
  360. $suvey_data = [];
  361. foreach ($suveys_array as $key => $value) {
  362. if ($value == '') {
  363. continue;
  364. }
  365. if (is_array($value)) {
  366. $suvey_data[] = [
  367. 'sur_id' => $uuid,
  368. 'art_id' => $id,
  369. 'website_id' => $website_id ?? 2,
  370. 'survey_name' => $survey_name,
  371. 'choice_name' => $value[1],
  372. 'is_other' => 1,
  373. 'other_id' => 0,
  374. 'results' => 0,
  375. ];
  376. } else {
  377. $suvey_data[] = [
  378. 'sur_id' => $uuid,
  379. 'art_id' => $id,
  380. 'website_id' => $website_id ?? 2,
  381. 'survey_name' => $survey_name,
  382. 'choice_name' => $value,
  383. 'is_other' => 0,
  384. 'other_id' => 0,
  385. 'results' => 0,
  386. ];
  387. }
  388. if (empty($suvey_data)) {
  389. throw new \Exception("投票数据为空");
  390. }
  391. }
  392. $result = ArticleSurvey::insert($suvey_data);
  393. if (!$result) {
  394. throw new \Exception("投票失败,ArticleSurvey插入失败");
  395. }
  396. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  397. if (!$result) {
  398. throw new \Exception("投票失败,更新主表失败");
  399. }
  400. }
  401. Db::commit();
  402. return Result::success(['id' => $id]);
  403. } catch (\Throwable $ex) {
  404. Db::rollBack();
  405. var_dump($ex->getMessage());
  406. return Result::error("创建失败", 0);
  407. }
  408. }
  409. /**
  410. * @param array $data
  411. * @return array
  412. */
  413. public function delArticle(array $data): array
  414. {
  415. $result = Article::where($data)->delete();
  416. //survey投票删除
  417. articleSurvey::where(['art_id' => $data['id']])->delete();
  418. if (!$result) {
  419. return Result::error("删除失败");
  420. }
  421. return Result::success($result);
  422. }
  423. /**
  424. * @param array $data
  425. * @return array
  426. */
  427. public function updateArticle(array $data): array
  428. {
  429. var_dump($data, '----------12-----------1');
  430. Db::beginTransaction();
  431. unset($data['user_type']);
  432. // unset($data['web_site_id']);
  433. unset($data['nav_add_pool_id']);
  434. try {
  435. //处理投票
  436. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  437. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  438. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  439. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  440. unset($data['is_survey']);
  441. unset($data['survey_name']);
  442. unset($data['suvey_array']);
  443. unset($data['website_id']);
  444. $data['web_site_id'] = is_array($data['web_site_id']) ? json_encode($data['web_site_id']) : ($data['web_site_id']);
  445. if ($data['hits'] == '') {
  446. $data['hits'] = 0;
  447. }
  448. if ($data['is_original'] == '') {
  449. $data['is_original'] = 0;
  450. }
  451. if ($data['status'] == '') {
  452. $data['status'] = 0;
  453. }
  454. $data['cat_arr_id'] = is_array($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : $data['cat_arr_id'];
  455. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  456. $articleData = $data;
  457. unset($articleData['content']);
  458. unset($articleData['status_name']);
  459. unset($articleData['name']);
  460. unset($articleData['content']);
  461. unset($articleData['pid_arr']);
  462. unset($articleData['pid']);
  463. //自动处理缩略图、关键字、描述
  464. if ($articleData['imgurl'] == '') {
  465. //如果没有图,设置level=0
  466. $levelArr = json_decode($articleData['level'], true);
  467. var_dump($levelArr, '----------levelArr-----------1');
  468. //content中提取图片第一个图,正则提取
  469. $reg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  470. preg_match_all($reg, $data['content'], $matches);
  471. if (isset($matches[1][0])) {
  472. $articleData['imgurl'] = $matches[1][0];
  473. //如果有图,设置level=3
  474. if (!in_array(3, $levelArr)) {$levelArr[] = 3;}
  475. $articleData['level'] = json_encode($levelArr);
  476. } else {
  477. if (!in_array(0, $levelArr)) {
  478. $levelArr[] = 0;
  479. }
  480. $articleData['level'] = json_encode($levelArr);
  481. }
  482. }
  483. var_dump($articleData['level'], '----------$articleData[level]----------1');
  484. if ($articleData['keyword'] == '') {
  485. //提取标题+内容中的关键词
  486. $articleData['keyword'] = $data['title'];
  487. // . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
  488. Jieba::init(); // 初始化 jieba-php
  489. Finalseg::init();
  490. $segList = Jieba::cut($articleData['keyword']);
  491. $segList1 = array_slice($segList, 0, 8);
  492. $articleData['keyword'] = implode(',', $segList1);
  493. }
  494. if ($articleData['introduce'] == '') {
  495. //提取内容中的描述
  496. $articleData['introduce'] = substr(str_replace(' ', '', strip_tags($data['content'])), 0, 100);
  497. }
  498. $id = Article::where(['id' => $data['id']])->update($articleData);
  499. $articleDataContent = [
  500. 'content' => $data['content'],
  501. ];
  502. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  503. //处理投票
  504. $id = $data['id'];
  505. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  506. var_dump($suvey_array, 'suvey_array________delete');
  507. //处理投票
  508. if ($is_survey == 1) {
  509. //生成年月日时分秒+8位随机数
  510. $uuid = date('YmdHis') . rand(10000000, 99999999);
  511. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  512. var_dump($suveys_array, '---------------------1');
  513. $suvey_data = [];
  514. if (is_array($suveys_array)) {
  515. foreach ($suveys_array as $key => $value) {
  516. if ($value == '') {
  517. continue;
  518. }
  519. if (is_array($value)) {
  520. $suvey_data[] = [
  521. 'sur_id' => $uuid,
  522. 'art_id' => $id,
  523. 'website_id' => $website_id ?? 2,
  524. 'survey_name' => $survey_name,
  525. 'choice_name' => $value[1],
  526. 'is_other' => 1,
  527. 'other_id' => 0,
  528. 'results' => 0,
  529. ];
  530. } else {
  531. $suvey_data[] = [
  532. 'sur_id' => $uuid,
  533. 'art_id' => $id,
  534. 'website_id' => $website_id ?? 2,
  535. 'survey_name' => $survey_name,
  536. 'choice_name' => $value,
  537. 'is_other' => 0,
  538. 'other_id' => 0,
  539. 'results' => 0,
  540. ];
  541. }
  542. if (empty($suvey_data)) {
  543. throw new \Exception("投票数据为空");
  544. }
  545. }
  546. }
  547. $result = ArticleSurvey::insert($suvey_data);
  548. if (!$result) {
  549. throw new \Exception("投票失败");
  550. }
  551. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  552. if (!$result) {
  553. throw new \Exception("投票失败");
  554. }
  555. } else {
  556. $result = Article::where('id', $id)->update(['survey_id' => '', 'survey_name' => '', 'is_survey' => 0]);
  557. }
  558. Db::commit();
  559. return Result::success([]);
  560. } catch (\Throwable $ex) {
  561. Db::rollBack();
  562. var_dump($ex->getMessage());
  563. return Result::error("更新失败1" . $ex->getMessage(), 0);
  564. }
  565. }
  566. /**
  567. * 更新资讯状态
  568. * @param array $data
  569. * @return array
  570. */
  571. public function upArticleStatus(array $data): array
  572. {
  573. $result = Article::where(['id' => $data['id']])->update($data);
  574. if ($result) {
  575. return Result::success();
  576. } else {
  577. return Result::error("更新状态失败", 0);
  578. }
  579. }
  580. /**
  581. * @param array $data
  582. * @return array
  583. */
  584. public function getArticleInfo(array $data): array
  585. {
  586. $where = [
  587. 'article.id' => $data['id'],
  588. // 'article.status' => 1,
  589. ];
  590. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  591. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  592. $info = $result;
  593. // var_dump($info, 'info');
  594. $info['survey_array'] = $articleSurvey;
  595. if ($result) {
  596. return Result::success($info);
  597. } else {
  598. return Result::error("查询失败", 0);
  599. }
  600. }
  601. /**
  602. * 获取新闻
  603. * @param array $data
  604. * @return array
  605. */
  606. public function getWebsiteArticlett(array $data): array
  607. {
  608. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  609. $result = [];
  610. if ($category) {
  611. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  612. $where = [
  613. 'status' => 1,
  614. ];
  615. var_dump($data, 'data-----------------');
  616. // level=7 根据文章key来匹配文章
  617. if (isset($data['level']) && $data['level'] == 7) {
  618. // 根据文章id获取key
  619. // $data['id'] = 50142;
  620. if (isset($data['id']) && !empty($data['id'])) {
  621. $keyword = Article::where('id', $data['id'])->value('keyword');
  622. $keywordArray = explode(',', $keyword);
  623. $whereL7 = [];
  624. foreach ($keywordArray as $k => $v) {
  625. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  626. }
  627. $result = Article::where($whereL7)
  628. ->offset($placeid)
  629. ->limit($data['pageSize'])
  630. ->orderBy('updated_at', 'desc')
  631. ->get();
  632. if (empty($result)) {
  633. return Result::success([]);
  634. }
  635. return Result::success($result);
  636. } else {
  637. return Result::error("参数错误level=7,id不能为空", 0);
  638. }
  639. }
  640. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  641. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  642. var_dump($where, 'where-----------------');
  643. $result = Article::where($where)
  644. ->whereIn("catid", $category)
  645. ->where(function ($query) use ($data) {
  646. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  647. ->orWhereNull("ignore_ids");
  648. })
  649. //$data['level'] == 4 || $data['level'] == 5 查询随机
  650. ->when($data['level'] == 5, function ($query) {
  651. $query->inRandomOrder()
  652. //updated_at最近三十天;
  653. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  654. })
  655. ->when($data['level'] == 4, function ($query) {
  656. $query->orderBy("updated_at", "desc");
  657. })
  658. ->when(!empty($data['level']), function ($query) use ($data) {
  659. if ($data['level'] != 4 && $data['level'] != 5) {
  660. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  661. ->orderBy("updated_at", "desc");
  662. }
  663. })
  664. ->offset($placeid)
  665. ->limit($data['pageSize'])
  666. ->get();
  667. if (empty($result)) {
  668. return Result::error("暂无头条新闻", 0);
  669. }
  670. return Result::success($result);
  671. } else {
  672. return Result::error("本网站下暂无相关栏目", 0);
  673. }
  674. }
  675. /**
  676. * 获取模块新闻
  677. * @param array $data
  678. * @return array
  679. */
  680. public function getWebsiteModelArticles(array $data): array
  681. {
  682. $catid = $data['catid'];
  683. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  684. $category = $category->toArray();
  685. if (!empty($category)) {
  686. $where = [
  687. 'status' => 1,
  688. 'catid' => $catid,
  689. ];
  690. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  691. // 1:文字新闻;2:轮播图;3:图文;
  692. // 级别:0:未分类
  693. // 3:推荐图片
  694. if ($data['level'] == 1) {
  695. $data['level'] = 0;
  696. }
  697. $result = Article::where($where)
  698. ->where(function ($query) use ($data) {
  699. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  700. ->orWhereNull("level")
  701. ->orWhereRaw("level = '[]'");
  702. })
  703. ->where(function ($query) use ($data) {
  704. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  705. ->orWhereNull("ignore_ids");
  706. })
  707. ->orderBy("updated_at", "desc")
  708. ->offset($placeid)
  709. ->limit($data['pagesize'])
  710. ->get();
  711. if (empty($result)) {
  712. return Result::error("此栏目暂无相关新闻", 0);
  713. }
  714. } else {
  715. return Result::error("此网站暂无此栏目", 0);
  716. }
  717. return Result::success($result);
  718. }
  719. /**
  720. *获取新闻列表
  721. * @param array $data
  722. * @return array
  723. */
  724. public function getWebsiteArticleList(array $data): array
  725. {
  726. $where[] = ['status', '=', 1];
  727. if (isset($data['keyword']) && !empty($data['keyword'])) {
  728. array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
  729. }
  730. if (isset($data['catid']) && !empty($data['catid'])) {
  731. if (is_array($data['catid'])) {
  732. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  733. array_push($where, ['catid', 'in', $data['catid']]);
  734. } else {
  735. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  736. array_push($where, ['catid', '=', $data['catid']]);
  737. }
  738. if (empty($category)) {
  739. return Result::error("此网站暂无此栏目", 0);
  740. }
  741. }
  742. // return Result::success($where);
  743. $rep = Article::where(function ($query) use ($where) {
  744. foreach ($where as $condition) {
  745. if ($condition[1] === 'in') {
  746. $query->whereIn($condition[0], $condition[2]);
  747. } else {
  748. $query->where($condition[0], $condition[1], $condition[2]);
  749. }
  750. }
  751. })
  752. ->where(function ($query) use ($data) {
  753. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  754. ->orWhereNull("ignore_ids");
  755. })
  756. ->orderBy("updated_at", "desc")
  757. ->limit($data['pageSize'])
  758. ->offset(($data['page'] - 1) * $data['pageSize'])
  759. ->get();
  760. $count = Article::where(function ($query) use ($where) {
  761. foreach ($where as $condition) {
  762. if ($condition[1] === 'in') {
  763. $query->whereIn($condition[0], $condition[2]);
  764. } else {
  765. $query->where($condition[0], $condition[1], $condition[2]);
  766. }
  767. }
  768. })->count();
  769. $data = [
  770. 'rows' => $rep->toArray(),
  771. 'count' => $count,
  772. ];
  773. if (empty($rep)) {
  774. return Result::error("没有信息数据");
  775. }
  776. return Result::success($data);
  777. }
  778. /**
  779. * 前端-获取新闻详情
  780. * @param array $data
  781. * @return array
  782. */
  783. public function selectWebsiteArticleInfo(array $data): array
  784. {
  785. $where = [
  786. 'article.id' => $data['id'],
  787. 'article.status' => 1,
  788. ];
  789. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")
  790. ->where(function ($query) use ($data) {
  791. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  792. ->orWhereNull("ignore_ids");
  793. })
  794. ->first();
  795. if (empty($result)) {
  796. return Result::error("暂无此新闻!", 0);
  797. }
  798. $category = WebsiteCategory::leftJoin('website', 'website.id', '=', 'website_category.website_id')
  799. ->select('website_category.*', 'website.website_name', 'website.suffix')
  800. ->where('website_category.website_id', $data['website_id'])
  801. ->where(['website_category.category_id' => $result['catid']])
  802. ->first();
  803. if (empty($category)) {
  804. return Result::error("查询失败", 0);
  805. }
  806. //手动推荐文章
  807. $commend_ids = $result['commend_id'] ? json_decode($result['commend_id']) : [];
  808. $commendArticle = Article::whereIn('id', $commend_ids)
  809. ->where('status', 1)
  810. ->select('id', 'title', 'catid', 'imgurl', 'hits', 'created_at')
  811. ->orderBy('updated_at', 'desc')
  812. ->limit(5)
  813. ->get();
  814. $result['category_id'] = $category['category_id'];
  815. $result['cat_name'] = $category['alias'];
  816. $result['website_name'] = $category['website_name'] ?? "";
  817. $result['suffix'] = $category['suffix'] ?? "";
  818. $result['commendArticle'] = $commendArticle;
  819. return Result::success($result);
  820. }
  821. /**
  822. * 前端-获取网站调查问卷
  823. * @param array $data
  824. * @return array
  825. */
  826. public function getWebsiteSurvey(array $data): array
  827. {
  828. if (isset($data['website_id']) && !empty($data['website_id'])) {
  829. $website = Website::where('id', $data['website_id'])->first();
  830. if (empty($website)) {
  831. return Result::error("暂无此网站", 0);
  832. }
  833. }
  834. if (isset($data['art_id']) && !empty($data['art_id'])) {
  835. $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
  836. if (empty($article)) {
  837. return Result::error("暂无此文章", 0);
  838. }
  839. // return Result::error($data,0);
  840. $where['art_id'] = $data['art_id'];
  841. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  842. } else {
  843. $survey = Article::where(function ($query) {
  844. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  845. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  846. })
  847. ->where('status', 1)
  848. ->where('is_survey', 1)
  849. ->select('survey_id')
  850. ->orderBy('updated_at', 'desc')
  851. ->first();
  852. if (empty($survey)) {
  853. return Result::error("暂无调查问卷", 0);
  854. }
  855. $where['sur_id'] = $survey['survey_id'];
  856. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  857. }
  858. // return Result::success($where);
  859. $result = ArticleSurvey::where($where)
  860. ->where(function ($query) {
  861. $query->where('is_other', 0)
  862. ->orWhere(function ($subQuery) {
  863. $subQuery->where('is_other', 1)
  864. ->where('other_id', 0);
  865. });
  866. })
  867. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  868. ->select('article_survey.*', 'article.survey_type')
  869. ->get()->all();
  870. if (empty($result)) {
  871. return Result::error("此文章暂无调查问卷", 0);
  872. }
  873. return Result::success($result);
  874. }
  875. /**
  876. * 前端-添加网站调查问卷选项
  877. * @param array $data
  878. * @return array
  879. */
  880. public function addWebsiteSurveyOption(array $data): array
  881. {
  882. if (isset($data['website_id']) && !empty($data['website_id'])) {
  883. $website = Website::where('id', $data['website_id'])->first();
  884. if (empty($website)) {
  885. return Result::error("暂无此网站", 0);
  886. }
  887. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  888. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  889. if (empty($survey)) {
  890. return Result::error("此调查问卷不可添加选项", 0);
  891. }
  892. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  893. $choice = [
  894. 'art_id' => $survey['art_id'],
  895. 'website_id' => $data['website_id'],
  896. 'survey_name' => $survey['survey_name'],
  897. 'choice_name' => $data['choice_name'],
  898. 'sur_id' => $survey['sur_id'],
  899. 'is_other' => 1,
  900. 'other_id' => $survey['id'],
  901. ];
  902. $result = ArticleSurvey::insertGetId($choice);
  903. if (empty($result)) {
  904. return Result::error("添加失败", 0);
  905. }
  906. return Result::success($result);
  907. }
  908. }
  909. return Result::error("添加失败", 0);
  910. }
  911. return Result::error("添加失败", 0);
  912. }
  913. /**
  914. * 前端-调查问卷投票
  915. * @param array $data
  916. * @return array
  917. */
  918. public function addWebsiteSurveyVote(array $data): array
  919. {
  920. // return Result::success($data);
  921. if (isset($data['website_id']) && !empty($data['website_id'])) {
  922. $website = Website::where('id', $data['website_id'])->first();
  923. if (empty($website)) {
  924. return Result::error("暂无此网站", 0);
  925. }
  926. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  927. $is_survey = ArticleSurvey::where('sur_id', $data['sur_id'])->first();
  928. // return Result::success($survey);
  929. if (empty($is_survey)) {
  930. return Result::error("此调查问卷不存在", 0);
  931. }
  932. // return Result::success($survey);
  933. // 调查问卷类型
  934. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  935. //多选 若是json型则转化成数组类型
  936. if (strpos($data['choice_id'], '[') === 0) {
  937. $data['choice_id'] = json_decode($data['choice_id'], true);
  938. } else {
  939. // 单选 也转换成数组
  940. $data['choice_id'] = [$data['choice_id']];
  941. }
  942. $data['choice_id'] = array_map('intval', $data['choice_id']);
  943. $other = ArticleSurvey::whereIn('id', $data['choice_id'])
  944. ->where('website_id', $data['website_id'])
  945. ->where('is_other', 1)
  946. ->where('other_id', 0)
  947. ->first();
  948. if (!empty($other)) {
  949. return Result::error("请选择已有的选项!", 0);
  950. }
  951. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  952. ->where('website_id', $data['website_id'])
  953. ->where('is_other', 1)
  954. ->where('other_id', '!=', 0)
  955. ->first();
  956. // return Result::success($choice['other']);
  957. $choice_id = $data['choice_id'];
  958. if (!empty($choice['other'])) {
  959. // array_push($data['choice_id'],$choice['other']['other_id']);
  960. if (!empty($choice_id)) {
  961. $key = array_search($choice['other']['id'], $choice_id);
  962. if ($key !== false) {
  963. unset($choice_id[$key]);
  964. $choice_id = array_values($choice_id);
  965. }
  966. array_push($choice_id, $choice['other']['other_id']);
  967. } else {
  968. $choice_id[0] = $choice['other']['other_id'];
  969. }
  970. array_push($data['choice_id'],$choice['other']['other_id']);
  971. }
  972. // return Result::success($data);
  973. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  974. ->where('website_id', $data['website_id'])
  975. ->increment('results', 1);
  976. if (empty($choice)) {
  977. return Result::error("请选择已有的选项!", 0);
  978. }
  979. $survey['data'] = ArticleSurvey::where('sur_id', $data['sur_id'])
  980. ->where('website_id', $data['website_id'])
  981. ->where('other_id', 0)
  982. ->get();
  983. $survey['choice'] = $choice_id;
  984. return Result::success($survey);
  985. }
  986. return Result::error("参数必填!");
  987. }
  988. return Result::error("此调查问卷不存在", 0);
  989. }
  990. return Result::error("参数必填!");
  991. }
  992. /**
  993. * 后端-获取网站调查问卷列表
  994. * @param array $data
  995. * @return array
  996. */
  997. public function getSurveyList(array $data): array
  998. {
  999. $where = [];
  1000. if (isset($data['survey_name']) && !empty($data['survey_name'])) {
  1001. array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
  1002. }
  1003. if (isset($data['survey_type']) && $data['survey_type'] != null) {
  1004. array_push($where, ['survey_type', '=', $data['survey_type']]);
  1005. }
  1006. if (isset($data['is_survey']) && $data['is_survey'] != null) {
  1007. array_push($where, ['is_survey', '=', $data['is_survey']]);
  1008. }
  1009. // return Result::success($where);
  1010. if (!empty($where)) {
  1011. $query = Article::where($where)->where(function ($q) {
  1012. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  1013. });
  1014. } else {
  1015. $query = Article::where(function ($q) {
  1016. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  1017. });
  1018. }
  1019. $count = $query->count();
  1020. $survey = $query->orderByDesc('id')
  1021. ->limit($data['pageSize'])
  1022. ->offset(($data['page'] - 1) * $data['pageSize'])
  1023. ->get();
  1024. if (empty($survey->toArray())) {
  1025. return Result::error("暂无调查问卷!", 0);
  1026. }
  1027. $result = [
  1028. 'rows' => $survey,
  1029. 'count' => $count,
  1030. ];
  1031. return Result::success($result);
  1032. }
  1033. /**
  1034. * 后端-获取网站调查问卷详情
  1035. * @param array $data
  1036. * @return array
  1037. */
  1038. public function getSurveyInfo(array $data): array
  1039. {
  1040. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  1041. $where = ['sur_id' => $data['sur_id']];
  1042. $choose = ArticleSurvey::where($where)->where('is_other', 0)
  1043. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  1044. ->select('article_survey.*', 'article.survey_type')
  1045. ->get()->all();
  1046. if (empty($choose)) {
  1047. return Result::error("此调查问卷不存在", 0);
  1048. }
  1049. $resultsArray = array_column($choose, 'results');
  1050. $total = array_sum($resultsArray);
  1051. $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  1052. $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
  1053. // $total = 0;
  1054. if (!empty($other)) {
  1055. $total = $total + $other['results'];
  1056. $other['choice_name'] ='(其他)';
  1057. if(!empty($others)){
  1058. $other['hasChildren'] = true;
  1059. // array_push($other,['hasChildren','=',true]);
  1060. $other['children'] = $others;
  1061. $other_choices = [$other->toArray()];
  1062. $mer_choice = array_merge($choose, $other_choices);
  1063. $value_choice = array_values($mer_choice);
  1064. } else {
  1065. // return Result::error('1111');
  1066. $other_choices = [$other->toArray()];
  1067. $other_choices = array_merge($choose, $other_choices);
  1068. $value_choice = array_values($other_choices);
  1069. // return Result::success($result);
  1070. }
  1071. } else {
  1072. $value_choice = $choose;
  1073. }
  1074. $result = [
  1075. 'choose' => $value_choice,
  1076. 'total' => $total,
  1077. ];
  1078. }
  1079. return Result::success($result);
  1080. }
  1081. /**
  1082. * 前端-搜索新闻列表
  1083. * @param array $data
  1084. * @return array
  1085. */
  1086. public function selectWebsiteArticle(array $data): array
  1087. {
  1088. $where = [];
  1089. // 初始化查询构造器
  1090. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  1091. $query = Article::where('status', 1)
  1092. ->whereIn('catid', $category)
  1093. ->where(function ($query) use ($data) {
  1094. $query->where(function ($subQuery) use ($data) {
  1095. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  1096. })->orWhereNull("ignore_ids");
  1097. });
  1098. // return Result::success($all_articles);
  1099. // 检查是否存在 cityid 参数
  1100. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1101. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  1102. }
  1103. // 检查是否存在 department_id 参数
  1104. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1105. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  1106. }
  1107. // 检查是否存在 keyword 参数
  1108. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1109. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  1110. }
  1111. // 计算总数
  1112. $count = $query->count();
  1113. // 分页查询
  1114. $articles = $query->orderBy("updated_at", "desc")
  1115. ->limit($data['pageSize'])
  1116. ->offset(($data['page'] - 1) * $data['pageSize'])
  1117. ->get()->all();
  1118. if (empty($articles)) {
  1119. return Result::error("没有符合条件的资讯数据");
  1120. }
  1121. $data = [
  1122. 'rows' => $articles,
  1123. 'count' => $count,
  1124. ];
  1125. return Result::success($data);
  1126. }
  1127. /**
  1128. * 模块新闻加强版
  1129. * @param array $data
  1130. * @return array
  1131. */
  1132. public function getWebsiteCatidArticle(array $data): array
  1133. {
  1134. // return Result::success($data);
  1135. $where = [
  1136. // 'category.status' => 1,
  1137. 'website_category.category_id' => $data['catid'],
  1138. 'website_category.website_id' => $data['website_id'],
  1139. // 'article.status' => 1,
  1140. ];
  1141. // $category = WebsiteCategory::where($where);
  1142. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  1143. $child_catnum = $data['child_catnum']?? 1;
  1144. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  1145. $childCategoryIds = array_column($category['child'], 'category_id');
  1146. if(empty($childCategoryIds)){
  1147. return Result::error("暂无子栏目",0);
  1148. }
  1149. $imgArticles = [];
  1150. $textArticles = [];
  1151. // return Result::success($childCategoryIds);
  1152. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  1153. // 初始化子分类图片新闻和文字新闻数组
  1154. // 查询所有子级栏目的图文新闻
  1155. $imgArticles = Article::where('catid', $childCategoryIds[0])
  1156. ->where('status', 1)
  1157. ->where('imgurl', '!=', '')
  1158. ->select('*')
  1159. ->orderBy('updated_at', 'desc')
  1160. ->limit($data['child_imgnum'])
  1161. ->get();
  1162. }
  1163. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  1164. // 查询所有子级栏目的文字新闻
  1165. $textArticles = Article::where('catid', $childCategoryIds[0])
  1166. ->where('status', 1)
  1167. // ->where(function ($query) {
  1168. // $query->whereNull('imgurl')
  1169. // ->orWhere('imgurl', '');
  1170. // })
  1171. ->select('*')
  1172. ->orderBy('updated_at', 'desc')
  1173. ->limit($data['child_textnum'])
  1174. ->get();
  1175. }
  1176. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  1177. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  1178. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  1179. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  1180. return $child;
  1181. }, $category['child']);
  1182. }
  1183. // }
  1184. if (isset($data['img_num']) && !empty($data['img_num'])) {
  1185. $category['img'] = WebsiteCategory::where($where)
  1186. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1187. ->where('article.status', 1)
  1188. ->where('article.imgurl', '!=', '')
  1189. ->select('article.*','website_category.category_id','website_category.alias')
  1190. ->orderBy('article.updated_at', 'desc')
  1191. ->limit($data['img_num'])
  1192. ->get();
  1193. }
  1194. if (isset($data['text_num']) && !empty($data['text_num'])) {
  1195. $category['text'] = WebsiteCategory::where($where)
  1196. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1197. ->where('article.status', 1)
  1198. // ->where(function ($query) {
  1199. // $query->whereNull('article.imgurl')
  1200. // ->orWhere('article.imgurl', '');
  1201. // })
  1202. ->select('article.*','website_category.category_id','website_category.alias')
  1203. ->orderBy('article.updated_at', 'desc')
  1204. ->limit($data['text_num'])
  1205. ->get();
  1206. }
  1207. // $category = $category->get();
  1208. if(empty($category)){
  1209. return Result::error("查询失败", 0);
  1210. }
  1211. return Result::success($category);
  1212. }
  1213. /**
  1214. * 验证导航名称是否重复
  1215. * @return void
  1216. */
  1217. public function checkCategoryName(array $data): array
  1218. {
  1219. $result = Category::when($data, function ($query) use ($data) {
  1220. if (isset($data['name']) && $data['name']) {
  1221. $query->where("name", $data['name']);
  1222. }
  1223. if (isset($data['id']) && $data['id']) {
  1224. $query->where("id", "!=", $data['id']);
  1225. }
  1226. })->first();
  1227. if ($result) {
  1228. return Result::error("已存在");
  1229. } else {
  1230. return Result::success();
  1231. }
  1232. }
  1233. //20250226 产品列表
  1234. public function getGoodList(array $data): array
  1235. {
  1236. $type_id = isset($data['type_id']) ? $data['type_id'] : '';
  1237. unset($data['type_id']);
  1238. $user_id = isset($data['user_id']) ? $data['user_id'] : '';
  1239. $where = [];
  1240. if ($type_id != '10000') {
  1241. $where = [
  1242. 'good.user_id' => $user_id,
  1243. ];
  1244. }
  1245. //类型
  1246. if (isset($data['type_id']) && $data['type_id']) {
  1247. $where = [
  1248. 'type_id' => $data['type_id'],
  1249. ];
  1250. }
  1251. //名称
  1252. if (isset($data['name']) && $data['name']) {
  1253. $where = [
  1254. 'good.name' => $data['name'],
  1255. ];
  1256. }
  1257. $where1 = [];
  1258. //website_id
  1259. // if (isset($data['website_id']) && $data['website_id']) {
  1260. // $where1 = [
  1261. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  1262. // ];
  1263. // }
  1264. // website_name
  1265. if (isset($data['website_name']) && $data['website_name']) {
  1266. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  1267. }
  1268. // catid
  1269. if (isset($data['category_name']) && $data['category_name']) {
  1270. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  1271. }
  1272. // $result = Good::where($where)
  1273. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  1274. $result = Good::where($where)
  1275. ->when(!empty($where1), function ($query) use ($where1) {
  1276. return $query->where($where1);
  1277. })
  1278. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  1279. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  1280. ->leftJoin('category', 'good.catid', '=', 'category.id')
  1281. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  1282. ->orderBy("id", "desc")
  1283. ->limit($data['page_size'])
  1284. ->offset(($data['page'] - 1) * $data['page_size'])
  1285. ->get();
  1286. $count = Good::where($where)
  1287. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  1288. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  1289. ->leftJoin('category', 'good.catid', '=', 'category.id')
  1290. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  1291. ->orderBy("updated_at", "desc")->count();
  1292. $data = [
  1293. 'rows' => $result->toArray(),
  1294. 'count' => $count,
  1295. ];
  1296. if (empty($result)) {
  1297. return Result::error("此栏目暂无相关产品", 0);
  1298. }
  1299. return Result::success($data);
  1300. }
  1301. public function getGoodInfo(array $data): array
  1302. {
  1303. $result = Good::where('id', $data['id'])->first();
  1304. if (empty($result)) {
  1305. return Result::error("此产品不存在", 0);
  1306. }
  1307. return Result::success($result);
  1308. }
  1309. public function addGood(array $data): array
  1310. {
  1311. // unset($data['city_arr_id']);
  1312. // unset($data['cat_arr_id']);
  1313. $data['city_id'] = end($data['city_arr_id']);
  1314. $data['catid'] = end($data['cat_arr_id']);
  1315. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  1316. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  1317. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  1318. unset($data['imgUrl']);
  1319. $result = Good::insert($data);
  1320. if (empty($result)) {
  1321. return Result::error("添加失败", 0);
  1322. }
  1323. return Result::success($result);
  1324. }
  1325. public function updateGood(array $data): array
  1326. {
  1327. $data['city_id'] = end($data['city_arr_id']);
  1328. $data['catid'] = end($data['cat_arr_id']);
  1329. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  1330. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  1331. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  1332. //设置东八区
  1333. date_default_timezone_set('Asia/Shanghai');
  1334. $data['updated_at'] = date('Y-m-d H:i:s');
  1335. $result = Good::where('id', $data['id'])->update($data);
  1336. if (empty($result)) {
  1337. return Result::error("更新失败", 0);
  1338. }
  1339. return Result::success($result);
  1340. }
  1341. public function delGood(array $data): array
  1342. {
  1343. $result = Good::where('id', $data['id'])->delete();
  1344. if (empty($result)) {
  1345. return Result::error("删除失败", 0);
  1346. }
  1347. return Result::success($result);
  1348. }
  1349. //20250226 产品列表
  1350. //20250306 求职信息
  1351. public function getJobHuntingList(array $data): array
  1352. {
  1353. $where = [];
  1354. if (isset($data['username']) && !empty($data['username'])) {
  1355. $where[] = ['user.user_name', 'like', '%' . $data['username'] . '%'];
  1356. }
  1357. $type_id = isset($data['type_id']) ? $data['type_id'] : '';
  1358. $user_id = isset($data['user_id']) ? $data['user_id'] : '';
  1359. unset($data['type_id']);
  1360. if ($type_id != '10000') {
  1361. $where[] = ['job_hunting.user_id', '=', $user_id];
  1362. }
  1363. $result = JobHunting::where($where)
  1364. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1365. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1366. ->select('job_hunting.*', 'user.nickname as nickname', 'user.user_name as username', 'website.website_name as website_name')
  1367. ->orderBy("id", "desc")
  1368. ->limit($data['page_size'])
  1369. ->offset(($data['page'] - 1) * $data['page_size'])
  1370. ->get();
  1371. if (empty($result)) {
  1372. return Result::error("查询失败", 0);
  1373. }
  1374. $count = JobHunting::where($where)
  1375. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1376. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1377. ->count();
  1378. $data = [
  1379. 'rows' => $result->toArray(),
  1380. 'count' => $count,
  1381. ];
  1382. return Result::success($data);
  1383. }
  1384. public function addJobHunting(array $data): array
  1385. {
  1386. date_default_timezone_set('Asia/Shanghai');
  1387. unset($data['company_name']);
  1388. unset($data['job_industry']);
  1389. unset($data['job_name']);
  1390. unset($data['department']);
  1391. unset($data['job_timeList']);
  1392. unset($data['job_content']);
  1393. $data['created_at'] = date('Y-m-d H:i:s');
  1394. $data['updated_at'] = date('Y-m-d H:i:s');
  1395. var_dump($data, '-----------------test---------');
  1396. $result = JobHunting::create($data);
  1397. if (empty($result)) {
  1398. return Result::error("添加失败", 0);
  1399. }
  1400. return Result::success($result);
  1401. }
  1402. public function delJobHunting(array $data): array
  1403. {
  1404. $result = JobHunting::where('id', $data['id'])->delete();
  1405. if (empty($result)) {
  1406. return Result::error("删除失败", 0);
  1407. }
  1408. return Result::success($result);
  1409. }
  1410. public function updateJobHunting(array $data): array
  1411. {
  1412. //设置东八区
  1413. date_default_timezone_set('Asia/Shanghai');
  1414. unset($data['company_name']);
  1415. unset($data['job_industry']);
  1416. unset($data['job_name']);
  1417. unset($data['department']);
  1418. unset($data['job_timeList']);
  1419. unset($data['job_content']);
  1420. $data['created_at'] = date('Y-m-d H:i:s');
  1421. $data['updated_at'] = date('Y-m-d H:i:s');
  1422. $result = JobHunting::where('id', $data['id'])->update($data);
  1423. if (empty($result)) {
  1424. return Result::error("更新失败", 0);
  1425. }
  1426. return Result::success($result);
  1427. }
  1428. public function getJobHuntingInfo(array $data): array
  1429. {
  1430. $result = JobHunting::where('id', $data['id'])->first();
  1431. if (empty($result)) {
  1432. return Result::error("查询失败", 0);
  1433. }
  1434. return Result::success($result);
  1435. }
  1436. public function getJobHuntingData(array $data): array
  1437. {
  1438. $jobEnum = JobEnum::get();
  1439. $jobIndustry = JobIndustry::get();
  1440. $jobNature = JobNature::get();
  1441. $jobPosition = JobPosition::get();
  1442. $data = [
  1443. 'jobEnum' => $jobEnum,
  1444. 'jobIndustry' => $jobIndustry,
  1445. 'jobNature' => $jobNature,
  1446. 'jobPosition' => $jobPosition,
  1447. ];
  1448. return Result::success($data);
  1449. }
  1450. public function delJobHuntingInfo(array $data): array
  1451. {
  1452. $result = JobHunting::where('id', $data['id'])->delete();
  1453. return Result::success();
  1454. }
  1455. }