FooterService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\FooterCategory;
  4. use App\Model\Website;
  5. use App\Model\FooterContent;
  6. use Hyperf\RpcServer\Annotation\RpcService;
  7. use App\Tools\Result;
  8. #[RpcService(name: "FooterService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  9. class FooterService implements FooterServiceInterface
  10. {
  11. /**
  12. * 获取底部导航
  13. * @param array $data
  14. * @return array
  15. */
  16. public function getFooterCategory(array $data): array
  17. {
  18. $where = [];
  19. if(isset($data['website_name'])){
  20. array_push($where, ['website.website_name','like','%'.$data['website_name'].'%']);
  21. }
  22. if(isset($data['name'])){
  23. array_push($where, ['footer_category.name','like','%'.$data['name'].'%']);
  24. }
  25. // var_dump($where);
  26. if(!empty($where)){
  27. $rep = FooterCategory::where($where)
  28. ->leftJoin("website","website.id","footer_category.website_id")
  29. ->select("footer_category.*","website.website_name","website.id as website_id")
  30. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("updated_at","desc")
  31. ->get();
  32. $count = count($rep);
  33. }else{
  34. $rep = FooterCategory::leftJoin("website","website.id","footer_category.website_id")
  35. ->select("footer_category.*","website.website_name","website.id as website_id")
  36. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("updated_at","desc")
  37. ->get();
  38. $count = FooterCategory::count();
  39. }
  40. $result = [];
  41. $result = [
  42. 'rows'=>$rep,
  43. 'count'=>$count
  44. ];
  45. if($count == 0){
  46. return Result::error("没有查到相关数据!");
  47. }
  48. return Result::success($result);
  49. }
  50. /**
  51. * 添加底部导航
  52. * @param array $data
  53. * @return array
  54. */
  55. public function addFooterCategory(array $data): array
  56. {
  57. if(empty($data)){
  58. $result = Website::select('website_name','id')->get();
  59. }else{
  60. $name = FooterCategory::where('name',$data['name'])->first();
  61. if(!empty($name)){
  62. return Result::error("该底部导航名称已存在!");
  63. }else{
  64. $result = FooterCategory::insertGetId($data);
  65. }
  66. }
  67. if(empty($result)){
  68. return Result::error("添加失败!");
  69. }else{
  70. return Result::success($result);
  71. }
  72. }
  73. /**
  74. * 修改底部导航
  75. * @param array $data
  76. * @return array
  77. */
  78. public function upFooterCategory(array $data): array
  79. {
  80. $footer_category = FooterCategory::where('id', $data['id'])->first();
  81. if (!$footer_category) {
  82. return Result::error("该底部导航不存在!");
  83. }
  84. if(empty($data['website_id'])){
  85. $web = Website::select('website_name','id')->get();
  86. $footer_category = FooterCategory::where('footer_category.id',$data['id'])
  87. ->leftJoin("website","website.id","footer_category.website_id")
  88. ->select("footer_category.*","website.website_name","website.id as website_id")
  89. ->first();
  90. $result = [
  91. 'rows'=>$footer_category,
  92. 'web'=>$web
  93. ];
  94. }else{
  95. $all_categories = FooterCategory::all()->pluck('name')->toArray();
  96. // 检查修改后的数据是否与已有数据重复
  97. if (in_array($data['name'], $all_categories)) {
  98. return Result::error("修改后的底部导航名称已存在!");
  99. }
  100. $result = FooterCategory::where('id', $data['id'])->update($data);
  101. }
  102. if (empty($result)) {
  103. return Result::error("修改失败!");
  104. }else{
  105. return Result::success($result);
  106. }
  107. }
  108. /**
  109. * 删除底部导航
  110. * @param array $data
  111. * @return array
  112. */
  113. public function delFooterCategory(array $data): array
  114. {
  115. $footer_category = FooterCategory::where('id', $data['id'])->first();
  116. if (!$footer_category) {
  117. return Result::error("该底部导航不存在!");
  118. }else{
  119. $result = FooterCategory::where('footer_category.id',$data['id'])
  120. ->leftJoin("footer_content","footer_content.fcat_id","footer_category.id")
  121. ->delete();
  122. }
  123. if(empty($result)){
  124. return Result::error("删除失败!");
  125. }else{
  126. return Result::success($result);
  127. }
  128. }
  129. /**
  130. * 添加底部导航(列表)内容
  131. * @param array $data
  132. * @return array
  133. */
  134. public function addFooterContent(array $data): array
  135. {
  136. // 底部导航类型 0:内容型;1:列表型;
  137. // var_dump($data);
  138. if($data['type'] == 0){
  139. $countent = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  140. if(!empty($countent)){
  141. return Result::error("该底部导航内容已存在!");
  142. }
  143. }else{
  144. $countent = FooterContent::where('list_title', $data['list_title'])->first();
  145. if(!empty($countent)){
  146. return Result::error("该列表标题已存在!");
  147. }
  148. }
  149. unset($data['type']);
  150. $result = FooterContent::insertGetId($data);
  151. if(empty($result)){
  152. return Result::error("添加失败!");
  153. }else{
  154. return Result::success($result);
  155. }
  156. }
  157. /**
  158. * 获取底部导航(列表)内容
  159. * @param array $data
  160. * @return array
  161. */
  162. public function getFooterContent(array $data): array
  163. {
  164. $where = [];
  165. array_push($where, ['fcat_id',$data['fcat_id']]);
  166. $type = FooterCategory::where('id', $data['fcat_id'])->value('type');
  167. if($type == 1){
  168. if(isset($data['list_title'])){
  169. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  170. }
  171. if(isset($data['con_title'])){
  172. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  173. }
  174. }
  175. $rep = FooterContent::where($where)
  176. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  177. ->orderBy("updated_at","desc")->get();
  178. $count = FooterContent::where($where)->count();
  179. if($count == 0){
  180. return Result::error("没有查到相关数据!");
  181. }else{
  182. $result = [
  183. 'rows'=>$rep,
  184. 'count'=>$count
  185. ];
  186. return Result::success($result);
  187. }
  188. }
  189. /**
  190. * 获取某个底部导航(列表)内容
  191. * @param array $data
  192. * @return array
  193. */
  194. public function getOneFooterContent(array $data): array
  195. {
  196. $result = FooterContent::where('footer_content.id', $data['id'])
  197. ->leftJoin('footer_category','footer_category.id','fcat_id')
  198. ->select('footer_content.*','footer_category.type')
  199. ->first();
  200. if(empty($result)){
  201. return Result::error("请输入正确的底部导航内容id!");
  202. }else{
  203. return Result::success($result);
  204. }
  205. }
  206. /**
  207. * 编辑底部导航(列表)内容
  208. * @param array $data
  209. * @return array
  210. */
  211. public function upFooterContent(array $data): array
  212. {
  213. if($data['type'] == 1){
  214. $list_title = FooterContent::all()->pluck('list_title')->toArray();
  215. // 检查修改后的数据是否与已有数据重复
  216. if (in_array($data['list_title'], $list_title)) {
  217. return Result::error("修改后的列表名称已存在!");
  218. }
  219. }
  220. unset($data['type']);
  221. $result = FooterContent::where('id', $data['id'])->update($data);
  222. if(empty($result)){
  223. return Result::error("修改失败!");
  224. }else{
  225. return Result::success($result);
  226. }
  227. }
  228. /**
  229. * 删除底部导航(列表)内容
  230. * @param array $data
  231. * @return array
  232. */
  233. public function delFooterContent(array $data): array
  234. {
  235. $result = FooterContent::where('id', $data['id'])->delete($data);
  236. if(empty($result)){
  237. return Result::error("删除失败!");
  238. }else{
  239. return Result::success($result);
  240. }
  241. }
  242. }