NewsService.php 40 KB

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