NewsService.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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\Good;
  10. use App\Model\Website;
  11. use Hyperf\DbConnection\Db;
  12. use Hyperf\RpcServer\Annotation\RpcService;
  13. use App\Tools\Result;
  14. use Ramsey\Uuid\Uuid;
  15. use Hyperf\Utils\Random;
  16. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  17. class NewsService implements NewsServiceInterface
  18. {
  19. /**
  20. * 获取导航池列表
  21. * @param array $data
  22. * @return array
  23. */
  24. public function getCategoryList(array $data): array
  25. {
  26. $where = [];
  27. if (isset($data['name']) && $data['name']) {
  28. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  29. }
  30. if (isset($data['department_id']) && $data['department_id']) {
  31. array_push($where, ['category.department_id', '=', $data['department_id']]);
  32. }
  33. $city_id = '';
  34. if (isset($data['city_id']) && $data['city_id']) {
  35. $city_id = intval($data['city_id']);
  36. }
  37. $rep = Category::where($where)
  38. ->when($city_id, function ($query) use ($city_id) {
  39. if (isset($city_id) && $city_id) {
  40. $query->whereJsonContains("category.city_arr_id", $city_id);
  41. }
  42. })
  43. ->leftJoin('district', 'category.city_id', 'district.id')
  44. ->leftJoin('department', 'category.department_id', 'department.id')
  45. ->select("category.*", "district.name as city_name", "department.name as department_name")
  46. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page'] - 1) * $data['pageSize'])->get();
  47. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  48. if (isset($city_id) && $city_id) {
  49. $query->whereJsonContains("category.city_arr_id", $city_id);
  50. }
  51. })->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. public function myCategoryList(array $data): array
  62. {
  63. $sszq = $data['sszq'] ?? '';
  64. unset($data['sszq']);
  65. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  66. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  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. //根据分类id,从category表中取出分类信息
  75. $result = Category::where($where)
  76. ->whereIn('category.id', $catorytids)
  77. ->select('category.*', 'category.id as category_id')->get();
  78. if (empty($result)) {
  79. return Result::error("没有栏目数据");
  80. }
  81. return Result::success($result);
  82. }
  83. /**
  84. * @param array $data
  85. * @return array
  86. */
  87. public function categoryList(array $data): array
  88. {
  89. $where[] = [
  90. 'pid', '=', $data['pid'],
  91. ];
  92. if (isset($data['name'])) {
  93. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  94. }
  95. var_dump($where);
  96. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  97. if (empty($result)) {
  98. return Result::error("没有栏目数据");
  99. }
  100. return Result::success($result);
  101. }
  102. /**
  103. * @param array $data
  104. * @return array
  105. */
  106. public function addCategory(array $data): array
  107. {
  108. $id = Category::insertGetId($data);
  109. if (empty($id)) {
  110. return Result::error("添加失败");
  111. }
  112. return Result::success(['id' => $id]);
  113. }
  114. /**
  115. * @param array $data
  116. * @return array
  117. */
  118. public function delCategory(array $data): array
  119. {
  120. $categoryList = Category::where(['pid' => $data['id']])->get();
  121. var_dump("分类列表:", $data, $categoryList);
  122. if ($categoryList->toArray()) {
  123. return Result::error("分类下面有子分类不能删除");
  124. }
  125. $articleList = Article::where(['catid' => $data['id']])->get();
  126. var_dump("文章列表:", $articleList);
  127. if ($articleList->toArray()) {
  128. return Result::error("分类下面有资讯不能删除");
  129. }
  130. $result = Category::where($data)->delete();
  131. if (!$result) {
  132. return Result::error("删除失败");
  133. }
  134. return Result::success($result);
  135. }
  136. /**
  137. * @param array $data
  138. * @return array
  139. */
  140. public function updateCategory(array $data): array
  141. {
  142. $where = [
  143. 'id' => $data['id'],
  144. ];
  145. $result = Category::where($where)->update($data);
  146. if ($result) {
  147. return Result::success($result);
  148. } else {
  149. return Result::error("更新失败");
  150. }
  151. }
  152. /**
  153. * 获取导航池信息
  154. * @param array $data
  155. * @return array
  156. */
  157. public function getCategoryInfo(array $data): array
  158. {
  159. $where = [
  160. 'id' => $data['id'],
  161. ];
  162. $result = Category::where($where)->first();
  163. if ($result) {
  164. return Result::success($result);
  165. } else {
  166. return Result::error("更新失败");
  167. }
  168. }
  169. /**
  170. * @param array $data
  171. * @return array
  172. */
  173. public function getArticleList(array $data): array
  174. {
  175. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  176. $type_id = $data['type_id'];
  177. unset($data['type_id']);
  178. $user_id = $data['user_id'];
  179. unset($data['user_id']);
  180. $where = [];
  181. $status1 = [];
  182. if (isset($data['title']) && $data['title']) {
  183. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  184. }
  185. if (isset($data['category_name']) && $data['category_name']) {
  186. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  187. }
  188. if (isset($data['author']) && $data['author']) {
  189. array_push($where, ['article.author', '=', $data['author']]);
  190. }
  191. if (isset($data['islink']) && $data['islink'] !== "") {
  192. array_push($where, ['article.islink', '=', $data['islink']]);
  193. }
  194. if (isset($data['status']) && $data['status'] !== "") {
  195. array_push($where, ['article.status', '=', $data['status']]);
  196. }
  197. if (isset($data['status1'])) {
  198. $status1 = json_decode(($data['status1']));
  199. }
  200. //不是管理员展示个人数据;
  201. if ($type_id != 10000) {
  202. $where[] = ['article.admin_user_id', '=', $user_id];
  203. }
  204. $rep = Article::where($where)
  205. ->whereNotIn('article.status', [404])
  206. ->when($status1, function ($query) use ($status1) {
  207. if (isset($status1) && $status1) {
  208. $query->whereIn('article.status', $status1);
  209. }
  210. })
  211. ->leftJoin('category', 'article.catid', 'category.id')
  212. ->select("article.*", "category.name as category_name")
  213. ->orderBy("article.id", "desc")
  214. ->limit($data['pageSize'])
  215. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  216. $count = Article::where($where)->whereNotIn('article.status', [404])
  217. ->when($status1, function ($query) use ($status1) {
  218. if (isset($status1) && $status1) {
  219. $query->whereIn('article.status', $status1);
  220. }
  221. })
  222. ->leftJoin('category', 'article.catid', 'category.id')->count();
  223. $data = [
  224. 'rows' => $rep->toArray(),
  225. 'count' => $count,
  226. ];
  227. if (empty($rep)) {
  228. return Result::error("没有信息数据");
  229. }
  230. return Result::success($data);
  231. }
  232. /**
  233. * @param array $data
  234. * @return array
  235. */
  236. public function addArticle(array $data): array
  237. {
  238. var_dump($data, '----------12-----------1');
  239. unset($data['user_type']);
  240. // unset($data['web_site_id']);
  241. unset($data['nav_add_pool_id']);
  242. // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  243. Db::beginTransaction();
  244. try {
  245. //处理投票
  246. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  247. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  248. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  249. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  250. unset($data['is_survey']);
  251. unset($data['survey_name']);
  252. unset($data['suvey_array']);
  253. unset($data['website_id']);
  254. $articleData = $data;
  255. unset($articleData['content']);
  256. $id = Article::insertGetId($articleData);
  257. $articleDataContent = [
  258. 'article_id' => $id,
  259. 'content' => $data['content'],
  260. ];
  261. ArticleData::insertGetId($articleDataContent);
  262. //处理投票
  263. if ($is_survey == 1) {
  264. //生成年月日时分秒+8位随机数
  265. $uuid = date('YmdHis') . rand(10000000, 99999999);
  266. var_dump($suvey_array, 'suvey_array________');
  267. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  268. var_dump($suveys_array, '---------------------1');
  269. var_dump($suvey_array, '---------------------2');
  270. $suvey_data = [];
  271. foreach ($suveys_array as $key => $value) {
  272. if ($value == '') {
  273. continue;
  274. }
  275. if (is_array($value)) {
  276. $suvey_data[] = [
  277. 'sur_id' => $uuid,
  278. 'art_id' => $id,
  279. 'website_id' => $website_id ?? 2,
  280. 'survey_name' => $survey_name,
  281. 'choice_name' => $value[1],
  282. 'is_other' => 1,
  283. 'other_id' => 0,
  284. 'results' => 0,
  285. ];
  286. } else {
  287. $suvey_data[] = [
  288. 'sur_id' => $uuid,
  289. 'art_id' => $id,
  290. 'website_id' => $website_id ?? 2,
  291. 'survey_name' => $survey_name,
  292. 'choice_name' => $value,
  293. 'is_other' => 0,
  294. 'other_id' => 0,
  295. 'results' => 0,
  296. ];
  297. }
  298. if (empty($suvey_data)) {
  299. throw new \Exception("投票数据为空");
  300. }
  301. }
  302. $result = ArticleSurvey::insert($suvey_data);
  303. if (!$result) {
  304. throw new \Exception("投票失败,ArticleSurvey插入失败");
  305. }
  306. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  307. if (!$result) {
  308. throw new \Exception("投票失败,更新主表失败");
  309. }
  310. }
  311. Db::commit();
  312. return Result::success(['id' => $id]);
  313. } catch (\Throwable $ex) {
  314. Db::rollBack();
  315. var_dump($ex->getMessage());
  316. return Result::error("创建失败", 0);
  317. }
  318. }
  319. /**
  320. * @param array $data
  321. * @return array
  322. */
  323. public function delArticle(array $data): array
  324. {
  325. $result = Article::where($data)->delete();
  326. //survey投票删除
  327. articleSurvey::where(['art_id' => $data['id']])->delete();
  328. if (!$result) {
  329. return Result::error("删除失败");
  330. }
  331. return Result::success($result);
  332. }
  333. /**
  334. * @param array $data
  335. * @return array
  336. */
  337. public function updateArticle(array $data): array
  338. {
  339. var_dump($data, '----------12-----------1');
  340. Db::beginTransaction();
  341. unset($data['user_type']);
  342. // unset($data['web_site_id']);
  343. unset($data['nav_add_pool_id']);
  344. try {
  345. //处理投票
  346. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  347. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  348. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  349. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  350. unset($data['is_survey']);
  351. unset($data['survey_name']);
  352. unset($data['suvey_array']);
  353. unset($data['website_id']);
  354. if ($data['hits'] == '') {
  355. $data['hits'] = 0;
  356. }
  357. if ($data['is_original'] == '') {
  358. $data['is_original'] = 0;
  359. }
  360. if ($data['status'] == '') {
  361. $data['status'] = 0;
  362. }
  363. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  364. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  365. $articleData = $data;
  366. unset($articleData['content']);
  367. unset($articleData['status_name']);
  368. unset($articleData['name']);
  369. unset($articleData['content']);
  370. unset($articleData['pid_arr']);
  371. unset($articleData['pid']);
  372. $id = Article::where(['id' => $data['id']])->update($articleData);
  373. $articleDataContent = [
  374. 'content' => $data['content'],
  375. ];
  376. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  377. //处理投票
  378. $id = $data['id'];
  379. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  380. var_dump($suvey_array, 'suvey_array________delete');
  381. //处理投票
  382. if ($is_survey == 1) {
  383. //生成年月日时分秒+8位随机数
  384. $uuid = date('YmdHis') . rand(10000000, 99999999);
  385. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  386. var_dump($suveys_array, '---------------------1');
  387. var_dump($suvey_array, '---------------------2');
  388. $suvey_data = [];
  389. if (is_array($suveys_array)) {
  390. foreach ($suveys_array as $key => $value) {
  391. if ($value == '') {
  392. continue;
  393. }
  394. if (is_array($value)) {
  395. $suvey_data[] = [
  396. 'sur_id' => $uuid,
  397. 'art_id' => $id,
  398. 'website_id' => $website_id ?? 2,
  399. 'survey_name' => $survey_name,
  400. 'choice_name' => $value[1],
  401. 'is_other' => 1,
  402. 'other_id' => 0,
  403. 'results' => 0,
  404. ];
  405. } else {
  406. $suvey_data[] = [
  407. 'sur_id' => $uuid,
  408. 'art_id' => $id,
  409. 'website_id' => $website_id ?? 2,
  410. 'survey_name' => $survey_name,
  411. 'choice_name' => $value,
  412. 'is_other' => 0,
  413. 'other_id' => 0,
  414. 'results' => 0,
  415. ];
  416. }
  417. if (empty($suvey_data)) {
  418. throw new \Exception("投票数据为空");
  419. }
  420. }
  421. }
  422. $result = ArticleSurvey::insert($suvey_data);
  423. if (!$result) {
  424. throw new \Exception("投票失败");
  425. }
  426. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  427. if (!$result) {
  428. throw new \Exception("投票失败");
  429. }
  430. } else {
  431. $result = Article::where('id', $id)->update(['survey_id' => '', 'survey_name' => '', 'is_survey' => 0]);
  432. }
  433. Db::commit();
  434. return Result::success([]);
  435. } catch (\Throwable $ex) {
  436. Db::rollBack();
  437. var_dump($ex->getMessage());
  438. return Result::error("更新失败1" . $ex->getMessage(), 0);
  439. }
  440. }
  441. /**
  442. * 更新资讯状态
  443. * @param array $data
  444. * @return array
  445. */
  446. public function upArticleStatus(array $data): array
  447. {
  448. $result = Article::where(['id' => $data['id']])->update($data);
  449. if ($result) {
  450. return Result::success();
  451. } else {
  452. return Result::error("更新状态失败", 0);
  453. }
  454. }
  455. /**
  456. * @param array $data
  457. * @return array
  458. */
  459. public function getArticleInfo(array $data): array
  460. {
  461. $where = [
  462. 'article.id' => $data['id'],
  463. // 'article.status' => 1,
  464. ];
  465. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  466. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get()->all();
  467. var_dump($articleSurvey, 'articleSurvey');
  468. $info = $result;
  469. var_dump($info, 'info');
  470. // $info['survey_array'] = $articleSurvey == null ? [] : $info['survey_array'];
  471. if ($result) {
  472. return Result::success($info);
  473. } else {
  474. return Result::error("查询失败", 0);
  475. }
  476. }
  477. /**
  478. * 获取新闻
  479. * @param array $data
  480. * @return array
  481. */
  482. public function getWebsiteArticlett(array $data): array
  483. {
  484. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  485. $category = $category->toArray();
  486. $result = [];
  487. if ($category) {
  488. $category_ids = [];
  489. foreach ($category as $val) {
  490. array_push($category_ids, $val['category_id']);
  491. }
  492. if (isset($data['placeid'])) {
  493. $placeid = $data['placeid'] - 1;
  494. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("updated_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  495. } else {
  496. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("updated_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  497. }
  498. if (empty($result)) {
  499. return Result::error("暂无头条新闻", 0);
  500. }
  501. return Result::success($result);
  502. } else {
  503. return Result::error("本网站下暂无相关栏目", 0);
  504. }
  505. }
  506. /**
  507. * 获取模块新闻
  508. * @param array $data
  509. * @return array
  510. */
  511. public function getWebsiteModelArticles(array $data): array
  512. {
  513. $catid = $data['catid'];
  514. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  515. $category = $category->toArray();
  516. if (!empty($category)) {
  517. $where = [
  518. 'status' => 1,
  519. 'catid' => $catid,
  520. ];
  521. if ($data['level'] == 1) {
  522. $level = [
  523. 0 => '1',
  524. 1 => '4',
  525. 2 => '5',
  526. 3 => '0',
  527. ];
  528. $result = Article::where($where)->whereIn('level', $level)->orderBy("updated_at", "desc")->limit($data['pagesize'])->get();
  529. } elseif ($data['level'] == 2) {
  530. $level = '2';
  531. $result = Article::where($where)->where('level', $level)->orderBy("updated_at", "desc")->limit($data['pagesize'])->get();
  532. } else {
  533. $level = '3';
  534. $result = Article::where($where)->where('level', $level)->orderBy("updated_at", "desc")->limit($data['pagesize'])->get();
  535. }
  536. $result = $result->toArray();
  537. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  538. $placeid = $data['placeid'] - 1;
  539. if ($level == 2 || $level == 3) {
  540. $where = [
  541. 'level' => $level,
  542. ];
  543. $result = Article::where($where)
  544. ->orderBy("updated_at", "desc")
  545. ->offset($placeid)
  546. ->limit($data['pagesize'])->get();
  547. } else {
  548. $result = Article::where($where)
  549. ->whereIn('level', $level)
  550. ->offset($placeid)
  551. ->orderBy("updated_at", "desc")
  552. ->limit($data['pagesize'])->get();
  553. }
  554. }
  555. if (empty($result)) {
  556. return Result::error("此栏目暂无相关新闻", 0);
  557. }
  558. } else {
  559. return Result::error("此网站暂无此栏目", 0);
  560. }
  561. return Result::success($result);
  562. }
  563. /**
  564. *获取新闻列表
  565. * @param array $data
  566. * @return array
  567. */
  568. public function getWebsiteArticleList(array $data): array
  569. {
  570. $where[] = ['status', '=', 1];
  571. if (isset($data['keyword']) && !empty($data['keyword'])) {
  572. array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
  573. }
  574. if (isset($data['catid']) && !empty($data['catid'])) {
  575. if (is_array($data['catid'])) {
  576. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  577. array_push($where, ['catid', 'in', $data['catid']]);
  578. } else {
  579. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  580. array_push($where, ['catid', '=', $data['catid']]);
  581. }
  582. if (empty($category)) {
  583. return Result::error("此网站暂无此栏目", 0);
  584. }
  585. }
  586. // return Result::success($where);
  587. $rep = Article::where(function ($query) use ($where) {
  588. foreach ($where as $condition) {
  589. if ($condition[1] === 'in') {
  590. $query->whereIn($condition[0], $condition[2]);
  591. } else {
  592. $query->where($condition[0], $condition[1], $condition[2]);
  593. }
  594. }
  595. })
  596. ->orderBy("updated_at", "desc")
  597. ->limit($data['pageSize'])
  598. ->offset(($data['page'] - 1) * $data['pageSize'])
  599. ->get();
  600. $count = Article::where(function ($query) use ($where) {
  601. foreach ($where as $condition) {
  602. if ($condition[1] === 'in') {
  603. $query->whereIn($condition[0], $condition[2]);
  604. } else {
  605. $query->where($condition[0], $condition[1], $condition[2]);
  606. }
  607. }
  608. })->count();
  609. $data = [
  610. 'rows' => $rep->toArray(),
  611. 'count' => $count,
  612. ];
  613. if (empty($rep)) {
  614. return Result::error("没有信息数据");
  615. }
  616. return Result::success($data);
  617. }
  618. /**
  619. * 前端-获取新闻详情
  620. * @param array $data
  621. * @return array
  622. */
  623. public function selectWebsiteArticleInfo(array $data): array
  624. {
  625. $where = [
  626. 'article.id' => $data['id'],
  627. 'article.status' => 1,
  628. ];
  629. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  630. if (empty($result)) {
  631. return Result::error("查询失败", 0);
  632. }
  633. $category = WebsiteCategory::where('website_id', $data['website_id'])->where(['category_id' => $result['catid']])->first();
  634. if (empty($category)) {
  635. return Result::error("查询失败", 0);
  636. }
  637. $result['category_id'] = $category['category_id'];
  638. $result['cat_name'] = $category['alias'];
  639. return Result::success($result);
  640. }
  641. /**
  642. * 前端-获取网站调查问卷
  643. * @param array $data
  644. * @return array
  645. */
  646. public function getWebsiteSurvey(array $data): array
  647. {
  648. if (isset($data['website_id']) && !empty($data['website_id'])) {
  649. $website = Website::where('id', $data['website_id'])->first();
  650. if (empty($website)) {
  651. return Result::error("暂无此网站", 0);
  652. }
  653. }
  654. if (isset($data['art_id']) && !empty($data['art_id'])) {
  655. $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
  656. if (empty($article)) {
  657. return Result::error("暂无此文章", 0);
  658. }
  659. // return Result::error($data,0);
  660. $where['art_id'] = $data['art_id'];
  661. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  662. } else {
  663. $survey = ArticleSurvey::where('website_id', $data['website_id'])->orderBy('created_at')->first();
  664. if (empty($survey)) {
  665. return Result::error("暂无调查问卷", 0);
  666. }
  667. $where['sur_id'] = $survey['sur_id'];
  668. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  669. }
  670. $result['survey'] = ArticleSurvey::where($where)->where('is_other', 0)
  671. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  672. ->select('article_survey.*', 'article.survey_type')
  673. ->get()->all();
  674. $result['other'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  675. $result['others'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->first();
  676. if (empty($result)) {
  677. return Result::error("此文章暂无调查问卷", 0);
  678. }
  679. return Result::success($result);
  680. }
  681. /**
  682. * 前端-添加网站调查问卷选项
  683. * @param array $data
  684. * @return array
  685. */
  686. public function addWebsiteSurveyOption(array $data): array
  687. {
  688. if (isset($data['website_id']) && !empty($data['website_id'])) {
  689. $website = Website::where('id', $data['website_id'])->first();
  690. if (empty($website)) {
  691. return Result::error("暂无此网站", 0);
  692. }
  693. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  694. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  695. if (empty($survey)) {
  696. return Result::error("此调查问卷不可添加选项", 0);
  697. }
  698. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  699. $choice = [
  700. 'art_id' => $survey['art_id'],
  701. 'website_id' => $data['website_id'],
  702. 'survey_name' => $survey['survey_name'],
  703. 'choice_name' => $data['choice_name'],
  704. 'sur_id' => $survey['sur_id'],
  705. 'is_other' => 1,
  706. 'other_id' => $survey['id'],
  707. ];
  708. $result = ArticleSurvey::insertGetId($choice);
  709. if (empty($result)) {
  710. return Result::error("添加失败", 0);
  711. }
  712. return Result::success($result);
  713. }
  714. }
  715. return Result::error("添加失败", 0);
  716. }
  717. return Result::error("添加失败", 0);
  718. }
  719. /**
  720. * 前端-调查问卷投票
  721. * @param array $data
  722. * @return array
  723. */
  724. public function addWebsiteSurveyVote(array $data): array
  725. {
  726. if (isset($data['website_id']) && !empty($data['website_id'])) {
  727. $website = Website::where('id', $data['website_id'])->first();
  728. if (empty($website)) {
  729. return Result::error("暂无此网站", 0);
  730. }
  731. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  732. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->pluck('sur_id');
  733. if (empty($survey)) {
  734. return Result::error("此调查问卷不存在", 0);
  735. }
  736. // return Result::success($survey);
  737. // 调查问卷类型 0:单选 1:多选
  738. $type = Article::where('survey_id', $data['sur_id'])->pluck('survey_type');
  739. // return Result::success($type);
  740. if (empty($type) || ($type[0] != 1 && $type[0] != 0)) {
  741. return Result::error("此调查问卷不可投票", 0);
  742. }
  743. // return Result::success($type[0]);
  744. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  745. if ($type[0] == 0) {
  746. if (is_array($data['choice_id'])) {
  747. return Result::error("请选择一个选项!", 0);
  748. }
  749. $data['choice_id'] = [$data['choice_id']];
  750. } else {
  751. if (!is_array($data['choice_id'])) {
  752. return Result::error("请传递数组!", 0);
  753. }
  754. }
  755. // return Result::success($data['choice_id']);
  756. $other = ArticleSurvey::whereIn('id', $data['choice_id'])
  757. ->where('website_id', $data['website_id'])
  758. ->where('is_other', 1)
  759. ->where('other_id', 0)
  760. ->first();
  761. if (!empty($other)) {
  762. return Result::error("请选择已有的选项!", 0);
  763. }
  764. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  765. ->where('website_id', $data['website_id'])
  766. ->where('is_other', 1)
  767. ->where('other_id', '!=', 0)
  768. ->first();
  769. // return Result::success($data);
  770. if (!empty($choice['other'])) {
  771. array_push($data['choice_id'], $choice['other']['other_id']);
  772. // return Result::success($data['choice_id']);
  773. }
  774. // return Result::success($data);
  775. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  776. ->where('website_id', $data['website_id'])
  777. ->increment('results', 1);
  778. if (empty($choice)) {
  779. return Result::error("请选择已有的选项!", 0);
  780. }
  781. return Result::success($choice);
  782. }
  783. return Result::error("参数必填!");
  784. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  785. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  786. // }
  787. }
  788. return Result::error("此调查问卷不存在", 0);
  789. }
  790. return Result::error("参数必填!");
  791. }
  792. /**
  793. * 后端-获取网站调查问卷列表
  794. * @param array $data
  795. * @return array
  796. */
  797. public function getSurveyList(array $data): array
  798. {
  799. $where = [];
  800. if (isset($data['survey_name']) && !empty($data['survey_name'])) {
  801. array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
  802. }
  803. if (isset($data['survey_type']) && $data['survey_type'] != null) {
  804. array_push($where, ['survey_type', '=', $data['survey_type']]);
  805. }
  806. if (isset($data['is_survey']) && $data['is_survey'] != null) {
  807. array_push($where, ['is_survey', '=', $data['is_survey']]);
  808. }
  809. // return Result::success($where);
  810. if (!empty($where)) {
  811. $query = Article::where($where)->whereNotNull('survey_name');
  812. } else {
  813. $query = Article::whereNotNull('survey_name');
  814. }
  815. $count = $query->count();
  816. $survey = $query->orderByDesc('id')
  817. ->limit($data['pageSize'])
  818. ->offset(($data['page'] - 1) * $data['pageSize'])
  819. ->get();
  820. if (empty($survey->toArray())) {
  821. return Result::error("暂无调查问卷!", 0);
  822. }
  823. $result = [
  824. 'rows' => $survey,
  825. 'count' => $count,
  826. ];
  827. return Result::success($result);
  828. }
  829. /**
  830. * 后端-获取网站调查问卷详情
  831. * @param array $data
  832. * @return array
  833. */
  834. public function getSurveyInfo(array $data): array
  835. {
  836. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  837. $where = ['sur_id' => $data['sur_id']];
  838. $choose = ArticleSurvey::where($where)->where('is_other', 0)
  839. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  840. ->select('article_survey.*', 'article.survey_type')
  841. ->get()->all();
  842. if (empty($choose)) {
  843. return Result::error("此调查问卷不存在", 0);
  844. }
  845. $resultsArray = array_column($choose, 'results');
  846. $total = array_sum($resultsArray);
  847. $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  848. $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
  849. // $total = 0;
  850. if (!empty($other)) {
  851. $total = $total + $other['results'];
  852. $other['choice_name'] = $other['choice_name'] . '(其他)';
  853. if (!empty($others)) {
  854. $other['hasChildren'] = true;
  855. // array_push($other,['hasChildren','=',true]);
  856. $other['children'] = $others;
  857. $other_choices = [$other->toArray()];
  858. $mer_choice = array_merge($choose, $other_choices);
  859. $value_choice = array_values($mer_choice);
  860. } else {
  861. // return Result::error('1111');
  862. $other_choices = [$other->toArray()];
  863. $other_choices = array_merge($choose, $other_choices);
  864. $value_choice = array_values($other_choices);
  865. // return Result::success($result);
  866. }
  867. } else {
  868. $value_choice = $choose;
  869. }
  870. $result = [
  871. 'choose' => $value_choice,
  872. 'total' => $total,
  873. ];
  874. }
  875. return Result::success($result);
  876. }
  877. /**
  878. * 验证导航名称是否重复
  879. * @return void
  880. */
  881. public function checkCategoryName(array $data): array
  882. {
  883. $result = Category::when($data, function ($query) use ($data) {
  884. if (isset($data['name']) && $data['name']) {
  885. $query->where("name", $data['name']);
  886. }
  887. if (isset($data['id']) && $data['id']) {
  888. $query->where("id", "!=", $data['id']);
  889. }
  890. })->first();
  891. if ($result) {
  892. return Result::error("已存在");
  893. } else {
  894. return Result::success();
  895. }
  896. }
  897. //20250226 产品列表
  898. public function getGoodList(array $data): array
  899. {
  900. $where = [];
  901. //类型
  902. if (isset($data['type_id']) && $data['type_id']) {
  903. $where = [
  904. 'type_id' => $data['type_id'],
  905. ];
  906. }
  907. //名称
  908. if (isset($data['name']) && $data['name']) {
  909. $where = [
  910. 'good.name' => $data['name'],
  911. ];
  912. }
  913. $where1 = [];
  914. //website_id
  915. // if (isset($data['website_id']) && $data['website_id']) {
  916. // $where1 = [
  917. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  918. // ];
  919. // }
  920. // website_name
  921. if (isset($data['website_name']) && $data['website_name']) {
  922. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  923. }
  924. // catid
  925. if (isset($data['category_name']) && $data['category_name']) {
  926. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  927. }
  928. // $result = Good::where($where)
  929. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  930. $result = Good::where($where)
  931. ->when(!empty($where1), function ($query) use ($where1) {
  932. return $query->where($where1);
  933. })
  934. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  935. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  936. ->leftJoin('category', 'good.catid', '=', 'category.id')
  937. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  938. ->orderBy("id", "desc")
  939. ->limit($data['page_size'])
  940. ->offset(($data['page'] - 1) * $data['page_size'])
  941. ->get();
  942. $count = Good::where($where)
  943. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  944. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  945. ->leftJoin('category', 'good.catid', '=', 'category.id')
  946. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  947. ->orderBy("updated_at", "desc")->count();
  948. $data = [
  949. 'rows' => $result->toArray(),
  950. 'count' => $count,
  951. ];
  952. if (empty($result)) {
  953. return Result::error("此栏目暂无相关产品", 0);
  954. }
  955. return Result::success($data);
  956. }
  957. public function getGoodInfo(array $data): array
  958. {
  959. $result = Good::where('id', $data['id'])->first();
  960. if (empty($result)) {
  961. return Result::error("此产品不存在", 0);
  962. }
  963. return Result::success($result);
  964. }
  965. public function addGood(array $data): array
  966. {
  967. // unset($data['city_arr_id']);
  968. // unset($data['cat_arr_id']);
  969. $data['city_id'] = end($data['city_arr_id']);
  970. $data['catid'] = end($data['cat_arr_id']);
  971. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  972. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  973. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  974. unset($data['imgUrl']);
  975. $result = Good::insert($data);
  976. if (empty($result)) {
  977. return Result::error("添加失败", 0);
  978. }
  979. return Result::success($result);
  980. }
  981. public function updateGood(array $data): array
  982. {
  983. $data['city_id'] = end($data['city_arr_id']);
  984. $data['catid'] = end($data['cat_arr_id']);
  985. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  986. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  987. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  988. //设置东八区
  989. date_default_timezone_set('Asia/Shanghai');
  990. $data['updated_at'] = date('Y-m-d H:i:s');
  991. $result = Good::where('id', $data['id'])->update($data);
  992. if (empty($result)) {
  993. return Result::error("更新失败", 0);
  994. }
  995. return Result::success($result);
  996. }
  997. public function delGood(array $data): array
  998. {
  999. $result = Good::where('id', $data['id'])->delete();
  1000. if (empty($result)) {
  1001. return Result::error("删除失败", 0);
  1002. }
  1003. return Result::success($result);
  1004. }
  1005. //20250226 产品列表
  1006. //20250306 求职信息
  1007. public function getJobHuntingList(array $data): array
  1008. {
  1009. $where = [];
  1010. if (isset($data['name']) && !empty($data['name'])) {
  1011. $where[] = ['name', 'like', '%' . $data['name'] . '%'];
  1012. }
  1013. $result = JobHunting::where($where)
  1014. ->leftJoin('user', 'user.id', '=', 'job_hunting.user_id')
  1015. ->leftJoin('website', 'website.id', '=', 'job_hunting.website_id')
  1016. ->select('job_hunting.*', 'user.nickname as nickname', 'user.user_name as username', 'website.website_name as website_name')
  1017. ->orderBy("updated_at", "desc")
  1018. ->limit($data['page_size'])
  1019. ->offset(($data['page'] - 1) * $data['page_size'])
  1020. ->get();
  1021. if (empty($result)) {
  1022. return Result::error("查询失败", 0);
  1023. }
  1024. $count = JobHunting::where($where)->count();
  1025. $data = [
  1026. 'rows' => $result->toArray(),
  1027. 'count' => $count,
  1028. ];
  1029. return Result::success($data);
  1030. }
  1031. public function addJobHunting(array $data): array
  1032. {
  1033. $data['created_at'] = date('Y-m-d H:i:s');
  1034. $data['updated_at'] = date('Y-m-d H:i:s');
  1035. $result = JobHunting::create($data);
  1036. if (empty($result)) {
  1037. return Result::error("添加失败", 0);
  1038. }
  1039. return Result::success($result);
  1040. }
  1041. public function delJobHunting(array $data): array
  1042. {
  1043. $result = JobHunting::where('id', $data['id'])->delete();
  1044. if (empty($result)) {
  1045. return Result::error("删除失败", 0);
  1046. }
  1047. return Result::success($result);
  1048. }
  1049. public function updateJobHunting(array $data): array
  1050. {
  1051. $result = JobHunting::where('id', $data['id'])->update($data);
  1052. if (empty($result)) {
  1053. return Result::error("更新失败", 0);
  1054. }
  1055. return Result::success($result);
  1056. }
  1057. public function getJobHuntingInfo(array $data): array
  1058. {
  1059. $result = JobHunting::where('id', $data['id'])->first();
  1060. if (empty($result)) {
  1061. return Result::error("查询失败", 0);
  1062. }
  1063. return Result::success($result);
  1064. }
  1065. }