FooterService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\FooterCategory;
  4. use App\Model\Website;
  5. use App\Model\FooterContent;
  6. use App\Model\Web;
  7. use Hyperf\RpcServer\Annotation\RpcService;
  8. use App\Tools\Result;
  9. use Hyperf\DbConnection\Db;
  10. use PhpParser\Node\Expr\Clone_;
  11. use Overtrue\Pinyin\Pinyin;
  12. use function Hyperf\Support\retry;
  13. use function PHPSTORM_META\type;
  14. use function PHPUnit\Framework\assertIsNotInt;
  15. use function PHPUnit\Framework\isNull;
  16. #[RpcService(name: "FooterService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  17. class FooterService implements FooterServiceInterface
  18. {
  19. /**
  20. * 获取底部导航列表
  21. * @param array $data
  22. * @return array
  23. */
  24. public function getFooterCategory(array $data): array
  25. {
  26. $where = [];
  27. if(isset($data['website_name'])){
  28. array_push($where, ['website.website_name','like','%'.$data['website_name'].'%']);
  29. }
  30. if(isset($data['name'])){
  31. array_push($where, ['footer_category.name','like','%'.$data['name'].'%']);
  32. }
  33. $query = FooterCategory::query();
  34. if (!empty($where)) {
  35. $query->where($where);
  36. }
  37. $count = $query->count();
  38. $rep = $query->leftJoin("website", "website.id", "footer_category.website_id")
  39. ->select("footer_category.*", "website.website_name", "website.id as website_id")
  40. ->offset(($data['page'] - 1) * $data['pageSize'])
  41. ->limit($data['pageSize'])
  42. ->orderByDesc("updated_at")
  43. ->get();
  44. // var_dump($where);
  45. $result = [];
  46. $result = [
  47. 'rows'=>$rep,
  48. 'count'=>$count
  49. ];
  50. if(empty($result)){
  51. return Result::error("没有查到相关数据!");
  52. }
  53. return Result::success($result);
  54. }
  55. /**
  56. * 添加底部导航
  57. * @param array $data
  58. * @return array
  59. */
  60. public function addFooterCategory(array $data): array
  61. {
  62. if(empty($data)){
  63. $result = Website::select('website_name','id')->get();
  64. }else{
  65. // 底部导航类型 0:内容型;1:列表型;
  66. $webid = Website::select('website_name','id')->where('id',$data['website_id'])->first();
  67. if(empty($webid)){
  68. return Result::error("该网站不存在!");
  69. }
  70. $isChild = 0;
  71. if(isset($data['is_child']) && !empty($data['is_child']) && $data['is_child'] == 1){
  72. $child = [
  73. 'name' => $data['child_name'],
  74. 'website_id' => $data['website_id'],
  75. 'type' => 0,
  76. ];
  77. $isChild = $data['is_child'];
  78. unset($data['child_name']);
  79. unset($data['is_child']);
  80. }
  81. // return Result::success($data);
  82. Db::beginTransaction();
  83. try{
  84. // 同一网站下的底部导航名称不能重复
  85. $name = FooterCategory::where('website_id',$data['website_id'])->where('name',$data['name'])->first();
  86. if(!empty($name)){
  87. Db::rollBack();
  88. return Result::error("该底部导航名称已存在!");
  89. }
  90. $pinyin = new Pinyin();
  91. $data['name_pinyin'] = $pinyin->permalink($data['name'], '') ;
  92. $result = [];
  93. $result['parent'] = FooterCategory::insertGetId($data);
  94. if($isChild == 1){
  95. $child['pid'] = $result['parent'];
  96. $child['name_pinyin'] = $pinyin->permalink($child['name'], '');
  97. // 暂时用不到 若是一个栏目对应多个子级栏目 则需要判断子级栏目名称是否重复
  98. // $child_name = FooterCategory::where('website_id',$data['website_id'])->where('pid',$child['pid'])->first();
  99. // if(!empty($child_name)){
  100. // Db::rollBack();
  101. // return Result::error("该子级栏目名称已存在!");
  102. // }
  103. $result['child'] = FooterCategory::insertGetId($child);
  104. if(empty($result['child'])){
  105. Db::rollBack();
  106. return Result::error("子级栏目添加失败!");
  107. }
  108. }
  109. Db::commit();
  110. } catch(\Throwable $ex){
  111. Db::rollBack();
  112. $errorMessage = $ex->getMessage();
  113. return Result::error("添加失败!",$errorMessage);
  114. }
  115. }
  116. return Result::success($result);
  117. }
  118. /**
  119. * 修改底部导航
  120. * @param array $data
  121. * @return array
  122. */
  123. public function upFooterCategory(array $data): array
  124. {
  125. $footer_category = FooterCategory::where('id', $data['id'])->first();
  126. if(empty($footer_category)) {
  127. return Result::error("该底部导航不存在!");
  128. }
  129. if(empty($data['website_id'])){
  130. $web = Website::select('website_name','id')->get();
  131. $footer_category = FooterCategory::where('footer_category.id',$data['id'])
  132. ->leftJoin("website","website.id","footer_category.website_id")
  133. ->select("footer_category.*","website.website_name","website.id as website_id")
  134. ->first();
  135. $child = FooterCategory::where('pid',$footer_category['id'])->first();
  136. if(!empty($child)){
  137. $footer_category['child_name'] = $child['name'];
  138. $footer_category['is_child'] = 1;
  139. }
  140. $result = [
  141. 'rows'=>$footer_category,
  142. 'web'=>$web
  143. ];
  144. }else{
  145. $all_categories = FooterCategory::where('website_id',$data['website_id'])->where('id','!=',$data['id'])->pluck('name')->toArray();
  146. // 检查修改后的数据是否与已有数据重复
  147. if (in_array($data['name'], $all_categories)) {
  148. return Result::error("修改后的底部导航名称已存在!");
  149. }
  150. $webid = Website::where('id',$data['website_id'])->first();
  151. if(empty($webid)){
  152. return Result::error("该网站不存在!");
  153. }
  154. $pinyin = new Pinyin();
  155. $child_data = [];
  156. $result = [];
  157. Db::beginTransaction();
  158. try{
  159. if(isset($data['is_child']) && $data['is_child']!=''){
  160. $child = FooterCategory::where('pid',$data['id'])->first();
  161. if($data['is_child'] == 1){
  162. if(empty($data['child_name'])){
  163. Db::rollBack();
  164. return Result::error("请输入子级栏目名称!");
  165. var_dump($data['child_name']);
  166. }
  167. if(empty($child)){
  168. $child_data = [
  169. 'name' => $data['child_name'],
  170. 'name_pinyin' => $pinyin->permalink($data['child_name'], ''),
  171. 'website_id' => $data['website_id'],
  172. 'type' => 0,
  173. 'pid' => $data['id'],
  174. ];
  175. $result['addchild'] = FooterCategory::insertGetId($child_data);
  176. if(empty($result['addchild'])){
  177. Db::rollBack();
  178. return Result::error("子级栏目添加失败!");
  179. }
  180. }else{
  181. $child_data = [
  182. 'name' => $data['child_name'],
  183. 'name_pinyin' => $pinyin->permalink($data['child_name'], ''),
  184. ];
  185. $result['upchild'] = FooterCategory::where('pid',$data['id'])->update($child_data);
  186. if(empty($result['upchild'])){
  187. Db::rollBack();
  188. return Result::error("子级栏目修改失败!");
  189. }
  190. }
  191. }else{
  192. if(!empty($child)){
  193. $result['del_child_content'] = FooterContent::where('fcat_id',$child['id'])->delete();
  194. $result['delchild'] = FooterCategory::where('pid',$data['id'])->delete();
  195. if(empty($result['delchild'])) {
  196. Db::rollBack();
  197. return Result::error("子级栏目删除失败!");
  198. }
  199. }
  200. }
  201. unset($data['child_name']);
  202. unset($data['is_child']);
  203. $data['name_pinyin'] = $pinyin->permalink($data['name'], '');
  204. $result['rows'] = FooterCategory::where('id', $data['id'])->update($data);
  205. if(empty($result['rows'])){
  206. Db::rollBack();
  207. return Result::error("栏目修改失败!");
  208. }
  209. Db::commit();
  210. }else{
  211. return Result::error("请选择是否添加子级栏目!");
  212. }
  213. } catch(\Throwable $ex){
  214. Db::rollBack();
  215. var_dump($ex->getMessage());
  216. return Result::error("修改失败!",0);
  217. }
  218. }
  219. if (empty($result)) {
  220. return Result::error("修改失败!");
  221. }else{
  222. return Result::success($result);
  223. }
  224. }
  225. /**
  226. * 删除底部导航
  227. * @param array $data
  228. * @return array
  229. */
  230. public function delFooterCategory(array $data): array
  231. {
  232. Db::beginTransaction();
  233. try{
  234. $footer_category = FooterCategory::where('id', $data['id'])->first();
  235. if (!$footer_category) {
  236. Db::rollBack();
  237. return Result::error("该底部导航不存在!");
  238. }else{
  239. $result['footer_category'] = FooterCategory::where('id', $data['id'])->delete();
  240. $result['footer_content'] = FooterContent::where('fcat_id', $data['id'])->delete();
  241. $child = FooterCategory::where('pid', $data['id'])->first();
  242. if(!empty($child)){
  243. $result['child'] = FooterCategory::where('pid', $data['id'])->delete();
  244. $result['child_content'] = FooterContent::where('fcat_id', $child['id'])->delete();
  245. }
  246. Db::commit();
  247. }
  248. } catch(\Throwable $ex){
  249. Db::rollBack();
  250. var_dump($ex->getMessage());
  251. return Result::error("删除失败",0);
  252. }
  253. return Result::success($result);
  254. }
  255. /**
  256. * 添加底部导航(列表)内容
  257. * @param array $data
  258. * @return array
  259. */
  260. public function addFooterContent(array $data): array
  261. {
  262. // 底部导航类型 0:内容型;1:列表型;
  263. // var_dump($data);
  264. $cat = FooterCategory::where('id', $data['fcat_id'])->first();
  265. // return Result::success($cat);
  266. if (!$cat) {
  267. return Result::error("该底部导航不存在!");
  268. }
  269. if($cat['type'] != $data['type_id']){
  270. return Result::error("请输入正确的底部导航类型!");
  271. }
  272. // 内容型底部导航只能有一条内容
  273. if($cat['type'] == 0){
  274. $content = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  275. if(!empty($content)){
  276. return Result::error("该底部导航已添加内容!");
  277. }
  278. }else{
  279. // 列表型底部导航\列表标头型底部导航列表标题不能重复
  280. if(!isset($data['list_title']) || empty($data['list_title'])){
  281. return Result::error("请输入底部导航列表标题!");
  282. }
  283. $content = FooterContent::where('fcat_id', $data['fcat_id'])->where('list_title',$data['list_title'])->first();
  284. if(!empty($content)){
  285. return Result::error("该列表标题已存在!");
  286. }
  287. }
  288. $result = FooterContent::insertGetId($data);
  289. if(empty($result)){
  290. return Result::error("添加失败!");
  291. }else{
  292. return Result::success($result);
  293. }
  294. }
  295. /**
  296. * 获取底部导航(列表)内容
  297. * @param array $data
  298. * @return array
  299. */
  300. public function getFooterContent(array $data): array
  301. {
  302. $where = [];
  303. $fcat_id = [];
  304. $footer_category = FooterCategory::where('id', $data['fcat_id'])->first();
  305. if(empty($footer_category)){
  306. return Result::error("该底部导航不存在!");
  307. }
  308. $footer_category = $footer_category->toArray();
  309. $fcat_id = $footer_category['id'];
  310. if($footer_category['type'] == 1){
  311. if(isset($data['list_title'])){
  312. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  313. }
  314. if(isset($data['con_title'])){
  315. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  316. }
  317. $child = FooterCategory::where('pid', $data['fcat_id'])->first();
  318. if($child){
  319. $fcat_id = [$footer_category['id'], $child->id];
  320. }
  321. }
  322. $count = FooterContent::whereIn('fcat_id', (array)$fcat_id)->where($where)->count();
  323. $rep = FooterContent::whereIn('fcat_id', (array)$fcat_id)->where($where)
  324. ->leftJoin('footer_category', 'footer_category.id', '=', 'footer_content.fcat_id')
  325. ->select('footer_content.*', 'footer_category.type', 'footer_category.name as fcat_name')
  326. ->limit($data['pageSize'])
  327. ->offset(($data['page'] - 1) * $data['pageSize'])
  328. ->orderBy("updated_at", "desc")
  329. ->get();
  330. if(empty($rep)){
  331. return Result::error("没有查到相关数据!");
  332. }else{
  333. $result = [
  334. 'rows' => $rep,
  335. 'count' => $count
  336. ];
  337. return Result::success($result);
  338. }
  339. }
  340. /**
  341. * 获取某个底部导航(列表)内容
  342. * @param array $data
  343. * @return array
  344. */
  345. public function getOneFooterContent(array $data): array
  346. {
  347. $result = FooterContent::where('footer_content.id', $data['id'])
  348. ->leftJoin('footer_category','footer_category.id','fcat_id')
  349. ->select('footer_content.*','footer_category.type','footer_category.pid','footer_category.name as fcat_name')
  350. ->first();
  351. if(empty($result)){
  352. return Result::error("请输入正确的底部导航内容id!");
  353. }
  354. return Result::success($result);
  355. }
  356. /**
  357. * 编辑底部导航(列表)内容
  358. * @param array $data
  359. * @return array
  360. */
  361. public function upFooterContent(array $data): array
  362. {
  363. $content = FooterContent::where('footer_content.id', $data['id'])
  364. ->leftJoin('footer_category','footer_category.id','fcat_id')
  365. ->select('footer_content.*','footer_category.type','footer_category.id as fcat_id')
  366. ->first();
  367. if(!$content){
  368. return Result::error("该底部导航内容不存在!");
  369. }
  370. if($content['type'] != $data['type_id']){
  371. return Result::error("请输入正确的底部导航类型!");
  372. }
  373. if($content['type_id'] == 1){
  374. if(!isset($data['list_title']) || empty($data['list_title'])){
  375. return Result::error("请输入底部导航列表标题!");
  376. }
  377. $list_title = FooterContent::where('fcat_id', $data['fcat_id'])->where('list_title',$data['list_title'])->first();
  378. if(!empty($list_title)){
  379. return Result::error("该列表标题已存在!");
  380. }
  381. }
  382. unset($data['type_id']);
  383. $result = FooterContent::where('id', $data['id'])->update($data);
  384. if(empty($result)){
  385. return Result::error("修改失败!");
  386. }else{
  387. return Result::success($result);
  388. }
  389. }
  390. /**
  391. * 删除底部导航(列表)内容
  392. * @param array $data
  393. * @return array
  394. */
  395. public function delFooterContent(array $data): array
  396. {
  397. $result = FooterContent::where('id', $data['id'])->delete($data);
  398. if(empty($result)){
  399. return Result::error("删除失败!");
  400. }else{
  401. return Result::success($result);
  402. }
  403. }
  404. }