FooterService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. if(isset($data['con_title'])){
  165. $rep = FooterContent::where('con_title', 'like', '%'.$data['con_title'].'%')
  166. ->where('fcat_id', $data['fcat_id'])
  167. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  168. ->orderBy("updated_at","desc")->get();
  169. }else{
  170. $rep = FooterContent::where('fcat_id', $data['fcat_id'])
  171. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  172. ->orderBy("updated_at","desc")->get();
  173. }
  174. $count = count($rep);
  175. if($count == 0){
  176. return Result::error("没有查到相关数据!");
  177. }else{
  178. $result = [
  179. 'rows'=>$rep,
  180. 'count'=>$count
  181. ];
  182. return Result::success($result);
  183. }
  184. }
  185. /**
  186. * 编辑底部导航(列表)内容
  187. * @param array $data
  188. * @return array
  189. */
  190. public function upFooterContent(array $data): array
  191. {
  192. if($data['type'] == 1){
  193. $list_title = FooterContent::all()->pluck('list_title')->toArray();
  194. // 检查修改后的数据是否与已有数据重复
  195. if (in_array($data['list_title'], $list_title)) {
  196. return Result::error("修改后的列表名称已存在!");
  197. }
  198. }
  199. unset($data['type']);
  200. $result = FooterContent::where('id', $data['id'])->update($data);
  201. if(empty($result)){
  202. return Result::error("修改失败!");
  203. }else{
  204. return Result::success($result);
  205. }
  206. }
  207. /**
  208. * 删除底部导航(列表)内容
  209. * @param array $data
  210. * @return array
  211. */
  212. public function delFooterContent(array $data): array
  213. {
  214. $result = FooterContent::where('id', $data['id'])->delete($data);
  215. if(empty($result)){
  216. return Result::error("删除失败!");
  217. }else{
  218. return Result::success($result);
  219. }
  220. }
  221. }