NewsService.php 40 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. if (is_array($suveys_array)) {
  364. foreach ($suveys_array as $key => $value) {
  365. if ($value == '') {
  366. continue;
  367. }
  368. if (is_array($value)) {
  369. $suvey_data[] = [
  370. 'sur_id' => $uuid,
  371. 'art_id' => $id,
  372. 'website_id' => $website_id ?? 2,
  373. 'survey_name' => $survey_name,
  374. 'choice_name' => $value[1],
  375. 'is_other' => 1,
  376. 'orther_id' => 0,
  377. 'results' => 0,
  378. ];
  379. } else {
  380. $suvey_data[] = [
  381. 'sur_id' => $uuid,
  382. 'art_id' => $id,
  383. 'website_id' => $website_id ?? 2,
  384. 'survey_name' => $survey_name,
  385. 'choice_name' => $value,
  386. 'is_other' => 0,
  387. 'orther_id' => 0,
  388. 'results' => 0,
  389. ];
  390. }
  391. if (empty($suvey_data)) {
  392. throw new \Exception("投票数据为空");
  393. }
  394. }
  395. }
  396. $result = ArticleSurvey::insert($suvey_data);
  397. if (!$result) {
  398. throw new \Exception("投票失败");
  399. }
  400. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  401. if (!$result) {
  402. throw new \Exception("投票失败");
  403. }
  404. }
  405. Db::commit();
  406. return Result::success([]);
  407. } catch (\Throwable $ex) {
  408. Db::rollBack();
  409. var_dump($ex->getMessage());
  410. return Result::error("更新失败", 0);
  411. }
  412. }
  413. /**
  414. * 更新资讯状态
  415. * @param array $data
  416. * @return array
  417. */
  418. public function upArticleStatus(array $data): array
  419. {
  420. $result = Article::where(['id' => $data['id']])->update($data);
  421. if ($result) {
  422. return Result::success();
  423. } else {
  424. return Result::error("更新状态失败", 0);
  425. }
  426. }
  427. /**
  428. * @param array $data
  429. * @return array
  430. */
  431. public function getArticleInfo(array $data): array
  432. {
  433. $where = [
  434. 'article.id' => $data['id'],
  435. 'article.status' => 1,
  436. ];
  437. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  438. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  439. $result['survey_array'] = $articleSurvey->toArray();
  440. if ($result) {
  441. return Result::success($result->toArray());
  442. } else {
  443. return Result::error("查询失败", 0);
  444. }
  445. }
  446. /**
  447. * 获取新闻
  448. * @param array $data
  449. * @return array
  450. */
  451. public function getWebsiteArticlett(array $data): array
  452. {
  453. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  454. $category = $category->toArray();
  455. $result = [];
  456. if ($category) {
  457. $category_ids = [];
  458. foreach ($category as $val) {
  459. array_push($category_ids, $val['category_id']);
  460. }
  461. if (isset($data['placeid'])) {
  462. $placeid = $data['placeid'] - 1;
  463. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  464. } else {
  465. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  466. }
  467. if (empty($result)) {
  468. return Result::error("暂无头条新闻", 0);
  469. }
  470. return Result::success($result);
  471. } else {
  472. return Result::error("本网站下暂无相关栏目", 0);
  473. }
  474. }
  475. /**
  476. * 获取模块新闻
  477. * @param array $data
  478. * @return array
  479. */
  480. public function getWebsiteModelArticles(array $data): array
  481. {
  482. $catid = $data['catid'];
  483. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  484. $category = $category->toArray();
  485. if (!empty($category)) {
  486. $where = [
  487. 'status' => 1,
  488. 'catid' => $catid,
  489. ];
  490. if ($data['level'] == 1) {
  491. $level = [
  492. 0 => '1',
  493. 1 => '4',
  494. 2 => '5',
  495. ];
  496. $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  497. } elseif ($data['level'] == 2) {
  498. $level = '2';
  499. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  500. } else {
  501. $level = '3';
  502. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  503. }
  504. $result = $result->toArray();
  505. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  506. $placeid = $data['placeid'] - 1;
  507. if ($level == 2 || $level == 3) {
  508. $where = [
  509. 'level' => $level,
  510. ];
  511. $result = Article::where($where)
  512. ->orderBy("created_at", "desc")
  513. ->offset($placeid)
  514. ->limit($data['pagesize'])->get();
  515. } else {
  516. $result = Article::where($where)
  517. ->whereIn('level', $level)
  518. ->offset($placeid)
  519. ->orderBy("created_at", "desc")
  520. ->limit($data['pagesize'])->get();
  521. }
  522. }
  523. if (empty($result)) {
  524. return Result::error("此栏目暂无相关新闻", 0);
  525. }
  526. } else {
  527. return Result::error("此网站暂无此栏目", 0);
  528. }
  529. return Result::success($result);
  530. }
  531. /**
  532. *获取新闻列表
  533. * @param array $data
  534. * @return array
  535. */
  536. public function getWebsiteArticleList(array $data): array
  537. {
  538. $where[] = ['status', '=', 1];
  539. if (isset($data['keyword']) && !empty($data['keyword'])) {
  540. array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
  541. }
  542. if (isset($data['catid']) && !empty($data['catid'])) {
  543. if (is_array($data['catid'])) {
  544. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  545. array_push($where, ['catid', 'in', $data['catid']]);
  546. } else {
  547. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  548. array_push($where, ['catid', '=', $data['catid']]);
  549. }
  550. if (empty($category)) {
  551. return Result::error("此网站暂无此栏目", 0);
  552. }
  553. }
  554. // return Result::success($where);
  555. $rep = Article::where(function ($query) use ($where) {
  556. foreach ($where as $condition) {
  557. if ($condition[1] === 'in') {
  558. $query->whereIn($condition[0], $condition[2]);
  559. } else {
  560. $query->where($condition[0], $condition[1], $condition[2]);
  561. }
  562. }
  563. })
  564. ->orderBy("created_at", "desc")
  565. ->limit($data['pageSize'])
  566. ->offset(($data['page'] - 1) * $data['pageSize'])
  567. ->get();
  568. $count = Article::where(function ($query) use ($where) {
  569. foreach ($where as $condition) {
  570. if ($condition[1] === 'in') {
  571. $query->whereIn($condition[0], $condition[2]);
  572. } else {
  573. $query->where($condition[0], $condition[1], $condition[2]);
  574. }
  575. }
  576. })->count();
  577. $data = [
  578. 'rows' => $rep->toArray(),
  579. 'count' => $count,
  580. ];
  581. if (empty($rep)) {
  582. return Result::error("没有信息数据");
  583. }
  584. return Result::success($data);
  585. }
  586. /**
  587. * 前端-获取新闻详情
  588. * @param array $data
  589. * @return array
  590. */
  591. public function selectWebsiteArticleInfo(array $data): array
  592. {
  593. $where = [
  594. 'article.id' => $data['id'],
  595. 'article.status' => 1,
  596. ];
  597. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  598. if (empty($result)) {
  599. return Result::error("查询失败", 0);
  600. }
  601. $category = WebsiteCategory::where('website_id', $data['website_id'])->where(['category_id' => $result['catid']])->first();
  602. if (empty($category)) {
  603. return Result::error("查询失败", 0);
  604. }
  605. $result['category_id'] = $category['category_id'];
  606. $result['cat_name'] = $category['name'];
  607. return Result::success($result);
  608. }
  609. /**
  610. * 前端-获取网站调查问卷
  611. * @param array $data
  612. * @return array
  613. */
  614. public function getWebsiteSurvey(array $data): array
  615. {
  616. if (isset($data['website_id']) && !empty($data['website_id'])) {
  617. $website = Website::where('id', $data['website_id'])->first();
  618. if (empty($website)) {
  619. return Result::error("暂无此网站", 0);
  620. }
  621. }
  622. if (isset($data['art_id']) && !empty($data['art_id'])) {
  623. $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
  624. if (empty($article)) {
  625. return Result::error("暂无此文章", 0);
  626. }
  627. // return Result::error($data,0);
  628. $where['art_id'] = $data['art_id'];
  629. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  630. } else {
  631. $survey = ArticleSurvey::where('website_id', $data['website_id'])->orderBy('created_at')->first();
  632. if (empty($survey)) {
  633. return Result::error("暂无调查问卷", 0);
  634. }
  635. $where['sur_id'] = $survey['sur_id'];
  636. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  637. }
  638. $result['survey'] = ArticleSurvey::where($where)->where('is_other', 0)
  639. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  640. ->select('article_survey.*', 'article.survey_type')
  641. ->get()->all();
  642. $result['other'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  643. $result['others'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->first();
  644. if (empty($result)) {
  645. return Result::error("此文章暂无调查问卷", 0);
  646. }
  647. return Result::success($result);
  648. }
  649. /**
  650. * 前端-添加网站调查问卷选项
  651. * @param array $data
  652. * @return array
  653. */
  654. public function addWebsiteSurveyOption(array $data): array
  655. {
  656. if (isset($data['website_id']) && !empty($data['website_id'])) {
  657. $website = Website::where('id', $data['website_id'])->first();
  658. if (empty($website)) {
  659. return Result::error("暂无此网站", 0);
  660. }
  661. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  662. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  663. if (empty($survey)) {
  664. return Result::error("此调查问卷不可添加选项", 0);
  665. }
  666. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  667. $choice = [
  668. 'art_id' => $survey['art_id'],
  669. 'website_id' => $data['website_id'],
  670. 'survey_name' => $survey['survey_name'],
  671. 'choice_name' => $data['choice_name'],
  672. 'sur_id' => $survey['sur_id'],
  673. 'is_other' => 1,
  674. 'other_id' => $survey['id'],
  675. ];
  676. $result = ArticleSurvey::insertGetId($choice);
  677. if (empty($result)) {
  678. return Result::error("添加失败", 0);
  679. }
  680. return Result::success($result);
  681. }
  682. }
  683. return Result::error("添加失败", 0);
  684. }
  685. return Result::error("添加失败", 0);
  686. }
  687. /**
  688. * 前端-调查问卷投票
  689. * @param array $data
  690. * @return array
  691. */
  692. public function addWebsiteSurveyVote(array $data): array
  693. {
  694. if (isset($data['website_id']) && !empty($data['website_id'])) {
  695. $website = Website::where('id', $data['website_id'])->first();
  696. if (empty($website)) {
  697. return Result::error("暂无此网站", 0);
  698. }
  699. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  700. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->pluck('sur_id');
  701. if (empty($survey)) {
  702. return Result::error("此调查问卷不存在", 0);
  703. }
  704. // return Result::success($survey);
  705. // 调查问卷类型 0:单选 1:多选
  706. $type = Article::where('survey_id', $data['sur_id'])->pluck('survey_type');
  707. // return Result::success($type);
  708. if (empty($type) || ($type[0] != 1 && $type[0] != 0)) {
  709. return Result::error("此调查问卷不可投票", 0);
  710. }
  711. // return Result::success($type[0]);
  712. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  713. if ($type[0] == 0) {
  714. if (is_array($data['choice_id'])) {
  715. return Result::error("请选择一个选项!", 0);
  716. }
  717. $data['choice_id'] = [$data['choice_id']];
  718. } else {
  719. if (!is_array($data['choice_id'])) {
  720. return Result::error("请传递数组!", 0);
  721. }
  722. }
  723. // return Result::success($data['choice_id']);
  724. $other = ArticleSurvey::whereIn('id', $data['choice_id'])
  725. ->where('website_id', $data['website_id'])
  726. ->where('is_other', 1)
  727. ->where('other_id', 0)
  728. ->first();
  729. if (!empty($other)) {
  730. return Result::error("请选择已有的选项!", 0);
  731. }
  732. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  733. ->where('website_id', $data['website_id'])
  734. ->where('is_other', 1)
  735. ->where('other_id', '!=', 0)
  736. ->first();
  737. // return Result::success($data);
  738. if (!empty($choice['other'])) {
  739. array_push($data['choice_id'], $choice['other']['other_id']);
  740. // return Result::success($data['choice_id']);
  741. }
  742. // return Result::success($data);
  743. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  744. ->where('website_id', $data['website_id'])
  745. ->increment('results', 1);
  746. if (empty($choice)) {
  747. return Result::error("请选择已有的选项!", 0);
  748. }
  749. return Result::success($choice);
  750. }
  751. return Result::error("参数必填!");
  752. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  753. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  754. // }
  755. }
  756. return Result::error("此调查问卷不存在", 0);
  757. }
  758. return Result::error("参数必填!");
  759. }
  760. /**
  761. * 后端-获取网站调查问卷列表
  762. * @param array $data
  763. * @return array
  764. */
  765. public function getSurveyList(array $data): array
  766. {
  767. $where = [];
  768. if (isset($data['survey_name']) && !empty($data['survey_name'])) {
  769. array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
  770. }
  771. if (isset($data['survey_type']) && $data['survey_type'] != null) {
  772. array_push($where, ['survey_type', '=', $data['survey_type']]);
  773. }
  774. if (isset($data['is_survey']) && $data['is_survey'] != null) {
  775. array_push($where, ['is_survey', '=', $data['is_survey']]);
  776. }
  777. // return Result::success($where);
  778. if (!empty($where)) {
  779. $query = Article::where($where)->whereNotNull('survey_name');
  780. } else {
  781. $query = Article::whereNotNull('survey_name');
  782. }
  783. $count = $query->count();
  784. $survey = $query->orderByDesc('id')
  785. ->limit($data['pageSize'])
  786. ->offset(($data['page'] - 1) * $data['pageSize'])
  787. ->get();
  788. if (empty($survey->toArray())) {
  789. return Result::error("暂无调查问卷!", 0);
  790. }
  791. $result = [
  792. 'rows' => $survey,
  793. 'count' => $count,
  794. ];
  795. return Result::success($result);
  796. }
  797. /**
  798. * 后端-获取网站调查问卷详情
  799. * @param array $data
  800. * @return array
  801. */
  802. public function getSurveyInfo(array $data): array
  803. {
  804. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  805. $where = ['sur_id' => $data['sur_id']];
  806. $choose = ArticleSurvey::where($where)->where('is_other', 0)
  807. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  808. ->select('article_survey.*', 'article.survey_type')
  809. ->get()->all();
  810. if (empty($choose)) {
  811. return Result::error("此调查问卷不存在", 0);
  812. }
  813. $resultsArray = array_column($choose, 'results');
  814. $total = array_sum($resultsArray);
  815. $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  816. $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
  817. // $total = 0;
  818. if (!empty($other)) {
  819. $total = $total + $other['results'];
  820. $other['choice_name'] = $other['choice_name'] . '(其他)';
  821. if (!empty($others)) {
  822. $other['hasChildren'] = true;
  823. // array_push($other,['hasChildren','=',true]);
  824. $other['children'] = $others;
  825. $other_choices = [$other->toArray()];
  826. $mer_choice = array_merge($choose, $other_choices);
  827. $value_choice = array_values($mer_choice);
  828. } else {
  829. // return Result::error('1111');
  830. $other_choices = [$other->toArray()];
  831. $other_choices = array_merge($choose, $other_choices);
  832. $value_choice = array_values($other_choices);
  833. // return Result::success($result);
  834. }
  835. } else {
  836. $value_choice = $choose;
  837. }
  838. $result = [
  839. 'choose' => $value_choice,
  840. 'total' => $total,
  841. ];
  842. }
  843. return Result::success($result);
  844. }
  845. /**
  846. * 验证导航名称是否重复
  847. * @return void
  848. */
  849. public function checkCategoryName(array $data): array
  850. {
  851. $result = Category::when($data, function ($query) use ($data) {
  852. if (isset($data['name']) && $data['name']) {
  853. $query->where("name", $data['name']);
  854. }
  855. if (isset($data['id']) && $data['id']) {
  856. $query->where("id", "!=", $data['id']);
  857. }
  858. })->first();
  859. if ($result) {
  860. return Result::error("已存在");
  861. } else {
  862. return Result::success();
  863. }
  864. }
  865. //20250226 产品列表
  866. public function getGoodList(array $data): array
  867. {
  868. $where = [];
  869. //类型
  870. if (isset($data['type_id']) && $data['type_id']) {
  871. $where = [
  872. 'type_id' => $data['type_id'],
  873. ];
  874. }
  875. //名称
  876. if (isset($data['name']) && $data['name']) {
  877. $where = [
  878. 'good.name' => $data['name'],
  879. ];
  880. }
  881. $where1 = [];
  882. //website_id
  883. // if (isset($data['website_id']) && $data['website_id']) {
  884. // $where1 = [
  885. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  886. // ];
  887. // }
  888. // website_name
  889. if (isset($data['website_name']) && $data['website_name']) {
  890. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  891. }
  892. // catid
  893. if (isset($data['category_name']) && $data['category_name']) {
  894. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  895. }
  896. // $result = Good::where($where)
  897. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  898. $result = Good::where($where)
  899. ->when(!empty($where1), function ($query) use ($where1) {
  900. return $query->where($where1);
  901. })
  902. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  903. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  904. ->leftJoin('category', 'good.catid', '=', 'category.id')
  905. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  906. ->orderBy("id", "desc")
  907. ->limit($data['page_size'])
  908. ->offset(($data['page'] - 1) * $data['page_size'])
  909. ->get();
  910. $count = Good::where($where)
  911. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  912. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  913. ->leftJoin('category', 'good.catid', '=', 'category.id')
  914. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  915. ->orderBy("updated_at", "desc")->count();
  916. $data = [
  917. 'rows' => $result->toArray(),
  918. 'count' => $count,
  919. ];
  920. if (empty($result)) {
  921. return Result::error("此栏目暂无相关产品", 0);
  922. }
  923. return Result::success($data);
  924. }
  925. public function getGoodInfo(array $data): array
  926. {
  927. $result = Good::where('id', $data['id'])->first();
  928. if (empty($result)) {
  929. return Result::error("此产品不存在", 0);
  930. }
  931. return Result::success($result);
  932. }
  933. public function addGood(array $data): array
  934. {
  935. // unset($data['city_arr_id']);
  936. // unset($data['cat_arr_id']);
  937. $data['city_id'] = end($data['city_arr_id']);
  938. $data['catid'] = end($data['cat_arr_id']);
  939. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  940. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  941. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  942. unset($data['imgUrl']);
  943. $result = Good::insert($data);
  944. if (empty($result)) {
  945. return Result::error("添加失败", 0);
  946. }
  947. return Result::success($result);
  948. }
  949. public function updateGood(array $data): array
  950. {
  951. $data['city_id'] = end($data['city_arr_id']);
  952. $data['catid'] = end($data['cat_arr_id']);
  953. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  954. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  955. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  956. //设置东八区
  957. date_default_timezone_set('Asia/Shanghai');
  958. $data['updated_at'] = date('Y-m-d H:i:s');
  959. $result = Good::where('id', $data['id'])->update($data);
  960. if (empty($result)) {
  961. return Result::error("更新失败", 0);
  962. }
  963. return Result::success($result);
  964. }
  965. public function delGood(array $data): array
  966. {
  967. $result = Good::where('id', $data['id'])->delete();
  968. if (empty($result)) {
  969. return Result::error("删除失败", 0);
  970. }
  971. return Result::success($result);
  972. }
  973. //20250226 产品列表
  974. }