NewsService.php 39 KB

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